Skip spring controller and dropwizard trace if no trace present
Also prefer early return instead of deep nesting for null check.
MODIFY
dd-java-agent/instrumentation/apache-httpclient-4/src/main/java/datadog/trace/instrumentation/apachehttpclient/ApacheHttpClientInstrumentation.java
dd-java-agent/instrumentation/apache-httpclient-4/src/main/java/datadog/trace/instrumentation/apachehttpclient/ApacheHttpClientInstrumentation.java
@@ -166,7 +166,9 @@ public class ApacheHttpClientInstrumentation extends Instrumenter.Default {
public static void doMethodExit(
final AgentScope scope, final Object result, final Throwable throwable) {
- if (scope != null) {
+ if (scope == null) {
+ return;
+ }
try {
final AgentSpan span = scope.span();
@@ -182,7 +184,6 @@ public class ApacheHttpClientInstrumentation extends Instrumenter.Default {
}
}
}
- }
public static class UriRequestAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)

MODIFY
dd-java-agent/instrumentation/dropwizard/dropwizard-views/src/main/java/datadog/trace/instrumentation/dropwizard/view/DropwizardViewInstrumentation.java
dd-java-agent/instrumentation/dropwizard/dropwizard-views/src/main/java/datadog/trace/instrumentation/dropwizard/view/DropwizardViewInstrumentation.java
@@ -2,6 +2,7 @@ package datadog.trace.instrumentation.dropwizard.view;
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.isInterface;
@@ -51,11 +52,13 @@ public final class DropwizardViewInstrumentation extends Instrumenter.Default {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope onEnter(
@Advice.This final Object obj, @Advice.Argument(0) final View view) {
+ if (activeSpan() == null) {
+ return null;
+ }
final AgentSpan span =
startSpan("view.render")
.setTag(DDTags.RESOURCE_NAME, "View " + view.getTemplateName())
.setTag(Tags.COMPONENT.getKey(), "dropwizard-view")
- .setTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER)
.setTag("span.origin.type", obj.getClass().getSimpleName());
return activateSpan(span, true);
}
@@ -63,6 +66,9 @@ public final class DropwizardViewInstrumentation extends Instrumenter.Default {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Enter final AgentScope scope, @Advice.Thrown final Throwable throwable) {
+ if (scope == null) {
+ return;
+ }
final AgentSpan span = scope.span();
if (throwable != null) {
span.setError(true);

MODIFY
dd-java-agent/instrumentation/dropwizard/dropwizard-views/src/test/groovy/ViewRenderTest.groovy
dd-java-agent/instrumentation/dropwizard/dropwizard-views/src/test/groovy/ViewRenderTest.groovy
@@ -6,6 +6,8 @@ import io.dropwizard.views.mustache.MustacheViewRenderer
import java.nio.charset.StandardCharsets
import static datadog.trace.agent.test.asserts.ListWriterAssert.assertTraces
+import static datadog.trace.agent.test.utils.TraceUtils.basicSpan
+import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
class ViewRenderTest extends AgentTestRunner {
@@ -14,19 +16,22 @@ class ViewRenderTest extends AgentTestRunner {
def outputStream = new ByteArrayOutputStream()
when:
+ runUnderTrace("parent") {
renderer.render(view, Locale.ENGLISH, outputStream)
+ }
then:
outputStream.toString().contains("This is an example of a view")
assertTraces(TEST_WRITER, 1) {
- trace(0, 1) {
- span(0) {
+ trace(0, 2) {
+ basicSpan(it, 0, "parent")
+ span(1) {
resourceName "View $template"
operationName "view.render"
+ childOf span(0)
tags {
"component" "dropwizard-view"
"span.origin.type" renderer.class.simpleName
- "span.kind" "server"
defaultTags()
}
}

MODIFY
dd-java-agent/instrumentation/jax-rs-client-1.1/src/main/java/datadog/trace/instrumentation/jaxrs/v1/JaxRsClientV1Instrumentation.java
dd-java-agent/instrumentation/jax-rs-client-1.1/src/main/java/datadog/trace/instrumentation/jaxrs/v1/JaxRsClientV1Instrumentation.java
@@ -88,7 +88,9 @@ public final class JaxRsClientV1Instrumentation extends Instrumenter.Default {
@Advice.Enter final AgentScope scope,
@Advice.Return final ClientResponse response,
@Advice.Thrown final Throwable throwable) {
- if (null != scope) {
+ if (scope == null) {
+ return;
+ }
final AgentSpan span = scope.span();
DECORATE.onResponse(span, response);
DECORATE.onError(span, throwable);
@@ -97,4 +99,3 @@ public final class JaxRsClientV1Instrumentation extends Instrumenter.Default {
}
}
}
-}

MODIFY
dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/PreparedStatementInstrumentation.java
dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/PreparedStatementInstrumentation.java
@@ -93,7 +93,9 @@ public final class PreparedStatementInstrumentation extends Instrumenter.Default
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Enter final AgentScope scope, @Advice.Thrown final Throwable throwable) {
- if (scope != null) {
+ if (scope == null) {
+ return;
+ }
DECORATE.onError(scope.span(), throwable);
DECORATE.beforeFinish(scope.span());
scope.close();
@@ -101,4 +103,3 @@ public final class PreparedStatementInstrumentation extends Instrumenter.Default
}
}
}
-}

MODIFY
dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/StatementInstrumentation.java
dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/StatementInstrumentation.java
@@ -94,7 +94,9 @@ public final class StatementInstrumentation extends Instrumenter.Default {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Enter final AgentScope scope, @Advice.Thrown final Throwable throwable) {
- if (scope != null) {
+ if (scope == null) {
+ return;
+ }
DECORATE.onError(scope.span(), throwable);
DECORATE.beforeFinish(scope.span());
scope.close();
@@ -102,4 +104,3 @@ public final class StatementInstrumentation extends Instrumenter.Default {
}
}
}
-}

MODIFY
dd-java-agent/instrumentation/jetty-8/src/main/java/datadog/trace/instrumentation/jetty8/JettyHandlerAdvice.java
dd-java-agent/instrumentation/jetty-8/src/main/java/datadog/trace/instrumentation/jetty8/JettyHandlerAdvice.java
@@ -50,7 +50,9 @@ public class JettyHandlerAdvice {
@Advice.Argument(3) final HttpServletResponse resp,
@Advice.Enter final AgentScope scope,
@Advice.Thrown final Throwable throwable) {
- if (scope != null) {
+ if (scope == null) {
+ return;
+ }
final AgentSpan span = scope.span();
if (req.getUserPrincipal() != null) {
span.setTag(DDTags.USER_NAME, req.getUserPrincipal().getName());
@@ -84,4 +86,3 @@ public class JettyHandlerAdvice {
scope.close();
}
}
-}

MODIFY
dd-java-agent/instrumentation/jms/src/main/java/datadog/trace/instrumentation/jms/JMSMessageListenerInstrumentation.java
dd-java-agent/instrumentation/jms/src/main/java/datadog/trace/instrumentation/jms/JMSMessageListenerInstrumentation.java
@@ -80,11 +80,12 @@ public final class JMSMessageListenerInstrumentation extends Instrumenter.Defaul
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Enter final AgentScope scope, @Advice.Thrown final Throwable throwable) {
- if (scope != null) {
+ if (scope == null) {
+ return;
+ }
CONSUMER_DECORATE.onError(scope, throwable);
CONSUMER_DECORATE.beforeFinish(scope);
scope.close();
}
}
}
-}

MODIFY
dd-java-agent/instrumentation/jms/src/main/java/datadog/trace/instrumentation/jms/JMSMessageProducerInstrumentation.java
dd-java-agent/instrumentation/jms/src/main/java/datadog/trace/instrumentation/jms/JMSMessageProducerInstrumentation.java
@@ -98,15 +98,15 @@ public final class JMSMessageProducerInstrumentation extends Instrumenter.Defaul
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Enter final AgentScope scope, @Advice.Thrown final Throwable throwable) {
-
- if (scope != null) {
+ if (scope == null) {
+ return;
+ }
PRODUCER_DECORATE.onError(scope, throwable);
PRODUCER_DECORATE.beforeFinish(scope);
scope.close();
CallDepthThreadLocalMap.reset(MessageProducer.class);
}
}
- }
public static class ProducerWithDestinationAdvice {
@@ -133,7 +133,9 @@ public final class JMSMessageProducerInstrumentation extends Instrumenter.Defaul
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Enter final AgentScope scope, @Advice.Thrown final Throwable throwable) {
- if (scope != null) {
+ if (scope == null) {
+ return;
+ }
PRODUCER_DECORATE.onError(scope, throwable);
PRODUCER_DECORATE.beforeFinish(scope);
scope.close();
@@ -141,4 +143,3 @@ public final class JMSMessageProducerInstrumentation extends Instrumenter.Defaul
}
}
}
-}

