Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureActiveSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation;
import static datadog.trace.instrumentation.netty41.AttributeKeys.CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY;
import static datadog.trace.instrumentation.netty41.AttributeKeys.HTTP2_CONNECTION_CODEC_ATTRIBUTE_KEY;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.returns;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
Expand Down Expand Up @@ -175,6 +176,9 @@ public static void addHandler(
handler2 instanceof ChannelHandler ? (ChannelHandler) handler2 : handler3;

try {
if (NettyHttp2Helper.isHttp2ConnectionCodec(handler)) {
pipeline.channel().attr(HTTP2_CONNECTION_CODEC_ATTRIBUTE_KEY).set(Boolean.TRUE);
}
// Server pipeline handlers
if (handler instanceof HttpServerCodec) {
NettyPipelineHelper.addHandlerAfter(
Expand Down Expand Up @@ -250,6 +254,10 @@ else if (handler instanceof HttpClientCodec) {
public static class ConnectAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void addParentSpan(@Advice.This final ChannelPipeline pipeline) {
if (Boolean.TRUE.equals(
pipeline.channel().attr(HTTP2_CONNECTION_CODEC_ATTRIBUTE_KEY).get())) {
return;
}
Comment thread
amarziali marked this conversation as resolved.
Outdated
AgentScope.Continuation continuation = captureActiveSpan();
if (continuation != noopContinuation()) {
final Attribute<AgentScope.Continuation> attribute =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

public class NettyHttp2Helper {
private static final Class HTTP2_CODEC_CLS;
private static final Class HTTP2_FRAME_CODEC_CLS;
private static final MethodHandle IS_SERVER_FIELD;
private static final Logger LOGGER = LoggerFactory.getLogger(NettyHttp2Helper.class);

static {
Class codecClass;
Class frameCodecClass;
MethodHandle isServerField;
try {
codecClass =
Expand All @@ -38,14 +40,33 @@ public class NettyHttp2Helper {
isServerField = null;
LOGGER.debug("Unable to setup netty http2 instrumentation", t);
}
try {
frameCodecClass =
Class.forName(
"io.netty.handler.codec.http2.Http2FrameCodec",
false,
NettyHttp2Helper.class.getClassLoader());
} catch (final ClassNotFoundException cnfe) {
// can be expected
frameCodecClass = null;
} catch (Throwable t) {
// unexpected
frameCodecClass = null;
LOGGER.debug("Unable to setup netty http2 connection detection", t);
}
HTTP2_CODEC_CLS = codecClass;
HTTP2_FRAME_CODEC_CLS = frameCodecClass;
IS_SERVER_FIELD = isServerField;
}

public static boolean isHttp2FrameCodec(final ChannelHandler handler) {
return HTTP2_CODEC_CLS != null && HTTP2_CODEC_CLS.isInstance(handler);
}

public static boolean isHttp2ConnectionCodec(final ChannelHandler handler) {
return HTTP2_FRAME_CODEC_CLS != null && HTTP2_FRAME_CODEC_CLS.isInstance(handler);
}

public static boolean isServer(final ChannelHandler handler) {
try {
return IS_SERVER_FIELD != null && (boolean) IS_SERVER_FIELD.invokeExact(handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import datadog.trace.api.Config;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
Expand Down Expand Up @@ -54,8 +55,7 @@ public void write(final ChannelHandlerContext ctx, final Object msg, final Chann
}

AgentScope parentScope = null;
final AgentScope.Continuation continuation =
ctx.channel().attr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY).getAndRemove();
final AgentScope.Continuation continuation = takeConnectParentContinuation(ctx);
if (continuation != null) {
parentScope = continuation.activate();
}
Expand Down Expand Up @@ -111,4 +111,16 @@ public void write(final ChannelHandlerContext ctx, final Object msg, final Chann
}
}
}

private static AgentScope.Continuation takeConnectParentContinuation(
final ChannelHandlerContext ctx) {
final Channel channel = ctx.channel();
AgentScope.Continuation continuation =
channel.attr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY).getAndRemove();
if (continuation == null && channel.parent() != null) {
continuation =
channel.parent().attr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY).getAndRemove();
}
return continuation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public final class AttributeKeys {
CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY =
attributeKey("datadog.connect.parent.continuation");

public static final AttributeKey<Boolean> HTTP2_CONNECTION_CODEC_ATTRIBUTE_KEY =
attributeKey("datadog.http2.connection.codec");

public static final AttributeKey<Context> PARENT_CONTEXT_ATTRIBUTE_KEY =
attributeKey("datadog.server.parent-context");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ class ReactorNettyHttp2ClientTest extends InstrumentationSpecification {
.handle { req, res -> res.status(200).send() }
.bindNow()

@Override
boolean useStrictTraceWrites() {
false
}

@Override
def cleanupSpec() {
server?.disposeNow()
Expand Down
Loading