Use Scope when opening span in netty instrumentation
MODIFY
dd-java-agent/instrumentation/netty-4.0/src/main/java/datadog/trace/instrumentation/netty40/ChannelFutureListenerInstrumentation.java
dd-java-agent/instrumentation/netty-4.0/src/main/java/datadog/trace/instrumentation/netty40/ChannelFutureListenerInstrumentation.java
@@ -83,18 +83,19 @@ public class ChannelFutureListenerInstrumentation extends Instrumenter.Default {
if (continuation == null) {
return null;
}
- final TraceScope scope = continuation.activate();
+ final TraceScope parentScope = continuation.activate();
final Span errorSpan =
GlobalTracer.get()
.buildSpan("netty.connect")
.withTag(Tags.COMPONENT.getKey(), "netty")
.start();
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(errorSpan, true)) {
Tags.ERROR.set(errorSpan, true);
errorSpan.log(singletonMap(ERROR_OBJECT, cause));
- errorSpan.finish();
+ }
- return scope;
+ return parentScope;
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)

MODIFY
dd-java-agent/instrumentation/netty-4.0/src/main/java/datadog/trace/instrumentation/netty40/client/HttpClientRequestTracingHandler.java
dd-java-agent/instrumentation/netty-4.0/src/main/java/datadog/trace/instrumentation/netty40/client/HttpClientRequestTracingHandler.java
@@ -8,6 +8,7 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.HttpRequest;
+import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.propagation.Format;
import io.opentracing.util.GlobalTracer;
@@ -24,16 +25,17 @@ public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapt
return;
}
- TraceScope scope = null;
+ TraceScope parentScope = null;
final TraceScope.Continuation continuation =
ctx.channel().attr(AttributeKeys.PARENT_CONNECT_CONTINUATION_ATTRIBUTE_KEY).getAndRemove();
if (continuation != null) {
- scope = continuation.activate();
+ parentScope = continuation.activate();
}
final HttpRequest request = (HttpRequest) msg;
final Span span = GlobalTracer.get().buildSpan("netty.client.request").start();
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.afterStart(span);
DECORATE.onRequest(span, request);
DECORATE.onPeerConnection(span, (InetSocketAddress) ctx.channel().remoteAddress());
@@ -42,7 +44,9 @@ public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapt
if (!request.headers().contains("amz-sdk-invocation-id")) {
GlobalTracer.get()
.inject(
- span.context(), Format.Builtin.HTTP_HEADERS, new NettyResponseInjectAdapter(request));
+ span.context(),
+ Format.Builtin.HTTP_HEADERS,
+ new NettyResponseInjectAdapter(request));
}
ctx.channel().attr(AttributeKeys.CLIENT_ATTRIBUTE_KEY).set(span);
@@ -55,9 +59,10 @@ public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapt
span.finish();
throw throwable;
}
+ }
- if (null != scope) {
- scope.close();
+ if (null != parentScope) {
+ parentScope.close();
}
}
}

MODIFY
dd-java-agent/instrumentation/netty-4.0/src/main/java/datadog/trace/instrumentation/netty40/server/HttpServerRequestTracingHandler.java
dd-java-agent/instrumentation/netty-4.0/src/main/java/datadog/trace/instrumentation/netty40/server/HttpServerRequestTracingHandler.java
@@ -29,12 +29,9 @@ public class HttpServerRequestTracingHandler extends ChannelInboundHandlerAdapte
GlobalTracer.get()
.extract(Format.Builtin.HTTP_HEADERS, new NettyRequestExtractAdapter(request));
- final Scope scope =
- GlobalTracer.get()
- .buildSpan("netty.request")
- .asChildOf(extractedContext)
- .startActive(false);
- final Span span = scope.span();
+ final Span span =
+ GlobalTracer.get().buildSpan("netty.request").asChildOf(extractedContext).start();
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.afterStart(span);
DECORATE.onRequest(span, request);
DECORATE.onPeerConnection(span, remoteAddress);
@@ -52,8 +49,7 @@ public class HttpServerRequestTracingHandler extends ChannelInboundHandlerAdapte
DECORATE.beforeFinish(span);
span.finish(); // Finish the span manually since finishSpanOnClose was false
throw throwable;
- } finally {
- scope.close();
+ }
}
}
}

