Better span names for mongo
MODIFY
instrumentation/mongo/mongo-3.1/src/test/groovy/MongoClientTest.groovy
instrumentation/mongo/mongo-3.1/src/test/groovy/MongoClientTest.groovy
@@ -252,7 +252,7 @@ class MongoClientTest extends MongoBaseTest {
def mongoSpan(TraceAssert trace, int index, String statement, boolean renameService = false, String instance = "some-description", Object parentSpan = null, Throwable exception = null) {
trace.span(index) {
- operationName "mongo.query"
+ operationName { it.replace(" ", "") == statement }
spanKind CLIENT
if (parentSpan == null) {
parent()
@@ -261,7 +261,6 @@ class MongoClientTest extends MongoBaseTest {
}
tags {
"$MoreTags.SERVICE_NAME" renameService ? instance : "mongo"
- "$MoreTags.RESOURCE_NAME" { it.replace(" ", "") == statement }
"$MoreTags.SPAN_TYPE" SpanTypes.MONGO
"$Tags.COMPONENT" "java-mongo"
"$MoreTags.NET_PEER_NAME" "localhost"

MODIFY
instrumentation/mongo/mongo-3.7/src/test/groovy/MongoClientTest.groovy
instrumentation/mongo/mongo-3.7/src/test/groovy/MongoClientTest.groovy
@@ -255,7 +255,7 @@ class MongoClientTest extends MongoBaseTest {
def mongoSpan(TraceAssert trace, int index, String statement, boolean renameService = false, String instance = "some-description", Object parentSpan = null, Throwable exception = null) {
trace.span(index) {
- operationName "mongo.query"
+ operationName { it.replace(" ", "") == statement }
spanKind CLIENT
if (parentSpan == null) {
parent()
@@ -264,7 +264,6 @@ class MongoClientTest extends MongoBaseTest {
}
tags {
"$MoreTags.SERVICE_NAME" renameService ? instance : "mongo"
- "$MoreTags.RESOURCE_NAME" { it.replace(" ", "") == statement }
"$MoreTags.SPAN_TYPE" SpanTypes.MONGO
"$Tags.COMPONENT" "java-mongo"
"$MoreTags.NET_PEER_NAME" "localhost"

MODIFY
instrumentation/mongo/mongo-async-3.3/src/test/groovy/MongoAsyncClientTest.groovy
instrumentation/mongo/mongo-async-3.3/src/test/groovy/MongoAsyncClientTest.groovy
@@ -290,7 +290,7 @@ class MongoAsyncClientTest extends MongoBaseTest {
def mongoSpan(TraceAssert trace, int index, Closure<Boolean> statementEval, String instance = "some-description", Object parentSpan = null, Throwable exception = null) {
trace.span(index) {
- operationName "mongo.query"
+ operationName statementEval
spanKind CLIENT
if (parentSpan == null) {
parent()
@@ -299,7 +299,6 @@ class MongoAsyncClientTest extends MongoBaseTest {
}
tags {
"$MoreTags.SERVICE_NAME" "mongo"
- "$MoreTags.RESOURCE_NAME" statementEval
"$MoreTags.SPAN_TYPE" SpanTypes.MONGO
"$Tags.COMPONENT" "java-mongo"
"$MoreTags.NET_PEER_NAME" "localhost"

MODIFY
instrumentation/mongo/mongo-common/src/main/java/io/opentelemetry/auto/instrumentation/mongo/MongoClientDecorator.java
instrumentation/mongo/mongo-common/src/main/java/io/opentelemetry/auto/instrumentation/mongo/MongoClientDecorator.java
@@ -22,9 +22,7 @@ import com.mongodb.connection.ServerId;
import com.mongodb.event.CommandStartedEvent;
import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.auto.bootstrap.instrumentation.decorator.DatabaseClientDecorator;
-import io.opentelemetry.auto.instrumentation.api.MoreTags;
import io.opentelemetry.auto.instrumentation.api.SpanTypes;
-import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.Tracer;
import java.util.Arrays;
import java.util.List;
@@ -89,14 +87,10 @@ public class MongoClientDecorator extends DatabaseClientDecorator<CommandStarted
return event.getDatabaseName();
}
- public Span onStatement(final Span span, final BsonDocument statement) {
-
+ public String statement(final BsonDocument statement) {
// scrub the Mongo command so that parameters are removed from the string
final BsonDocument scrubbed = scrub(statement);
- final String mongoCmd = scrubbed.toString();
-
- span.setAttribute(MoreTags.RESOURCE_NAME, mongoCmd);
- return onStatement(span, mongoCmd);
+ return scrubbed.toString();
}
/**

MODIFY
instrumentation/mongo/mongo-common/src/main/java/io/opentelemetry/auto/instrumentation/mongo/TracingCommandListener.java
instrumentation/mongo/mongo-common/src/main/java/io/opentelemetry/auto/instrumentation/mongo/TracingCommandListener.java
@@ -36,7 +36,8 @@ public class TracingCommandListener implements CommandListener {
@Override
public void commandStarted(final CommandStartedEvent event) {
- final Span span = TRACER.spanBuilder("mongo.query").setSpanKind(CLIENT).startSpan();
+ final String statement = DECORATE.statement(event.getCommand());
+ final Span span = TRACER.spanBuilder(statement).setSpanKind(CLIENT).startSpan();
try (final Scope scope = TRACER.withSpan(span)) {
DECORATE.afterStart(span);
DECORATE.onConnection(span, event);
@@ -46,7 +47,7 @@ public class TracingCommandListener implements CommandListener {
DECORATE.onPeerConnection(
span, event.getConnectionDescription().getServerAddress().getSocketAddress());
}
- DECORATE.onStatement(span, event.getCommand());
+ DECORATE.onStatement(span, statement);
spanMap.put(event.getRequestId(), span);
}
}

MODIFY
testing/src/main/groovy/io/opentelemetry/auto/test/asserts/SpanAssert.groovy
testing/src/main/groovy/io/opentelemetry/auto/test/asserts/SpanAssert.groovy
@@ -71,6 +71,11 @@ class SpanAssert {
checked.name = true
}
+ def operationName(Closure spec) {
+ assert ((Closure) spec).call(span.name)
+ checked.name = true
+ }
+
def operationNameContains(String... operationNameParts) {
assertSpanNameContains(span.name, operationNameParts)
checked.name = true

Better span names for mongo (#242)