Update jax-rs-annotations-2 to new agent api
MODIFY
dd-java-agent/instrumentation/jax-rs-annotations-2/src/main/java/datadog/trace/instrumentation/jaxrs2/JaxRsAnnotationsDecorator.java
dd-java-agent/instrumentation/jax-rs-annotations-2/src/main/java/datadog/trace/instrumentation/jaxrs2/JaxRsAnnotationsDecorator.java
@@ -6,8 +6,7 @@ import datadog.trace.agent.decorator.BaseDecorator;
import datadog.trace.api.DDSpanTypes;
import datadog.trace.api.DDTags;
import datadog.trace.bootstrap.WeakMap;
-import io.opentracing.Scope;
-import io.opentracing.Span;
+import datadog.trace.instrumentation.api.AgentSpan;
import io.opentracing.tag.Tags;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@@ -37,11 +36,10 @@ public class JaxRsAnnotationsDecorator extends BaseDecorator {
return "jax-rs-controller";
}
- public void onControllerStart(final Scope scope, final Scope parent, final Method method) {
+ public void onControllerStart(final AgentSpan span, final AgentSpan parent, final Method method) {
final String resourceName = getPathResourceName(method);
updateParent(parent, resourceName);
- final Span span = scope.span();
span.setTag(DDTags.SPAN_TYPE, DDSpanTypes.HTTP_SERVER);
// When jax-rs is the root, we want to name using the path, otherwise use the class/method.
@@ -53,12 +51,11 @@ public class JaxRsAnnotationsDecorator extends BaseDecorator {
}
}
- private void updateParent(final Scope scope, final String resourceName) {
- if (scope == null) {
+ private void updateParent(final AgentSpan span, final String resourceName) {
+ if (span == null) {
return;
}
- final Span span = scope.span();
- Tags.COMPONENT.set(span, "jax-rs");
+ span.setTag(Tags.COMPONENT.getKey(), "jax-rs");
if (!resourceName.isEmpty()) {
span.setTag(DDTags.RESOURCE_NAME, resourceName);

MODIFY
dd-java-agent/instrumentation/jax-rs-annotations-2/src/main/java/datadog/trace/instrumentation/jaxrs2/JaxRsAnnotationsInstrumentation.java
dd-java-agent/instrumentation/jax-rs-annotations-2/src/main/java/datadog/trace/instrumentation/jaxrs2/JaxRsAnnotationsInstrumentation.java
package datadog.trace.instrumentation.jaxrs2;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
+import static datadog.trace.instrumentation.api.AgentTracer.activateSpan;
+import static datadog.trace.instrumentation.api.AgentTracer.activeSpan;
+import static datadog.trace.instrumentation.api.AgentTracer.startSpan;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.declaresMethod;
import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith;
@@ -9,11 +12,8 @@ import static net.bytebuddy.matcher.ElementMatchers.named;
import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.bootstrap.InstrumentationContext;
-import datadog.trace.context.TraceScope;
-import io.opentracing.Scope;
-import io.opentracing.Span;
-import io.opentracing.Tracer;
-import io.opentracing.util.GlobalTracer;
+import datadog.trace.instrumentation.api.AgentScope;
+import datadog.trace.instrumentation.api.AgentSpan;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Map;
@@ -34,7 +34,8 @@ public final class JaxRsAnnotationsInstrumentation extends Instrumenter.Default
@Override
public Map<String, String> contextStore() {
- return Collections.singletonMap("javax.ws.rs.container.AsyncResponse", Span.class.getName());
+ return Collections.singletonMap(
+ "javax.ws.rs.container.AsyncResponse", AgentSpan.class.getName());
}
@Override
@@ -68,26 +69,25 @@ public final class JaxRsAnnotationsInstrumentation extends Instrumenter.Default
public static class JaxRsAnnotationsAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
- public static Scope nameSpan(@Advice.Origin final Method method) {
- final Tracer tracer = GlobalTracer.get();
+ public static AgentScope nameSpan(@Advice.Origin final Method method) {
// Rename the parent span according to the path represented by these annotations.
- final Scope parent = tracer.scopeManager().active();
- final Scope scope = tracer.buildSpan(JAX_ENDPOINT_OPERATION_NAME).startActive(false);
+ final AgentSpan parent = activeSpan();
- if (scope instanceof TraceScope) {
- ((TraceScope) scope).setAsyncPropagation(true);
- }
+ final AgentSpan span = startSpan(JAX_ENDPOINT_OPERATION_NAME);
+ JaxRsAnnotationsDecorator.DECORATE.onControllerStart(span, parent, method);
+ JaxRsAnnotationsDecorator.DECORATE.afterStart(span);
- JaxRsAnnotationsDecorator.DECORATE.onControllerStart(scope, parent, method);
- return JaxRsAnnotationsDecorator.DECORATE.afterStart(scope);
+ final AgentScope scope = activateSpan(span, false);
+ scope.setAsyncPropagation(true);
+ return scope;
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
- @Advice.Enter final Scope scope,
+ @Advice.Enter final AgentScope scope,
@Advice.Thrown final Throwable throwable,
@Advice.AllArguments final Object[] args) {
- final Span span = scope.span();
+ final AgentSpan span = scope.span();
if (throwable != null) {
JaxRsAnnotationsDecorator.DECORATE.onError(span, throwable);
JaxRsAnnotationsDecorator.DECORATE.beforeFinish(span);
@@ -104,7 +104,7 @@ public final class JaxRsAnnotationsInstrumentation extends Instrumenter.Default
}
}
if (asyncResponse != null && asyncResponse.isSuspended()) {
- InstrumentationContext.get(AsyncResponse.class, Span.class).put(asyncResponse, span);
+ InstrumentationContext.get(AsyncResponse.class, AgentSpan.class).put(asyncResponse, span);
} else {
JaxRsAnnotationsDecorator.DECORATE.beforeFinish(span);
span.finish();

MODIFY
dd-java-agent/instrumentation/jax-rs-annotations-2/src/main/java/datadog/trace/instrumentation/jaxrs2/JaxRsAsyncResponseInstrumentation.java
dd-java-agent/instrumentation/jax-rs-annotations-2/src/main/java/datadog/trace/instrumentation/jaxrs2/JaxRsAsyncResponseInstrumentation.java
@@ -10,7 +10,7 @@ import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.bootstrap.ContextStore;
import datadog.trace.bootstrap.InstrumentationContext;
-import io.opentracing.Span;
+import datadog.trace.instrumentation.api.AgentSpan;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -29,7 +29,8 @@ public final class JaxRsAsyncResponseInstrumentation extends Instrumenter.Defaul
@Override
public Map<String, String> contextStore() {
- return Collections.singletonMap("javax.ws.rs.container.AsyncResponse", Span.class.getName());
+ return Collections.singletonMap(
+ "javax.ws.rs.container.AsyncResponse", AgentSpan.class.getName());
}
@Override
@@ -62,10 +63,10 @@ public final class JaxRsAsyncResponseInstrumentation extends Instrumenter.Defaul
@Advice.OnMethodExit(suppress = Throwable.class)
public static void stopSpan(@Advice.This final AsyncResponse asyncResponse) {
- final ContextStore<AsyncResponse, Span> contextStore =
- InstrumentationContext.get(AsyncResponse.class, Span.class);
+ final ContextStore<AsyncResponse, AgentSpan> contextStore =
+ InstrumentationContext.get(AsyncResponse.class, AgentSpan.class);
- final Span span = contextStore.get(asyncResponse);
+ final AgentSpan span = contextStore.get(asyncResponse);
if (span != null) {
contextStore.put(asyncResponse, null);
DECORATE.beforeFinish(span);
@@ -81,10 +82,10 @@ public final class JaxRsAsyncResponseInstrumentation extends Instrumenter.Defaul
@Advice.This final AsyncResponse asyncResponse,
@Advice.Argument(0) final Throwable throwable) {
- final ContextStore<AsyncResponse, Span> contextStore =
- InstrumentationContext.get(AsyncResponse.class, Span.class);
+ final ContextStore<AsyncResponse, AgentSpan> contextStore =
+ InstrumentationContext.get(AsyncResponse.class, AgentSpan.class);
- final Span span = contextStore.get(asyncResponse);
+ final AgentSpan span = contextStore.get(asyncResponse);
if (span != null) {
contextStore.put(asyncResponse, null);
DECORATE.onError(span, throwable);
@@ -99,10 +100,10 @@ public final class JaxRsAsyncResponseInstrumentation extends Instrumenter.Defaul
@Advice.OnMethodExit(suppress = Throwable.class)
public static void stopSpan(@Advice.This final AsyncResponse asyncResponse) {
- final ContextStore<AsyncResponse, Span> contextStore =
- InstrumentationContext.get(AsyncResponse.class, Span.class);
+ final ContextStore<AsyncResponse, AgentSpan> contextStore =
+ InstrumentationContext.get(AsyncResponse.class, AgentSpan.class);
- final Span span = contextStore.get(asyncResponse);
+ final AgentSpan span = contextStore.get(asyncResponse);
if (span != null) {
contextStore.put(asyncResponse, null);
span.setTag("canceled", true);

Update jax-rs-annotations-2 to new agent api