diff --git a/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/ChannelFutureListenerInstrumentation.java b/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/ChannelFutureListenerInstrumentation.java index af5f36113fa..179d54c8365 100644 --- a/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/ChannelFutureListenerInstrumentation.java +++ b/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/ChannelFutureListenerInstrumentation.java @@ -78,7 +78,7 @@ public static ContextScope activateScope(@Advice.Argument(0) final ChannelFuture - To capture scope only in case of error. */ final Throwable cause = future.cause(); - if (cause == null) { + if (cause == null || !future.channel().hasAttr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY)) { return null; } final ContextContinuation continuation = diff --git a/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/Http2ConnectContinuationListener.java b/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/Http2ConnectContinuationListener.java index c7709e3c39f..0163ed577c2 100644 --- a/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/Http2ConnectContinuationListener.java +++ b/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/Http2ConnectContinuationListener.java @@ -22,7 +22,7 @@ public void operationComplete(final ChannelFuture future) { } public static void cancel(final Channel channel) { - if (channel == null) { + if (channel == null || !channel.hasAttr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY)) { return; } final ContextContinuation continuation = diff --git a/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/NettyChannelHandlerContextInstrumentation.java b/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/NettyChannelHandlerContextInstrumentation.java index 0246d87e69d..7713946dea4 100644 --- a/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/NettyChannelHandlerContextInstrumentation.java +++ b/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/NettyChannelHandlerContextInstrumentation.java @@ -3,9 +3,9 @@ import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.implementsInterface; import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.nameStartsWith; import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; -import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; -import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan; +import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.currentContext; import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.spanFromContext; +import static datadog.trace.instrumentation.netty41.AttributeKeys.CLIENT_CALLBACK_SPAN_KEY; import static datadog.trace.instrumentation.netty41.AttributeKeys.CONTEXT_ATTRIBUTE_KEY; import static datadog.trace.instrumentation.netty41.NettyChannelPipelineInstrumentation.ADDITIONAL_INSTRUMENTATION_NAMES; import static datadog.trace.instrumentation.netty41.NettyChannelPipelineInstrumentation.INSTRUMENTATION_NAME; @@ -13,6 +13,7 @@ import static net.bytebuddy.matcher.ElementMatchers.isPublic; import com.google.auto.service.AutoService; +import datadog.context.Context; import datadog.context.ContextScope; import datadog.trace.agent.tooling.Instrumenter; import datadog.trace.agent.tooling.InstrumenterModule; @@ -71,13 +72,24 @@ public void methodAdvice(MethodTransformer transformer) { public static class FireAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) public static ContextScope scopeSpan(@Advice.This final ChannelHandlerContext ctx) { - final AgentSpan channelSpan = - spanFromContext(ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).get()); - if (channelSpan == null || channelSpan == activeSpan()) { + final Context channelContext = ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).get(); + final AgentSpan channelSpan = spanFromContext(channelContext); + if (channelSpan == null) { + return null; + } + final Context current = currentContext(); + if (channelSpan == spanFromContext(current) + || channelSpan == current.get(CLIENT_CALLBACK_SPAN_KEY)) { // don't modify the scope return null; } - return activateSpan(channelSpan); + if (ServerRequestContext.currentRequest(ctx.channel()) != null) { + // Keep baggage added by server handlers when restoring the server span. + return channelSpan.attachWithContext(); + } + // Client events such as TLS completion may arrive without request context. + // Restore the captured context so subsequent requests inherit its baggage. + return channelContext.attach(); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) diff --git a/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/client/HttpClientRequestTracingHandler.java b/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/client/HttpClientRequestTracingHandler.java index 47134f4a0cf..9426ba807b3 100644 --- a/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/client/HttpClientRequestTracingHandler.java +++ b/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/client/HttpClientRequestTracingHandler.java @@ -2,10 +2,12 @@ import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan; +import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan; import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan; import static datadog.trace.instrumentation.netty41.AttributeKeys.CLIENT_PARENT_ATTRIBUTE_KEY; import static datadog.trace.instrumentation.netty41.AttributeKeys.CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY; import static datadog.trace.instrumentation.netty41.AttributeKeys.CONTEXT_ATTRIBUTE_KEY; +import static datadog.trace.instrumentation.netty41.AttributeKeys.HTTP_CLIENT_UPGRADED_ATTRIBUTE_KEY; import static datadog.trace.instrumentation.netty41.client.NettyHttpClientDecorator.DECORATE; import static datadog.trace.instrumentation.netty41.client.NettyHttpClientDecorator.DECORATE_SECURE; import static datadog.trace.instrumentation.netty41.client.NettyHttpClientDecorator.NETTY_CLIENT; @@ -53,10 +55,22 @@ public void write(final ChannelHandlerContext ctx, final Object msg, final Chann return; } + // A new HTTP request can follow CONNECT or a rejected WebSocket handshake. + if (ctx.channel().hasAttr(HTTP_CLIENT_UPGRADED_ATTRIBUTE_KEY)) { + ctx.channel().attr(HTTP_CLIENT_UPGRADED_ATTRIBUTE_KEY).remove(); + } + ContextScope parentScope = null; final ContextContinuation continuation = takeConnectParentContinuation(ctx); if (continuation != null) { - parentScope = continuation.resume(); + // Root and a bare noop span carry no caller data, so use the connection context. + // Otherwise, keep the current context and release the unused continuation. + final Context current = Context.current(); + if (current == Context.root() || current == noopSpan()) { + parentScope = continuation.resume(); + } else { + continuation.release(); + } } final HttpRequest request = (HttpRequest) msg; @@ -114,9 +128,13 @@ public void write(final ChannelHandlerContext ctx, final Object msg, final Chann private static ContextContinuation takeConnectParentContinuation( final ChannelHandlerContext ctx) { final Channel channel = ctx.channel(); - ContextContinuation continuation = - channel.attr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY).getAndRemove(); - if (continuation == null && channel.parent() != null) { + ContextContinuation continuation = null; + if (channel.hasAttr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY)) { + continuation = channel.attr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY).getAndRemove(); + } + if (continuation == null + && channel.parent() != null + && channel.parent().hasAttr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY)) { continuation = channel.parent().attr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY).getAndRemove(); } diff --git a/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/client/HttpClientResponseTracingHandler.java b/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/client/HttpClientResponseTracingHandler.java index b80f967a3fb..68f272c225f 100644 --- a/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/client/HttpClientResponseTracingHandler.java +++ b/dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/client/HttpClientResponseTracingHandler.java @@ -1,10 +1,13 @@ package datadog.trace.instrumentation.netty41.client; -import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan; +import static datadog.trace.bootstrap.instrumentation.api.Tags.HTTP_METHOD; +import static datadog.trace.instrumentation.netty41.AttributeKeys.CLIENT_CALLBACK_SPAN_KEY; import static datadog.trace.instrumentation.netty41.AttributeKeys.CLIENT_PARENT_ATTRIBUTE_KEY; import static datadog.trace.instrumentation.netty41.AttributeKeys.CONTEXT_ATTRIBUTE_KEY; +import static datadog.trace.instrumentation.netty41.AttributeKeys.HTTP_CLIENT_UPGRADED_ATTRIBUTE_KEY; import static datadog.trace.instrumentation.netty41.client.NettyHttpClientDecorator.DECORATE; +import static io.netty.handler.codec.http.HttpMethod.CONNECT; import datadog.context.Context; import datadog.context.ContextScope; @@ -13,8 +16,10 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.LastHttpContent; import io.netty.util.Attribute; @ChannelHandler.Sharable @@ -25,91 +30,98 @@ public class HttpClientResponseTracingHandler extends ChannelInboundHandlerAdapt @Override public void channelRead(final ChannelHandlerContext ctx, final Object msg) { final Attribute parentAttr = ctx.channel().attr(CLIENT_PARENT_ATTRIBUTE_KEY); - parentAttr.setIfAbsent(noopSpan()); - final AgentSpan parent = parentAttr.get(); - final Context storedContext = ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).get(); + final AgentSpan storedParent = parentAttr.get(); + final AgentSpan parent = storedParent == null ? noopSpan() : storedParent; + final Attribute contextAttr = ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY); + final Context storedContext = contextAttr.get(); final AgentSpan span = AgentSpan.fromContext(storedContext); + final boolean hasClientSpan = span != null && span != parent; + final Context callbackContext = storedContext == null ? parent : storedContext.with(parent); - // Set parent context back to maintain the same functionality as getAndSet(parent) - if (storedContext != null) { - ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).set(storedContext.with(parent)); + final HttpResponse response = msg instanceof HttpResponse ? (HttpResponse) msg : null; + final int status = response == null ? 0 : response.status().code(); + final boolean websocketUpgrade = + status == HttpResponseStatus.SWITCHING_PROTOCOLS.code() + && response + .headers() + .containsValue(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET, true); + final boolean upgraded = + websocketUpgrade + || (hasClientSpan + && status >= 200 + && status < 300 + && CONNECT.name().equals(span.getTag(HTTP_METHOD))); + if (upgraded) { + // WebSocket and CONNECT retain context until the next HTTP request or channel close. + ctx.channel().attr(HTTP_CLIENT_UPGRADED_ATTRIBUTE_KEY).set(Boolean.TRUE); } + final boolean finishSpan = hasClientSpan && (status >= 200 || upgraded); - if (span != null) { - final boolean finishSpan = - msg instanceof HttpResponse - && (!HttpResponseStatus.SWITCHING_PROTOCOLS.equals(((HttpResponse) msg).status()) - || "websocket" - .equals(((HttpResponse) msg).headers().get(HttpHeaderNames.UPGRADE))); - if (finishSpan) { - try (final ContextScope scope = activateSpan(span)) { - DECORATE.onResponse(span, (HttpResponse) msg); - DECORATE.beforeFinish(span); - span.finish(); - } - } else { - if (storedContext != null) { - ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).set(storedContext); - } + if (finishSpan) { + // Keep the parent context for body chunks arriving after the client span finishes. + contextAttr.set(callbackContext); + try (final ContextScope scope = storedContext.attach()) { + DECORATE.onResponse(span, response); + DECORATE.beforeFinish(span); + span.finish(); } } - // We want the callback in the scope of the parent, not the client span - try (final ContextScope scope = activateSpan(parent)) { + // Informational responses retain the client span for completion, but fire-method advice must + // leave their callbacks under the parent, including when downstream handlers forward again. + final Context deliveryContext = + hasClientSpan && !finishSpan + ? callbackContext.with(CLIENT_CALLBACK_SPAN_KEY, span) + : callbackContext; + try (final ContextScope scope = deliveryContext.attach()) { + // Clear completed HTTP exchanges before callbacks can start the next request. + // The attached scope keeps this response's context active during delivery. + if (msg instanceof LastHttpContent + && (finishSpan || span == parent) + && !ctx.channel().hasAttr(HTTP_CLIENT_UPGRADED_ATTRIBUTE_KEY)) { + contextAttr.set(null); + parentAttr.set(null); + } ctx.fireChannelRead(msg); } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - final Attribute parentAttr = ctx.channel().attr(CLIENT_PARENT_ATTRIBUTE_KEY); - parentAttr.setIfAbsent(noopSpan()); - final AgentSpan parent = parentAttr.get(); - final Context storedContext = ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).get(); - final AgentSpan span = AgentSpan.fromContext(storedContext); - - // Set parent context back to maintain the same functionality as getAndSet(parent) - if (storedContext != null) { - ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).set(storedContext.with(parent)); - } - - if (span != null) { - // If an exception is passed to this point, it likely means it was unhandled and the - // client span won't be finished with a proper response, so we should finish the span here. - try (final ContextScope scope = activateSpan(span)) { - DECORATE.onError(span, cause); - DECORATE.beforeFinish(span); - span.finish(); - } - } - // We want the callback in the scope of the parent, not the client span - try (final ContextScope scope = activateSpan(parent)) { + // Upgraded protocols can recover from errors, so retain context until the channel closes. + final Context callbackContext = + finishExchange(ctx, cause, !ctx.channel().hasAttr(HTTP_CLIENT_UPGRADED_ATTRIBUTE_KEY)); + try (final ContextScope scope = callbackContext.attach()) { super.exceptionCaught(ctx, cause); } } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { + final Context callbackContext = finishExchange(ctx, null, true); + try (final ContextScope scope = callbackContext.attach()) { + super.channelInactive(ctx); + } + } + + private static Context finishExchange(ChannelHandlerContext ctx, Throwable cause, boolean clear) { + // Clear HTTP exchange state before callbacks can start the next request. final Attribute parentAttr = ctx.channel().attr(CLIENT_PARENT_ATTRIBUTE_KEY); - parentAttr.setIfAbsent(noopSpan()); - final AgentSpan parent = parentAttr.get(); - final Context storedContext = ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).get(); + final AgentSpan storedParent = clear ? parentAttr.getAndSet(null) : parentAttr.get(); + final AgentSpan parent = storedParent == null ? noopSpan() : storedParent; + final Attribute contextAttr = ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY); + final Context storedContext = clear ? contextAttr.getAndSet(null) : contextAttr.get(); final AgentSpan span = AgentSpan.fromContext(storedContext); - // Set parent context back to maintain the same functionality as getAndSet(parent) - if (storedContext != null) { - ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).set(storedContext.with(parent)); - } - if (span != null && span != parent) { - try (final ContextScope scope = activateSpan(span)) { + try (final ContextScope scope = storedContext.attach()) { + if (cause != null) { + DECORATE.onError(span, cause); + } DECORATE.beforeFinish(span); span.finish(); } } - // We want the callback in the scope of the parent, not the client span - try (final ContextScope scope = activateSpan(parent)) { - super.channelInactive(ctx); - } + return storedContext == null ? parent : storedContext.with(parent); } } diff --git a/dd-java-agent/instrumentation/netty/netty-4.1/src/test/java/datadog/trace/instrumentation/netty41/client/NettyClientContextLifecycleTest.java b/dd-java-agent/instrumentation/netty/netty-4.1/src/test/java/datadog/trace/instrumentation/netty41/client/NettyClientContextLifecycleTest.java new file mode 100644 index 00000000000..e966f00a703 --- /dev/null +++ b/dd-java-agent/instrumentation/netty/netty-4.1/src/test/java/datadog/trace/instrumentation/netty41/client/NettyClientContextLifecycleTest.java @@ -0,0 +1,598 @@ +package datadog.trace.instrumentation.netty41.client; + +import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan; +import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan; +import static datadog.trace.instrumentation.netty41.AttributeKeys.CLIENT_PARENT_ATTRIBUTE_KEY; +import static datadog.trace.instrumentation.netty41.AttributeKeys.CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY; +import static datadog.trace.instrumentation.netty41.AttributeKeys.CONTEXT_ATTRIBUTE_KEY; +import static datadog.trace.instrumentation.netty41.AttributeKeys.HTTP_CLIENT_UPGRADED_ATTRIBUTE_KEY; +import static io.netty.handler.codec.http.HttpResponseStatus.OK; +import static io.netty.handler.codec.http.HttpResponseStatus.SWITCHING_PROTOCOLS; +import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; +import static io.netty.util.ReferenceCountUtil.release; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import datadog.context.Context; +import datadog.context.ContextKey; +import datadog.context.ContextScope; +import datadog.trace.agent.test.AbstractInstrumentationTest; +import datadog.trace.bootstrap.instrumentation.api.AgentSpan; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.channel.embedded.EmbeddedChannel; +import io.netty.handler.codec.DecoderException; +import io.netty.handler.codec.http.DefaultFullHttpRequest; +import io.netty.handler.codec.http.DefaultFullHttpResponse; +import io.netty.handler.codec.http.DefaultHttpHeaders; +import io.netty.handler.codec.http.DefaultHttpResponse; +import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpMethod; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.LastHttpContent; +import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; +import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker; +import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory; +import io.netty.handler.codec.http.websocketx.WebSocketHandshakeException; +import io.netty.handler.codec.http.websocketx.WebSocketVersion; +import java.net.URI; +import java.util.concurrent.atomic.AtomicReference; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.tabletest.junit.TableTest; + +class NettyClientContextLifecycleTest extends AbstractInstrumentationTest { + + @Test + void callerContextTakesPrecedenceOverConnectionCreationContext() { + ContextKey requestKey = ContextKey.named("netty-test-request"); + AgentSpan poolParent = startSpan("test", "pool-parent"); + AgentSpan requestParent = startSpan("test", "request-parent"); + EmbeddedChannel channel = new EmbeddedChannel(new HttpClientTracingHandler()); + channel + .attr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY) + .set(Context.root().with(poolParent).with(requestKey, "pool").capture()); + try { + try (ContextScope ignored = + Context.root().with(requestParent).with(requestKey, "request").attach()) { + writeRequest(channel); + assertSame(requestParent, channel.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); + assertEquals("request", channel.attr(CONTEXT_ATTRIBUTE_KEY).get().get(requestKey)); + assertNull(channel.attr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY).get()); + } + } finally { + completeResponse(channel); + channel.finishAndReleaseAll(); + requestParent.finish(); + poolParent.finish(); + } + } + + @Test + void callerContextWithoutASpanStillTakesPrecedenceOverConnectionContext() { + ContextKey requestKey = ContextKey.named("netty-test-request"); + AgentSpan poolParent = startSpan("test", "pool-parent"); + EmbeddedChannel channel = new EmbeddedChannel(new HttpClientTracingHandler()); + channel + .attr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY) + .set(Context.root().with(poolParent).capture()); + try { + try (ContextScope ignored = Context.root().with(requestKey, "request").attach()) { + writeRequest(channel); + assertNull(channel.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); + assertEquals("request", channel.attr(CONTEXT_ATTRIBUTE_KEY).get().get(requestKey)); + } + } finally { + completeResponse(channel); + channel.finishAndReleaseAll(); + poolParent.finish(); + } + } + + @Test + void connectionCreationContextRemainsTheFallbackWithoutCallerContext() { + ContextKey requestKey = ContextKey.named("netty-test-request"); + AgentSpan parent = startSpan("test", "connection-parent"); + EmbeddedChannel channel = new EmbeddedChannel(new HttpClientTracingHandler()); + channel + .attr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY) + .set(Context.root().with(parent).with(requestKey, "connection").capture()); + try { + writeRequest(channel); + assertSame(parent, channel.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); + assertEquals("connection", channel.attr(CONTEXT_ATTRIBUTE_KEY).get().get(requestKey)); + assertNull(channel.attr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY).get()); + } finally { + completeResponse(channel); + channel.finishAndReleaseAll(); + parent.finish(); + } + } + + @Test + void untrackedCallbackDoesNotHideConnectionCreationContext() { + ContextKey requestKey = ContextKey.named("netty-test-request"); + AgentSpan parent = startSpan("test", "connection-parent"); + EmbeddedChannel destination = new EmbeddedChannel(new HttpClientTracingHandler()); + destination + .attr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY) + .set(Context.root().with(parent).with(requestKey, "connection").capture()); + EmbeddedChannel untracked = + new EmbeddedChannel( + new HttpClientTracingHandler(), + new ChannelInboundHandlerAdapter() { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + assertSame(noopSpan(), Context.current()); + writeRequest(destination); + ctx.fireChannelRead(msg); + } + }); + try { + completeResponse(untracked); + assertNull(untracked.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); + assertSame(parent, destination.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); + assertEquals("connection", destination.attr(CONTEXT_ATTRIBUTE_KEY).get().get(requestKey)); + } finally { + completeResponse(destination); + untracked.finishAndReleaseAll(); + destination.finishAndReleaseAll(); + parent.finish(); + } + } + + @Test + void noopSpanWithCallerDataStillTakesPrecedenceOverConnectionContext() { + ContextKey requestKey = ContextKey.named("netty-test-request"); + AgentSpan parent = startSpan("test", "connection-parent"); + EmbeddedChannel channel = new EmbeddedChannel(new HttpClientTracingHandler()); + channel.attr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY).set(parent.capture()); + try { + try (ContextScope ignored = + Context.root().with(noopSpan()).with(requestKey, "caller").attach()) { + writeRequest(channel); + assertSame(noopSpan(), channel.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); + assertEquals("caller", channel.attr(CONTEXT_ATTRIBUTE_KEY).get().get(requestKey)); + } + } finally { + completeResponse(channel); + channel.finishAndReleaseAll(); + parent.finish(); + } + } + + @TableTest({ + "Scenario | Fail | Headers received", + "error before headers | true | false ", + "inactive before headers | false | false ", + "error after headers | true | true ", + "inactive after headers | false | true " + }) + void terminalCallbacksPreserveContextAndClearExchange(boolean fail, boolean headersReceived) { + ContextKey requestKey = ContextKey.named("netty-test-request"); + AgentSpan parent = startSpan("test", "request-parent"); + AtomicReference seenValue = new AtomicReference<>(); + AtomicReference seenSpan = new AtomicReference<>(); + AtomicReference seenEvent = new AtomicReference<>(); + EmbeddedChannel channel = + new EmbeddedChannel( + new HttpClientTracingHandler(), + new ChannelInboundHandlerAdapter() { + private void record(String event) { + seenValue.set(Context.current().get(requestKey)); + seenSpan.set(AgentSpan.current()); + seenEvent.set(event); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + record("error"); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) { + record("inactive"); + ctx.fireChannelInactive(); + } + }); + try { + try (ContextScope ignored = + Context.root().with(parent).with(requestKey, "request").attach()) { + writeRequest(channel); + } + if (headersReceived) { + deliver(channel, new DefaultHttpResponse(HTTP_1_1, OK)); + } + if (fail) { + channel.pipeline().fireExceptionCaught(new IllegalStateException("request failed")); + } else { + channel.close(); + channel.runPendingTasks(); + } + assertEquals(fail ? "error" : "inactive", seenEvent.get()); + assertEquals("request", seenValue.get()); + assertSame(parent, seenSpan.get()); + assertEquals(0, parent.getDurationNano()); + assertNull(channel.attr(CONTEXT_ATTRIBUTE_KEY).get()); + assertNull(channel.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); + } finally { + channel.finishAndReleaseAll(); + parent.finish(); + } + } + + @TableTest({ + "Scenario | Fail ", + "response | false", + "error | true " + }) + void httpRequestAfterRejectedWebsocketHandshakeClearsExchange(boolean fail) { + AgentSpan handshakeParent = startSpan("test", "handshake-parent"); + AgentSpan httpParent = startSpan("test", "http-parent"); + AtomicReference failure = new AtomicReference<>(); + WebSocketClientHandshaker handshaker = + WebSocketClientHandshakerFactory.newHandshaker( + URI.create("ws://localhost/"), + WebSocketVersion.V13, + null, + false, + new DefaultHttpHeaders()); + EmbeddedChannel channel = + new EmbeddedChannel( + new HttpClientTracingHandler(), + new ChannelInboundHandlerAdapter() { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + if (msg instanceof FullHttpResponse + && SWITCHING_PROTOCOLS.equals(((FullHttpResponse) msg).status())) { + try { + handshaker.finishHandshake(ctx.channel(), (FullHttpResponse) msg); + } finally { + release(msg); + } + } else { + ctx.fireChannelRead(msg); + } + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + failure.set(cause); + } + }); + try { + try (ContextScope ignored = handshakeParent.attach()) { + writeRequest(channel); + } + DefaultFullHttpResponse upgrade = new DefaultFullHttpResponse(HTTP_1_1, SWITCHING_PROTOCOLS); + upgrade.headers().set("upgrade", "websocket"); + upgrade.headers().set("connection", "upgrade"); + upgrade.headers().set("sec-websocket-accept", "invalid"); + deliver(channel, upgrade); + assertInstanceOf(WebSocketHandshakeException.class, failure.get()); + assertFalse(handshaker.isHandshakeComplete()); + assertTrue(channel.isOpen()); + try (ContextScope ignored = httpParent.attach()) { + writeRequest(channel); + } + assertSame(httpParent, channel.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); + if (fail) { + channel.pipeline().fireExceptionCaught(new IllegalStateException("request failed")); + } else { + completeResponse(channel); + } + assertNull(channel.attr(CONTEXT_ATTRIBUTE_KEY).get()); + assertNull(channel.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); + assertFalse(channel.hasAttr(HTTP_CLIENT_UPGRADED_ATTRIBUTE_KEY)); + } finally { + channel.finishAndReleaseAll(); + httpParent.finish(); + handshakeParent.finish(); + } + } + + @Test + void connectRetainsContextUntilTheTunneledRequest() { + ContextKey requestKey = ContextKey.named("netty-test-request"); + AgentSpan parent = startSpan("test", "request-parent"); + EmbeddedChannel channel = + new EmbeddedChannel( + new HttpClientTracingHandler(), + new ChannelInboundHandlerAdapter() { + @Override + public void userEventTriggered(ChannelHandlerContext ctx, Object event) { + // TLS handshake completion starts the tunneled request after CONNECT delivery. + writeRequest((EmbeddedChannel) ctx.channel()); + } + }); + try { + try (ContextScope ignored = + Context.root().with(parent).with(requestKey, "request").attach()) { + writeRequest(channel, HttpMethod.CONNECT); + } + AgentSpan connect = AgentSpan.fromContext(channel.attr(CONTEXT_ATTRIBUTE_KEY).get()); + completeResponse(channel); + assertTrue(connect.getDurationNano() > 0); + channel.pipeline().fireUserEventTriggered(new Object()); + assertSame(parent, channel.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); + assertEquals("request", channel.attr(CONTEXT_ATTRIBUTE_KEY).get().get(requestKey)); + assertFalse(channel.hasAttr(HTTP_CLIENT_UPGRADED_ATTRIBUTE_KEY)); + completeResponse(channel); + assertNull(channel.attr(CONTEXT_ATTRIBUTE_KEY).get()); + assertNull(channel.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); + } finally { + channel.finishAndReleaseAll(); + parent.finish(); + } + } + + @Test + void rejectedConnectClearsExchangeContext() { + AgentSpan parent = startSpan("test", "request-parent"); + EmbeddedChannel channel = new EmbeddedChannel(new HttpClientTracingHandler()); + try { + try (ContextScope ignored = parent.attach()) { + writeRequest(channel, HttpMethod.CONNECT); + } + AgentSpan connect = AgentSpan.fromContext(channel.attr(CONTEXT_ATTRIBUTE_KEY).get()); + deliver( + channel, + new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.PROXY_AUTHENTICATION_REQUIRED)); + assertTrue(connect.getDurationNano() > 0); + assertNull(channel.attr(CONTEXT_ATTRIBUTE_KEY).get()); + assertNull(channel.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); + assertFalse(channel.hasAttr(HTTP_CLIENT_UPGRADED_ATTRIBUTE_KEY)); + } finally { + channel.finishAndReleaseAll(); + parent.finish(); + } + } + + @Test + void contextIsRetainedThroughResponseBodyAndClearedAtItsEnd() { + ContextKey requestKey = ContextKey.named("netty-test-request"); + AgentSpan parent = startSpan("test", "request-parent"); + EmbeddedChannel channel = new EmbeddedChannel(new HttpClientTracingHandler()); + try { + try (ContextScope ignored = + Context.root().with(parent).with(requestKey, "request").attach()) { + writeRequest(channel); + } + deliver(channel, new DefaultHttpResponse(HTTP_1_1, OK)); + assertSame(parent, AgentSpan.fromContext(channel.attr(CONTEXT_ATTRIBUTE_KEY).get())); + deliver(channel, LastHttpContent.EMPTY_LAST_CONTENT); + assertNull(channel.attr(CONTEXT_ATTRIBUTE_KEY).get()); + assertNull(channel.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); + } finally { + channel.finishAndReleaseAll(); + parent.finish(); + } + } + + @Test + void responseCallbacksRestoreTheFullRequestContext() { + ContextKey requestKey = ContextKey.named("netty-test-request"); + AgentSpan parent = startSpan("test", "request-parent"); + AtomicReference seen = new AtomicReference<>(); + EmbeddedChannel channel = + new EmbeddedChannel( + new HttpClientTracingHandler(), + new ChannelInboundHandlerAdapter() { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + seen.set(Context.current().get(requestKey)); + ctx.fireChannelRead(msg); + } + }); + try { + try (ContextScope ignored = + Context.root().with(parent).with(requestKey, "request").attach()) { + writeRequest(channel); + } + completeResponse(channel); + assertEquals("request", seen.get()); + } finally { + channel.finishAndReleaseAll(); + parent.finish(); + } + } + + @Test + void completingAResponseDoesNotClearARequestStartedByItsCallback() { + AgentSpan parent = startSpan("test", "request-parent"); + AtomicReference next = new AtomicReference<>(); + EmbeddedChannel channel = + new EmbeddedChannel( + new HttpClientTracingHandler(), + new ChannelInboundHandlerAdapter() { + private boolean first = true; + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + if (first) { + first = false; + writeRequest((EmbeddedChannel) ctx.channel()); + next.set(ctx.channel().attr(CONTEXT_ATTRIBUTE_KEY).get()); + } + ctx.fireChannelRead(msg); + } + }); + try { + try (ContextScope ignored = Context.root().with(parent).attach()) { + writeRequest(channel); + } + completeResponse(channel); + assertSame(next.get(), channel.attr(CONTEXT_ATTRIBUTE_KEY).get()); + completeResponse(channel); + } finally { + channel.finishAndReleaseAll(); + parent.finish(); + } + } + + @TableTest({ + "Scenario | Status", + "continue | 100 ", + "early hints | 103 ", + "other upgrade | 101 " + }) + void informationalResponseRestoresParentWithoutCompletingTheExchange(int status) { + ContextKey requestKey = ContextKey.named("netty-test-request"); + AgentSpan parent = startSpan("test", "request-parent"); + AtomicReference seenSpan = new AtomicReference<>(); + AtomicReference seenValue = new AtomicReference<>(); + EmbeddedChannel channel = + new EmbeddedChannel( + new HttpClientTracingHandler(), + new ChannelInboundHandlerAdapter(), + new ChannelInboundHandlerAdapter() { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + seenSpan.set(AgentSpan.current()); + seenValue.set(Context.current().get(requestKey)); + ctx.fireChannelRead(msg); + } + }); + try { + try (ContextScope ignored = + Context.root().with(parent).with(requestKey, "request").attach()) { + writeRequest(channel); + } + Context original = channel.attr(CONTEXT_ATTRIBUTE_KEY).get(); + AgentSpan client = AgentSpan.fromContext(original); + deliver(channel, new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(status))); + assertSame(parent, seenSpan.get()); + assertEquals("request", seenValue.get()); + assertSame(original, channel.attr(CONTEXT_ATTRIBUTE_KEY).get()); + assertEquals(0, client.getDurationNano()); + completeResponse(channel); + assertTrue(client.getDurationNano() > 0); + assertNull(channel.attr(CONTEXT_ATTRIBUTE_KEY).get()); + assertNull(channel.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); + } finally { + channel.finishAndReleaseAll(); + parent.finish(); + } + } + + @ParameterizedTest + @ValueSource(strings = {"websocket", "WebSocket"}) + void websocketUpgradeRetainsItsParentContext(String protocol) { + AgentSpan parent = startSpan("test", "request-parent"); + EmbeddedChannel channel = new EmbeddedChannel(new HttpClientTracingHandler()); + try { + try (ContextScope ignored = Context.root().with(parent).attach()) { + writeRequest(channel); + } + DefaultFullHttpResponse upgrade = new DefaultFullHttpResponse(HTTP_1_1, SWITCHING_PROTOCOLS); + upgrade.headers().set("upgrade", protocol); + deliver(channel, upgrade); + assertSame(parent, AgentSpan.fromContext(channel.attr(CONTEXT_ATTRIBUTE_KEY).get())); + } finally { + channel.finishAndReleaseAll(); + parent.finish(); + } + } + + @Test + void websocketExceptionRetainsContextForLaterFrames() { + ContextKey requestKey = ContextKey.named("netty-test-request"); + AgentSpan parent = startSpan("test", "request-parent"); + AtomicReference seenValue = new AtomicReference<>(); + AtomicReference seenSpan = new AtomicReference<>(); + EmbeddedChannel channel = + new EmbeddedChannel( + new ChannelInboundHandlerAdapter() { + private boolean firstFrame = true; + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + if (msg instanceof TextWebSocketFrame && firstFrame) { + firstFrame = false; + release(msg); + throw new DecoderException("recoverable frame error"); + } + ctx.fireChannelRead(msg); + } + }, + new HttpClientTracingHandler(), + new ChannelInboundHandlerAdapter() { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + if (msg instanceof TextWebSocketFrame) { + seenValue.set(Context.current().get(requestKey)); + seenSpan.set(AgentSpan.current()); + } + ctx.fireChannelRead(msg); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + assertSame(parent, AgentSpan.current()); + } + }); + try { + try (ContextScope ignored = + Context.root().with(parent).with(requestKey, "request").attach()) { + writeRequest(channel); + } + DefaultFullHttpResponse upgrade = new DefaultFullHttpResponse(HTTP_1_1, SWITCHING_PROTOCOLS); + upgrade.headers().set("upgrade", "websocket"); + deliver(channel, upgrade); + deliver(channel, new TextWebSocketFrame("invalid")); + deliver(channel, new TextWebSocketFrame("valid")); + assertEquals("request", seenValue.get()); + assertSame(parent, seenSpan.get()); + assertEquals(0, parent.getDurationNano()); + channel.close(); + channel.runPendingTasks(); + assertNull(channel.attr(CONTEXT_ATTRIBUTE_KEY).get()); + assertNull(channel.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); + } finally { + channel.finishAndReleaseAll(); + parent.finish(); + } + } + + @Test + void streamedWebsocketUpgradeRetainsItsParentContext() { + AgentSpan parent = startSpan("test", "request-parent"); + EmbeddedChannel channel = new EmbeddedChannel(new HttpClientTracingHandler()); + try { + try (ContextScope ignored = Context.root().with(parent).attach()) { + writeRequest(channel); + } + DefaultHttpResponse upgrade = new DefaultHttpResponse(HTTP_1_1, SWITCHING_PROTOCOLS); + upgrade.headers().set("upgrade", "websocket"); + deliver(channel, upgrade); + deliver(channel, LastHttpContent.EMPTY_LAST_CONTENT); + assertSame(parent, AgentSpan.fromContext(channel.attr(CONTEXT_ATTRIBUTE_KEY).get())); + } finally { + channel.finishAndReleaseAll(); + parent.finish(); + } + } + + private static void writeRequest(EmbeddedChannel channel) { + writeRequest(channel, HttpMethod.GET); + } + + private static void writeRequest(EmbeddedChannel channel, HttpMethod method) { + channel.writeOutbound(new DefaultFullHttpRequest(HTTP_1_1, method, "/")); + release(channel.readOutbound()); + } + + private static void completeResponse(EmbeddedChannel channel) { + deliver(channel, new DefaultFullHttpResponse(HTTP_1_1, OK)); + } + + private static void deliver(EmbeddedChannel channel, Object message) { + channel.writeInbound(message); + release(channel.readInbound()); + } +} diff --git a/dd-java-agent/instrumentation/netty/netty-4.1/src/test/java/datadog/trace/instrumentation/netty41/server/NettyServerContextLifecycleTest.java b/dd-java-agent/instrumentation/netty/netty-4.1/src/test/java/datadog/trace/instrumentation/netty41/server/NettyServerContextLifecycleTest.java new file mode 100644 index 00000000000..76a307cfad3 --- /dev/null +++ b/dd-java-agent/instrumentation/netty/netty-4.1/src/test/java/datadog/trace/instrumentation/netty41/server/NettyServerContextLifecycleTest.java @@ -0,0 +1,81 @@ +package datadog.trace.instrumentation.netty41.server; + +import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan; +import static datadog.trace.instrumentation.netty41.AttributeKeys.CLIENT_PARENT_ATTRIBUTE_KEY; +import static datadog.trace.instrumentation.netty41.AttributeKeys.CONTEXT_ATTRIBUTE_KEY; +import static io.netty.handler.codec.http.HttpMethod.GET; +import static io.netty.handler.codec.http.HttpResponseStatus.OK; +import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; +import static io.netty.util.ReferenceCountUtil.release; +import static java.util.Collections.singletonMap; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; + +import datadog.context.Context; +import datadog.context.ContextScope; +import datadog.trace.agent.test.AbstractInstrumentationTest; +import datadog.trace.bootstrap.instrumentation.api.AgentSpan; +import datadog.trace.bootstrap.instrumentation.api.Baggage; +import datadog.trace.instrumentation.netty41.client.HttpClientTracingHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.channel.embedded.EmbeddedChannel; +import io.netty.handler.codec.http.DefaultFullHttpRequest; +import io.netty.handler.codec.http.DefaultFullHttpResponse; +import java.util.concurrent.atomic.AtomicReference; +import org.junit.jupiter.api.Test; + +class NettyServerContextLifecycleTest extends AbstractInstrumentationTest { + + @Test + void forwardingWithAChildSpanPreservesHandlerContextForDownstreamWork() { + Baggage handlerBaggage = Baggage.create(singletonMap("handler", "handler-value")); + AtomicReference serverSpan = new AtomicReference<>(); + AtomicReference downstreamContext = new AtomicReference<>(); + EmbeddedChannel client = new EmbeddedChannel(new HttpClientTracingHandler()); + EmbeddedChannel server = + new EmbeddedChannel( + new HttpServerTracingHandler(), + new ChannelInboundHandlerAdapter() { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + serverSpan.set(AgentSpan.current()); + AgentSpan child = startSpan("test", "handler-child"); + try (ContextScope ignored = + Context.current().with(handlerBaggage).with(child).attach()) { + ctx.fireChannelRead(msg); + assertSame(child, AgentSpan.current()); + } finally { + child.finish(); + } + } + }, + new ChannelInboundHandlerAdapter() { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + downstreamContext.set(Context.current()); + client.writeOutbound(new DefaultFullHttpRequest(HTTP_1_1, GET, "/downstream")); + release(client.readOutbound()); + ctx.fireChannelRead(msg); + } + }); + try { + server.writeInbound(new DefaultFullHttpRequest(HTTP_1_1, GET, "/")); + release(server.readInbound()); + + assertNotNull(serverSpan.get()); + assertNotNull(downstreamContext.get()); + assertSame(serverSpan.get(), AgentSpan.fromContext(downstreamContext.get())); + assertSame(handlerBaggage, Baggage.fromContext(downstreamContext.get())); + assertSame(serverSpan.get(), client.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); + assertSame(handlerBaggage, Baggage.fromContext(client.attr(CONTEXT_ATTRIBUTE_KEY).get())); + } finally { + client.writeInbound(new DefaultFullHttpResponse(HTTP_1_1, OK)); + release(client.readInbound()); + server.writeOutbound(new DefaultFullHttpResponse(HTTP_1_1, OK)); + release(server.readOutbound()); + client.finishAndReleaseAll(); + server.finishAndReleaseAll(); + } + } +} diff --git a/dd-java-agent/instrumentation/netty/netty-common/src/main/java/datadog/trace/instrumentation/netty41/AttributeKeys.java b/dd-java-agent/instrumentation/netty/netty-common/src/main/java/datadog/trace/instrumentation/netty41/AttributeKeys.java index addf40b55d6..97ee670ca98 100644 --- a/dd-java-agent/instrumentation/netty/netty-common/src/main/java/datadog/trace/instrumentation/netty41/AttributeKeys.java +++ b/dd-java-agent/instrumentation/netty/netty-common/src/main/java/datadog/trace/instrumentation/netty41/AttributeKeys.java @@ -4,6 +4,7 @@ import datadog.context.Context; import datadog.context.ContextContinuation; +import datadog.context.ContextKey; import datadog.trace.api.GenericClassValue; import datadog.trace.bootstrap.instrumentation.api.AgentSpan; import datadog.trace.bootstrap.instrumentation.websocket.HandlerContext; @@ -22,12 +23,20 @@ public final class AttributeKeys { public static final AttributeKey CLIENT_PARENT_ATTRIBUTE_KEY = attributeKey("datadog.client.parent.span"); + /** Identifies the unfinished client span whose callbacks are running under its parent. */ + public static final ContextKey CLIENT_CALLBACK_SPAN_KEY = + ContextKey.named("netty-client-callback-span"); + public static final AttributeKey CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY = attributeKey("datadog.connect.parent.continuation"); public static final AttributeKey HTTP2_CONNECTION_CODEC_ATTRIBUTE_KEY = attributeKey("datadog.http2.connection.codec"); + /** Retains WebSocket or tunnel context until the next HTTP request or channel close. */ + public static final AttributeKey HTTP_CLIENT_UPGRADED_ATTRIBUTE_KEY = + attributeKey("datadog.http.client.upgraded"); + public static final AttributeKey PARENT_CONTEXT_ATTRIBUTE_KEY = attributeKey("datadog.server.parent-context");