Update aws-java-sdk-2.2 to new agent api
MODIFY
dd-java-agent/instrumentation/aws-java-sdk-2.2/src/main/java/datadog/trace/instrumentation/aws/v2/AwsHttpClientInstrumentation.java
dd-java-agent/instrumentation/aws-java-sdk-2.2/src/main/java/datadog/trace/instrumentation/aws/v2/AwsHttpClientInstrumentation.java
package datadog.trace.instrumentation.aws.v2;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
+import static datadog.trace.instrumentation.api.AgentTracer.activeScope;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
@@ -9,8 +10,7 @@ import static net.bytebuddy.matcher.ElementMatchers.not;
import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
-import io.opentracing.Scope;
-import io.opentracing.util.GlobalTracer;
+import datadog.trace.context.TraceScope;
import java.util.Collections;
import java.util.Map;
import net.bytebuddy.asm.Advice;
@@ -57,7 +57,7 @@ public final class AwsHttpClientInstrumentation extends AbstractAwsClientInstrum
@Advice.OnMethodEnter(suppress = Throwable.class)
public static boolean methodEnter(@Advice.This final Object thiz) {
if (thiz instanceof MakeAsyncHttpRequestStage) {
- final Scope scope = GlobalTracer.get().scopeManager().active();
+ final TraceScope scope = activeScope();
if (scope != null) {
scope.close();
return true;
@@ -69,7 +69,7 @@ public final class AwsHttpClientInstrumentation extends AbstractAwsClientInstrum
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(@Advice.Enter final boolean scopeAlreadyClosed) {
if (!scopeAlreadyClosed) {
- final Scope scope = GlobalTracer.get().scopeManager().active();
+ final TraceScope scope = activeScope();
if (scope != null) {
scope.close();
}

MODIFY
dd-java-agent/instrumentation/aws-java-sdk-2.2/src/main/java8/datadog/trace/instrumentation/aws/v2/AwsSdkClientDecorator.java
dd-java-agent/instrumentation/aws-java-sdk-2.2/src/main/java8/datadog/trace/instrumentation/aws/v2/AwsSdkClientDecorator.java
@@ -2,7 +2,7 @@ package datadog.trace.instrumentation.aws.v2;
import datadog.trace.agent.decorator.HttpClientDecorator;
import datadog.trace.api.DDTags;
-import io.opentracing.Span;
+import datadog.trace.instrumentation.api.AgentSpan;
import java.net.URI;
import software.amazon.awssdk.awscore.AwsResponse;
import software.amazon.awssdk.core.SdkRequest;
@@ -17,7 +17,7 @@ public class AwsSdkClientDecorator extends HttpClientDecorator<SdkHttpRequest, S
static final String COMPONENT_NAME = "java-aws-sdk";
- public Span onSdkRequest(final Span span, final SdkRequest request) {
+ public AgentSpan onSdkRequest(final AgentSpan span, final SdkRequest request) {
// S3
request
.getValueForField("Bucket", String.class)
@@ -40,7 +40,7 @@ public class AwsSdkClientDecorator extends HttpClientDecorator<SdkHttpRequest, S
return span;
}
- public Span onAttributes(final Span span, final ExecutionAttributes attributes) {
+ public AgentSpan onAttributes(final AgentSpan span, final ExecutionAttributes attributes) {
final String awsServiceName = attributes.getAttribute(SdkExecutionAttribute.SERVICE_NAME);
final String awsOperation = attributes.getAttribute(SdkExecutionAttribute.OPERATION_NAME);
@@ -56,7 +56,7 @@ public class AwsSdkClientDecorator extends HttpClientDecorator<SdkHttpRequest, S
}
// Not overriding the super. Should call both with each type of response.
- public Span onResponse(final Span span, final SdkResponse response) {
+ public AgentSpan onResponse(final AgentSpan span, final SdkResponse response) {
if (response instanceof AwsResponse) {
span.setTag("aws.requestId", ((AwsResponse) response).responseMetadata().requestId());
}

MODIFY
dd-java-agent/instrumentation/aws-java-sdk-2.2/src/main/java8/datadog/trace/instrumentation/aws/v2/TracingExecutionInterceptor.java
dd-java-agent/instrumentation/aws-java-sdk-2.2/src/main/java8/datadog/trace/instrumentation/aws/v2/TracingExecutionInterceptor.java
package datadog.trace.instrumentation.aws.v2;
+import static datadog.trace.instrumentation.api.AgentTracer.activateSpan;
+import static datadog.trace.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.instrumentation.aws.v2.AwsSdkClientDecorator.DECORATE;
-import datadog.trace.context.TraceScope;
-import io.opentracing.Scope;
-import io.opentracing.Span;
-import io.opentracing.propagation.TextMap;
-import io.opentracing.util.GlobalTracer;
-import java.util.Iterator;
-import java.util.Map;
+import datadog.trace.instrumentation.api.AgentPropagation;
+import datadog.trace.instrumentation.api.AgentScope;
+import datadog.trace.instrumentation.api.AgentSpan;
import java.util.function.Consumer;
import software.amazon.awssdk.core.client.builder.SdkClientBuilder;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
@@ -27,14 +25,14 @@ public class TracingExecutionInterceptor implements ExecutionInterceptor {
OVERRIDE_CONFIGURATION_CONSUMER =
builder -> builder.addExecutionInterceptor(new TracingExecutionInterceptor());
- private static final ExecutionAttribute<Span> SPAN_ATTRIBUTE =
+ private static final ExecutionAttribute<AgentSpan> SPAN_ATTRIBUTE =
new ExecutionAttribute<>("DatadogSpan");
@Override
public void beforeExecution(
final Context.BeforeExecution context, final ExecutionAttributes executionAttributes) {
- final Span span = GlobalTracer.get().buildSpan("aws.command").start();
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
+ final AgentSpan span = startSpan("aws.command");
+ try (final AgentScope scope = activateSpan(span, false)) {
DECORATE.afterStart(span);
executionAttributes.putAttribute(SPAN_ATTRIBUTE, span);
}
@@ -43,7 +41,7 @@ public class TracingExecutionInterceptor implements ExecutionInterceptor {
@Override
public void afterMarshalling(
final Context.AfterMarshalling context, final ExecutionAttributes executionAttributes) {
- final Span span = executionAttributes.getAttribute(SPAN_ATTRIBUTE);
+ final AgentSpan span = executionAttributes.getAttribute(SPAN_ATTRIBUTE);
DECORATE.onRequest(span, context.httpRequest());
DECORATE.onSdkRequest(span, context.request());
@@ -53,18 +51,18 @@ public class TracingExecutionInterceptor implements ExecutionInterceptor {
@Override
public void beforeTransmission(
final Context.BeforeTransmission context, final ExecutionAttributes executionAttributes) {
- final Span span = executionAttributes.getAttribute(SPAN_ATTRIBUTE);
+ final AgentSpan span = executionAttributes.getAttribute(SPAN_ATTRIBUTE);
// This scope will be closed by AwsHttpClientInstrumentation since ExecutionInterceptor API
// doesn't provide a way to run code in the same thread after transmission has been scheduled.
- final Scope scope = GlobalTracer.get().scopeManager().activate(span, false);
- ((TraceScope) scope).setAsyncPropagation(true);
+ final AgentScope scope = activateSpan(span, false);
+ scope.setAsyncPropagation(true);
}
@Override
public void afterExecution(
final Context.AfterExecution context, final ExecutionAttributes executionAttributes) {
- final Span span = executionAttributes.getAttribute(SPAN_ATTRIBUTE);
+ final AgentSpan span = executionAttributes.getAttribute(SPAN_ATTRIBUTE);
if (span != null) {
executionAttributes.putAttribute(SPAN_ATTRIBUTE, null);
// Call onResponse on both types of responses:
@@ -78,7 +76,7 @@ public class TracingExecutionInterceptor implements ExecutionInterceptor {
@Override
public void onExecutionFailure(
final Context.FailedExecution context, final ExecutionAttributes executionAttributes) {
- final Span span = executionAttributes.getAttribute(SPAN_ATTRIBUTE);
+ final AgentSpan span = executionAttributes.getAttribute(SPAN_ATTRIBUTE);
if (span != null) {
executionAttributes.putAttribute(SPAN_ATTRIBUTE, null);
DECORATE.onError(span, context.exception());
@@ -105,23 +103,13 @@ public class TracingExecutionInterceptor implements ExecutionInterceptor {
* <p>Note: we inject headers at aws-client level because aws requests may be signed and adding
* headers on http-client level may break signature.
*/
- public static class InjectAdapter implements TextMap {
+ public static class InjectAdapter implements AgentPropagation.Setter<SdkHttpRequest.Builder> {
- private final SdkHttpRequest.Builder builder;
-
- public InjectAdapter(final SdkHttpRequest.Builder builder) {
- this.builder = builder;
- }
-
- @Override
- public Iterator<Map.Entry<String, String>> iterator() {
- throw new UnsupportedOperationException(
- "This class should be used only with Tracer.extract()!");
- }
+ public static final InjectAdapter SETTER = new InjectAdapter();
@Override
- public void put(final String key, final String value) {
- builder.putHeader(key, value);
+ public void set(final SdkHttpRequest.Builder carrier, final String key, final String value) {
+ carrier.putHeader(key, value);
}
}
}

Update aws-java-sdk-2.2 to new agent api