Skip to content

Commit

Permalink
Merge pull request #6405 from DataDog/ban/patch-6358
Browse files Browse the repository at this point in the history
[🍒 6358] MongoDB 4.10-11 instrumentation fix
  • Loading branch information
bantonsson authored Dec 20, 2023
2 parents 75afef4 + eb10b0e commit a6bb01b
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 100 deletions.
3 changes: 1 addition & 2 deletions dd-java-agent/instrumentation/mongo/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
apply from: "$rootDir/gradle/java.gradle"

dependencies {
testImplementation group: 'de.flapdoodle.embed', name: 'de.flapdoodle.embed.mongo', version: '4.5.1'
testImplementation group: 'commons-io', name: 'commons-io', version: '2.11.0'
testImplementation "org.testcontainers:mongodb:${versions.testcontainers}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ addTestSuiteForDir('latestDepTest', 'test')

dependencies {
testImplementation project(':dd-java-agent:instrumentation:mongo').sourceSets.test.output
testImplementation group: 'de.flapdoodle.embed', name: 'de.flapdoodle.embed.mongo', version: '4.5.1'
testImplementation group: 'commons-io', name: 'commons-io', version: '2.11.0'
testImplementation "org.testcontainers:mongodb:${versions.testcontainers}"

// We need to pull in this dependency to get the 'suspend span' instrumentation for spock tests
// as well as to test the instrumentaiton 'layering' (3.4 instrumentation should take precedence
Expand Down
4 changes: 1 addition & 3 deletions dd-java-agent/instrumentation/mongo/driver-3.1/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ dependencies {
}

testImplementation project(':dd-java-agent:instrumentation:mongo').sourceSets.test.output
testImplementation group: 'de.flapdoodle.embed', name: 'de.flapdoodle.embed.mongo', version: '4.5.1'
testImplementation group: 'commons-io', name: 'commons-io', version: '2.11.0'

testImplementation "org.testcontainers:mongodb:${versions.testcontainers}"

testImplementation project(':dd-java-agent:instrumentation:mongo:bson-document')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ dependencies {
}

testImplementation project(':dd-java-agent:instrumentation:mongo').sourceSets.test.output
testImplementation group: 'de.flapdoodle.embed', name: 'de.flapdoodle.embed.mongo', version: '4.5.1'
testImplementation group: 'commons-io', name: 'commons-io', version: '2.11.0'
testImplementation "org.testcontainers:mongodb:${versions.testcontainers}"

testImplementation group: 'org.mongodb', name: 'mongodb-driver-sync', version: '3.10.0'
latestDepTestImplementation group: 'org.mongodb', name: 'mongodb-driver-sync', version: '3.+'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ dependencies {
}

testImplementation project(':dd-java-agent:instrumentation:mongo').sourceSets.test.output
testImplementation group: 'de.flapdoodle.embed', name: 'de.flapdoodle.embed.mongo', version: '4.5.1'
testImplementation group: 'commons-io', name: 'commons-io', version: '2.11.0'
testImplementation "org.testcontainers:mongodb:${versions.testcontainers}"

testImplementation group: 'org.mongodb', name: 'mongodb-driver-async', version: '3.3.0'
latestDepTestImplementation group: 'org.mongodb', name: 'mongodb-driver-async', version: '+'
Expand Down
4 changes: 1 addition & 3 deletions dd-java-agent/instrumentation/mongo/driver-3.4/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ dependencies {
}

testImplementation project(':dd-java-agent:instrumentation:mongo').sourceSets.test.output
testImplementation group: 'de.flapdoodle.embed', name: 'de.flapdoodle.embed.mongo', version: '4.5.1'
testImplementation group: 'commons-io', name: 'commons-io', version: '2.11.0'

testImplementation "org.testcontainers:mongodb:${versions.testcontainers}"

// We need to pull in this dependency to get the 'suspend span' instrumentation for spock tests
// as well as to test the instrumentaiton 'layering' (3.4 instrumentation should take precedence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ addTestSuiteForDir('latestDepTest', 'test')

dependencies {
testImplementation project(':dd-java-agent:instrumentation:mongo').sourceSets.test.output
testImplementation group: 'de.flapdoodle.embed', name: 'de.flapdoodle.embed.mongo', version: '4.5.1'
testImplementation group: 'commons-io', name: 'commons-io', version: '2.11.0'
testImplementation "org.testcontainers:mongodb:${versions.testcontainers}"

// We need to pull in this dependency to get the 'suspend span' instrumentation for spock tests
// as well as to test the instrumentaiton 'layering' (3.4 instrumentation should take precedence
Expand Down
24 changes: 20 additions & 4 deletions dd-java-agent/instrumentation/mongo/driver-4.0/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ muzzle {
apply from: "$rootDir/gradle/java.gradle"

addTestSuiteForDir('latestDepTest', 'test')
addTestSuiteForDir('mongo43Test', 'test')
addTestSuiteForDir('mongo43ForkedTest', 'test')
addTestSuiteForDir('mongo410Test', 'test')
addTestSuiteForDir('mongo410ForkedTest', 'test')

dependencies {
compileOnly group: 'org.mongodb', name: 'mongodb-driver-sync', version: '4.0.0'
Expand All @@ -33,11 +37,23 @@ dependencies {
}

testImplementation project(':dd-java-agent:instrumentation:mongo').sourceSets.test.output
testImplementation group: 'de.flapdoodle.embed', name: 'de.flapdoodle.embed.mongo', version: '4.5.1'
testImplementation group: 'commons-io', name: 'commons-io', version: '2.11.0'
testImplementation "org.testcontainers:mongodb:${versions.testcontainers}"

testImplementation group: 'org.mongodb', name: 'mongodb-driver-sync', version: '4.0.1'
testImplementation group: 'org.mongodb', name: 'mongodb-driver-reactivestreams', version: '4.0.1' // race condition bug in 4.0.0
latestDepTestImplementation group: 'org.mongodb', name: 'mongodb-driver-reactivestreams', version: '4.2+'
latestDepTestImplementation group: 'org.mongodb', name: 'mongodb-driver-sync', version: '4.2+'

mongo43TestImplementation group: 'org.mongodb', name: 'mongodb-driver-reactivestreams', version: '4.3.+'
mongo43TestImplementation group: 'org.mongodb', name: 'mongodb-driver-sync', version: '4.3.+'

mongo43ForkedTestImplementation group: 'org.mongodb', name: 'mongodb-driver-reactivestreams', version: '4.3.+'
mongo43ForkedTestImplementation group: 'org.mongodb', name: 'mongodb-driver-sync', version: '4.3.+'

mongo410TestImplementation group: 'org.mongodb', name: 'mongodb-driver-reactivestreams', version: '4.10.+'
mongo410TestImplementation group: 'org.mongodb', name: 'mongodb-driver-sync', version: '4.10.+'

mongo410ForkedTestImplementation group: 'org.mongodb', name: 'mongodb-driver-reactivestreams', version: '4.10.+'
mongo410ForkedTestImplementation group: 'org.mongodb', name: 'mongodb-driver-sync', version: '4.10.+'

latestDepTestImplementation group: 'org.mongodb', name: 'mongodb-driver-reactivestreams', version: '+'
latestDepTestImplementation group: 'org.mongodb', name: 'mongodb-driver-sync', version: '+'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package datadog.trace.instrumentation.mongo;

import com.mongodb.internal.async.SingleResultCallback;
import net.bytebuddy.asm.Advice;

public class Arg2Advice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void wrap(
@Advice.Argument(value = 2, readOnly = false) SingleResultCallback<Object> callback) {
callback = CallbackWrapper.wrapIfRequired(callback);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package datadog.trace.instrumentation.mongo;

import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;

@AutoService(Instrumenter.class)
public class BaseClusterInstrumentation410 extends Instrumenter.Tracing
implements Instrumenter.ForSingleType {
public BaseClusterInstrumentation410() {
super("mongo", "mongo-reactivestreams");
}

@Override
public String instrumentedType() {
return "com.mongodb.internal.connection.BaseCluster";
}

@Override
public String[] helperClassNames() {
return new String[] {packageName + ".CallbackWrapper"};
}

@Override
public void adviceTransformations(AdviceTransformation transformation) {
transformation.applyAdvice(
isMethod()
.and(named("selectServerAsync"))
.and(takesArgument(2, named("com.mongodb.internal.async.SingleResultCallback"))),
packageName + ".Arg2Advice");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package datadog.trace.instrumentation.mongo;

import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;

@AutoService(Instrumenter.class)
public class DefaultConnectionPoolInstrumentation410 extends Instrumenter.Tracing
implements Instrumenter.ForSingleType {
public DefaultConnectionPoolInstrumentation410() {
super("mongo", "mongo-reactivestreams");
}

@Override
public String instrumentedType() {
return "com.mongodb.internal.connection.DefaultConnectionPool";
}

@Override
public String[] helperClassNames() {
return new String[] {packageName + ".CallbackWrapper"};
}

@Override
public void adviceTransformations(AdviceTransformation transformation) {
transformation.applyAdvice(
isMethod()
.and(named("getAsync"))
.and(takesArgument(1, named("com.mongodb.internal.async.SingleResultCallback"))),
packageName + ".Arg1Advice");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import com.mongodb.client.MongoClient
import com.mongodb.client.MongoClients
import com.mongodb.client.MongoCollection
import com.mongodb.client.MongoDatabase
import com.mongodb.internal.build.MongoDriverVersion
import datadog.trace.core.DDSpan
import org.bson.BsonDocument
import org.bson.BsonString
Expand All @@ -28,6 +29,19 @@ abstract class Mongo4ClientTest extends MongoBaseTest {
client = null
}

@Shared
String query = {
def parts = MongoDriverVersion.VERSION.split('\\.')
switch (parts[1]) {
case '0':
case '1':
case '2':
return ',"query":{}'
default:
return ''
}
}.call()

def "test create collection"() {
setup:
MongoDatabase db = client.getDatabase(databaseName)
Expand Down Expand Up @@ -78,7 +92,7 @@ abstract class Mongo4ClientTest extends MongoBaseTest {
count == 0
assertTraces(1) {
trace(1) {
mongoSpan(it, 0, "count", "{\"count\":\"$collectionName\",\"query\":{}}")
mongoSpan(it, 0, "count", "{\"count\":\"$collectionName\"$query}")
}
}

Expand Down Expand Up @@ -110,7 +124,7 @@ abstract class Mongo4ClientTest extends MongoBaseTest {
mongoSpan(it, 0, "insert", "{\"insert\":\"$collectionName\",\"ordered\":true,\"documents\":[]}")
}
trace(1) {
mongoSpan(it, 0, "count", "{\"count\":\"$collectionName\",\"query\":{}}")
mongoSpan(it, 0, "count", "{\"count\":\"$collectionName\"$query}")
}
}

Expand Down Expand Up @@ -147,7 +161,7 @@ abstract class Mongo4ClientTest extends MongoBaseTest {
mongoSpan(it, 0, "update", "{\"update\":\"$collectionName\",\"ordered\":true,\"updates\":[]}")
}
trace(1) {
mongoSpan(it, 0, "count", "{\"count\":\"$collectionName\",\"query\":{}}")
mongoSpan(it, 0, "count", "{\"count\":\"$collectionName\"$query}")
}
}

Expand Down Expand Up @@ -182,7 +196,7 @@ abstract class Mongo4ClientTest extends MongoBaseTest {
mongoSpan(it, 0, "delete", "{\"delete\":\"$collectionName\",\"ordered\":true,\"deletes\":[]}")
}
trace(1) {
mongoSpan(it, 0, "count", "{\"count\":\"$collectionName\",\"query\":{}}")
mongoSpan(it, 0, "count", "{\"count\":\"$collectionName\"$query}")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import com.mongodb.client.result.DeleteResult
import com.mongodb.client.result.UpdateResult
import com.mongodb.internal.build.MongoDriverVersion
import com.mongodb.reactivestreams.client.MongoClient
import com.mongodb.reactivestreams.client.MongoClients
import com.mongodb.reactivestreams.client.MongoCollection
Expand Down Expand Up @@ -33,6 +34,19 @@ abstract class MongoReactiveClientTest extends MongoBaseTest {
client = null
}

@Shared
String query = {
def parts = MongoDriverVersion.VERSION.split('\\.')
switch (parts[1]) {
case '0':
case '1':
case '2':
return ',"query":{}'
default:
return ''
}
}.call()

MongoCollection<Document> setupCollection(String collectionName) {
DDSpan setupSpan = null
MongoCollection<Document> collection = runUnderTrace("setup") {
Expand Down Expand Up @@ -158,7 +172,7 @@ abstract class MongoReactiveClientTest extends MongoBaseTest {
count.get() == 0
assertTraces(1) {
trace(1) {
mongoSpan(it, 0, "count", "{\"count\":\"$collectionName\",\"query\":{}}")
mongoSpan(it, 0, "count", "{\"count\":\"$collectionName\"$query}")
}
}

Expand All @@ -182,7 +196,7 @@ abstract class MongoReactiveClientTest extends MongoBaseTest {
trace(2) {
sortSpansByStart()
basicSpan(it, 0,"parent")
mongoSpan(it, 1, "count", "{\"count\":\"$collectionName\",\"query\":{}}", false, "some-description", span(0))
mongoSpan(it, 1, "count", "{\"count\":\"$collectionName\"$query}", false, "some-description", span(0))
}
}

Expand All @@ -207,7 +221,7 @@ abstract class MongoReactiveClientTest extends MongoBaseTest {
mongoSpan(it, 0, "insert", "{\"insert\":\"$collectionName\",\"ordered\":true,\"documents\":[]}")
}
trace(1) {
mongoSpan(it, 0, "count", "{\"count\":\"$collectionName\",\"query\":{}}")
mongoSpan(it, 0, "count", "{\"count\":\"$collectionName\"$query}")
}
}

Expand All @@ -234,7 +248,7 @@ abstract class MongoReactiveClientTest extends MongoBaseTest {
sortSpansByStart()
basicSpan(it, 0,"parent")
mongoSpan(it, 1, "insert", "{\"insert\":\"$collectionName\",\"ordered\":true,\"documents\":[]}", false, "some-description", span(0))
mongoSpan(it, 2, "count", "{\"count\":\"$collectionName\",\"query\":{}}", false, "some-description", span(0))
mongoSpan(it, 2, "count", "{\"count\":\"$collectionName\"$query}", false, "some-description", span(0))
}
}

Expand Down Expand Up @@ -265,7 +279,7 @@ abstract class MongoReactiveClientTest extends MongoBaseTest {
mongoSpan(it, 0, "update", "{\"update\":\"$collectionName\",\"ordered\":true,\"updates\":[]}")
}
trace(1) {
mongoSpan(it, 0, "count", "{\"count\":\"$collectionName\",\"query\":{}}")
mongoSpan(it, 0, "count", "{\"count\":\"$collectionName\"$query}")
}
}

Expand Down Expand Up @@ -298,7 +312,7 @@ abstract class MongoReactiveClientTest extends MongoBaseTest {
sortSpansByStart()
basicSpan(it, 0,"parent")
mongoSpan(it, 1, "update", "{\"update\":\"$collectionName\",\"ordered\":true,\"updates\":[]}", false, "some-description", span(0))
mongoSpan(it, 2, "count", "{\"count\":\"$collectionName\",\"query\":{}}", false, "some-description", span(0))
mongoSpan(it, 2, "count", "{\"count\":\"$collectionName\"$query}", false, "some-description", span(0))
}
}

Expand Down Expand Up @@ -327,7 +341,7 @@ abstract class MongoReactiveClientTest extends MongoBaseTest {
mongoSpan(it, 0, "delete", "{\"delete\":\"$collectionName\",\"ordered\":true,\"deletes\":[]}")
}
trace(1) {
mongoSpan(it, 0, "count", "{\"count\":\"$collectionName\",\"query\":{}}")
mongoSpan(it, 0, "count", "{\"count\":\"$collectionName\"$query}")
}
}

Expand Down Expand Up @@ -358,7 +372,7 @@ abstract class MongoReactiveClientTest extends MongoBaseTest {
sortSpansByStart()
basicSpan(it, 0,"parent")
mongoSpan(it, 1, "delete", "{\"delete\":\"$collectionName\",\"ordered\":true,\"deletes\":[]}", false, "some-description", span(0))
mongoSpan(it, 2, "count", "{\"count\":\"$collectionName\",\"query\":{}}", false, "some-description", span(0))
mongoSpan(it, 2, "count", "{\"count\":\"$collectionName\"$query}", false, "some-description", span(0))
}
}

Expand Down
Loading

0 comments on commit a6bb01b

Please sign in to comment.