|
1 | 1 | package datadog.trace.instrumentation.springmessaging; |
2 | 2 |
|
3 | 3 | import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; |
4 | | -import static datadog.trace.api.datastreams.DataStreamsContext.create; |
5 | | -import static datadog.trace.api.datastreams.DataStreamsTags.Direction.INBOUND; |
6 | 4 | import static datadog.trace.bootstrap.instrumentation.api.AgentPropagation.extractContextAndGetSpanContext; |
7 | 5 | import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.*; |
8 | 6 | import static datadog.trace.instrumentation.springmessaging.SpringMessageDecorator.DECORATE; |
|
15 | 13 | import com.google.auto.service.AutoService; |
16 | 14 | import datadog.trace.agent.tooling.Instrumenter; |
17 | 15 | import datadog.trace.agent.tooling.InstrumenterModule; |
18 | | -import datadog.trace.api.datastreams.DataStreamsTags; |
19 | 16 | import datadog.trace.bootstrap.InstrumentationContext; |
20 | 17 | import datadog.trace.bootstrap.instrumentation.api.AgentScope; |
21 | 18 | import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
22 | 19 | import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext; |
23 | | -import datadog.trace.bootstrap.instrumentation.api.AgentTracer; |
24 | 20 | import datadog.trace.bootstrap.instrumentation.java.concurrent.State; |
25 | 21 | import java.util.Map; |
26 | 22 | import net.bytebuddy.asm.Advice; |
@@ -70,77 +66,89 @@ public static AgentScope onEnter( |
70 | 66 | @Advice.This InvocableHandlerMethod thiz, @Advice.Argument(0) Message<?> message) { |
71 | 67 | AgentSpanContext parentContext; |
72 | 68 | AgentSpan parent = activeSpan(); |
73 | | - |
| 69 | + |
74 | 70 | // First try to get context from continuation (preferred method) |
75 | 71 | State state = InstrumentationContext.get(Message.class, State.class).get(message); |
76 | 72 | if (null != state) { |
77 | | - System.out.println("[Spring] Found state in Spring message, attempting to activate continuation on thread: " + |
78 | | - Thread.currentThread().getId()); |
| 73 | + System.out.println( |
| 74 | + "[Spring] Found state in Spring message, attempting to activate continuation on thread: " |
| 75 | + + Thread.currentThread().getId()); |
79 | 76 | AgentScope.Continuation continuation = state.getAndResetContinuation(); |
80 | 77 | if (null != continuation) { |
81 | 78 | try (AgentScope scope = continuation.activate()) { |
82 | 79 | AgentSpan span = startSpan(SPRING_INBOUND); |
83 | 80 | DECORATE.afterStart(span); |
84 | 81 | span.setResourceName(DECORATE.spanNameForMethod(thiz.getMethod())); |
85 | | - System.out.println("[Spring] Successfully activated continuation from Spring Message with span: " + |
86 | | - span.getSpanId() + " on thread: " + Thread.currentThread().getId()); |
| 82 | + System.out.println( |
| 83 | + "[Spring] Successfully activated continuation from Spring Message with span: " |
| 84 | + + span.getSpanId() |
| 85 | + + " on thread: " |
| 86 | + + Thread.currentThread().getId()); |
87 | 87 | return activateSpan(span); |
88 | 88 | } |
89 | 89 | } else { |
90 | | - System.out.println("[Spring] No continuation found in state on thread: " + Thread.currentThread().getId()); |
| 90 | + System.out.println( |
| 91 | + "[Spring] No continuation found in state on thread: " |
| 92 | + + Thread.currentThread().getId()); |
91 | 93 | } |
92 | 94 | } else { |
93 | | - System.out.println("[Spring] No state found in Spring message 2, falling back to header extraction on thread: " + |
94 | | - Thread.currentThread().getId()); |
| 95 | + System.out.println( |
| 96 | + "[Spring] No state found in Spring message 2, falling back to header extraction on thread: " |
| 97 | + + Thread.currentThread().getId()); |
95 | 98 | } |
96 | | - |
| 99 | + |
97 | 100 | // Fallback to existing context or header extraction |
98 | 101 | if (null != parent) { |
99 | 102 | // prefer existing context, assume it was already extracted from this message |
100 | 103 | parentContext = parent.context(); |
101 | | - System.out.println("[Spring] Using existing active span context on thread: " + Thread.currentThread().getId()); |
| 104 | + System.out.println( |
| 105 | + "[Spring] Using existing active span context on thread: " |
| 106 | + + Thread.currentThread().getId()); |
102 | 107 | } else { |
103 | 108 | // otherwise try to re-extract the message context to avoid disconnected trace |
104 | 109 | parentContext = extractContextAndGetSpanContext(message, GETTER); |
105 | | - System.out.println("[Spring] Extracted context from message headers on thread: " + Thread.currentThread().getId()); |
| 110 | + System.out.println( |
| 111 | + "[Spring] Extracted context from message headers on thread: " |
| 112 | + + Thread.currentThread().getId()); |
106 | 113 | } |
107 | | - |
| 114 | + |
108 | 115 | AgentSpan span = startSpan(SPRING_INBOUND, parentContext); |
109 | 116 | DECORATE.afterStart(span); |
110 | 117 | span.setResourceName(DECORATE.spanNameForMethod(thiz.getMethod())); |
111 | | - |
| 118 | + |
112 | 119 | // Extract SQS queue information - try different header patterns |
113 | 120 | Object queueUrl = message.getHeaders().get("Sqs_QueueUrl"); |
114 | 121 | Object queueName = message.getHeaders().get("Sqs_QueueName"); |
115 | | - |
| 122 | + |
116 | 123 | // If not found in Sqs_ prefixed headers, try aws. prefixed headers |
117 | 124 | if (queueUrl == null) { |
118 | 125 | queueUrl = message.getHeaders().get("aws.queue.url"); |
119 | 126 | } |
120 | 127 | if (queueName == null) { |
121 | 128 | queueName = message.getHeaders().get("aws.queue.name"); |
122 | 129 | } |
123 | | - |
| 130 | + |
124 | 131 | // If still not found, try to extract from QueueAttributes |
125 | 132 | if (queueUrl == null || queueName == null) { |
126 | 133 | Object queueAttributes = message.getHeaders().get("Sqs_QueueAttributes"); |
127 | 134 | if (queueAttributes != null) { |
128 | 135 | String attributesStr = queueAttributes.toString(); |
129 | 136 | // Extract queue name from attributes if available |
130 | 137 | if (queueName == null && attributesStr.contains("queueName=")) { |
131 | | - queueName = attributesStr.substring(attributesStr.indexOf("queueName=") + 10).split(",")[0]; |
| 138 | + queueName = |
| 139 | + attributesStr.substring(attributesStr.indexOf("queueName=") + 10).split(",")[0]; |
132 | 140 | } |
133 | 141 | } |
134 | 142 | } |
135 | | - |
| 143 | + |
136 | 144 | // Add SQS queue tags to the span |
137 | 145 | if (queueUrl != null) { |
138 | 146 | span.setTag("aws.sqs.queue_url", queueUrl.toString()); |
139 | 147 | } |
140 | 148 | if (queueName != null) { |
141 | 149 | span.setTag("aws.sqs.queue_name", queueName.toString()); |
142 | 150 | } |
143 | | - |
| 151 | + |
144 | 152 | return activateSpan(span); |
145 | 153 | } |
146 | 154 |
|
|
0 commit comments