Propagate context not span in grpc instrumentation
MODIFY
instrumentation/grpc-1.5/src/main/java/io/opentelemetry/instrumentation/auto/grpc/client/TracingClientInterceptor.java
instrumentation/grpc-1.5/src/main/java/io/opentelemetry/instrumentation/auto/grpc/client/TracingClientInterceptor.java
package io.opentelemetry.instrumentation.auto.grpc.client;
+import static io.opentelemetry.context.ContextUtils.withScopedContext;
import static io.opentelemetry.instrumentation.auto.grpc.client.GrpcClientDecorator.DECORATE;
import static io.opentelemetry.instrumentation.auto.grpc.client.GrpcClientDecorator.TRACER;
import static io.opentelemetry.instrumentation.auto.grpc.client.GrpcInjectAdapter.SETTER;
import static io.opentelemetry.trace.Span.Kind.CLIENT;
-import static io.opentelemetry.trace.TracingContextUtils.currentContextWith;
+import static io.opentelemetry.trace.TracingContextUtils.getSpan;
import static io.opentelemetry.trace.TracingContextUtils.withSpan;
import io.grpc.CallOptions;
@@ -57,7 +58,8 @@ public class TracingClientInterceptor implements ClientInterceptor {
String methodName = method.getFullMethodName();
Span span = TRACER.spanBuilder(methodName).setSpanKind(CLIENT).startSpan();
- try (Scope scope = currentContextWith(span)) {
+ Context context = withSpan(span, Context.current());
+ try (Scope ignored = withScopedContext(context)) {
DECORATE.afterStart(span);
GrpcHelper.prepareSpan(span, methodName, peerAddress, false);
@@ -71,17 +73,17 @@ public class TracingClientInterceptor implements ClientInterceptor {
span.end();
throw e;
}
- return new TracingClientCall<>(span, result);
+ return new TracingClientCall<>(context, result);
}
}
static final class TracingClientCall<ReqT, RespT>
extends ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT> {
- final Span span;
+ final Context context;
- TracingClientCall(final Span span, final ClientCall<ReqT, RespT> delegate) {
+ TracingClientCall(final Context context, final ClientCall<ReqT, RespT> delegate) {
super(delegate);
- this.span = span;
+ this.context = context;
}
@Override
@@ -91,11 +93,11 @@ public class TracingClientInterceptor implements ClientInterceptor {
// (luckily the grpc instrumentation doesn't need to reference unshaded grpc Context, so we
// don't need to worry about distinguishing them like in the opentelemetry-api
// instrumentation)
- Context context = withSpan(span, Context.current());
OpenTelemetry.getPropagators().getHttpTextFormat().inject(context, headers, SETTER);
- try (Scope scope = currentContextWith(span)) {
- super.start(new TracingClientCallListener<>(span, responseListener), headers);
+ try (Scope ignored = withScopedContext(context)) {
+ super.start(new TracingClientCallListener<>(context, responseListener), headers);
} catch (final Throwable e) {
+ Span span = getSpan(context);
DECORATE.onError(span, e);
DECORATE.beforeFinish(span);
span.end();
@@ -105,9 +107,10 @@ public class TracingClientInterceptor implements ClientInterceptor {
@Override
public void sendMessage(final ReqT message) {
- try (Scope scope = currentContextWith(span)) {
+ try (Scope ignored = withScopedContext(context)) {
super.sendMessage(message);
} catch (final Throwable e) {
+ Span span = getSpan(context);
DECORATE.onError(span, e);
DECORATE.beforeFinish(span);
span.end();
@@ -118,31 +121,33 @@ public class TracingClientInterceptor implements ClientInterceptor {
static final class TracingClientCallListener<RespT>
extends ForwardingClientCallListener.SimpleForwardingClientCallListener<RespT> {
- private final Span span;
+ private final Context context;
private final AtomicInteger messageId = new AtomicInteger();
- TracingClientCallListener(final Span span, final ClientCall.Listener<RespT> delegate) {
+ TracingClientCallListener(final Context context, final ClientCall.Listener<RespT> delegate) {
super(delegate);
- this.span = span;
+ this.context = context;
}
@Override
public void onMessage(final RespT message) {
+ Span span = getSpan(context);
Attributes attributes =
Attributes.of(
"message.type", AttributeValue.stringAttributeValue("SENT"),
"message.id", AttributeValue.longAttributeValue(messageId.incrementAndGet()));
span.addEvent("message", attributes);
- try (Scope scope = currentContextWith(span)) {
+ try (Scope ignored = withScopedContext(context)) {
delegate().onMessage(message);
}
}
@Override
public void onClose(final Status status, final Metadata trailers) {
+ Span span = getSpan(context);
DECORATE.onClose(span, status);
// Finishes span.
- try (Scope scope = currentContextWith(span)) {
+ try (Scope ignored = withScopedContext(context)) {
delegate().onClose(status, trailers);
} catch (final Throwable e) {
DECORATE.onError(span, e);
@@ -155,9 +160,10 @@ public class TracingClientInterceptor implements ClientInterceptor {
@Override
public void onReady() {
- try (Scope scope = currentContextWith(span)) {
+ try (Scope ignored = withScopedContext(context)) {
delegate().onReady();
} catch (final Throwable e) {
+ Span span = getSpan(context);
DECORATE.onError(span, e);
DECORATE.beforeFinish(span);
span.end();

Propagate context not span in grpc instrumentation (#987)