Inject context into AWS SDK 2.2 HTTP request from interceptor.
ADD
instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/AwsSdkInjectAdapter.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.instrumentation.awssdk.v2_2;
+
+import io.opentelemetry.context.propagation.TextMapPropagator;
+import software.amazon.awssdk.http.SdkHttpRequest;
+
+final class AwsSdkInjectAdapter implements TextMapPropagator.Setter<SdkHttpRequest.Builder> {
+
+ static final AwsSdkInjectAdapter INSTANCE = new AwsSdkInjectAdapter();
+
+ @Override
+ public void set(SdkHttpRequest.Builder builder, String name, String value) {
+ builder.appendHeader(name, value);
+ }
+}

MODIFY
instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/TracingExecutionInterceptor.java
instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/TracingExecutionInterceptor.java
@@ -9,6 +9,7 @@ import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsSdk.getSpanFromAtt
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsSdkHttpClientTracer.tracer;
import static io.opentelemetry.instrumentation.awssdk.v2_2.RequestType.ofSdkRequest;
+import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Span.Kind;
import java.util.EnumMap;
@@ -23,6 +24,7 @@ import software.amazon.awssdk.core.interceptor.ExecutionAttribute;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
import software.amazon.awssdk.core.interceptor.SdkExecutionAttribute;
+import software.amazon.awssdk.http.SdkHttpRequest;
/** AWS request execution interceptor. */
final class TracingExecutionInterceptor implements ExecutionInterceptor {
@@ -86,6 +88,23 @@ final class TracingExecutionInterceptor implements ExecutionInterceptor {
}
}
+ @Override
+ public SdkHttpRequest modifyHttpRequest(
+ Context.ModifyHttpRequest context, ExecutionAttributes executionAttributes) {
+ io.opentelemetry.context.Context otelContext =
+ executionAttributes.getAttribute(CONTEXT_ATTRIBUTE);
+ // Never null in practice unless another interceptor cleared out the attribute, which
+ // is theoretically possible.
+ if (otelContext == null) {
+ return context.httpRequest();
+ }
+ SdkHttpRequest.Builder builder = context.httpRequest().toBuilder();
+ OpenTelemetry.getGlobalPropagators()
+ .getTextMapPropagator()
+ .inject(otelContext, builder, AwsSdkInjectAdapter.INSTANCE);
+ return builder.build();
+ }
+
@Override
public void afterMarshalling(
Context.AfterMarshalling context, ExecutionAttributes executionAttributes) {

MODIFY
instrumentation/aws-sdk/aws-sdk-2.2/testing/src/main/groovy/io/opentelemetry/instrumentation/awssdk/v2_2/AbstractAws2ClientTest.groovy
instrumentation/aws-sdk/aws-sdk-2.2/testing/src/main/groovy/io/opentelemetry/instrumentation/awssdk/v2_2/AbstractAws2ClientTest.groovy
@@ -148,7 +148,7 @@ abstract class AbstractAws2ClientTest extends InstrumentationSpecification {
}
}
}
- server.lastRequest.headers.get("traceparent") == null
+ server.lastRequest.headers.get("traceparent") != null
}
static dynamoDbRequestDataTable(client) {
@@ -226,7 +226,7 @@ abstract class AbstractAws2ClientTest extends InstrumentationSpecification {
}
}
}
- server.lastRequest.headers.get("traceparent") == null
+ server.lastRequest.headers.get("traceparent") != null
where:
service | operation | method | path | requestId | builder | call | body
@@ -314,7 +314,7 @@ abstract class AbstractAws2ClientTest extends InstrumentationSpecification {
}
}
}
- server.lastRequest.headers.get("traceparent") == null
+ server.lastRequest.headers.get("traceparent") != null
where:
service | operation | method | path | requestId | builder | call | body

Inject context into AWS SDK 2.2 HTTP request from interceptor. (#1763)