Update trace-annotation to new agent api

This commit is contained in:
Trask Stalnaker 2019-10-19 11:57:49 -07:00
parent 05e6544ded
commit 377df7b789
1 changed files with 11 additions and 14 deletions

View File

@ -1,13 +1,13 @@
package datadog.trace.instrumentation.trace_annotation; package datadog.trace.instrumentation.trace_annotation;
import static datadog.trace.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.instrumentation.trace_annotation.TraceDecorator.DECORATE; import static datadog.trace.instrumentation.trace_annotation.TraceDecorator.DECORATE;
import datadog.trace.api.DDTags; import datadog.trace.api.DDTags;
import datadog.trace.api.Trace; import datadog.trace.api.Trace;
import datadog.trace.context.TraceScope; import datadog.trace.instrumentation.api.AgentScope;
import io.opentracing.Scope; import datadog.trace.instrumentation.api.AgentSpan;
import io.opentracing.Tracer;
import io.opentracing.util.GlobalTracer;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice;
@ -15,33 +15,30 @@ public class TraceAdvice {
private static final String DEFAULT_OPERATION_NAME = "trace.annotation"; private static final String DEFAULT_OPERATION_NAME = "trace.annotation";
@Advice.OnMethodEnter(suppress = Throwable.class) @Advice.OnMethodEnter(suppress = Throwable.class)
public static Scope startSpan(@Advice.Origin final Method method) { public static AgentScope onEnter(@Advice.Origin final Method method) {
final Trace traceAnnotation = method.getAnnotation(Trace.class); final Trace traceAnnotation = method.getAnnotation(Trace.class);
String operationName = traceAnnotation == null ? null : traceAnnotation.operationName(); String operationName = traceAnnotation == null ? null : traceAnnotation.operationName();
if (operationName == null || operationName.isEmpty()) { if (operationName == null || operationName.isEmpty()) {
operationName = DEFAULT_OPERATION_NAME; operationName = DEFAULT_OPERATION_NAME;
} }
Tracer.SpanBuilder spanBuilder = GlobalTracer.get().buildSpan(operationName); final AgentSpan span = startSpan(operationName);
String resourceName = traceAnnotation == null ? null : traceAnnotation.resourceName(); String resourceName = traceAnnotation == null ? null : traceAnnotation.resourceName();
if (resourceName == null || resourceName.isEmpty()) { if (resourceName == null || resourceName.isEmpty()) {
resourceName = DECORATE.spanNameForMethod(method); resourceName = DECORATE.spanNameForMethod(method);
} }
spanBuilder = spanBuilder.withTag(DDTags.RESOURCE_NAME, resourceName); span.setTag(DDTags.RESOURCE_NAME, resourceName);
DECORATE.afterStart(span);
final Scope scope = DECORATE.afterStart(spanBuilder.startActive(true));
if (scope instanceof TraceScope) {
((TraceScope) scope).setAsyncPropagation(true);
}
final AgentScope scope = activateSpan(span, true);
scope.setAsyncPropagation(true);
return scope; return scope;
} }
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan( 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.onError(scope, throwable);
DECORATE.beforeFinish(scope); DECORATE.beforeFinish(scope);
scope.close(); scope.close();