diff --git a/instrumentation/mongo/mongo-3.1/src/test/groovy/MongoClientTest.groovy b/instrumentation/mongo/mongo-3.1/src/test/groovy/MongoClientTest.groovy index 6d3b9ea8e4..5d7a27a55d 100644 --- a/instrumentation/mongo/mongo-3.1/src/test/groovy/MongoClientTest.groovy +++ b/instrumentation/mongo/mongo-3.1/src/test/groovy/MongoClientTest.groovy @@ -9,9 +9,6 @@ import com.mongodb.MongoTimeoutException import com.mongodb.ServerAddress import com.mongodb.client.MongoCollection import com.mongodb.client.MongoDatabase -import io.opentelemetry.instrumentation.test.asserts.TraceAssert -import io.opentelemetry.sdk.trace.data.SpanData -import io.opentelemetry.trace.attributes.SemanticAttributes import org.bson.BsonDocument import org.bson.BsonString import org.bson.Document @@ -19,7 +16,6 @@ import spock.lang.Shared import static io.opentelemetry.instrumentation.test.utils.PortUtils.UNUSABLE_PORT import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTrace -import static io.opentelemetry.trace.Span.Kind.CLIENT class MongoClientTest extends MongoBaseTest { @@ -48,7 +44,7 @@ class MongoClientTest extends MongoBaseTest { then: assertTraces(1) { trace(0, 1) { - mongoSpan(it, 0, "create", collectionName, "{\"create\":\"$collectionName\",\"capped\":\"?\"}") + mongoSpan(it, 0, "create", collectionName, dbName, "{\"create\":\"$collectionName\",\"capped\":\"?\"}") } } @@ -72,7 +68,7 @@ class MongoClientTest extends MongoBaseTest { then: assertTraces(1) { trace(0, 1) { - mongoSpan(it, 0, "create", collectionName, "{\"create\":\"$collectionName\",\"capped\":\"?\"}") + mongoSpan(it, 0, "create", collectionName, dbName, "{\"create\":\"$collectionName\",\"capped\":\"?\"}") } } @@ -91,7 +87,7 @@ class MongoClientTest extends MongoBaseTest { then: assertTraces(1) { trace(0, 1) { - mongoSpan(it, 0, "create", collectionName, "{\"create\":\"$collectionName\",\"capped\":\"?\"}", dbName) + mongoSpan(it, 0, "create", collectionName, dbName, "{\"create\":\"$collectionName\",\"capped\":\"?\"}") } } @@ -111,7 +107,7 @@ class MongoClientTest extends MongoBaseTest { count == 0 assertTraces(1) { trace(0, 1) { - mongoSpan(it, 0, "count", collectionName, "{\"count\":\"$collectionName\",\"query\":{}}") + mongoSpan(it, 0, "count", collectionName, dbName, "{\"count\":\"$collectionName\",\"query\":{}}") } } @@ -137,10 +133,10 @@ class MongoClientTest extends MongoBaseTest { collection.count() == 1 assertTraces(2) { trace(0, 1) { - mongoSpan(it, 0, "insert", collectionName, "{\"insert\":\"$collectionName\",\"ordered\":\"?\",\"documents\":[{\"_id\":\"?\",\"password\":\"?\"}]}") + mongoSpan(it, 0, "insert", collectionName, dbName, "{\"insert\":\"$collectionName\",\"ordered\":\"?\",\"documents\":[{\"_id\":\"?\",\"password\":\"?\"}]}") } trace(1, 1) { - mongoSpan(it, 0, "count", collectionName, "{\"count\":\"$collectionName\",\"query\":{}}") + mongoSpan(it, 0, "count", collectionName, dbName, "{\"count\":\"$collectionName\",\"query\":{}}") } } @@ -171,10 +167,10 @@ class MongoClientTest extends MongoBaseTest { collection.count() == 1 assertTraces(2) { trace(0, 1) { - mongoSpan(it, 0, "update", collectionName, "{\"update\":\"?\",\"ordered\":\"?\",\"updates\":[{\"q\":{\"password\":\"?\"},\"u\":{\"\$set\":{\"password\":\"?\"}}}]}") + mongoSpan(it, 0, "update", collectionName, dbName, "{\"update\":\"?\",\"ordered\":\"?\",\"updates\":[{\"q\":{\"password\":\"?\"},\"u\":{\"\$set\":{\"password\":\"?\"}}}]}") } trace(1, 1) { - mongoSpan(it, 0, "count", collectionName, "{\"count\":\"$collectionName\",\"query\":{}}") + mongoSpan(it, 0, "count", collectionName, dbName, "{\"count\":\"$collectionName\",\"query\":{}}") } } @@ -203,10 +199,10 @@ class MongoClientTest extends MongoBaseTest { collection.count() == 0 assertTraces(2) { trace(0, 1) { - mongoSpan(it, 0, "delete", collectionName, "{\"delete\":\"?\",\"ordered\":\"?\",\"deletes\":[{\"q\":{\"password\":\"?\"},\"limit\":\"?\"}]}") + mongoSpan(it, 0, "delete", collectionName, dbName, "{\"delete\":\"?\",\"ordered\":\"?\",\"deletes\":[{\"q\":{\"password\":\"?\"},\"limit\":\"?\"}]}") } trace(1, 1) { - mongoSpan(it, 0, "count", collectionName, "{\"count\":\"$collectionName\",\"query\":{}}") + mongoSpan(it, 0, "count", collectionName, dbName, "{\"count\":\"$collectionName\",\"query\":{}}") } } @@ -256,29 +252,4 @@ class MongoClientTest extends MongoBaseTest { dbName = "test_db" collectionName = "testCollection" } - - def mongoSpan(TraceAssert trace, int index, String operation, String collection, String statement, String instance = "some-description", Object parentSpan = null, Throwable exception = null) { - trace.span(index) { - name { it.replace(" ", "") == statement } - kind CLIENT - if (parentSpan == null) { - hasNoParent() - } else { - childOf((SpanData) parentSpan) - } - attributes { - "${SemanticAttributes.NET_PEER_NAME.key()}" "localhost" - "${SemanticAttributes.NET_PEER_IP.key()}" "127.0.0.1" - "${SemanticAttributes.NET_PEER_PORT.key()}" port - "${SemanticAttributes.DB_STATEMENT.key()}" { - it.replace(" ", "") == statement - } - "${SemanticAttributes.DB_CONNECTION_STRING.key()}" "mongodb://localhost:" + port - "${SemanticAttributes.DB_SYSTEM.key()}" "mongodb" - "${SemanticAttributes.DB_NAME.key()}" instance - "${SemanticAttributes.DB_OPERATION.key()}" operation - "${SemanticAttributes.MONGODB_COLLECTION.key()}" collection - } - } - } } diff --git a/instrumentation/mongo/mongo-3.7/src/test/groovy/MongoClientTest.groovy b/instrumentation/mongo/mongo-3.7/src/test/groovy/MongoClientTest.groovy index e1f6db8496..2a79c6d929 100644 --- a/instrumentation/mongo/mongo-3.7/src/test/groovy/MongoClientTest.groovy +++ b/instrumentation/mongo/mongo-3.7/src/test/groovy/MongoClientTest.groovy @@ -10,9 +10,6 @@ import com.mongodb.client.MongoClient import com.mongodb.client.MongoClients import com.mongodb.client.MongoCollection import com.mongodb.client.MongoDatabase -import io.opentelemetry.instrumentation.test.asserts.TraceAssert -import io.opentelemetry.sdk.trace.data.SpanData -import io.opentelemetry.trace.attributes.SemanticAttributes import org.bson.BsonDocument import org.bson.BsonString import org.bson.Document @@ -20,7 +17,6 @@ import spock.lang.Shared import static io.opentelemetry.instrumentation.test.utils.PortUtils.UNUSABLE_PORT import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTrace -import static io.opentelemetry.trace.Span.Kind.CLIENT class MongoClientTest extends MongoBaseTest { @@ -52,7 +48,7 @@ class MongoClientTest extends MongoBaseTest { then: assertTraces(1) { trace(0, 1) { - mongoSpan(it, 0, "create", collectionName, "{\"create\":\"$collectionName\",\"capped\":\"?\"}") + mongoSpan(it, 0, "create", collectionName, dbName, "{\"create\":\"$collectionName\",\"capped\":\"?\"}") } } @@ -82,7 +78,7 @@ class MongoClientTest extends MongoBaseTest { then: assertTraces(1) { trace(0, 1) { - mongoSpan(it, 0, "create", collectionName, "{\"create\":\"$collectionName\",\"capped\":\"?\"}") + mongoSpan(it, 0, "create", collectionName, dbName, "{\"create\":\"$collectionName\",\"capped\":\"?\"}") } } @@ -101,7 +97,7 @@ class MongoClientTest extends MongoBaseTest { then: assertTraces(1) { trace(0, 1) { - mongoSpan(it, 0, "create", collectionName, "{\"create\":\"$collectionName\",\"capped\":\"?\"}", dbName) + mongoSpan(it, 0, "create", collectionName, dbName, "{\"create\":\"$collectionName\",\"capped\":\"?\"}") } } @@ -121,7 +117,7 @@ class MongoClientTest extends MongoBaseTest { count == 0 assertTraces(1) { trace(0, 1) { - mongoSpan(it, 0, "count", collectionName, "{\"count\":\"$collectionName\",\"query\":{}}") + mongoSpan(it, 0, "count", collectionName, dbName, "{\"count\":\"$collectionName\",\"query\":{}}") } } @@ -147,10 +143,10 @@ class MongoClientTest extends MongoBaseTest { collection.count() == 1 assertTraces(2) { trace(0, 1) { - mongoSpan(it, 0, "insert", collectionName, "{\"insert\":\"$collectionName\",\"ordered\":\"?\",\"documents\":[{\"_id\":\"?\",\"password\":\"?\"}]}") + mongoSpan(it, 0, "insert", collectionName, dbName, "{\"insert\":\"$collectionName\",\"ordered\":\"?\",\"documents\":[{\"_id\":\"?\",\"password\":\"?\"}]}") } trace(1, 1) { - mongoSpan(it, 0, "count", collectionName, "{\"count\":\"$collectionName\",\"query\":{}}") + mongoSpan(it, 0, "count", collectionName, dbName, "{\"count\":\"$collectionName\",\"query\":{}}") } } @@ -181,10 +177,10 @@ class MongoClientTest extends MongoBaseTest { collection.count() == 1 assertTraces(2) { trace(0, 1) { - mongoSpan(it, 0, "update", collectionName, "{\"update\":\"?\",\"ordered\":\"?\",\"updates\":[{\"q\":{\"password\":\"?\"},\"u\":{\"\$set\":{\"password\":\"?\"}}}]}") + mongoSpan(it, 0, "update", collectionName, dbName, "{\"update\":\"?\",\"ordered\":\"?\",\"updates\":[{\"q\":{\"password\":\"?\"},\"u\":{\"\$set\":{\"password\":\"?\"}}}]}") } trace(1, 1) { - mongoSpan(it, 0, "count", collectionName, "{\"count\":\"$collectionName\",\"query\":{}}") + mongoSpan(it, 0, "count", collectionName, dbName, "{\"count\":\"$collectionName\",\"query\":{}}") } } @@ -213,10 +209,10 @@ class MongoClientTest extends MongoBaseTest { collection.count() == 0 assertTraces(2) { trace(0, 1) { - mongoSpan(it, 0, "delete", collectionName, "{\"delete\":\"?\",\"ordered\":\"?\",\"deletes\":[{\"q\":{\"password\":\"?\"},\"limit\":\"?\"}]}") + mongoSpan(it, 0, "delete", collectionName, dbName, "{\"delete\":\"?\",\"ordered\":\"?\",\"deletes\":[{\"q\":{\"password\":\"?\"},\"limit\":\"?\"}]}") } trace(1, 1) { - mongoSpan(it, 0, "count", collectionName, "{\"count\":\"$collectionName\",\"query\":{}}") + mongoSpan(it, 0, "count", collectionName, dbName, "{\"count\":\"$collectionName\",\"query\":{}}") } } @@ -265,29 +261,4 @@ class MongoClientTest extends MongoBaseTest { dbName = "test_db" collectionName = "testCollection" } - - def mongoSpan(TraceAssert trace, int index, String operation, String collection, String statement, String instance = "some-description", Object parentSpan = null, Throwable exception = null) { - trace.span(index) { - name { it.replace(" ", "") == statement } - kind CLIENT - if (parentSpan == null) { - hasNoParent() - } else { - childOf((SpanData) parentSpan) - } - attributes { - "${SemanticAttributes.NET_PEER_NAME.key()}" "localhost" - "${SemanticAttributes.NET_PEER_IP.key()}" "127.0.0.1" - "${SemanticAttributes.NET_PEER_PORT.key()}" port - "${SemanticAttributes.DB_STATEMENT.key()}" { - it.replace(" ", "") == statement - } - "${SemanticAttributes.DB_SYSTEM.key()}" "mongodb" - "${SemanticAttributes.DB_CONNECTION_STRING.key()}" "mongodb://localhost:" + port - "${SemanticAttributes.DB_NAME.key()}" instance - "${SemanticAttributes.DB_OPERATION.key()}" operation - "${SemanticAttributes.MONGODB_COLLECTION.key()}" collection - } - } - } } diff --git a/instrumentation/mongo/mongo-async-3.3/src/test/groovy/MongoAsyncClientTest.groovy b/instrumentation/mongo/mongo-async-3.3/src/test/groovy/MongoAsyncClientTest.groovy index 330ede88d7..2a93a20907 100644 --- a/instrumentation/mongo/mongo-async-3.3/src/test/groovy/MongoAsyncClientTest.groovy +++ b/instrumentation/mongo/mongo-async-3.3/src/test/groovy/MongoAsyncClientTest.groovy @@ -59,7 +59,7 @@ class MongoAsyncClientTest extends MongoBaseTest { then: assertTraces(1) { trace(0, 1) { - mongoSpan(it, 0, "create", collectionName) { + mongoSpan(it, 0, "create", collectionName, dbName) { assert it.replaceAll(" ", "") == "{\"create\":\"$collectionName\",\"capped\":\"?\"}" || it == "{\"create\": \"$collectionName\", \"capped\": \"?\", \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}" true @@ -87,7 +87,7 @@ class MongoAsyncClientTest extends MongoBaseTest { then: assertTraces(1) { trace(0, 1) { - mongoSpan(it, 0, "create", collectionName) { + mongoSpan(it, 0, "create", collectionName, dbName) { assert it.replaceAll(" ", "") == "{\"create\":\"$collectionName\",\"capped\":\"?\"}" || it == "{\"create\": \"$collectionName\", \"capped\": \"?\", \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}" true @@ -110,11 +110,11 @@ class MongoAsyncClientTest extends MongoBaseTest { then: assertTraces(1) { trace(0, 1) { - mongoSpan(it, 0, "create", collectionName, { + mongoSpan(it, 0, "create", collectionName, dbName){ assert it.replaceAll(" ", "") == "{\"create\":\"$collectionName\",\"capped\":\"?\"}" || it == "{\"create\": \"$collectionName\", \"capped\": \"?\", \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}" true - }, dbName) + } } } @@ -135,7 +135,7 @@ class MongoAsyncClientTest extends MongoBaseTest { count.get() == 0 assertTraces(1) { trace(0, 1) { - mongoSpan(it, 0, "count", collectionName) { + mongoSpan(it, 0, "count", collectionName, dbName) { assert it.replaceAll(" ", "") == "{\"count\":\"$collectionName\",\"query\":{}}" || it == "{\"count\": \"$collectionName\", \"query\": {}, \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}" true @@ -170,14 +170,14 @@ class MongoAsyncClientTest extends MongoBaseTest { count.get() == 1 assertTraces(2) { trace(0, 1) { - mongoSpan(it, 0, "insert", collectionName) { + mongoSpan(it, 0, "insert", collectionName, dbName) { assert it.replaceAll(" ", "") == "{\"insert\":\"$collectionName\",\"ordered\":\"?\",\"documents\":[{\"_id\":\"?\",\"password\":\"?\"}]}" || it == "{\"insert\": \"$collectionName\", \"ordered\": \"?\", \"\$db\": \"?\", \"documents\": [{\"_id\": \"?\", \"password\": \"?\"}]}" true } } trace(1, 1) { - mongoSpan(it, 0, "count", collectionName) { + mongoSpan(it, 0, "count", collectionName, dbName) { assert it.replaceAll(" ", "") == "{\"count\":\"$collectionName\",\"query\":{}}" || it == "{\"count\": \"$collectionName\", \"query\": {}, \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}" true @@ -221,14 +221,14 @@ class MongoAsyncClientTest extends MongoBaseTest { count.get() == 1 assertTraces(2) { trace(0, 1) { - mongoSpan(it, 0, "update", collectionName) { + mongoSpan(it, 0, "update", collectionName, dbName) { assert it.replaceAll(" ", "") == "{\"update\":\"?\",\"ordered\":\"?\",\"updates\":[{\"q\":{\"password\":\"?\"},\"u\":{\"\$set\":{\"password\":\"?\"}}}]}" || it == "{\"update\": \"?\", \"ordered\": \"?\", \"\$db\": \"?\", \"updates\": [{\"q\": {\"password\": \"?\"}, \"u\": {\"\$set\": {\"password\": \"?\"}}}]}" true } } trace(1, 1) { - mongoSpan(it, 0, "count", collectionName) { + mongoSpan(it, 0, "count", collectionName, dbName) { assert it.replaceAll(" ", "") == "{\"count\":\"$collectionName\",\"query\":{}}" || it == "{\"count\": \"$collectionName\", \"query\": {}, \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}" true @@ -270,14 +270,14 @@ class MongoAsyncClientTest extends MongoBaseTest { count.get() == 0 assertTraces(2) { trace(0, 1) { - mongoSpan(it, 0, "delete", collectionName) { + mongoSpan(it, 0, "delete", collectionName, dbName) { assert it.replaceAll(" ", "") == "{\"delete\":\"?\",\"ordered\":\"?\",\"deletes\":[{\"q\":{\"password\":\"?\"},\"limit\":\"?\"}]}" || it == "{\"delete\": \"?\", \"ordered\": \"?\", \"\$db\": \"?\", \"deletes\": [{\"q\": {\"password\": \"?\"}, \"limit\": \"?\"}]}" true } } trace(1, 1) { - mongoSpan(it, 0, "count", collectionName) { + mongoSpan(it, 0, "count", collectionName, dbName) { assert it.replaceAll(" ", "") == "{\"count\":\"$collectionName\",\"query\":{}}" || it == "{\"count\": \"$collectionName\", \"query\": {}, \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}" true @@ -303,7 +303,10 @@ class MongoAsyncClientTest extends MongoBaseTest { } } - def mongoSpan(TraceAssert trace, int index, String operation, String collection, Closure statementEval, String instance = "some-description", Object parentSpan = null, Throwable exception = null) { + def mongoSpan(TraceAssert trace, int index, + String operation, String collection, + String dbName, Closure statementEval, + Object parentSpan = null, Throwable exception = null) { trace.span(index) { name statementEval kind CLIENT @@ -313,15 +316,15 @@ class MongoAsyncClientTest extends MongoBaseTest { childOf((SpanData) parentSpan) } attributes { - "${SemanticAttributes.NET_PEER_NAME.key()}" "localhost" - "${SemanticAttributes.NET_PEER_IP.key()}" "127.0.0.1" - "${SemanticAttributes.NET_PEER_PORT.key()}" port - "${SemanticAttributes.DB_CONNECTION_STRING.key()}" "mongodb://localhost:" + port - "${SemanticAttributes.DB_STATEMENT.key()}" statementEval - "${SemanticAttributes.DB_SYSTEM.key()}" "mongodb" - "${SemanticAttributes.DB_NAME.key()}" instance - "${SemanticAttributes.DB_OPERATION.key()}" operation - "${SemanticAttributes.MONGODB_COLLECTION.key()}" collection + "$SemanticAttributes.NET_PEER_NAME.key" "localhost" + "$SemanticAttributes.NET_PEER_IP.key" "127.0.0.1" + "$SemanticAttributes.NET_PEER_PORT.key" port + "$SemanticAttributes.DB_CONNECTION_STRING.key" "mongodb://localhost:" + port + "$SemanticAttributes.DB_STATEMENT.key" statementEval + "$SemanticAttributes.DB_SYSTEM.key" "mongodb" + "$SemanticAttributes.DB_NAME.key" dbName + "$SemanticAttributes.DB_OPERATION.key" operation + "$SemanticAttributes.MONGODB_COLLECTION.key" collection } } } diff --git a/instrumentation/mongo/mongo-common/src/main/java/io/opentelemetry/javaagent/instrumentation/mongo/MongoClientTracer.java b/instrumentation/mongo/mongo-common/src/main/java/io/opentelemetry/javaagent/instrumentation/mongo/MongoClientTracer.java index 009f4ed347..eb2167d766 100644 --- a/instrumentation/mongo/mongo-common/src/main/java/io/opentelemetry/javaagent/instrumentation/mongo/MongoClientTracer.java +++ b/instrumentation/mongo/mongo-common/src/main/java/io/opentelemetry/javaagent/instrumentation/mongo/MongoClientTracer.java @@ -6,10 +6,7 @@ package io.opentelemetry.javaagent.instrumentation.mongo; import com.mongodb.ServerAddress; -import com.mongodb.connection.ClusterId; import com.mongodb.connection.ConnectionDescription; -import com.mongodb.connection.ConnectionId; -import com.mongodb.connection.ServerId; import com.mongodb.event.CommandStartedEvent; import io.opentelemetry.instrumentation.api.tracer.DatabaseClientTracer; import io.opentelemetry.javaagent.instrumentation.api.jdbc.DbSystem; @@ -50,24 +47,6 @@ public class MongoClientTracer extends DatabaseClientTracer