|
2 | 2 |
|
3 | 3 | import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; |
4 | 4 | import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan; |
| 5 | +import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.spanFromContext; |
| 6 | +import static datadog.trace.instrumentation.synapse3.SynapseClientDecorator.SYNAPSE_CONTEXT_KEY; |
5 | 7 | import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
6 | 8 | import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
7 | 9 |
|
8 | 10 | import com.google.auto.service.AutoService; |
| 11 | +import datadog.context.Context; |
9 | 12 | import datadog.trace.agent.tooling.Instrumenter; |
10 | 13 | import datadog.trace.agent.tooling.InstrumenterModule; |
11 | 14 | import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
|
14 | 17 | import java.util.Map; |
15 | 18 | import net.bytebuddy.asm.Advice; |
16 | 19 | import org.apache.axis2.context.MessageContext; |
| 20 | +import org.apache.http.nio.NHttpServerConnection; |
17 | 21 |
|
18 | 22 | /** Helps propagate parent spans over 'passthru' mechanism to synapse-client instrumentation. */ |
19 | 23 | @AutoService(InstrumenterModule.class) |
@@ -53,10 +57,26 @@ public static void submit(@Advice.Argument(0) final MessageContext message) { |
53 | 57 | } |
54 | 58 | } |
55 | 59 |
|
56 | | - // use message context to propagate active spans across Synapse's 'passthru' mechanism |
57 | | - AgentSpan span = activeSpan(); |
| 60 | + // Propagate the server span to the client via the message context. |
| 61 | + // Prefer reading the span directly from the source connection's context (where |
| 62 | + // SynapseServerInstrumentation stored it) over activeSpan(). SourceHandler dispatches |
| 63 | + // request processing to a worker thread pool, and while java-concurrent instrumentation |
| 64 | + // normally propagates context across ThreadPoolExecutor, the connection-based lookup is |
| 65 | + // more robust as it doesn't depend on automatic context propagation. |
| 66 | + AgentSpan span = null; |
| 67 | + Object sourceConn = message.getProperty("pass-through.Source-Connection"); |
| 68 | + if (sourceConn instanceof NHttpServerConnection) { |
| 69 | + Object ctx = |
| 70 | + ((NHttpServerConnection) sourceConn).getContext().getAttribute(SYNAPSE_CONTEXT_KEY); |
| 71 | + if (ctx instanceof Context) { |
| 72 | + span = spanFromContext((Context) ctx); |
| 73 | + } |
| 74 | + } |
| 75 | + if (null == span) { |
| 76 | + span = activeSpan(); |
| 77 | + } |
58 | 78 | if (null != span) { |
59 | | - message.setNonReplicableProperty("dd.trace.synapse.span", span); |
| 79 | + message.setNonReplicableProperty(SYNAPSE_CONTEXT_KEY, span); |
60 | 80 | } |
61 | 81 | } |
62 | 82 | } |
|
0 commit comments