Replace Mongo tests with updated tests.
MODIFY
dd-java-agent/dd-java-agent.gradle
dd-java-agent/dd-java-agent.gradle
@@ -97,13 +97,6 @@ dependencies {
testCompile deps.opentracingMock
testCompile deps.testLogging
testCompile deps.guava
- testCompile group: 'org.assertj', name: 'assertj-core', version: '2.9.+'
- testCompile group: 'org.mockito', name: 'mockito-core', version: '2.19.0'
-
- testCompile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.4.2'
- testCompile group: 'org.mongodb', name: 'mongodb-driver-async', version: '3.4.2'
- // run embedded mongodb for integration testing
- testCompile group: 'de.flapdoodle.embed', name: 'de.flapdoodle.embed.mongo', version: '1.50.5'
}
tasks.withType(Test).configureEach {

MODIFY
dd-java-agent/instrumentation/mongo-3.1/mongo-3.1.gradle
dd-java-agent/instrumentation/mongo-3.1/mongo-3.1.gradle
@@ -29,8 +29,7 @@ dependencies {
testCompile project(':dd-java-agent:testing')
- testCompile group: 'org.assertj', name: 'assertj-core', version: '2.9.+'
- testCompile group: 'org.mockito', name: 'mockito-core', version: '2.19.0'
+ testCompile group: 'de.flapdoodle.embed', name: 'de.flapdoodle.embed.mongo', version: '2.2.0'
testCompile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.1.0'
latestDepTestCompile group: 'org.mongodb', name: 'mongo-java-driver', version: '+'

DELETE
dd-java-agent/instrumentation/mongo-3.1/src/test/groovy/MongoClientDecoratorTest.groovy
-import datadog.trace.api.DDTags
-import io.opentracing.Span
-import io.opentracing.tag.Tags
-import org.bson.BsonArray
-import org.bson.BsonDocument
-import org.bson.BsonString
-import spock.lang.Shared
-import spock.lang.Specification
-
-import static datadog.trace.instrumentation.mongo.MongoClientDecorator.DECORATE
-
-class MongoClientDecoratorTest extends Specification {
-
- @Shared
- def query1, query2
-
- def setupSpec() {
- query1 = new BsonDocument("find", new BsonString("show"))
- query1.put("stuff", new BsonString("secret"))
-
-
- query2 = new BsonDocument("insert", new BsonString("table"))
- def nestedDoc = new BsonDocument("count", new BsonString("show"))
- nestedDoc.put("id", new BsonString("secret"))
- query2.put("docs", new BsonArray(Arrays.asList(new BsonString("secret"), nestedDoc)))
- }
-
- def "test query scrubbing"() {
- setup:
- def span = Mock(Span)
- // all "secret" strings should be scrubbed out of these queries
-
- when:
- DECORATE.onStatement(span, query)
-
- then:
- 1 * span.setTag(Tags.DB_STATEMENT.key, expected)
- 1 * span.setTag(DDTags.RESOURCE_NAME, expected)
- 0 * _
-
- where:
- query << [query1, query2]
- expected = query.toString().replaceAll("secret", "?")
- }
-}

ADD
dd-java-agent/instrumentation/mongo-3.1/src/test/groovy/MongoClientTest.groovy
+import com.mongodb.MongoClient
+import com.mongodb.MongoClientOptions
+import com.mongodb.MongoTimeoutException
+import com.mongodb.ServerAddress
+import com.mongodb.client.MongoCollection
+import com.mongodb.client.MongoDatabase
+import datadog.opentracing.DDSpan
+import datadog.trace.agent.test.AgentTestRunner
+import datadog.trace.agent.test.asserts.TraceAssert
+import datadog.trace.agent.test.utils.PortUtils
+import datadog.trace.api.DDSpanTypes
+import de.flapdoodle.embed.mongo.MongodExecutable
+import de.flapdoodle.embed.mongo.MongodProcess
+import de.flapdoodle.embed.mongo.MongodStarter
+import de.flapdoodle.embed.mongo.config.IMongodConfig
+import de.flapdoodle.embed.mongo.config.MongodConfigBuilder
+import de.flapdoodle.embed.mongo.config.Net
+import de.flapdoodle.embed.mongo.distribution.Version
+import de.flapdoodle.embed.process.runtime.Network
+import io.opentracing.tag.Tags
+import org.bson.BsonDocument
+import org.bson.BsonString
+import org.bson.Document
+import spock.lang.Shared
+
+import static datadog.trace.agent.test.utils.PortUtils.UNUSABLE_PORT
+import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
+
+class MongoClientTest extends AgentTestRunner {
+
+ @Shared
+ MongoClient client
+ @Shared
+ int port = PortUtils.randomOpenPort()
+ @Shared
+ MongodExecutable mongodExe
+ @Shared
+ MongodProcess mongod
+
+ def setup() throws Exception {
+ final MongodStarter starter = MongodStarter.getDefaultInstance()
+ final IMongodConfig mongodConfig =
+ new MongodConfigBuilder()
+ .version(Version.Main.PRODUCTION)
+ .net(new Net("localhost", port, Network.localhostIsIPv6()))
+ .build()
+
+ mongodExe = starter.prepare(mongodConfig)
+ mongod = mongodExe.start()
+
+ client = new MongoClient("localhost", port)
+ }
+
+ def cleanup() throws Exception {
+ client?.close()
+ client = null
+ mongod?.stop()
+ mongod = null
+ mongodExe?.stop()
+ mongodExe = null
+ }
+
+ def "test create collection"() {
+ setup:
+ MongoDatabase db = client.getDatabase(dbName)
+
+ when:
+ db.createCollection(collectionName)
+
+ then:
+ assertTraces(1) {
+ trace(0, 1) {
+ mongoSpan(it, 0, "{ \"create\" : \"$collectionName\", \"capped\" : \"?\" }")
+ }
+ }
+
+ where:
+ dbName = "test_db"
+ collectionName = "testCollection"
+ }
+
+ def "test get collection"() {
+ setup:
+ MongoDatabase db = client.getDatabase(dbName)
+
+ when:
+ int count = db.getCollection(collectionName).count()
+
+ then:
+ count == 0
+ assertTraces(1) {
+ trace(0, 1) {
+ mongoSpan(it, 0, "{ \"count\" : \"$collectionName\", \"query\" : { } }")
+ }
+ }
+
+ where:
+ dbName = "test_db"
+ collectionName = "testCollection"
+ }
+
+ def "test insert"() {
+ setup:
+ MongoCollection<Document> collection = runUnderTrace("setup") {
+ MongoDatabase db = client.getDatabase(dbName)
+ db.createCollection(collectionName)
+ return db.getCollection(collectionName)
+ }
+ TEST_WRITER.waitForTraces(1)
+ TEST_WRITER.clear()
+
+ when:
+ collection.insertOne(new Document("password", "SECRET"))
+
+ then:
+ collection.count() == 1
+ assertTraces(2) {
+ trace(0, 1) {
+ mongoSpan(it, 0, "{ \"insert\" : \"$collectionName\", \"ordered\" : \"?\", \"documents\" : [{ \"_id\" : \"?\", \"password\" : \"?\" }] }")
+ }
+ trace(1, 1) {
+ mongoSpan(it, 0, "{ \"count\" : \"$collectionName\", \"query\" : { } }")
+ }
+ }
+
+ where:
+ dbName = "test_db"
+ collectionName = "testCollection"
+ }
+
+ def "test update"() {
+ setup:
+ MongoCollection<Document> collection = runUnderTrace("setup") {
+ MongoDatabase db = client.getDatabase(dbName)
+ db.createCollection(collectionName)
+ def coll = db.getCollection(collectionName)
+ coll.insertOne(new Document("password", "OLDPW"))
+ return coll
+ }
+ TEST_WRITER.waitForTraces(1)
+ TEST_WRITER.clear()
+
+ when:
+ def result = collection.updateOne(
+ new BsonDocument("password", new BsonString("OLDPW")),
+ new BsonDocument('$set', new BsonDocument("password", new BsonString("NEWPW"))))
+
+ then:
+ result.modifiedCount == 1
+ collection.count() == 1
+ assertTraces(2) {
+ trace(0, 1) {
+ mongoSpan(it, 0, "{ \"update\" : \"?\", \"ordered\" : \"?\", \"updates\" : [{ \"q\" : { \"password\" : \"?\" }, \"u\" : { \"\$set\" : { \"password\" : \"?\" } } }] }")
+ }
+ trace(1, 1) {
+ mongoSpan(it, 0, "{ \"count\" : \"$collectionName\", \"query\" : { } }")
+ }
+ }
+
+ where:
+ dbName = "test_db"
+ collectionName = "testCollection"
+ }
+
+ def "test delete"() {
+ setup:
+ MongoCollection<Document> collection = runUnderTrace("setup") {
+ MongoDatabase db = client.getDatabase(dbName)
+ db.createCollection(collectionName)
+ def coll = db.getCollection(collectionName)
+ coll.insertOne(new Document("password", "SECRET"))
+ return coll
+ }
+ TEST_WRITER.waitForTraces(1)
+ TEST_WRITER.clear()
+
+ when:
+ def result = collection.deleteOne(new BsonDocument("password", new BsonString("SECRET")))
+
+ then:
+ result.deletedCount == 1
+ collection.count() == 0
+ assertTraces(2) {
+ trace(0, 1) {
+ mongoSpan(it, 0, "{ \"delete\" : \"?\", \"ordered\" : \"?\", \"deletes\" : [{ \"q\" : { \"password\" : \"?\" }, \"limit\" : \"?\" }] }")
+ }
+ trace(1, 1) {
+ mongoSpan(it, 0, "{ \"count\" : \"$collectionName\", \"query\" : { } }")
+ }
+ }
+
+ where:
+ dbName = "test_db"
+ collectionName = "testCollection"
+ }
+
+ def "test error"() {
+ setup:
+ MongoCollection<Document> collection = runUnderTrace("setup") {
+ MongoDatabase db = client.getDatabase(dbName)
+ db.createCollection(collectionName)
+ return db.getCollection(collectionName)
+ }
+ TEST_WRITER.waitForTraces(1)
+ TEST_WRITER.clear()
+
+ when:
+ collection.updateOne(new BsonDocument(), new BsonDocument())
+
+ then:
+ thrown(IllegalArgumentException)
+ // Unfortunately not caught by our instrumentation.
+ assertTraces(0) {}
+
+ where:
+ dbName = "test_db"
+ collectionName = "testCollection"
+ }
+
+ def "test client failure"() {
+ setup:
+ def options = MongoClientOptions.builder().serverSelectionTimeout(10).build()
+ def client = new MongoClient(new ServerAddress("localhost", UNUSABLE_PORT), [], options)
+
+ when:
+ MongoDatabase db = client.getDatabase(dbName)
+ db.createCollection(collectionName)
+
+ then:
+ thrown(MongoTimeoutException)
+ // Unfortunately not caught by our instrumentation.
+ assertTraces(0) {}
+
+ where:
+ dbName = "test_db"
+ collectionName = "testCollection"
+ }
+
+ def mongoSpan(TraceAssert trace, int index, String statement, Object parentSpan = null, Throwable exception = null) {
+ trace.span(index) {
+ serviceName "mongo"
+ operationName "mongo.query"
+ resourceName statement
+ spanType DDSpanTypes.MONGO
+ if (parentSpan == null) {
+ parent()
+ } else {
+ childOf((DDSpan) parentSpan)
+ }
+ tags {
+ "$Tags.COMPONENT.key" "java-mongo"
+ "$Tags.SPAN_KIND.key" Tags.SPAN_KIND_CLIENT
+ "$Tags.DB_INSTANCE.key" "test_db"
+ "$Tags.DB_STATEMENT.key" statement
+ "$Tags.DB_TYPE.key" "mongo"
+ "$Tags.PEER_HOSTNAME.key" "localhost"
+ "$Tags.PEER_HOST_IPV4.key" "127.0.0.1"
+ "$Tags.PEER_PORT.key" port
+ defaultTags()
+ }
+ }
+ }
+}

DELETE
dd-java-agent/instrumentation/mongo-3.1/src/test/java/datadog/trace/instrumentation/mongo/MongoClientInstrumentationTest.java
-package datadog.trace.instrumentation.mongo;
-
-import static org.assertj.core.api.Java6Assertions.assertThat;
-
-import com.mongodb.ServerAddress;
-import com.mongodb.connection.ClusterId;
-import com.mongodb.connection.ConnectionDescription;
-import com.mongodb.connection.ServerId;
-import com.mongodb.event.CommandStartedEvent;
-import datadog.opentracing.DDSpan;
-import datadog.opentracing.DDTracer;
-import datadog.trace.api.DDSpanTypes;
-import io.opentracing.tag.Tags;
-import java.util.Arrays;
-import java.util.List;
-import org.bson.BsonArray;
-import org.bson.BsonDocument;
-import org.bson.BsonString;
-import org.junit.Test;
-
-public class MongoClientInstrumentationTest {
-
- private static ConnectionDescription makeConnection() {
- return new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress()));
- }
-
- @Test
- public void mongoSpan() {
- final CommandStartedEvent cmd =
- new CommandStartedEvent(1, makeConnection(), "databasename", "query", new BsonDocument());
-
- final DDSpan span = new DDTracer().buildSpan("foo").start();
- MongoClientDecorator.DECORATE.afterStart(span);
- MongoClientDecorator.DECORATE.onStatement(span, cmd.getCommand());
-
- assertThat(span.context().getSpanType()).isEqualTo("mongodb");
- assertThat(span.context().getResourceName())
- .isEqualTo(span.context().getTags().get("db.statement"));
- assertThat(span.getSpanType()).isEqualTo(DDSpanTypes.MONGO);
- }
-
- @Test
- public void queryScrubbing() {
- // all "secret" strings should be scrubbed out of these queries
- final BsonDocument query1 = new BsonDocument("find", new BsonString("show"));
- query1.put("stuff", new BsonString("secret"));
- final BsonDocument query2 = new BsonDocument("insert", new BsonString("table"));
- final BsonDocument query2_1 = new BsonDocument("count", new BsonString("show"));
- query2_1.put("id", new BsonString("secret"));
- query2.put("docs", new BsonArray(Arrays.asList(new BsonString("secret"), query2_1)));
- final List<BsonDocument> queries = Arrays.asList(query1, query2);
- for (final BsonDocument query : queries) {
- final CommandStartedEvent cmd =
- new CommandStartedEvent(1, makeConnection(), "databasename", "query", query);
-
- final DDSpan span = new DDTracer().buildSpan("foo").start();
- MongoClientDecorator.DECORATE.afterStart(span);
- MongoClientDecorator.DECORATE.onStatement(span, cmd.getCommand());
-
- assertThat(span.getSpanType()).isEqualTo(DDSpanTypes.MONGO);
- assertThat(span.getTags().get(Tags.DB_STATEMENT.getKey()))
- .isEqualTo(query.toString().replaceAll("secret", "?"));
- }
- }
-}

