1
1
package datadog .trace .instrumentation .aws .v2 ;
2
2
3
3
import static datadog .trace .bootstrap .instrumentation .api .AgentPropagation .XRAY_TRACING_CONCERN ;
4
+ import static datadog .trace .bootstrap .instrumentation .api .AgentSpan .fromContext ;
4
5
import static datadog .trace .bootstrap .instrumentation .api .AgentTracer .activateSpan ;
5
6
import static datadog .trace .bootstrap .instrumentation .api .AgentTracer .activateSpanWithoutScope ;
6
7
import static datadog .trace .bootstrap .instrumentation .api .AgentTracer .blackholeSpan ;
7
8
import static datadog .trace .bootstrap .instrumentation .api .AgentTracer .startSpan ;
8
9
import static datadog .trace .instrumentation .aws .v2 .AwsSdkClientDecorator .AWS_LEGACY_TRACING ;
9
10
import static datadog .trace .instrumentation .aws .v2 .AwsSdkClientDecorator .DECORATE ;
10
11
12
+ import datadog .context .Context ;
11
13
import datadog .context .propagation .Propagators ;
12
14
import datadog .trace .api .Config ;
13
15
import datadog .trace .bootstrap .ContextStore ;
19
21
import org .slf4j .LoggerFactory ;
20
22
import software .amazon .awssdk .core .SdkRequest ;
21
23
import software .amazon .awssdk .core .SdkResponse ;
22
- import software .amazon .awssdk .core .interceptor .Context ;
24
+ import software .amazon .awssdk .core .interceptor .Context .AfterExecution ;
25
+ import software .amazon .awssdk .core .interceptor .Context .AfterMarshalling ;
26
+ import software .amazon .awssdk .core .interceptor .Context .BeforeExecution ;
27
+ import software .amazon .awssdk .core .interceptor .Context .BeforeTransmission ;
28
+ import software .amazon .awssdk .core .interceptor .Context .FailedExecution ;
29
+ import software .amazon .awssdk .core .interceptor .Context .ModifyHttpRequest ;
23
30
import software .amazon .awssdk .core .interceptor .ExecutionAttribute ;
24
31
import software .amazon .awssdk .core .interceptor .ExecutionAttributes ;
25
32
import software .amazon .awssdk .core .interceptor .ExecutionInterceptor ;
28
35
/** AWS request execution interceptor */
29
36
public class TracingExecutionInterceptor implements ExecutionInterceptor {
30
37
31
- public static final ExecutionAttribute <AgentSpan > SPAN_ATTRIBUTE =
38
+ public static final ExecutionAttribute <Context > CONTEXT_ATTRIBUTE =
32
39
InstanceStore .of (ExecutionAttribute .class )
33
- .putIfAbsent ("DatadogSpan " , () -> new ExecutionAttribute <>("DatadogSpan " ));
40
+ .putIfAbsent ("DatadogContext " , () -> new ExecutionAttribute <>("DatadogContext " ));
34
41
35
42
private static final Logger log = LoggerFactory .getLogger (TracingExecutionInterceptor .class );
36
43
@@ -42,37 +49,40 @@ public TracingExecutionInterceptor(ContextStore<Object, String> responseQueueSto
42
49
43
50
@ Override
44
51
public void beforeExecution (
45
- final Context . BeforeExecution context , final ExecutionAttributes executionAttributes ) {
52
+ final BeforeExecution context , final ExecutionAttributes executionAttributes ) {
46
53
if (!AWS_LEGACY_TRACING && isPollingRequest (context .request ())) {
47
54
return ; // SQS messages spans are created by aws-java-sqs-2.0
48
55
}
49
56
50
- final AgentSpan span = startSpan (DECORATE .spanName (executionAttributes ));
57
+ final AgentSpan span = startSpan ("aws-sdk" , DECORATE .spanName (executionAttributes ));
58
+ // TODO If DSM is enabled, add DSM context here too
51
59
DECORATE .afterStart (span );
52
- executionAttributes .putAttribute (SPAN_ATTRIBUTE , span );
60
+ executionAttributes .putAttribute (CONTEXT_ATTRIBUTE , span );
53
61
}
54
62
55
63
@ Override
56
64
public void afterMarshalling (
57
- final Context .AfterMarshalling context , final ExecutionAttributes executionAttributes ) {
58
- final AgentSpan span = executionAttributes .getAttribute (SPAN_ATTRIBUTE );
59
- if (span != null ) {
65
+ final AfterMarshalling context , final ExecutionAttributes executionAttributes ) {
66
+ final Context ddContext = executionAttributes .getAttribute (CONTEXT_ATTRIBUTE );
67
+ final AgentSpan span = fromContext (ddContext );
68
+ if (context != null && span != null ) {
60
69
try (AgentScope ignored = activateSpan (span )) {
61
70
DECORATE .onRequest (span , context .httpRequest ());
62
- DECORATE .onSdkRequest (span , context .request (), context .httpRequest (), executionAttributes );
71
+ DECORATE .onSdkRequest (
72
+ ddContext , context .request (), context .httpRequest (), executionAttributes );
63
73
}
64
74
}
65
75
}
66
76
67
77
@ Override
68
78
public SdkHttpRequest modifyHttpRequest (
69
- Context . ModifyHttpRequest context , ExecutionAttributes executionAttributes ) {
79
+ ModifyHttpRequest context , ExecutionAttributes executionAttributes ) {
70
80
if (Config .get ().isAwsPropagationEnabled ()) {
71
81
try {
72
- final AgentSpan span = executionAttributes .getAttribute (SPAN_ATTRIBUTE );
73
- if (span != null ) {
82
+ final Context ddContext = executionAttributes .getAttribute (CONTEXT_ATTRIBUTE );
83
+ if (ddContext != null ) {
74
84
SdkHttpRequest .Builder requestBuilder = context .httpRequest ().toBuilder ();
75
- Propagators .forConcern (XRAY_TRACING_CONCERN ).inject (span , requestBuilder , DECORATE );
85
+ Propagators .forConcern (XRAY_TRACING_CONCERN ).inject (ddContext , requestBuilder , DECORATE );
76
86
return requestBuilder .build ();
77
87
}
78
88
} catch (Throwable e ) {
@@ -84,12 +94,13 @@ public SdkHttpRequest modifyHttpRequest(
84
94
85
95
@ Override
86
96
public void beforeTransmission (
87
- final Context . BeforeTransmission context , final ExecutionAttributes executionAttributes ) {
97
+ final BeforeTransmission context , final ExecutionAttributes executionAttributes ) {
88
98
final AgentSpan span ;
89
99
if (!AWS_LEGACY_TRACING ) {
90
100
span = blackholeSpan ();
91
101
} else {
92
- span = executionAttributes .getAttribute (SPAN_ATTRIBUTE );
102
+ final Context ddContext = executionAttributes .getAttribute (CONTEXT_ATTRIBUTE );
103
+ span = fromContext (ddContext );
93
104
}
94
105
if (span != null ) {
95
106
// This scope will be closed by AwsHttpClientInstrumentation since ExecutionInterceptor API
@@ -100,10 +111,11 @@ public void beforeTransmission(
100
111
101
112
@ Override
102
113
public void afterExecution (
103
- final Context .AfterExecution context , final ExecutionAttributes executionAttributes ) {
104
- final AgentSpan span = executionAttributes .getAttribute (SPAN_ATTRIBUTE );
114
+ final AfterExecution context , final ExecutionAttributes executionAttributes ) {
115
+ final Context ddContext = executionAttributes .getAttribute (CONTEXT_ATTRIBUTE );
116
+ final AgentSpan span = fromContext (ddContext );
105
117
if (span != null ) {
106
- executionAttributes .putAttribute (SPAN_ATTRIBUTE , null );
118
+ executionAttributes .putAttribute (CONTEXT_ATTRIBUTE , null );
107
119
// Call onResponse on both types of responses:
108
120
DECORATE .onSdkResponse (span , context .response (), context .httpResponse (), executionAttributes );
109
121
DECORATE .onResponse (span , context .httpResponse ());
@@ -121,14 +133,16 @@ public void afterExecution(
121
133
122
134
@ Override
123
135
public void onExecutionFailure (
124
- final Context .FailedExecution context , final ExecutionAttributes executionAttributes ) {
125
- final AgentSpan span = executionAttributes .getAttribute (SPAN_ATTRIBUTE );
126
- if (span != null ) {
127
- executionAttributes .putAttribute (SPAN_ATTRIBUTE , null );
136
+ final FailedExecution context , final ExecutionAttributes executionAttributes ) {
137
+ final Context ddContext = executionAttributes .getAttribute (CONTEXT_ATTRIBUTE );
138
+ final AgentSpan span = fromContext (ddContext );
139
+ if (ddContext != null && span != null ) {
140
+ executionAttributes .putAttribute (CONTEXT_ATTRIBUTE , null );
128
141
Optional <SdkResponse > responseOpt = context .response ();
129
142
if (responseOpt .isPresent ()) {
130
143
SdkResponse response = responseOpt .get ();
131
- DECORATE .onSdkResponse (span , response , response .sdkHttpResponse (), executionAttributes );
144
+ DECORATE .onSdkResponse (
145
+ ddContext , response , response .sdkHttpResponse (), executionAttributes );
132
146
DECORATE .onResponse (span , response .sdkHttpResponse ());
133
147
if (span .isError ()) {
134
148
DECORATE .onError (span , context .exception ());
0 commit comments