capture mongodb database name as mongo-db.name attribute instead of client description (#1419)

This commit is contained in:
Ioannis Mavroukakis 2020-10-19 18:52:50 +01:00 committed by GitHub
parent df2b6638fe
commit 191370657a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 120 deletions

View File

@ -9,9 +9,6 @@ import com.mongodb.MongoTimeoutException
import com.mongodb.ServerAddress import com.mongodb.ServerAddress
import com.mongodb.client.MongoCollection import com.mongodb.client.MongoCollection
import com.mongodb.client.MongoDatabase 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.BsonDocument
import org.bson.BsonString import org.bson.BsonString
import org.bson.Document 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.PortUtils.UNUSABLE_PORT
import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTrace import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTrace
import static io.opentelemetry.trace.Span.Kind.CLIENT
class MongoClientTest extends MongoBaseTest { class MongoClientTest extends MongoBaseTest {
@ -48,7 +44,7 @@ class MongoClientTest extends MongoBaseTest {
then: then:
assertTraces(1) { assertTraces(1) {
trace(0, 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: then:
assertTraces(1) { assertTraces(1) {
trace(0, 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: then:
assertTraces(1) { assertTraces(1) {
trace(0, 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 count == 0
assertTraces(1) { assertTraces(1) {
trace(0, 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 collection.count() == 1
assertTraces(2) { assertTraces(2) {
trace(0, 1) { 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) { 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 collection.count() == 1
assertTraces(2) { assertTraces(2) {
trace(0, 1) { 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) { 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 collection.count() == 0
assertTraces(2) { assertTraces(2) {
trace(0, 1) { 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) { 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" dbName = "test_db"
collectionName = "testCollection" 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
}
}
}
} }

View File

@ -10,9 +10,6 @@ import com.mongodb.client.MongoClient
import com.mongodb.client.MongoClients import com.mongodb.client.MongoClients
import com.mongodb.client.MongoCollection import com.mongodb.client.MongoCollection
import com.mongodb.client.MongoDatabase 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.BsonDocument
import org.bson.BsonString import org.bson.BsonString
import org.bson.Document 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.PortUtils.UNUSABLE_PORT
import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTrace import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTrace
import static io.opentelemetry.trace.Span.Kind.CLIENT
class MongoClientTest extends MongoBaseTest { class MongoClientTest extends MongoBaseTest {
@ -52,7 +48,7 @@ class MongoClientTest extends MongoBaseTest {
then: then:
assertTraces(1) { assertTraces(1) {
trace(0, 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: then:
assertTraces(1) { assertTraces(1) {
trace(0, 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: then:
assertTraces(1) { assertTraces(1) {
trace(0, 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 count == 0
assertTraces(1) { assertTraces(1) {
trace(0, 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 collection.count() == 1
assertTraces(2) { assertTraces(2) {
trace(0, 1) { 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) { 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 collection.count() == 1
assertTraces(2) { assertTraces(2) {
trace(0, 1) { 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) { 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 collection.count() == 0
assertTraces(2) { assertTraces(2) {
trace(0, 1) { 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) { 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" dbName = "test_db"
collectionName = "testCollection" 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
}
}
}
} }

View File

@ -59,7 +59,7 @@ class MongoAsyncClientTest extends MongoBaseTest {
then: then:
assertTraces(1) { assertTraces(1) {
trace(0, 1) { trace(0, 1) {
mongoSpan(it, 0, "create", collectionName) { mongoSpan(it, 0, "create", collectionName, dbName) {
assert it.replaceAll(" ", "") == "{\"create\":\"$collectionName\",\"capped\":\"?\"}" || assert it.replaceAll(" ", "") == "{\"create\":\"$collectionName\",\"capped\":\"?\"}" ||
it == "{\"create\": \"$collectionName\", \"capped\": \"?\", \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}" it == "{\"create\": \"$collectionName\", \"capped\": \"?\", \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}"
true true
@ -87,7 +87,7 @@ class MongoAsyncClientTest extends MongoBaseTest {
then: then:
assertTraces(1) { assertTraces(1) {
trace(0, 1) { trace(0, 1) {
mongoSpan(it, 0, "create", collectionName) { mongoSpan(it, 0, "create", collectionName, dbName) {
assert it.replaceAll(" ", "") == "{\"create\":\"$collectionName\",\"capped\":\"?\"}" || assert it.replaceAll(" ", "") == "{\"create\":\"$collectionName\",\"capped\":\"?\"}" ||
it == "{\"create\": \"$collectionName\", \"capped\": \"?\", \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}" it == "{\"create\": \"$collectionName\", \"capped\": \"?\", \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}"
true true
@ -110,11 +110,11 @@ class MongoAsyncClientTest extends MongoBaseTest {
then: then:
assertTraces(1) { assertTraces(1) {
trace(0, 1) { trace(0, 1) {
mongoSpan(it, 0, "create", collectionName, { mongoSpan(it, 0, "create", collectionName, dbName){
assert it.replaceAll(" ", "") == "{\"create\":\"$collectionName\",\"capped\":\"?\"}" || assert it.replaceAll(" ", "") == "{\"create\":\"$collectionName\",\"capped\":\"?\"}" ||
it == "{\"create\": \"$collectionName\", \"capped\": \"?\", \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}" it == "{\"create\": \"$collectionName\", \"capped\": \"?\", \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}"
true true
}, dbName) }
} }
} }
@ -135,7 +135,7 @@ class MongoAsyncClientTest extends MongoBaseTest {
count.get() == 0 count.get() == 0
assertTraces(1) { assertTraces(1) {
trace(0, 1) { trace(0, 1) {
mongoSpan(it, 0, "count", collectionName) { mongoSpan(it, 0, "count", collectionName, dbName) {
assert it.replaceAll(" ", "") == "{\"count\":\"$collectionName\",\"query\":{}}" || assert it.replaceAll(" ", "") == "{\"count\":\"$collectionName\",\"query\":{}}" ||
it == "{\"count\": \"$collectionName\", \"query\": {}, \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}" it == "{\"count\": \"$collectionName\", \"query\": {}, \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}"
true true
@ -170,14 +170,14 @@ class MongoAsyncClientTest extends MongoBaseTest {
count.get() == 1 count.get() == 1
assertTraces(2) { assertTraces(2) {
trace(0, 1) { trace(0, 1) {
mongoSpan(it, 0, "insert", collectionName) { mongoSpan(it, 0, "insert", collectionName, dbName) {
assert it.replaceAll(" ", "") == "{\"insert\":\"$collectionName\",\"ordered\":\"?\",\"documents\":[{\"_id\":\"?\",\"password\":\"?\"}]}" || assert it.replaceAll(" ", "") == "{\"insert\":\"$collectionName\",\"ordered\":\"?\",\"documents\":[{\"_id\":\"?\",\"password\":\"?\"}]}" ||
it == "{\"insert\": \"$collectionName\", \"ordered\": \"?\", \"\$db\": \"?\", \"documents\": [{\"_id\": \"?\", \"password\": \"?\"}]}" it == "{\"insert\": \"$collectionName\", \"ordered\": \"?\", \"\$db\": \"?\", \"documents\": [{\"_id\": \"?\", \"password\": \"?\"}]}"
true true
} }
} }
trace(1, 1) { trace(1, 1) {
mongoSpan(it, 0, "count", collectionName) { mongoSpan(it, 0, "count", collectionName, dbName) {
assert it.replaceAll(" ", "") == "{\"count\":\"$collectionName\",\"query\":{}}" || assert it.replaceAll(" ", "") == "{\"count\":\"$collectionName\",\"query\":{}}" ||
it == "{\"count\": \"$collectionName\", \"query\": {}, \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}" it == "{\"count\": \"$collectionName\", \"query\": {}, \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}"
true true
@ -221,14 +221,14 @@ class MongoAsyncClientTest extends MongoBaseTest {
count.get() == 1 count.get() == 1
assertTraces(2) { assertTraces(2) {
trace(0, 1) { 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\":\"?\"}}}]}" || assert it.replaceAll(" ", "") == "{\"update\":\"?\",\"ordered\":\"?\",\"updates\":[{\"q\":{\"password\":\"?\"},\"u\":{\"\$set\":{\"password\":\"?\"}}}]}" ||
it == "{\"update\": \"?\", \"ordered\": \"?\", \"\$db\": \"?\", \"updates\": [{\"q\": {\"password\": \"?\"}, \"u\": {\"\$set\": {\"password\": \"?\"}}}]}" it == "{\"update\": \"?\", \"ordered\": \"?\", \"\$db\": \"?\", \"updates\": [{\"q\": {\"password\": \"?\"}, \"u\": {\"\$set\": {\"password\": \"?\"}}}]}"
true true
} }
} }
trace(1, 1) { trace(1, 1) {
mongoSpan(it, 0, "count", collectionName) { mongoSpan(it, 0, "count", collectionName, dbName) {
assert it.replaceAll(" ", "") == "{\"count\":\"$collectionName\",\"query\":{}}" || assert it.replaceAll(" ", "") == "{\"count\":\"$collectionName\",\"query\":{}}" ||
it == "{\"count\": \"$collectionName\", \"query\": {}, \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}" it == "{\"count\": \"$collectionName\", \"query\": {}, \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}"
true true
@ -270,14 +270,14 @@ class MongoAsyncClientTest extends MongoBaseTest {
count.get() == 0 count.get() == 0
assertTraces(2) { assertTraces(2) {
trace(0, 1) { trace(0, 1) {
mongoSpan(it, 0, "delete", collectionName) { mongoSpan(it, 0, "delete", collectionName, dbName) {
assert it.replaceAll(" ", "") == "{\"delete\":\"?\",\"ordered\":\"?\",\"deletes\":[{\"q\":{\"password\":\"?\"},\"limit\":\"?\"}]}" || assert it.replaceAll(" ", "") == "{\"delete\":\"?\",\"ordered\":\"?\",\"deletes\":[{\"q\":{\"password\":\"?\"},\"limit\":\"?\"}]}" ||
it == "{\"delete\": \"?\", \"ordered\": \"?\", \"\$db\": \"?\", \"deletes\": [{\"q\": {\"password\": \"?\"}, \"limit\": \"?\"}]}" it == "{\"delete\": \"?\", \"ordered\": \"?\", \"\$db\": \"?\", \"deletes\": [{\"q\": {\"password\": \"?\"}, \"limit\": \"?\"}]}"
true true
} }
} }
trace(1, 1) { trace(1, 1) {
mongoSpan(it, 0, "count", collectionName) { mongoSpan(it, 0, "count", collectionName, dbName) {
assert it.replaceAll(" ", "") == "{\"count\":\"$collectionName\",\"query\":{}}" || assert it.replaceAll(" ", "") == "{\"count\":\"$collectionName\",\"query\":{}}" ||
it == "{\"count\": \"$collectionName\", \"query\": {}, \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}" it == "{\"count\": \"$collectionName\", \"query\": {}, \"\$db\": \"?\", \"\$readPreference\": {\"mode\": \"?\"}}"
true true
@ -303,7 +303,10 @@ class MongoAsyncClientTest extends MongoBaseTest {
} }
} }
def mongoSpan(TraceAssert trace, int index, String operation, String collection, Closure<Boolean> statementEval, String instance = "some-description", Object parentSpan = null, Throwable exception = null) { def mongoSpan(TraceAssert trace, int index,
String operation, String collection,
String dbName, Closure<Boolean> statementEval,
Object parentSpan = null, Throwable exception = null) {
trace.span(index) { trace.span(index) {
name statementEval name statementEval
kind CLIENT kind CLIENT
@ -313,15 +316,15 @@ class MongoAsyncClientTest extends MongoBaseTest {
childOf((SpanData) parentSpan) childOf((SpanData) parentSpan)
} }
attributes { attributes {
"${SemanticAttributes.NET_PEER_NAME.key()}" "localhost" "$SemanticAttributes.NET_PEER_NAME.key" "localhost"
"${SemanticAttributes.NET_PEER_IP.key()}" "127.0.0.1" "$SemanticAttributes.NET_PEER_IP.key" "127.0.0.1"
"${SemanticAttributes.NET_PEER_PORT.key()}" port "$SemanticAttributes.NET_PEER_PORT.key" port
"${SemanticAttributes.DB_CONNECTION_STRING.key()}" "mongodb://localhost:" + port "$SemanticAttributes.DB_CONNECTION_STRING.key" "mongodb://localhost:" + port
"${SemanticAttributes.DB_STATEMENT.key()}" statementEval "$SemanticAttributes.DB_STATEMENT.key" statementEval
"${SemanticAttributes.DB_SYSTEM.key()}" "mongodb" "$SemanticAttributes.DB_SYSTEM.key" "mongodb"
"${SemanticAttributes.DB_NAME.key()}" instance "$SemanticAttributes.DB_NAME.key" dbName
"${SemanticAttributes.DB_OPERATION.key()}" operation "$SemanticAttributes.DB_OPERATION.key" operation
"${SemanticAttributes.MONGODB_COLLECTION.key()}" collection "$SemanticAttributes.MONGODB_COLLECTION.key" collection
} }
} }
} }

View File

@ -6,10 +6,7 @@
package io.opentelemetry.javaagent.instrumentation.mongo; package io.opentelemetry.javaagent.instrumentation.mongo;
import com.mongodb.ServerAddress; import com.mongodb.ServerAddress;
import com.mongodb.connection.ClusterId;
import com.mongodb.connection.ConnectionDescription; import com.mongodb.connection.ConnectionDescription;
import com.mongodb.connection.ConnectionId;
import com.mongodb.connection.ServerId;
import com.mongodb.event.CommandStartedEvent; import com.mongodb.event.CommandStartedEvent;
import io.opentelemetry.instrumentation.api.tracer.DatabaseClientTracer; import io.opentelemetry.instrumentation.api.tracer.DatabaseClientTracer;
import io.opentelemetry.javaagent.instrumentation.api.jdbc.DbSystem; import io.opentelemetry.javaagent.instrumentation.api.jdbc.DbSystem;
@ -50,24 +47,6 @@ public class MongoClientTracer extends DatabaseClientTracer<CommandStartedEvent,
@Override @Override
protected String dbName(CommandStartedEvent event) { protected String dbName(CommandStartedEvent event) {
// Use description if set.
ConnectionDescription connectionDescription = event.getConnectionDescription();
if (connectionDescription != null) {
ConnectionId connectionId = connectionDescription.getConnectionId();
if (connectionId != null) {
ServerId serverId = connectionId.getServerId();
if (serverId != null) {
ClusterId clusterId = serverId.getClusterId();
if (clusterId != null) {
String description = clusterId.getDescription();
if (description != null) {
return description;
}
}
}
}
}
// Fallback to db name.
return event.getDatabaseName(); return event.getDatabaseName();
} }

View File

@ -12,9 +12,14 @@ import de.flapdoodle.embed.mongo.config.Net
import de.flapdoodle.embed.mongo.distribution.Version import de.flapdoodle.embed.mongo.distribution.Version
import de.flapdoodle.embed.process.runtime.Network import de.flapdoodle.embed.process.runtime.Network
import io.opentelemetry.instrumentation.test.AgentTestRunner import io.opentelemetry.instrumentation.test.AgentTestRunner
import io.opentelemetry.instrumentation.test.asserts.TraceAssert
import io.opentelemetry.instrumentation.test.utils.PortUtils import io.opentelemetry.instrumentation.test.utils.PortUtils
import io.opentelemetry.sdk.trace.data.SpanData
import io.opentelemetry.trace.attributes.SemanticAttributes
import spock.lang.Shared import spock.lang.Shared
import static io.opentelemetry.trace.Span.Kind.CLIENT
/** /**
* Testing needs to be in a centralized project. * Testing needs to be in a centralized project.
* If tests in multiple different projects are using embedded mongo, * If tests in multiple different projects are using embedded mongo,
@ -66,4 +71,32 @@ class MongoBaseTest extends AgentTestRunner {
then: then:
noExceptionThrown() noExceptionThrown()
} }
def mongoSpan(TraceAssert trace, int index,
String operation, String collection,
String dbName, String statement,
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" dbName
"$SemanticAttributes.DB_OPERATION.key" operation
"$SemanticAttributes.MONGODB_COLLECTION.key" collection
}
}
}
} }