MODIFY
dd-java-agent/instrumentation/rabbitmq-amqp-2.7/src/main/java/datadog/trace/instrumentation/rabbitmq/amqp/RabbitChannelInstrumentation.java
dd-java-agent/instrumentation/rabbitmq-amqp-2.7/src/main/java/datadog/trace/instrumentation/rabbitmq/amqp/RabbitChannelInstrumentation.java
@@ -132,14 +132,15 @@ public class RabbitChannelInstrumentation extends Instrumenter.Default {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Enter final AgentScope scope, @Advice.Thrown final Throwable throwable) {
- if (scope != null) {
+ if (scope == null) {
+ return;
+ }
DECORATE.onError(scope, throwable);
DECORATE.beforeFinish(scope);
scope.close();
CallDepthThreadLocalMap.reset(Channel.class);
}
}
- }
public static class ChannelPublishAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)

MODIFY
dd-java-agent/instrumentation/servlet-2/src/main/java/datadog/trace/instrumentation/servlet2/Servlet2Advice.java
dd-java-agent/instrumentation/servlet-2/src/main/java/datadog/trace/instrumentation/servlet2/Servlet2Advice.java
@@ -74,7 +74,9 @@ public class Servlet2Advice {
}
}
- if (scope != null) {
+ if (scope == null) {
+ return;
+ }
final AgentSpan span = scope.span();
DECORATE.onResponse(span, response);
if (throwable != null) {
@@ -92,4 +94,3 @@ public class Servlet2Advice {
scope.close();
}
}
-}

