|
| 1 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 2 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 3 | + |
| 4 | +import com.sun.net.httpserver.HttpServer; |
| 5 | +import datadog.context.Context; |
| 6 | +import datadog.context.ContextScope; |
| 7 | +import datadog.trace.agent.test.AbstractInstrumentationTest; |
| 8 | +import datadog.trace.bootstrap.instrumentation.api.AgentScope; |
| 9 | +import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
| 10 | +import datadog.trace.bootstrap.instrumentation.api.AgentTracer; |
| 11 | +import datadog.trace.bootstrap.instrumentation.api.Baggage; |
| 12 | +import java.io.IOException; |
| 13 | +import java.net.InetSocketAddress; |
| 14 | +import java.time.Duration; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.concurrent.Executors; |
| 17 | +import java.util.concurrent.atomic.AtomicReference; |
| 18 | +import org.junit.jupiter.api.AfterAll; |
| 19 | +import org.junit.jupiter.api.BeforeAll; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import reactor.netty.http.client.HttpClient; |
| 22 | + |
| 23 | +/** |
| 24 | + * Regression test for the W3C baggage header not propagating on outgoing Reactor Netty requests. |
| 25 | + * |
| 26 | + * <p>The bug: the connect-span path carried only {@code activeSpan()} across the subscription -> |
| 27 | + * I/O thread hand-off, dropping the rest of the Datadog {@code Context} (including baggage). The |
| 28 | + * outgoing request then had no baggage to inject, so the {@code baggage} header was silently |
| 29 | + * skipped. This test sets a baggage item in the active context, makes one outgoing request, and |
| 30 | + * asserts the server received the {@code baggage} header. |
| 31 | + * |
| 32 | + * <p>It is a <em>coupling</em> test: producer (sets baggage) -> Reactor Netty carrier -> consumer |
| 33 | + * (injects the header). The failure it guards against lives in the hand-off between integrations, |
| 34 | + * not in any one of them, so per-integration tests would not catch it. |
| 35 | + */ |
| 36 | +class ReactorNettyBaggagePropagationTest extends AbstractInstrumentationTest { |
| 37 | + |
| 38 | + private static HttpServer mockServer; |
| 39 | + private static String baseUrl; |
| 40 | + private static final AtomicReference<String> capturedBaggage = new AtomicReference<>(); |
| 41 | + |
| 42 | + @BeforeAll |
| 43 | + static void startServer() throws IOException { |
| 44 | + mockServer = HttpServer.create(new InetSocketAddress("localhost", 0), 0); |
| 45 | + mockServer.createContext( |
| 46 | + "/capture", |
| 47 | + exchange -> { |
| 48 | + capturedBaggage.set(exchange.getRequestHeaders().getFirst("baggage")); |
| 49 | + byte[] body = "ok".getBytes(); |
| 50 | + exchange.sendResponseHeaders(200, body.length); |
| 51 | + exchange.getResponseBody().write(body); |
| 52 | + exchange.close(); |
| 53 | + }); |
| 54 | + mockServer.setExecutor(Executors.newCachedThreadPool()); |
| 55 | + mockServer.start(); |
| 56 | + baseUrl = |
| 57 | + "http://" |
| 58 | + + mockServer.getAddress().getHostString() |
| 59 | + + ":" |
| 60 | + + mockServer.getAddress().getPort(); |
| 61 | + } |
| 62 | + |
| 63 | + @AfterAll |
| 64 | + static void stopServer() { |
| 65 | + if (mockServer != null) { |
| 66 | + mockServer.stop(0); |
| 67 | + mockServer = null; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void baggageHeaderPropagatedOnOutgoingRequest() { |
| 73 | + capturedBaggage.set(null); |
| 74 | + Baggage baggage = Baggage.create(Collections.singletonMap("user.id", "abc123")); |
| 75 | + |
| 76 | + AgentSpan span = AgentTracer.startSpan("test", "parent"); |
| 77 | + try (AgentScope spanScope = AgentTracer.activateSpan(span)) { |
| 78 | + // Active context now carries both the span and the baggage — the exact shape the connect-span |
| 79 | + // path must carry across the subscription -> I/O thread hand-off. |
| 80 | + try (ContextScope baggageScope = Context.current().with(baggage).attach()) { |
| 81 | + HttpClient.create() |
| 82 | + .get() |
| 83 | + .uri(baseUrl + "/capture") |
| 84 | + .response() |
| 85 | + .block(Duration.ofSeconds(10)); |
| 86 | + } |
| 87 | + } finally { |
| 88 | + span.finish(); |
| 89 | + } |
| 90 | + |
| 91 | + String header = capturedBaggage.get(); |
| 92 | + assertNotNull( |
| 93 | + header, |
| 94 | + "outgoing request must carry a W3C 'baggage' header when baggage is in the active context;" |
| 95 | + + " null means the connect-span path dropped the context (carried only the span)"); |
| 96 | + assertTrue( |
| 97 | + header.contains("user.id=abc123"), |
| 98 | + "baggage header should contain the propagated item, was: " + header); |
| 99 | + } |
| 100 | +} |
0 commit comments