Remove duplicate JMS spans from nested calls.
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
@@ -13,6 +13,7 @@ import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.api.DDSpanTypes;
import datadog.trace.api.DDTags;
+import datadog.trace.bootstrap.CallDepthThreadLocalMap;
import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.propagation.Format;
@@ -66,6 +67,11 @@ public final class JMSMessageProducerInstrumentation extends Instrumenter.Defaul
@Advice.OnMethodEnter(suppress = Throwable.class)
public static Scope startSpan(
@Advice.Argument(0) final Message message, @Advice.This final MessageProducer producer) {
+ final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(MessageProducer.class);
+ if (callDepth > 0) {
+ return null;
+ }
+
Destination defaultDestination;
try {
defaultDestination = producer.getDestination();
@@ -103,6 +109,7 @@ public final class JMSMessageProducerInstrumentation extends Instrumenter.Defaul
span.log(Collections.singletonMap(ERROR_OBJECT, throwable));
}
scope.close();
+ CallDepthThreadLocalMap.reset(MessageProducer.class);
}
}
}
@@ -114,6 +121,11 @@ public final class JMSMessageProducerInstrumentation extends Instrumenter.Defaul
@Advice.Argument(0) final Destination destination,
@Advice.Argument(1) final Message message,
@Advice.This final MessageProducer producer) {
+ final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(MessageProducer.class);
+ if (callDepth > 0) {
+ return null;
+ }
+
final Scope scope =
GlobalTracer.get()
.buildSpan("jms.produce")
@@ -143,6 +155,7 @@ public final class JMSMessageProducerInstrumentation extends Instrumenter.Defaul
span.log(Collections.singletonMap(ERROR_OBJECT, throwable));
}
scope.close();
+ CallDepthThreadLocalMap.reset(MessageProducer.class);
}
}
}

MODIFY
dd-java-agent/instrumentation/jms/src/test/groovy/JMS1Test.groovy
dd-java-agent/instrumentation/jms/src/test/groovy/JMS1Test.groovy
@@ -51,7 +51,7 @@ class JMS1Test extends AgentTestRunner {
producerTrace(it, 0, jmsResourceName)
trace(1, 1) { // Consumer trace
span(0) {
- childOf TEST_WRITER.firstTrace().get(2)
+ childOf TEST_WRITER[0][0]
serviceName "jms"
operationName "jms.consume"
resourceName "Consumed from $jmsResourceName"
@@ -103,7 +103,7 @@ class JMS1Test extends AgentTestRunner {
producerTrace(it, 0, jmsResourceName)
trace(1, 1) { // Consumer trace
span(0) {
- childOf TEST_WRITER.firstTrace().get(2)
+ childOf TEST_WRITER[0][0]
serviceName "jms"
operationName "jms.onMessage"
resourceName "Received from $jmsResourceName"
@@ -269,7 +269,7 @@ class JMS1Test extends AgentTestRunner {
}
def producerTrace(ListWriterAssert writer, int index, String jmsResourceName) {
- writer.trace(index, 3) {
+ writer.trace(index, 1) {
span(0) {
parent()
serviceName "jms"
@@ -278,38 +278,6 @@ class JMS1Test extends AgentTestRunner {
spanType DDSpanTypes.MESSAGE_PRODUCER
errored false
- tags {
- defaultTags()
- "${DDTags.SPAN_TYPE}" DDSpanTypes.MESSAGE_PRODUCER
- "${Tags.COMPONENT.key}" "jms"
- "${Tags.SPAN_KIND.key}" "producer"
- "span.origin.type" ActiveMQMessageProducer.name
- }
- }
- span(1) {
- childOf span(0)
- serviceName "jms"
- operationName "jms.produce"
- resourceName "Produced for $jmsResourceName"
- spanType DDSpanTypes.MESSAGE_PRODUCER
- errored false
-
- tags {
- defaultTags()
- "${DDTags.SPAN_TYPE}" DDSpanTypes.MESSAGE_PRODUCER
- "${Tags.COMPONENT.key}" "jms"
- "${Tags.SPAN_KIND.key}" "producer"
- "span.origin.type" ActiveMQMessageProducer.name
- }
- }
- span(2) {
- childOf span(1)
- serviceName "jms"
- operationName "jms.produce"
- resourceName "Produced for $jmsResourceName"
- spanType DDSpanTypes.MESSAGE_PRODUCER
- errored false
-
tags {
defaultTags()
"${DDTags.SPAN_TYPE}" DDSpanTypes.MESSAGE_PRODUCER
@@ -324,7 +292,7 @@ class JMS1Test extends AgentTestRunner {
def consumerTrace(ListWriterAssert writer, int index, String jmsResourceName, origin) {
writer.trace(index, 1) {
span(0) {
- childOf TEST_WRITER.firstTrace().get(2)
+ childOf TEST_WRITER[0][0]
serviceName "jms"
operationName "jms.onMessage"
resourceName "Received from $jmsResourceName"

Remove duplicate JMS spans from nested calls.