Update twilio to new agent api

This commit is contained in:
Trask Stalnaker 2019-10-19 11:57:49 -07:00
parent 377df7b789
commit 41185662df
3 changed files with 28 additions and 34 deletions

View File

@ -1,6 +1,8 @@
package datadog.trace.instrumentation.twilio; package datadog.trace.instrumentation.twilio;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType; import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static datadog.trace.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.instrumentation.twilio.TwilioClientDecorator.DECORATE; import static datadog.trace.instrumentation.twilio.TwilioClientDecorator.DECORATE;
import static java.util.Collections.singletonMap; import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isAbstract; import static net.bytebuddy.matcher.ElementMatchers.isAbstract;
@ -17,10 +19,8 @@ import com.google.common.util.concurrent.ListenableFuture;
import com.twilio.Twilio; import com.twilio.Twilio;
import datadog.trace.agent.tooling.Instrumenter; import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.bootstrap.CallDepthThreadLocalMap; import datadog.trace.bootstrap.CallDepthThreadLocalMap;
import datadog.trace.context.TraceScope; import datadog.trace.instrumentation.api.AgentScope;
import io.opentracing.Scope; import datadog.trace.instrumentation.api.AgentSpan;
import io.opentracing.Span;
import io.opentracing.util.GlobalTracer;
import java.util.Map; import java.util.Map;
import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.method.MethodDescription;
@ -86,7 +86,7 @@ public class TwilioAsyncInstrumentation extends Instrumenter.Default {
/** Method entry instrumentation. */ /** Method entry instrumentation. */
@Advice.OnMethodEnter(suppress = Throwable.class) @Advice.OnMethodEnter(suppress = Throwable.class)
public static Scope methodEnter( public static AgentScope methodEnter(
@Advice.This final Object that, @Advice.Origin("#m") final String methodName) { @Advice.This final Object that, @Advice.Origin("#m") final String methodName) {
// Ensure that we only create a span for the top-level Twilio client method; except in the // Ensure that we only create a span for the top-level Twilio client method; except in the
@ -99,33 +99,28 @@ public class TwilioAsyncInstrumentation extends Instrumenter.Default {
} }
// Don't automatically close the span with the scope if we're executing an async method // Don't automatically close the span with the scope if we're executing an async method
final Scope scope = GlobalTracer.get().buildSpan("twilio.sdk").startActive(false); final AgentSpan span = startSpan("twilio.sdk");
final Span span = scope.span();
DECORATE.afterStart(span); DECORATE.afterStart(span);
DECORATE.onServiceExecution(span, that, methodName); DECORATE.onServiceExecution(span, that, methodName);
// If an async operation was invoked and we have a TraceScope, final AgentScope scope = activateSpan(span, false);
if (scope instanceof TraceScope) { // Enable async propagation, so the newly spawned task will be associated back with this
// Enable async propagation, so the newly spawned task will be associated back with this // original trace.
// original trace. scope.setAsyncPropagation(true);
((TraceScope) scope).setAsyncPropagation(true);
}
return scope; return scope;
} }
/** Method exit instrumentation. */ /** Method exit instrumentation. */
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit( public static void methodExit(
@Advice.Enter final Scope scope, @Advice.Enter final AgentScope scope,
@Advice.Thrown final Throwable throwable, @Advice.Thrown final Throwable throwable,
@Advice.Return final ListenableFuture response) { @Advice.Return final ListenableFuture response) {
// If we have a scope (i.e. we were the top-level Twilio SDK invocation), // If we have a scope (i.e. we were the top-level Twilio SDK invocation),
if (scope != null) { if (scope != null) {
try { try {
final Span span = scope.span(); final AgentSpan span = scope.span();
if (throwable != null) { if (throwable != null) {
// There was an synchronous error, // There was an synchronous error,
@ -154,9 +149,9 @@ public class TwilioAsyncInstrumentation extends Instrumenter.Default {
public static class SpanFinishingCallback implements FutureCallback { public static class SpanFinishingCallback implements FutureCallback {
/** Span that we should finish and annotate when the future is complete. */ /** Span that we should finish and annotate when the future is complete. */
private final Span span; private final AgentSpan span;
public SpanFinishingCallback(final Span span) { public SpanFinishingCallback(final AgentSpan span) {
this.span = span; this.span = span;
} }

View File

@ -6,7 +6,7 @@ import com.twilio.rest.api.v2010.account.Message;
import datadog.trace.agent.decorator.ClientDecorator; import datadog.trace.agent.decorator.ClientDecorator;
import datadog.trace.api.DDSpanTypes; import datadog.trace.api.DDSpanTypes;
import datadog.trace.api.DDTags; import datadog.trace.api.DDTags;
import io.opentracing.Span; import datadog.trace.instrumentation.api.AgentSpan;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -40,8 +40,8 @@ public class TwilioClientDecorator extends ClientDecorator {
} }
/** Decorate trace based on service execution metadata. */ /** Decorate trace based on service execution metadata. */
public Span onServiceExecution( public AgentSpan onServiceExecution(
final Span span, final Object serviceExecutor, final String methodName) { final AgentSpan span, final Object serviceExecutor, final String methodName) {
// Drop common package prefix (com.twilio.rest) // Drop common package prefix (com.twilio.rest)
final String simpleClassName = final String simpleClassName =
@ -53,7 +53,7 @@ public class TwilioClientDecorator extends ClientDecorator {
} }
/** Annotate the span with the results of the operation. */ /** Annotate the span with the results of the operation. */
public Span onResult(final Span span, Object result) { public AgentSpan onResult(final AgentSpan span, Object result) {
// Unwrap ListenableFuture (if present) // Unwrap ListenableFuture (if present)
if (result instanceof ListenableFuture) { if (result instanceof ListenableFuture) {
@ -105,7 +105,7 @@ public class TwilioClientDecorator extends ClientDecorator {
* required. * required.
*/ */
private void setTagIfPresent( private void setTagIfPresent(
final Span span, final Object result, final String tag, final String getter) { final AgentSpan span, final Object result, final String tag, final String getter) {
try { try {
final Method method = result.getClass().getMethod(getter); final Method method = result.getClass().getMethod(getter);
final Object value = method.invoke(result); final Object value = method.invoke(result);

View File

@ -1,6 +1,8 @@
package datadog.trace.instrumentation.twilio; package datadog.trace.instrumentation.twilio;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType; import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static datadog.trace.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.instrumentation.twilio.TwilioClientDecorator.DECORATE; import static datadog.trace.instrumentation.twilio.TwilioClientDecorator.DECORATE;
import static java.util.Collections.singletonMap; import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isAbstract; import static net.bytebuddy.matcher.ElementMatchers.isAbstract;
@ -13,9 +15,8 @@ import com.google.auto.service.AutoService;
import com.twilio.Twilio; import com.twilio.Twilio;
import datadog.trace.agent.tooling.Instrumenter; import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.bootstrap.CallDepthThreadLocalMap; import datadog.trace.bootstrap.CallDepthThreadLocalMap;
import io.opentracing.Scope; import datadog.trace.instrumentation.api.AgentScope;
import io.opentracing.Span; import datadog.trace.instrumentation.api.AgentSpan;
import io.opentracing.util.GlobalTracer;
import java.util.Map; import java.util.Map;
import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.method.MethodDescription;
@ -81,7 +82,7 @@ public class TwilioSyncInstrumentation extends Instrumenter.Default {
/** Method entry instrumentation. */ /** Method entry instrumentation. */
@Advice.OnMethodEnter(suppress = Throwable.class) @Advice.OnMethodEnter(suppress = Throwable.class)
public static Scope methodEnter( public static AgentScope methodEnter(
@Advice.This final Object that, @Advice.Origin("#m") final String methodName) { @Advice.This final Object that, @Advice.Origin("#m") final String methodName) {
// Ensure that we only create a span for the top-level Twilio client method; except in the // Ensure that we only create a span for the top-level Twilio client method; except in the
@ -93,26 +94,24 @@ public class TwilioSyncInstrumentation extends Instrumenter.Default {
return null; return null;
} }
final Scope scope = GlobalTracer.get().buildSpan("twilio.sdk").startActive(true); final AgentSpan span = startSpan("twilio.sdk");
final Span span = scope.span();
DECORATE.afterStart(span); DECORATE.afterStart(span);
DECORATE.onServiceExecution(span, that, methodName); DECORATE.onServiceExecution(span, that, methodName);
return scope; return activateSpan(span, true);
} }
/** Method exit instrumentation. */ /** Method exit instrumentation. */
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit( public static void methodExit(
@Advice.Enter final Scope scope, @Advice.Enter final AgentScope scope,
@Advice.Thrown final Throwable throwable, @Advice.Thrown final Throwable throwable,
@Advice.Return final Object response) { @Advice.Return final Object response) {
// If we have a scope (i.e. we were the top-level Twilio SDK invocation), // If we have a scope (i.e. we were the top-level Twilio SDK invocation),
if (scope != null) { if (scope != null) {
try { try {
final Span span = scope.span(); final AgentSpan span = scope.span();
DECORATE.onResult(span, response); DECORATE.onResult(span, response);
DECORATE.onError(span, throwable); DECORATE.onError(span, throwable);