MODIFY
dd-java-agent/instrumentation/mongo-async-3.3/mongo-async-3.3.gradle
dd-java-agent/instrumentation/mongo-async-3.3/mongo-async-3.3.gradle
@@ -10,6 +10,14 @@ muzzle {
apply from: "${rootDir}/gradle/java.gradle"
+apply plugin: 'org.unbroken-dome.test-sets'
+
+testSets {
+ latestDepTest {
+ dirName = 'test'
+ }
+}
+
dependencies {
// use mongo listener
compile(project(':dd-java-agent:instrumentation:mongo-3.1')) {
@@ -24,4 +32,11 @@ dependencies {
compile deps.opentracing
annotationProcessor deps.autoservice
implementation deps.autoservice
+
+ testCompile project(':dd-java-agent:testing')
+
+ testCompile group: 'de.flapdoodle.embed', name: 'de.flapdoodle.embed.mongo', version: '2.2.0'
+
+ testCompile group: 'org.mongodb', name: 'mongodb-driver-async', version: '3.3.0'
+ latestDepTestCompile group: 'org.mongodb', name: 'mongodb-driver-async', version: '+'
}

ADD
dd-java-agent/instrumentation/mongo-async-3.3/src/test/groovy/MongoAsyncClientTest.groovy
+import com.mongodb.async.SingleResultCallback
+import com.mongodb.async.client.MongoClient
+import com.mongodb.async.client.MongoClients
+import com.mongodb.async.client.MongoCollection
+import com.mongodb.async.client.MongoDatabase
+import com.mongodb.client.result.DeleteResult
+import com.mongodb.client.result.UpdateResult
+import datadog.opentracing.DDSpan
+import datadog.trace.agent.test.AgentTestRunner
+import datadog.trace.agent.test.asserts.TraceAssert
+import datadog.trace.agent.test.utils.PortUtils
+import datadog.trace.api.DDSpanTypes
+import de.flapdoodle.embed.mongo.MongodExecutable
+import de.flapdoodle.embed.mongo.MongodProcess
+import de.flapdoodle.embed.mongo.MongodStarter
+import de.flapdoodle.embed.mongo.config.IMongodConfig
+import de.flapdoodle.embed.mongo.config.MongodConfigBuilder
+import de.flapdoodle.embed.mongo.config.Net
+import de.flapdoodle.embed.mongo.distribution.Version
+import de.flapdoodle.embed.process.runtime.Network
+import io.opentracing.tag.Tags
+import org.bson.BsonDocument
+import org.bson.BsonString
+import org.bson.Document
+import spock.lang.Shared
+import spock.lang.Timeout
+
+import java.util.concurrent.CompletableFuture
+import java.util.concurrent.CountDownLatch
+
+import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
+
+@Timeout(10)
+class MongoAsyncClientTest extends AgentTestRunner {
+
+ @Shared
+ MongoClient client
+ @Shared
+ int port = PortUtils.randomOpenPort()
+ @Shared
+ MongodExecutable mongodExe
+ @Shared
+ MongodProcess mongod
+
+ def setup() throws Exception {
+ final MongodStarter starter = MongodStarter.getDefaultInstance()
+ final IMongodConfig mongodConfig =
+ new MongodConfigBuilder()
+ .version(Version.Main.PRODUCTION)
+ .net(new Net("localhost", port, Network.localhostIsIPv6()))
+ .build()
+
+ mongodExe = starter.prepare(mongodConfig)
+ mongod = mongodExe.start()
+
+ client = MongoClients.create("mongodb://localhost:$port")
+ }
+
+ def cleanup() throws Exception {
+ client?.close()
+ client = null
+ mongod?.stop()
+ mongod = null
+ mongodExe?.stop()
+ mongodExe = null
+ }
+
+ def "test create collection"() {
+ setup:
+ MongoDatabase db = client.getDatabase(dbName)
+
+ when:
+ db.createCollection(collectionName, toCallback {})
+
+ then:
+ assertTraces(1) {
+ trace(0, 1) {
+ mongoSpan(it, 0, "{ \"create\" : \"$collectionName\", \"capped\" : \"?\" }")
+ }
+ }
+
+ where:
+ dbName = "test_db"
+ collectionName = "testCollection"
+ }
+
+ def "test get collection"() {
+ setup:
+ MongoDatabase db = client.getDatabase(dbName)
+
+ when:
+ def count = new CompletableFuture()
+ db.getCollection(collectionName).count toCallback { count.complete(it) }
+
+ then:
+ count.get() == 0
+ assertTraces(1) {
+ trace(0, 1) {
+ mongoSpan(it, 0, "{ \"count\" : \"$collectionName\", \"query\" : { } }")
+ }
+ }
+
+ where:
+ dbName = "test_db"
+ collectionName = "testCollection"
+ }
+
+ def "test insert"() {
+ setup:
+ MongoCollection<Document> collection = runUnderTrace("setup") {
+ MongoDatabase db = client.getDatabase(dbName)
+ def latch1 = new CountDownLatch(1)
+ db.createCollection(collectionName, toCallback { latch1.countDown() })
+ latch1.await()
+ return db.getCollection(collectionName)
+ }
+ TEST_WRITER.waitForTraces(1)
+ TEST_WRITER.clear()
+
+ when:
+ def count = new CompletableFuture()
+ collection.insertOne(new Document("password", "SECRET"), toCallback {
+ collection.count toCallback { count.complete(it) }
+ })
+
+ then:
+ count.get() == 1
+ assertTraces(2) {
+ trace(0, 1) {
+ mongoSpan(it, 0, "{ \"insert\" : \"$collectionName\", \"ordered\" : \"?\", \"documents\" : [{ \"_id\" : \"?\", \"password\" : \"?\" }] }")
+ }
+ trace(1, 1) {
+ mongoSpan(it, 0, "{ \"count\" : \"$collectionName\", \"query\" : { } }")
+ }
+ }
+
+ where:
+ dbName = "test_db"
+ collectionName = "testCollection"
+ }
+
+ def "test update"() {
+ setup:
+ MongoCollection<Document> collection = runUnderTrace("setup") {
+ MongoDatabase db = client.getDatabase(dbName)
+ def latch1 = new CountDownLatch(1)
+ db.createCollection(collectionName, toCallback { latch1.countDown() })
+ latch1.await()
+ def coll = db.getCollection(collectionName)
+ def latch2 = new CountDownLatch(1)
+ coll.insertOne(new Document("password", "OLDPW"), toCallback { latch2.countDown() })
+ latch2.await()
+ return coll
+ }
+ TEST_WRITER.waitForTraces(1)
+ TEST_WRITER.clear()
+
+ when:
+ def result = new CompletableFuture<UpdateResult>()
+ def count = new CompletableFuture()
+ collection.updateOne(
+ new BsonDocument("password", new BsonString("OLDPW")),
+ new BsonDocument('$set', new BsonDocument("password", new BsonString("NEWPW"))), toCallback {
+ result.complete(it)
+ collection.count toCallback { count.complete(it) }
+ })
+
+ then:
+ result.get().modifiedCount == 1
+ count.get() == 1
+ assertTraces(2) {
+ trace(0, 1) {
+ mongoSpan(it, 0, "{ \"update\" : \"?\", \"ordered\" : \"?\", \"updates\" : [{ \"q\" : { \"password\" : \"?\" }, \"u\" : { \"\$set\" : { \"password\" : \"?\" } } }] }")
+ }
+ trace(1, 1) {
+ mongoSpan(it, 0, "{ \"count\" : \"$collectionName\", \"query\" : { } }")
+ }
+ }
+
+ where:
+ dbName = "test_db"
+ collectionName = "testCollection"
+ }
+
+ def "test delete"() {
+ setup:
+ MongoCollection<Document> collection = runUnderTrace("setup") {
+ MongoDatabase db = client.getDatabase(dbName)
+ def latch1 = new CountDownLatch(1)
+ db.createCollection(collectionName, toCallback { latch1.countDown() })
+ latch1.await()
+ def coll = db.getCollection(collectionName)
+ def latch2 = new CountDownLatch(1)
+ coll.insertOne(new Document("password", "SECRET"), toCallback { latch2.countDown() })
+ latch2.await()
+ return coll
+ }
+ TEST_WRITER.waitForTraces(1)
+ TEST_WRITER.clear()
+
+ when:
+ def result = new CompletableFuture<DeleteResult>()
+ def count = new CompletableFuture()
+ collection.deleteOne(new BsonDocument("password", new BsonString("SECRET")), toCallback {
+ result.complete(it)
+ collection.count toCallback { count.complete(it) }
+ })
+
+ then:
+ result.get().deletedCount == 1
+ count.get() == 0
+ assertTraces(2) {
+ trace(0, 1) {
+ mongoSpan(it, 0, "{ \"delete\" : \"?\", \"ordered\" : \"?\", \"deletes\" : [{ \"q\" : { \"password\" : \"?\" }, \"limit\" : \"?\" }] }")
+ }
+ trace(1, 1) {
+ mongoSpan(it, 0, "{ \"count\" : \"$collectionName\", \"query\" : { } }")
+ }
+ }
+
+ where:
+ dbName = "test_db"
+ collectionName = "testCollection"
+ }
+
+ SingleResultCallback toCallback(Closure closure) {
+ return new SingleResultCallback() {
+ @Override
+ void onResult(Object result, Throwable t) {
+ if (t) {
+ closure.call(t)
+ } else {
+ closure.call(result)
+ }
+ }
+ }
+ }
+
+ def mongoSpan(TraceAssert trace, int index, String statement, Object parentSpan = null, Throwable exception = null) {
+ trace.span(index) {
+ serviceName "mongo"
+ operationName "mongo.query"
+ resourceName statement
+ spanType DDSpanTypes.MONGO
+ if (parentSpan == null) {
+ parent()
+ } else {
+ childOf((DDSpan) parentSpan)
+ }
+ tags {
+ "$Tags.COMPONENT.key" "java-mongo"
+ "$Tags.SPAN_KIND.key" Tags.SPAN_KIND_CLIENT
+ "$Tags.DB_INSTANCE.key" "test_db"
+ "$Tags.DB_STATEMENT.key" statement
+ "$Tags.DB_TYPE.key" "mongo"
+ "$Tags.PEER_HOSTNAME.key" "localhost"
+ "$Tags.PEER_HOST_IPV4.key" "127.0.0.1"
+ "$Tags.PEER_PORT.key" port
+ defaultTags()
+ }
+ }
+ }
+}

DELETE
dd-java-agent/src/test/java/datadog/trace/agent/integration/MongoAsyncClientInstrumentationTest.java
-package datadog.trace.agent.integration;
-
-import static datadog.trace.agent.integration.MongoClientInstrumentationTest.MONGO_DB_NAME;
-import static datadog.trace.agent.integration.MongoClientInstrumentationTest.MONGO_HOST;
-import static datadog.trace.agent.integration.MongoClientInstrumentationTest.MONGO_PORT;
-
-import com.mongodb.async.SingleResultCallback;
-import com.mongodb.async.client.MongoClient;
-import com.mongodb.async.client.MongoClients;
-import com.mongodb.async.client.MongoDatabase;
-import datadog.opentracing.DDSpan;
-import datadog.opentracing.DDTracer;
-import datadog.trace.agent.test.IntegrationTestUtils;
-import datadog.trace.common.writer.ListWriter;
-import io.opentracing.tag.Tags;
-import java.util.concurrent.atomic.AtomicBoolean;
-import org.bson.Document;
-import org.junit.AfterClass;
-import org.junit.Assert;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-public class MongoAsyncClientInstrumentationTest {
- private static MongoClient client;
- private static final ListWriter writer = new ListWriter();
- private static final DDTracer tracer = new DDTracer(writer);
-
- @BeforeClass
- public static void setup() throws Exception {
- IntegrationTestUtils.registerOrReplaceGlobalTracer(tracer);
- MongoClientInstrumentationTest.startLocalMongo();
- // Embeded Mongo uses HttpUrlConnection to download things so we have to clear traces before
- // going to tests
- writer.clear();
-
- client = MongoClients.create("mongodb://" + MONGO_HOST + ":" + MONGO_PORT);
- }
-
- @AfterClass
- public static void destroy() throws Exception {
- if (null != client) {
- client.close();
- client = null;
- }
- MongoClientInstrumentationTest.stopLocalMongo();
- }
-
- @Test
- public void asyncClientHasListener() {
- Assert.assertEquals(1, client.getSettings().getCommandListeners().size());
- Assert.assertEquals(
- "TracingCommandListener",
- client.getSettings().getCommandListeners().get(0).getClass().getSimpleName());
- }
-
- @Test
- public void insertOperation() throws Exception {
- final MongoDatabase db = client.getDatabase(MONGO_DB_NAME);
- final String collectionName = "asyncCollection";
- final AtomicBoolean done = new AtomicBoolean(false);
-
- db.createCollection(
- collectionName,
- new SingleResultCallback<Void>() {
- @Override
- public void onResult(final Void result, final Throwable t) {
- done.set(true);
- }
- });
- while (!done.get()) {
- Thread.sleep(1);
- }
-
- db.getCollection(collectionName)
- .insertOne(
- new Document("foo", "bar"),
- new SingleResultCallback<Void>() {
- @Override
- public void onResult(final Void result, final Throwable t) {
- done.set(true);
- }
- });
- while (!done.get()) {
- Thread.sleep(1);
- }
-
- done.set(false);
- db.getCollection(collectionName)
- .count(
- new SingleResultCallback<Long>() {
- @Override
- public void onResult(final Long result, final Throwable t) {
- Assert.assertEquals(1, result.longValue());
- done.set(true);
- }
- });
-
- while (!done.get()) {
- Thread.sleep(1);
- }
-
- // the final trace may still be reporting to the ListWriter,
- // but we're only testing the first trace.
- Assert.assertTrue(writer.size() >= 1);
-
- final String createCollectionQuery =
- "{ \"create\" : \"asyncCollection\", \"autoIndexId\" : \"?\", \"capped\" : \"?\" }";
- final DDSpan trace0 = writer.get(0).get(0);
- Assert.assertEquals("mongo.query", trace0.getOperationName());
- Assert.assertEquals(createCollectionQuery, trace0.getResourceName());
- Assert.assertEquals("mongodb", trace0.getType());
- Assert.assertEquals("mongo", trace0.getServiceName());
-
- Assert.assertEquals("java-mongo", trace0.getTags().get(Tags.COMPONENT.getKey()));
- Assert.assertEquals(createCollectionQuery, trace0.getTags().get(Tags.DB_STATEMENT.getKey()));
- Assert.assertEquals(MONGO_DB_NAME, trace0.getTags().get(Tags.DB_INSTANCE.getKey()));
- Assert.assertEquals(MONGO_HOST, trace0.getTags().get(Tags.PEER_HOSTNAME.getKey()));
- Assert.assertEquals("127.0.0.1", trace0.getTags().get(Tags.PEER_HOST_IPV4.getKey()));
- Assert.assertEquals(MONGO_PORT, trace0.getTags().get(Tags.PEER_PORT.getKey()));
- Assert.assertEquals("mongo", trace0.getTags().get(Tags.DB_TYPE.getKey()));
- }
-}

DELETE
dd-java-agent/src/test/java/datadog/trace/agent/integration/MongoClientInstrumentationTest.java
-package datadog.trace.agent.integration;
-
-import com.mongodb.MongoClient;
-import com.mongodb.client.MongoCollection;
-import com.mongodb.client.MongoDatabase;
-import datadog.opentracing.DDSpan;
-import datadog.opentracing.DDTracer;
-import datadog.trace.agent.test.IntegrationTestUtils;
-import datadog.trace.common.writer.ListWriter;
-import de.flapdoodle.embed.mongo.MongodExecutable;
-import de.flapdoodle.embed.mongo.MongodProcess;
-import de.flapdoodle.embed.mongo.MongodStarter;
-import de.flapdoodle.embed.mongo.config.IMongodConfig;
-import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
-import de.flapdoodle.embed.mongo.config.Net;
-import de.flapdoodle.embed.mongo.distribution.Version;
-import de.flapdoodle.embed.process.runtime.Network;
-import io.opentracing.tag.Tags;
-import java.util.concurrent.TimeoutException;
-import org.bson.Document;
-import org.junit.AfterClass;
-import org.junit.Assert;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-public class MongoClientInstrumentationTest {
- public static final String MONGO_DB_NAME = "embedded";
- public static final String MONGO_HOST = "localhost";
- public static final int MONGO_PORT = 12345;
- private static MongodExecutable mongodExe;
- private static MongodProcess mongod;
-
- private static MongoClient client;
- private static final ListWriter writer = new ListWriter();
- private static final DDTracer tracer = new DDTracer(writer);
-
- public static void startLocalMongo() throws Exception {
- final MongodStarter starter = MongodStarter.getDefaultInstance();
- final IMongodConfig mongodConfig =
- new MongodConfigBuilder()
- .version(Version.Main.PRODUCTION)
- .net(new Net(MONGO_HOST, MONGO_PORT, Network.localhostIsIPv6()))
- .build();
-
- mongodExe = starter.prepare(mongodConfig);
- mongod = mongodExe.start();
- }
-
- public static void stopLocalMongo() throws Exception {
- if (null != mongod) {
- mongod.stop();
- mongod = null;
- }
- if (null != mongodExe) {
- mongodExe.stop();
- mongodExe = null;
- }
- }
-
- @BeforeClass
- public static void setup() throws Exception {
- IntegrationTestUtils.registerOrReplaceGlobalTracer(tracer);
- startLocalMongo();
- // Embeded Mongo uses HttpUrlConnection to download things so we have to clear traces before
- // going to tests
- writer.clear();
-
- client = new MongoClient(MONGO_HOST, MONGO_PORT);
- }
-
- @AfterClass
- public static void destroy() throws Exception {
- if (null != client) {
- client.close();
- client = null;
- }
- stopLocalMongo();
- }
-
- @Test
- public void syncClientHasListener() {
- Assert.assertEquals(1, client.getMongoClientOptions().getCommandListeners().size());
- Assert.assertEquals(
- "TracingCommandListener",
- client.getMongoClientOptions().getCommandListeners().get(0).getClass().getSimpleName());
- }
-
- @Test
- public void insertOperation() throws TimeoutException, InterruptedException {
- final MongoDatabase db = client.getDatabase(MONGO_DB_NAME);
- final String collectionName = "testCollection";
- db.createCollection(collectionName);
- final MongoCollection<Document> collection = db.getCollection(collectionName);
-
- collection.insertOne(new Document("foo", "bar"));
-
- Assert.assertEquals(1, collection.count());
-
- Assert.assertEquals(3, writer.size());
-
- final String createCollectionQuery =
- "{ \"create\" : \"testCollection\", \"autoIndexId\" : \"?\", \"capped\" : \"?\" }";
- final DDSpan trace0 = writer.get(0).get(0);
- Assert.assertEquals("mongo.query", trace0.getOperationName());
- Assert.assertEquals(createCollectionQuery, trace0.getResourceName());
- Assert.assertEquals("mongodb", trace0.getType());
- Assert.assertEquals("mongo", trace0.getServiceName());
-
- Assert.assertEquals("java-mongo", trace0.getTags().get(Tags.COMPONENT.getKey()));
- Assert.assertEquals(createCollectionQuery, trace0.getTags().get(Tags.DB_STATEMENT.getKey()));
- Assert.assertEquals(MONGO_DB_NAME, trace0.getTags().get(Tags.DB_INSTANCE.getKey()));
- Assert.assertEquals(MONGO_HOST, trace0.getTags().get(Tags.PEER_HOSTNAME.getKey()));
- Assert.assertEquals("127.0.0.1", trace0.getTags().get(Tags.PEER_HOST_IPV4.getKey()));
- Assert.assertEquals(MONGO_PORT, trace0.getTags().get(Tags.PEER_PORT.getKey()));
- Assert.assertEquals("mongo", trace0.getTags().get(Tags.DB_TYPE.getKey()));
- }
-}

Replace Mongo tests with updated tests.