Update jms to new agent api
MODIFY
dd-java-agent/instrumentation/jms/src/main/java/datadog/trace/instrumentation/jms/JMSDecorator.java
dd-java-agent/instrumentation/jms/src/main/java/datadog/trace/instrumentation/jms/JMSDecorator.java
@@ -3,8 +3,7 @@ package datadog.trace.instrumentation.jms;
import datadog.trace.agent.decorator.ClientDecorator;
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 io.opentracing.tag.Tags;
import java.lang.reflect.Method;
import javax.jms.Destination;
@@ -59,22 +58,21 @@ public abstract class JMSDecorator extends ClientDecorator {
@Override
protected abstract String spanKind();
- public void onConsume(final Span span, final Message message) {
+ public void onConsume(final AgentSpan span, final Message message) {
span.setTag(DDTags.RESOURCE_NAME, "Consumed from " + toResourceName(message, null));
}
- public void onReceive(final Span span, final Method method) {
+ public void onReceive(final AgentSpan span, final Method method) {
span.setTag(DDTags.RESOURCE_NAME, "JMS " + method.getName());
}
- public void onReceive(final Scope scope, final Message message) {
- scope.span().setTag(DDTags.RESOURCE_NAME, "Received from " + toResourceName(message, null));
+ public void onReceive(final AgentSpan span, final Message message) {
+ span.setTag(DDTags.RESOURCE_NAME, "Received from " + toResourceName(message, null));
}
- public void onProduce(final Scope scope, final Message message, final Destination destination) {
- scope
- .span()
- .setTag(DDTags.RESOURCE_NAME, "Produced for " + toResourceName(message, destination));
+ public void onProduce(
+ final AgentSpan span, final Message message, final Destination destination) {
+ span.setTag(DDTags.RESOURCE_NAME, "Produced for " + toResourceName(message, destination));
}
private static final String TIBCO_TMP_PREFIX = "$TMP$";

MODIFY
dd-java-agent/instrumentation/jms/src/main/java/datadog/trace/instrumentation/jms/JMSMessageConsumerInstrumentation.java
dd-java-agent/instrumentation/jms/src/main/java/datadog/trace/instrumentation/jms/JMSMessageConsumerInstrumentation.java
package datadog.trace.instrumentation.jms;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
+import static datadog.trace.instrumentation.api.AgentTracer.activateSpan;
+import static datadog.trace.instrumentation.api.AgentTracer.propagate;
+import static datadog.trace.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.instrumentation.jms.JMSDecorator.CONSUMER_DECORATE;
+import static datadog.trace.instrumentation.jms.MessagePropertyTextMap.GETTER;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -10,12 +14,9 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
-import io.opentracing.Scope;
-import io.opentracing.Span;
-import io.opentracing.SpanContext;
-import io.opentracing.Tracer;
-import io.opentracing.propagation.Format;
-import io.opentracing.util.GlobalTracer;
+import datadog.trace.instrumentation.api.AgentScope;
+import datadog.trace.instrumentation.api.AgentSpan;
+import datadog.trace.instrumentation.api.AgentSpan.Context;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
@@ -66,7 +67,7 @@ public final class JMSMessageConsumerInstrumentation extends Instrumenter.Defaul
public static class ConsumerAdvice {
@Advice.OnMethodEnter
- public static long startSpan() {
+ public static long onEnter() {
return System.currentTimeMillis();
}
@@ -77,23 +78,17 @@ public final class JMSMessageConsumerInstrumentation extends Instrumenter.Defaul
@Advice.Origin final Method method,
@Advice.Return final Message message,
@Advice.Thrown final Throwable throwable) {
- Tracer.SpanBuilder spanBuilder =
- GlobalTracer.get()
- .buildSpan("jms.consume")
- .withTag("span.origin.type", consumer.getClass().getName())
- .withStartTimestamp(TimeUnit.MILLISECONDS.toMicros(startTime));
-
+ final AgentSpan span;
if (message != null) {
- final SpanContext extractedContext =
- GlobalTracer.get()
- .extract(Format.Builtin.TEXT_MAP, new MessagePropertyTextMap(message));
- if (extractedContext != null) {
- spanBuilder = spanBuilder.asChildOf(extractedContext);
- }
+ final Context extractedContext = propagate().extract(message, GETTER);
+ span =
+ startSpan("jms.consume", extractedContext, TimeUnit.MILLISECONDS.toMicros(startTime));
+ } else {
+ span = startSpan("jms.consume", TimeUnit.MILLISECONDS.toMicros(startTime));
}
+ span.setTag("span.origin.type", consumer.getClass().getName());
- final Span span = spanBuilder.start();
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
+ try (final AgentScope scope = activateSpan(span, false)) {
CONSUMER_DECORATE.afterStart(span);
if (message == null) {
CONSUMER_DECORATE.onReceive(span, method);

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
package datadog.trace.instrumentation.jms;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
+import static datadog.trace.instrumentation.api.AgentTracer.activateSpan;
+import static datadog.trace.instrumentation.api.AgentTracer.propagate;
+import static datadog.trace.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.instrumentation.jms.JMSDecorator.CONSUMER_DECORATE;
+import static datadog.trace.instrumentation.jms.MessagePropertyTextMap.GETTER;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
@@ -11,11 +15,9 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
-import datadog.trace.context.TraceScope;
-import io.opentracing.Scope;
-import io.opentracing.SpanContext;
-import io.opentracing.propagation.Format;
-import io.opentracing.util.GlobalTracer;
+import datadog.trace.instrumentation.api.AgentScope;
+import datadog.trace.instrumentation.api.AgentSpan;
+import datadog.trace.instrumentation.api.AgentSpan.Context;
import java.util.Map;
import javax.jms.Message;
import javax.jms.MessageListener;
@@ -58,31 +60,25 @@ public final class JMSMessageListenerInstrumentation extends Instrumenter.Defaul
public static class MessageListenerAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
- public static Scope startSpan(
+ public static AgentScope onEnter(
@Advice.Argument(0) final Message message, @Advice.This final MessageListener listener) {
- final SpanContext extractedContext =
- GlobalTracer.get().extract(Format.Builtin.TEXT_MAP, new MessagePropertyTextMap(message));
+ final Context extractedContext = propagate().extract(message, GETTER);
- final Scope scope =
- GlobalTracer.get()
- .buildSpan("jms.onMessage")
- .asChildOf(extractedContext)
- .withTag("span.origin.type", listener.getClass().getName())
- .startActive(true);
- CONSUMER_DECORATE.afterStart(scope);
- CONSUMER_DECORATE.onReceive(scope, message);
-
- if (scope instanceof TraceScope) {
- ((TraceScope) scope).setAsyncPropagation(true);
- }
+ final AgentSpan span =
+ startSpan("jms.onMessage", extractedContext)
+ .setTag("span.origin.type", listener.getClass().getName());
+ CONSUMER_DECORATE.afterStart(span);
+ CONSUMER_DECORATE.onReceive(span, message);
+ 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) {
if (scope != null) {
CONSUMER_DECORATE.onError(scope, throwable);
CONSUMER_DECORATE.beforeFinish(scope);

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
package datadog.trace.instrumentation.jms;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
+import static datadog.trace.instrumentation.api.AgentTracer.activateSpan;
+import static datadog.trace.instrumentation.api.AgentTracer.propagate;
+import static datadog.trace.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.instrumentation.jms.JMSDecorator.PRODUCER_DECORATE;
+import static datadog.trace.instrumentation.jms.MessagePropertyTextMap.SETTER;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -11,9 +15,8 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.bootstrap.CallDepthThreadLocalMap;
-import io.opentracing.Scope;
-import io.opentracing.propagation.Format;
-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 javax.jms.Destination;
@@ -67,7 +70,7 @@ public final class JMSMessageProducerInstrumentation extends Instrumenter.Defaul
public static class ProducerAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
- public static Scope startSpan(
+ public static AgentScope onEnter(
@Advice.Argument(0) final Message message, @Advice.This final MessageProducer producer) {
final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(MessageProducer.class);
if (callDepth > 0) {
@@ -81,24 +84,19 @@ public final class JMSMessageProducerInstrumentation extends Instrumenter.Defaul
defaultDestination = null;
}
- final Scope scope =
- GlobalTracer.get()
- .buildSpan("jms.produce")
- .withTag("span.origin.type", producer.getClass().getName())
- .startActive(true);
- PRODUCER_DECORATE.afterStart(scope);
- PRODUCER_DECORATE.onProduce(scope, message, defaultDestination);
+ final AgentSpan span =
+ startSpan("jms.produce").setTag("span.origin.type", producer.getClass().getName());
+ PRODUCER_DECORATE.afterStart(span);
+ PRODUCER_DECORATE.onProduce(span, message, defaultDestination);
- GlobalTracer.get()
- .inject(
- scope.span().context(), Format.Builtin.TEXT_MAP, new MessagePropertyTextMap(message));
+ propagate().inject(span, message, SETTER);
- return scope;
+ 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) {
if (scope != null) {
PRODUCER_DECORATE.onError(scope, throwable);
@@ -112,7 +110,7 @@ public final class JMSMessageProducerInstrumentation extends Instrumenter.Defaul
public static class ProducerWithDestinationAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
- public static Scope startSpan(
+ public static AgentScope onEnter(
@Advice.Argument(0) final Destination destination,
@Advice.Argument(1) final Message message,
@Advice.This final MessageProducer producer) {
@@ -121,24 +119,19 @@ public final class JMSMessageProducerInstrumentation extends Instrumenter.Defaul
return null;
}
- final Scope scope =
- GlobalTracer.get()
- .buildSpan("jms.produce")
- .withTag("span.origin.type", producer.getClass().getName())
- .startActive(true);
- PRODUCER_DECORATE.afterStart(scope);
- PRODUCER_DECORATE.onProduce(scope, message, destination);
+ final AgentSpan span =
+ startSpan("jms.produce").setTag("span.origin.type", producer.getClass().getName());
+ PRODUCER_DECORATE.afterStart(span);
+ PRODUCER_DECORATE.onProduce(span, message, destination);
- GlobalTracer.get()
- .inject(
- scope.span().context(), Format.Builtin.TEXT_MAP, new MessagePropertyTextMap(message));
+ propagate().inject(span, message, SETTER);
- return scope;
+ 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) {
if (scope != null) {
PRODUCER_DECORATE.onError(scope, throwable);
PRODUCER_DECORATE.beforeFinish(scope);

MODIFY
dd-java-agent/instrumentation/jms/src/main/java/datadog/trace/instrumentation/jms/MessagePropertyTextMap.java
dd-java-agent/instrumentation/jms/src/main/java/datadog/trace/instrumentation/jms/MessagePropertyTextMap.java
package datadog.trace.instrumentation.jms;
-import io.opentracing.propagation.TextMap;
+import datadog.trace.instrumentation.api.AgentPropagation;
+import java.util.ArrayList;
import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
+import java.util.List;
import javax.jms.JMSException;
import javax.jms.Message;
import lombok.extern.slf4j.Slf4j;
@Slf4j
-public class MessagePropertyTextMap implements TextMap {
- static final String DASH = "__dash__";
+public class MessagePropertyTextMap
+ implements AgentPropagation.Getter<Message>, AgentPropagation.Setter<Message> {
- private final Message message;
+ public static final MessagePropertyTextMap GETTER = new MessagePropertyTextMap();
+ public static final MessagePropertyTextMap SETTER = new MessagePropertyTextMap();
- public MessagePropertyTextMap(final Message message) {
- this.message = message;
- }
+ static final String DASH = "__dash__";
@Override
- public Iterator<Map.Entry<String, String>> iterator() {
- final Map<String, String> map = new HashMap<>();
+ public Iterable<String> keys(final Message carrier) {
+ final List<String> keys = new ArrayList<>();
try {
- final Enumeration<?> enumeration = message.getPropertyNames();
+ final Enumeration<?> enumeration = carrier.getPropertyNames();
if (enumeration != null) {
while (enumeration.hasMoreElements()) {
final String key = (String) enumeration.nextElement();
- final Object value = message.getObjectProperty(key);
+ final Object value = carrier.getObjectProperty(key);
if (value instanceof String) {
- map.put(key.replace(DASH, "-"), (String) value);
+ keys.add(key.replace(DASH, "-"));
}
}
}
} catch (final JMSException e) {
throw new RuntimeException(e);
}
- return map.entrySet().iterator();
+ return keys;
+ }
+
+ @Override
+ public String get(final Message carrier, final String key) {
+ final String propName = key.replace("-", DASH);
+ final Object value;
+ try {
+ value = carrier.getObjectProperty(propName);
+ } catch (final JMSException e) {
+ throw new RuntimeException(e);
+ }
+ if (value instanceof String) {
+ return (String) value;
+ } else {
+ return null;
+ }
}
@Override
- public void put(final String key, final String value) {
+ public void set(final Message carrier, final String key, final String value) {
final String propName = key.replace("-", DASH);
try {
- message.setStringProperty(propName, value);
+ carrier.setStringProperty(propName, value);
} catch (final JMSException e) {
if (log.isDebugEnabled()) {
log.debug("Failure setting jms property: " + propName, e);

Update jms to new agent api