MODIFY
dd-java-agent/instrumentation/servlet-3/src/main/java/datadog/trace/instrumentation/servlet3/Servlet3Advice.java
dd-java-agent/instrumentation/servlet-3/src/main/java/datadog/trace/instrumentation/servlet3/Servlet3Advice.java
@@ -62,7 +62,10 @@ public class Servlet3Advice {
}
}
- if (scope != null) {
+ if (scope == null) {
+ return;
+ }
+
if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
final HttpServletRequest req = (HttpServletRequest) request;
final HttpServletResponse resp = (HttpServletResponse) response;
@@ -100,4 +103,3 @@ public class Servlet3Advice {
}
}
}
-}

MODIFY
dd-java-agent/instrumentation/spring-webmvc-3.1/src/main/java/datadog/trace/instrumentation/springweb/HandlerAdapterInstrumentation.java
dd-java-agent/instrumentation/spring-webmvc-3.1/src/main/java/datadog/trace/instrumentation/springweb/HandlerAdapterInstrumentation.java
@@ -79,6 +79,10 @@ public final class HandlerAdapterInstrumentation extends Instrumenter.Default {
DECORATE.onRequest(parentSpan, request);
}
+ if (activeSpan() == null) {
+ return null;
+ }
+
// Now create a span for controller execution.
final Class<?> clazz;
@@ -120,6 +124,9 @@ public final class HandlerAdapterInstrumentation extends Instrumenter.Default {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Enter final AgentScope scope, @Advice.Thrown final Throwable throwable) {
+ if (scope == null) {
+ return;
+ }
DECORATE.onError(scope, throwable);
DECORATE.beforeFinish(scope);
scope.close();

MODIFY
dd-java-agent/instrumentation/spymemcached-2.12/src/main/java/datadog/trace/instrumentation/spymemcached/MemcachedClientInstrumentation.java
dd-java-agent/instrumentation/spymemcached-2.12/src/main/java/datadog/trace/instrumentation/spymemcached/MemcachedClientInstrumentation.java
@@ -156,10 +156,11 @@ public final class MemcachedClientInstrumentation extends Instrumenter.Default {
public static void methodExit(
@Advice.Enter final SyncCompletionListener listener,
@Advice.Thrown final Throwable thrown) {
- if (listener != null) {
+ if (listener == null) {
+ return;
+ }
CallDepthThreadLocalMap.reset(MemcachedClient.class);
listener.done(thrown);
}
}
}
-}

MODIFY
dd-java-agent/instrumentation/twilio/src/main/java/datadog/trace/instrumentation/twilio/TwilioAsyncInstrumentation.java
dd-java-agent/instrumentation/twilio/src/main/java/datadog/trace/instrumentation/twilio/TwilioAsyncInstrumentation.java
@@ -116,9 +116,10 @@ public class TwilioAsyncInstrumentation extends Instrumenter.Default {
@Advice.Enter final AgentScope scope,
@Advice.Thrown final Throwable throwable,
@Advice.Return final ListenableFuture response) {
-
+ if (scope == null) {
+ return;
+ }
// If we have a scope (i.e. we were the top-level Twilio SDK invocation),
- if (scope != null) {
try {
final AgentSpan span = scope.span();
@@ -140,7 +141,6 @@ public class TwilioAsyncInstrumentation extends Instrumenter.Default {
}
}
}
- }
/**
* FutureCallback, which automatically finishes the span and annotates with any appropriate

MODIFY
dd-java-agent/instrumentation/twilio/src/main/java/datadog/trace/instrumentation/twilio/TwilioSyncInstrumentation.java
dd-java-agent/instrumentation/twilio/src/main/java/datadog/trace/instrumentation/twilio/TwilioSyncInstrumentation.java
@@ -107,9 +107,11 @@ public class TwilioSyncInstrumentation extends Instrumenter.Default {
@Advice.Enter final AgentScope scope,
@Advice.Thrown final Throwable throwable,
@Advice.Return final Object response) {
+ if (scope == null) {
+ return;
+ }
// If we have a scope (i.e. we were the top-level Twilio SDK invocation),
- if (scope != null) {
try {
final AgentSpan span = scope.span();
@@ -123,4 +125,3 @@ public class TwilioSyncInstrumentation extends Instrumenter.Default {
}
}
}
-}

Skip spring controller and dropwizard trace if no trace present

Also prefer early return instead of deep nesting for null check.