Update spring-web to new agent api
MODIFY
dd-java-agent/instrumentation/spring-web/src/main/java/datadog/trace/instrumentation/springweb/DispatcherServletInstrumentation.java
dd-java-agent/instrumentation/spring-web/src/main/java/datadog/trace/instrumentation/springweb/DispatcherServletInstrumentation.java
package datadog.trace.instrumentation.springweb;
+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 datadog.trace.instrumentation.springweb.SpringWebHttpServerDecorator.DECORATE;
import static datadog.trace.instrumentation.springweb.SpringWebHttpServerDecorator.DECORATE_RENDER;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
@@ -10,10 +13,8 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
-import io.opentracing.Scope;
-import io.opentracing.Span;
-import io.opentracing.tag.Tags;
-import io.opentracing.util.GlobalTracer;
+import datadog.trace.instrumentation.api.AgentScope;
+import datadog.trace.instrumentation.api.AgentSpan;
import java.util.HashMap;
import java.util.Map;
import net.bytebuddy.asm.Advice;
@@ -66,16 +67,16 @@ public final class DispatcherServletInstrumentation extends Instrumenter.Default
public static class DispatcherAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
- public static Scope startSpan(@Advice.Argument(0) final ModelAndView mv) {
- final Scope scope = GlobalTracer.get().buildSpan("response.render").startActive(true);
- DECORATE_RENDER.afterStart(scope);
- DECORATE_RENDER.onRender(scope, mv);
- return scope;
+ public static AgentScope onEnter(@Advice.Argument(0) final ModelAndView mv) {
+ final AgentSpan span = startSpan("response.render");
+ DECORATE_RENDER.afterStart(span);
+ DECORATE_RENDER.onRender(span, mv);
+ return activateSpan(span, true);
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
- @Advice.Enter final Scope scope, @Advice.Thrown final Throwable throwable) {
+ @Advice.Enter final AgentScope scope, @Advice.Thrown final Throwable throwable) {
DECORATE_RENDER.onError(scope, throwable);
DECORATE_RENDER.beforeFinish(scope);
scope.close();
@@ -85,13 +86,12 @@ public final class DispatcherServletInstrumentation extends Instrumenter.Default
public static class ErrorHandlerAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void nameResource(@Advice.Argument(3) final Exception exception) {
- final Scope scope = GlobalTracer.get().scopeManager().active();
- if (scope != null && exception != null) {
- final Span span = scope.span();
+ final AgentSpan span = activeSpan();
+ if (span != null && exception != null) {
DECORATE.onError(span, exception);
// We want to capture the stacktrace, but that doesn't mean it should be an error.
// We rely on a decorator to set the error state based on response code. (5xx -> error)
- Tags.ERROR.set(span, false);
+ span.setError(false);
}
}
}

MODIFY
dd-java-agent/instrumentation/spring-web/src/main/java/datadog/trace/instrumentation/springweb/HandlerAdapterInstrumentation.java
dd-java-agent/instrumentation/spring-web/src/main/java/datadog/trace/instrumentation/springweb/HandlerAdapterInstrumentation.java
package datadog.trace.instrumentation.springweb;
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 datadog.trace.instrumentation.springweb.SpringWebHttpServerDecorator.DECORATE;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
@@ -14,9 +17,8 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
-import datadog.trace.context.TraceScope;
-import io.opentracing.Scope;
-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.Map;
import javax.servlet.Servlet;
@@ -67,14 +69,14 @@ public final class HandlerAdapterInstrumentation extends Instrumenter.Default {
public static class ControllerAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
- public static Scope nameResourceAndStartSpan(
+ public static AgentScope nameResourceAndStartSpan(
@Advice.Argument(0) final HttpServletRequest request,
@Advice.Argument(2) final Object handler) {
// Name the parent span based on the matching pattern
// This is likely the servlet.request span.
- final Scope parentScope = GlobalTracer.get().scopeManager().active();
- if (parentScope != null) {
- DECORATE.onRequest(parentScope.span(), request);
+ final AgentSpan parentSpan = activeSpan();
+ if (parentSpan != null) {
+ DECORATE.onRequest(parentSpan, request);
}
// Now create a span for controller execution.
@@ -107,17 +109,17 @@ public final class HandlerAdapterInstrumentation extends Instrumenter.Default {
final String operationName = DECORATE.spanNameForClass(clazz) + "." + methodName;
- final Scope scope = GlobalTracer.get().buildSpan(operationName).startActive(true);
- if (scope instanceof TraceScope) {
- ((TraceScope) scope).setAsyncPropagation(true);
- }
- DECORATE.afterStart(scope);
+ final AgentSpan span = startSpan(operationName);
+ DECORATE.afterStart(span);
+
+ final AgentScope scope = activateSpan(span, true);
+ scope.setAsyncPropagation(true);
return scope;
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
- @Advice.Enter final Scope scope, @Advice.Thrown final Throwable throwable) {
+ @Advice.Enter final AgentScope scope, @Advice.Thrown final Throwable throwable) {
DECORATE.onError(scope, throwable);
DECORATE.beforeFinish(scope);
scope.close();

MODIFY
dd-java-agent/instrumentation/spring-web/src/main/java/datadog/trace/instrumentation/springweb/SpringWebHttpServerDecorator.java
dd-java-agent/instrumentation/spring-web/src/main/java/datadog/trace/instrumentation/springweb/SpringWebHttpServerDecorator.java
@@ -3,8 +3,7 @@ package datadog.trace.instrumentation.springweb;
import datadog.trace.agent.decorator.HttpServerDecorator;
import datadog.trace.api.DDSpanTypes;
import datadog.trace.api.DDTags;
-import io.opentracing.Scope;
-import io.opentracing.Span;
+import datadog.trace.instrumentation.api.AgentSpan;
import java.net.URI;
import java.net.URISyntaxException;
import javax.servlet.http.HttpServletRequest;
@@ -71,7 +70,7 @@ public class SpringWebHttpServerDecorator
}
@Override
- public Span onRequest(final Span span, final HttpServletRequest request) {
+ public AgentSpan onRequest(final AgentSpan span, final HttpServletRequest request) {
super.onRequest(span, request);
if (request != null) {
final String method = request.getMethod();
@@ -86,14 +85,13 @@ public class SpringWebHttpServerDecorator
return span;
}
- public Scope onRender(final Scope scope, final ModelAndView mv) {
- final Span span = scope.span();
+ public AgentSpan onRender(final AgentSpan span, final ModelAndView mv) {
if (mv.getViewName() != null) {
span.setTag("view.name", mv.getViewName());
}
if (mv.getView() != null) {
span.setTag("view.type", mv.getView().getClass().getName());
}
- return scope;
+ return span;
}
}

Update spring-web to new agent api