-
Notifications
You must be signed in to change notification settings - Fork 345
Support W3C baggage propagation across reactor-netty connect hand-off #12027
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mcculls
wants to merge
1
commit into
master
Choose a base branch
from
mcculls/fix-reactor-w3c-propagation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 7 additions & 9 deletions
16
...tty-1.0/src/main/java/datadog/trace/instrumentation/reactor/netty/CaptureConnectSpan.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 15 additions & 15 deletions
30
...ty-1.0/src/main/java/datadog/trace/instrumentation/reactor/netty/TransferConnectSpan.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,29 @@ | ||
| package datadog.trace.instrumentation.reactor.netty; | ||
|
|
||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureSpan; | ||
| import static datadog.trace.instrumentation.netty41.AttributeKeys.CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY; | ||
| import static datadog.trace.instrumentation.reactor.netty.CaptureConnectSpan.CONNECT_SPAN; | ||
| import static datadog.trace.instrumentation.reactor.netty.CaptureConnectSpan.CONNECT_CONTEXT; | ||
|
|
||
| import datadog.context.Context; | ||
| import datadog.context.ContextContinuation; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import java.util.function.BiConsumer; | ||
| import reactor.netty.Connection; | ||
| import reactor.netty.http.client.HttpClientRequest; | ||
|
|
||
| public class TransferConnectSpan implements BiConsumer<HttpClientRequest, Connection> { | ||
| @Override | ||
| public void accept(HttpClientRequest httpClientRequest, Connection connection) { | ||
| final AgentSpan span = httpClientRequest.currentContextView().getOrDefault(CONNECT_SPAN, null); | ||
| final ContextContinuation continuation = null == span ? null : captureSpan(span); | ||
| if (null != continuation) { | ||
| ContextContinuation current = | ||
| connection | ||
| .channel() | ||
| .attr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY) | ||
| .getAndSet(continuation); | ||
| if (null != current) { | ||
| current.release(); | ||
| } | ||
| public void accept(HttpClientRequest clientRequest, Connection connection) { | ||
| final Context context = clientRequest.currentContextView().getOrDefault(CONNECT_CONTEXT, null); | ||
| if (null == context) { | ||
| return; | ||
| } | ||
| ContextContinuation newContinuation = context.capture(); | ||
| ContextContinuation oldContinuation = | ||
| connection | ||
| .channel() | ||
| .attr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY) | ||
| .getAndSet(newContinuation); | ||
| if (null != oldContinuation) { | ||
| oldContinuation.release(); | ||
| } | ||
| } | ||
| } | ||
100 changes: 100 additions & 0 deletions
100
...t/instrumentation/reactor-netty-1.0/src/test/java/ReactorNettyBaggagePropagationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import com.sun.net.httpserver.HttpServer; | ||
| import datadog.context.Context; | ||
| import datadog.context.ContextScope; | ||
| import datadog.trace.agent.test.AbstractInstrumentationTest; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentScope; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentTracer; | ||
| import datadog.trace.bootstrap.instrumentation.api.Baggage; | ||
| import java.io.IOException; | ||
| import java.net.InetSocketAddress; | ||
| import java.time.Duration; | ||
| import java.util.Collections; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import reactor.netty.http.client.HttpClient; | ||
|
|
||
| /** | ||
| * Regression test for the W3C baggage header not propagating on outgoing Reactor Netty requests. | ||
| * | ||
| * <p>The bug: the connect-span path carried only {@code activeSpan()} across the subscription -> | ||
| * I/O thread hand-off, dropping the rest of the Datadog {@code Context} (including baggage). The | ||
| * outgoing request then had no baggage to inject, so the {@code baggage} header was silently | ||
| * skipped. This test sets a baggage item in the active context, makes one outgoing request, and | ||
| * asserts the server received the {@code baggage} header. | ||
| * | ||
| * <p>It is a <em>coupling</em> test: producer (sets baggage) -> Reactor Netty carrier -> consumer | ||
| * (injects the header). The failure it guards against lives in the hand-off between integrations, | ||
| * not in any one of them, so per-integration tests would not catch it. | ||
| */ | ||
| class ReactorNettyBaggagePropagationTest extends AbstractInstrumentationTest { | ||
|
mcculls marked this conversation as resolved.
|
||
|
|
||
| private static HttpServer mockServer; | ||
| private static String baseUrl; | ||
| private static final AtomicReference<String> capturedBaggage = new AtomicReference<>(); | ||
|
|
||
| @BeforeAll | ||
| static void startServer() throws IOException { | ||
| mockServer = HttpServer.create(new InetSocketAddress("localhost", 0), 0); | ||
| mockServer.createContext( | ||
| "/capture", | ||
| exchange -> { | ||
| capturedBaggage.set(exchange.getRequestHeaders().getFirst("baggage")); | ||
| byte[] body = "ok".getBytes(); | ||
|
mcculls marked this conversation as resolved.
|
||
| exchange.sendResponseHeaders(200, body.length); | ||
| exchange.getResponseBody().write(body); | ||
| exchange.close(); | ||
| }); | ||
| mockServer.setExecutor(Executors.newCachedThreadPool()); | ||
|
mcculls marked this conversation as resolved.
|
||
| mockServer.start(); | ||
|
mcculls marked this conversation as resolved.
|
||
| baseUrl = | ||
| "http://" | ||
| + mockServer.getAddress().getHostString() | ||
| + ":" | ||
| + mockServer.getAddress().getPort(); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void stopServer() { | ||
| if (mockServer != null) { | ||
| mockServer.stop(0); | ||
| mockServer = null; | ||
| } | ||
| } | ||
|
mcculls marked this conversation as resolved.
|
||
|
|
||
| @Test | ||
| void baggageHeaderPropagatedOnOutgoingRequest() { | ||
| capturedBaggage.set(null); | ||
| Baggage baggage = Baggage.create(Collections.singletonMap("user.id", "abc123")); | ||
|
|
||
| AgentSpan span = AgentTracer.startSpan("test", "parent"); | ||
| try (AgentScope spanScope = AgentTracer.activateSpan(span)) { | ||
| // Active context now carries both the span and the baggage — the exact shape the connect-span | ||
| // path must carry across the subscription -> I/O thread hand-off. | ||
| try (ContextScope baggageScope = Context.current().with(baggage).attach()) { | ||
| HttpClient.create() | ||
| .get() | ||
| .uri(baseUrl + "/capture") | ||
| .response() | ||
| .block(Duration.ofSeconds(10)); | ||
| } | ||
| } finally { | ||
| span.finish(); | ||
| } | ||
|
|
||
| String header = capturedBaggage.get(); | ||
| assertNotNull( | ||
| header, | ||
| "outgoing request must carry a W3C 'baggage' header when baggage is in the active context;" | ||
| + " null means the connect-span path dropped the context (carried only the span)"); | ||
| assertTrue( | ||
| header.contains("user.id=abc123"), | ||
| "baggage header should contain the propagated item, was: " + header); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.