Make sure span is opened and closed with scope in Mongo
MODIFY
dd-java-agent/instrumentation/mongo-3.1/src/main/java/datadog/trace/instrumentation/mongo/MongoClientInstrumentation.java
dd-java-agent/instrumentation/mongo-3.1/src/main/java/datadog/trace/instrumentation/mongo/MongoClientInstrumentation.java
@@ -48,7 +48,7 @@ public final class MongoClientInstrumentation extends Instrumenter.Default {
"datadog.trace.agent.decorator.ClientDecorator",
"datadog.trace.agent.decorator.DatabaseClientDecorator",
packageName + ".MongoClientDecorator",
- packageName + ".DDTracingCommandListener"
+ packageName + ".TracingCommandListener"
};
}
@@ -66,7 +66,7 @@ public final class MongoClientInstrumentation extends Instrumenter.Default {
// referencing "this" in the method args causes the class to load under a transformer.
// This bypasses the Builder instrumentation. Casting as a workaround.
final MongoClientOptions.Builder builder = (MongoClientOptions.Builder) dis;
- final DDTracingCommandListener listener = new DDTracingCommandListener();
+ final TracingCommandListener listener = new TracingCommandListener();
builder.addCommandListener(listener);
}
}

RENAME
dd-java-agent/instrumentation/mongo-3.1/src/main/java/datadog/trace/instrumentation/mongo/DDTracingCommandListener.java
dd-java-agent/instrumentation/mongo-3.1/src/main/java/datadog/trace/instrumentation/mongo/TracingCommandListener.java
@@ -6,6 +6,7 @@ import com.mongodb.event.CommandFailedEvent;
import com.mongodb.event.CommandListener;
import com.mongodb.event.CommandStartedEvent;
import com.mongodb.event.CommandSucceededEvent;
+import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.util.GlobalTracer;
import java.util.Map;
@@ -13,13 +14,14 @@ import java.util.concurrent.ConcurrentHashMap;
import lombok.extern.slf4j.Slf4j;
@Slf4j
-public class DDTracingCommandListener implements CommandListener {
+public class TracingCommandListener implements CommandListener {
private final Map<Integer, Span> spanMap = new ConcurrentHashMap<>();
@Override
public void commandStarted(final CommandStartedEvent event) {
final Span span = GlobalTracer.get().buildSpan("mongo.query").start();
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.afterStart(span);
DECORATE.onConnection(span, event);
if (event.getConnectionDescription() != null
@@ -31,6 +33,7 @@ public class DDTracingCommandListener implements CommandListener {
DECORATE.onStatement(span, event.getCommand());
spanMap.put(event.getRequestId(), span);
}
+ }
@Override
public void commandSucceeded(final CommandSucceededEvent event) {

MODIFY
dd-java-agent/instrumentation/mongo-async-3.3/src/main/java/datadog/trace/instrumentation/mongo/MongoAsyncClientInstrumentation.java
dd-java-agent/instrumentation/mongo-async-3.3/src/main/java/datadog/trace/instrumentation/mongo/MongoAsyncClientInstrumentation.java
@@ -48,7 +48,7 @@ public final class MongoAsyncClientInstrumentation extends Instrumenter.Default
"datadog.trace.agent.decorator.ClientDecorator",
"datadog.trace.agent.decorator.DatabaseClientDecorator",
packageName + ".MongoClientDecorator",
- packageName + ".DDTracingCommandListener"
+ packageName + ".TracingCommandListener"
};
}
@@ -66,7 +66,7 @@ public final class MongoAsyncClientInstrumentation extends Instrumenter.Default
// referencing "this" in the method args causes the class to load under a transformer.
// This bypasses the Builder instrumentation. Casting as a workaround.
final MongoClientSettings.Builder builder = (MongoClientSettings.Builder) dis;
- final DDTracingCommandListener listener = new DDTracingCommandListener();
+ final TracingCommandListener listener = new TracingCommandListener();
builder.addCommandListener(listener);
}
}

MODIFY
dd-java-agent/src/test/java/datadog/trace/agent/integration/MongoAsyncClientInstrumentationTest.java
dd-java-agent/src/test/java/datadog/trace/agent/integration/MongoAsyncClientInstrumentationTest.java
@@ -49,7 +49,7 @@ public class MongoAsyncClientInstrumentationTest {
public void asyncClientHasListener() {
Assert.assertEquals(1, client.getSettings().getCommandListeners().size());
Assert.assertEquals(
- "DDTracingCommandListener",
+ "TracingCommandListener",
client.getSettings().getCommandListeners().get(0).getClass().getSimpleName());
}

MODIFY
dd-java-agent/src/test/java/datadog/trace/agent/integration/MongoClientInstrumentationTest.java
dd-java-agent/src/test/java/datadog/trace/agent/integration/MongoClientInstrumentationTest.java
@@ -81,7 +81,7 @@ public class MongoClientInstrumentationTest {
public void syncClientHasListener() {
Assert.assertEquals(1, client.getMongoClientOptions().getCommandListeners().size());
Assert.assertEquals(
- "DDTracingCommandListener",
+ "TracingCommandListener",
client.getMongoClientOptions().getCommandListeners().get(0).getClass().getSimpleName());
}

Make sure span is opened and closed with scope in Mongo