MODIFY
dd-java-agent/instrumentation/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/ChannelFutureListenerInstrumentation.java
dd-java-agent/instrumentation/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/ChannelFutureListenerInstrumentation.java
@@ -83,18 +83,19 @@ public class ChannelFutureListenerInstrumentation extends Instrumenter.Default {
if (continuation == null) {
return null;
}
- final TraceScope scope = continuation.activate();
+ final TraceScope parentScope = continuation.activate();
final Span errorSpan =
GlobalTracer.get()
.buildSpan("netty.connect")
.withTag(Tags.COMPONENT.getKey(), "netty")
.start();
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(errorSpan, true)) {
Tags.ERROR.set(errorSpan, true);
errorSpan.log(singletonMap(ERROR_OBJECT, cause));
- errorSpan.finish();
+ }
- return scope;
+ return parentScope;
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)

MODIFY
dd-java-agent/instrumentation/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/client/HttpClientRequestTracingHandler.java
dd-java-agent/instrumentation/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/client/HttpClientRequestTracingHandler.java
@@ -8,6 +8,7 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.HttpRequest;
+import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.propagation.Format;
import io.opentracing.util.GlobalTracer;
@@ -24,16 +25,17 @@ public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapt
return;
}
- TraceScope scope = null;
+ TraceScope parentScope = null;
final TraceScope.Continuation continuation =
ctx.channel().attr(AttributeKeys.PARENT_CONNECT_CONTINUATION_ATTRIBUTE_KEY).getAndRemove();
if (continuation != null) {
- scope = continuation.activate();
+ parentScope = continuation.activate();
}
final HttpRequest request = (HttpRequest) msg;
final Span span = GlobalTracer.get().buildSpan("netty.client.request").start();
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.afterStart(span);
DECORATE.onRequest(span, request);
DECORATE.onPeerConnection(span, (InetSocketAddress) ctx.channel().remoteAddress());
@@ -42,7 +44,9 @@ public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapt
if (!request.headers().contains("amz-sdk-invocation-id")) {
GlobalTracer.get()
.inject(
- span.context(), Format.Builtin.HTTP_HEADERS, new NettyResponseInjectAdapter(request));
+ span.context(),
+ Format.Builtin.HTTP_HEADERS,
+ new NettyResponseInjectAdapter(request));
}
ctx.channel().attr(AttributeKeys.CLIENT_ATTRIBUTE_KEY).set(span);
@@ -55,9 +59,10 @@ public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapt
span.finish();
throw throwable;
}
+ }
- if (null != scope) {
- scope.close();
+ if (null != parentScope) {
+ parentScope.close();
}
}
}

MODIFY
dd-java-agent/instrumentation/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/server/HttpServerRequestTracingHandler.java
dd-java-agent/instrumentation/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/server/HttpServerRequestTracingHandler.java
@@ -29,12 +29,9 @@ public class HttpServerRequestTracingHandler extends ChannelInboundHandlerAdapte
GlobalTracer.get()
.extract(Format.Builtin.HTTP_HEADERS, new NettyRequestExtractAdapter(request));
- final Scope scope =
- GlobalTracer.get()
- .buildSpan("netty.request")
- .asChildOf(extractedContext)
- .startActive(false);
- final Span span = scope.span();
+ final Span span =
+ GlobalTracer.get().buildSpan("netty.request").asChildOf(extractedContext).start();
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.afterStart(span);
DECORATE.onRequest(span, request);
DECORATE.onPeerConnection(span, remoteAddress);
@@ -52,8 +49,7 @@ public class HttpServerRequestTracingHandler extends ChannelInboundHandlerAdapte
DECORATE.beforeFinish(span);
span.finish(); // Finish the span manually since finishSpanOnClose was false
throw throwable;
- } finally {
- scope.close();
+ }
}
}
}

Use Scope when opening span in netty instrumentation