11package datadog .trace .instrumentation .aws .v2 ;
22
33import static datadog .trace .bootstrap .instrumentation .api .AgentPropagation .XRAY_TRACING_CONCERN ;
4+ import static datadog .trace .bootstrap .instrumentation .api .AgentSpan .fromContext ;
45import static datadog .trace .bootstrap .instrumentation .api .AgentTracer .activateSpan ;
56import static datadog .trace .bootstrap .instrumentation .api .AgentTracer .activateSpanWithoutScope ;
67import static datadog .trace .bootstrap .instrumentation .api .AgentTracer .blackholeSpan ;
78import static datadog .trace .bootstrap .instrumentation .api .AgentTracer .startSpan ;
89import static datadog .trace .instrumentation .aws .v2 .AwsSdkClientDecorator .AWS_LEGACY_TRACING ;
910import static datadog .trace .instrumentation .aws .v2 .AwsSdkClientDecorator .DECORATE ;
1011
12+ import datadog .context .Context ;
1113import datadog .context .propagation .Propagators ;
1214import datadog .trace .api .Config ;
1315import datadog .trace .bootstrap .ContextStore ;
1921import org .slf4j .LoggerFactory ;
2022import software .amazon .awssdk .core .SdkRequest ;
2123import 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 ;
2330import software .amazon .awssdk .core .interceptor .ExecutionAttribute ;
2431import software .amazon .awssdk .core .interceptor .ExecutionAttributes ;
2532import software .amazon .awssdk .core .interceptor .ExecutionInterceptor ;
2835/** AWS request execution interceptor */
2936public class TracingExecutionInterceptor implements ExecutionInterceptor {
3037
31- public static final ExecutionAttribute <AgentSpan > SPAN_ATTRIBUTE =
38+ public static final ExecutionAttribute <Context > CONTEXT_ATTRIBUTE =
3239 InstanceStore .of (ExecutionAttribute .class )
33- .putIfAbsent ("DatadogSpan " , () -> new ExecutionAttribute <>("DatadogSpan " ));
40+ .putIfAbsent ("DatadogContext " , () -> new ExecutionAttribute <>("DatadogContext " ));
3441
3542 private static final Logger log = LoggerFactory .getLogger (TracingExecutionInterceptor .class );
3643
@@ -42,37 +49,40 @@ public TracingExecutionInterceptor(ContextStore<Object, String> responseQueueSto
4249
4350 @ Override
4451 public void beforeExecution (
45- final Context . BeforeExecution context , final ExecutionAttributes executionAttributes ) {
52+ final BeforeExecution context , final ExecutionAttributes executionAttributes ) {
4653 if (!AWS_LEGACY_TRACING && isPollingRequest (context .request ())) {
4754 return ; // SQS messages spans are created by aws-java-sqs-2.0
4855 }
4956
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
5159 DECORATE .afterStart (span );
52- executionAttributes .putAttribute (SPAN_ATTRIBUTE , span );
60+ executionAttributes .putAttribute (CONTEXT_ATTRIBUTE , span );
5361 }
5462
5563 @ Override
5664 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 ) {
6069 try (AgentScope ignored = activateSpan (span )) {
6170 DECORATE .onRequest (span , context .httpRequest ());
62- DECORATE .onSdkRequest (span , context .request (), context .httpRequest (), executionAttributes );
71+ DECORATE .onSdkRequest (
72+ ddContext , context .request (), context .httpRequest (), executionAttributes );
6373 }
6474 }
6575 }
6676
6777 @ Override
6878 public SdkHttpRequest modifyHttpRequest (
69- Context . ModifyHttpRequest context , ExecutionAttributes executionAttributes ) {
79+ ModifyHttpRequest context , ExecutionAttributes executionAttributes ) {
7080 if (Config .get ().isAwsPropagationEnabled ()) {
7181 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 ) {
7484 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 );
7686 return requestBuilder .build ();
7787 }
7888 } catch (Throwable e ) {
@@ -84,12 +94,13 @@ public SdkHttpRequest modifyHttpRequest(
8494
8595 @ Override
8696 public void beforeTransmission (
87- final Context . BeforeTransmission context , final ExecutionAttributes executionAttributes ) {
97+ final BeforeTransmission context , final ExecutionAttributes executionAttributes ) {
8898 final AgentSpan span ;
8999 if (!AWS_LEGACY_TRACING ) {
90100 span = blackholeSpan ();
91101 } else {
92- span = executionAttributes .getAttribute (SPAN_ATTRIBUTE );
102+ final Context ddContext = executionAttributes .getAttribute (CONTEXT_ATTRIBUTE );
103+ span = fromContext (ddContext );
93104 }
94105 if (span != null ) {
95106 // This scope will be closed by AwsHttpClientInstrumentation since ExecutionInterceptor API
@@ -100,10 +111,11 @@ public void beforeTransmission(
100111
101112 @ Override
102113 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 );
105117 if (span != null ) {
106- executionAttributes .putAttribute (SPAN_ATTRIBUTE , null );
118+ executionAttributes .putAttribute (CONTEXT_ATTRIBUTE , null );
107119 // Call onResponse on both types of responses:
108120 DECORATE .onSdkResponse (span , context .response (), context .httpResponse (), executionAttributes );
109121 DECORATE .onResponse (span , context .httpResponse ());
@@ -121,14 +133,16 @@ public void afterExecution(
121133
122134 @ Override
123135 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 );
128141 Optional <SdkResponse > responseOpt = context .response ();
129142 if (responseOpt .isPresent ()) {
130143 SdkResponse response = responseOpt .get ();
131- DECORATE .onSdkResponse (span , response , response .sdkHttpResponse (), executionAttributes );
144+ DECORATE .onSdkResponse (
145+ ddContext , response , response .sdkHttpResponse (), executionAttributes );
132146 DECORATE .onResponse (span , response .sdkHttpResponse ());
133147 if (span .isError ()) {
134148 DECORATE .onError (span , context .exception ());
0 commit comments