-
Notifications
You must be signed in to change notification settings - Fork 318
Mongo Bytebuddy and Mongo Async #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,3 +51,4 @@ Thumbs.db | |
| */out | ||
| dd-java-agent/integrations/*/out | ||
| dd-trace-examples/*/out | ||
| derby.log | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...ts/src/test/java/com/datadoghq/agent/integration/MongoAsyncClientInstrumentationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package com.datadoghq.agent.integration; | ||
|
|
||
| import static com.datadoghq.agent.integration.MongoClientInstrumentationTest.MONGO_DB_NAME; | ||
| import static com.datadoghq.agent.integration.MongoClientInstrumentationTest.MONGO_HOST; | ||
| import static com.datadoghq.agent.integration.MongoClientInstrumentationTest.MONGO_PORT; | ||
|
|
||
| import com.datadoghq.trace.DDBaseSpan; | ||
| import com.datadoghq.trace.DDTracer; | ||
| import com.datadoghq.trace.writer.ListWriter; | ||
| import com.mongodb.async.SingleResultCallback; | ||
| import com.mongodb.async.client.MongoClient; | ||
| import com.mongodb.async.client.MongoClients; | ||
| import com.mongodb.async.client.MongoDatabase; | ||
| import io.opentracing.tag.Tags; | ||
| import java.net.InetAddress; | ||
| import java.nio.ByteBuffer; | ||
| 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 { | ||
| TestUtils.registerOrReplaceGlobalTracer(tracer); | ||
| MongoClientInstrumentationTest.startLocalMongo(); | ||
| 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( | ||
| "DDTracingCommandListener", | ||
| client.getSettings().getCommandListeners().get(0).getClass().getSimpleName()); | ||
| } | ||
|
|
||
| @Test | ||
| public void insertOperation() throws InterruptedException, Exception { | ||
| 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(Void result, 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(Long result, 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.getList().size() >= 1); | ||
|
|
||
| final String createCollectionQuery = | ||
| "{ \"create\" : \"asyncCollection\", \"autoIndexId\" : \"?\", \"capped\" : \"?\" }"; | ||
| final DDBaseSpan<?> 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( | ||
| ByteBuffer.wrap(InetAddress.getByName("127.0.0.1").getAddress()).getInt(), | ||
| 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())); | ||
| } | ||
| } |
122 changes: 106 additions & 16 deletions
122
...ittests/src/test/java/com/datadoghq/agent/integration/MongoClientInstrumentationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,116 @@ | ||
| package com.datadoghq.agent.integration; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import com.datadoghq.trace.DDBaseSpan; | ||
| import com.datadoghq.trace.DDTracer; | ||
| import com.datadoghq.trace.writer.ListWriter; | ||
| import com.mongodb.MongoClient; | ||
| import com.mongodb.client.MongoCollection; | ||
| import com.mongodb.client.MongoDatabase; | ||
| import de.flapdoodle.embed.mongo.*; | ||
| 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.net.InetAddress; | ||
| import java.net.UnknownHostException; | ||
| import java.nio.ByteBuffer; | ||
| 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 { | ||
| MongodStarter starter = MongodStarter.getDefaultInstance(); | ||
|
|
||
| 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 { | ||
| TestUtils.registerOrReplaceGlobalTracer(tracer); | ||
| startLocalMongo(); | ||
|
|
||
| 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( | ||
| "DDTracingCommandListener", | ||
| client.getMongoClientOptions().getCommandListeners().get(0).getClass().getSimpleName()); | ||
| } | ||
|
|
||
| @Test | ||
| public void test() { | ||
| final MongoClient mongoClient = new MongoClient(); | ||
|
|
||
| assertThat(mongoClient.getMongoClientOptions().getCommandListeners().size()).isEqualTo(1); | ||
| assertThat( | ||
| mongoClient | ||
| .getMongoClientOptions() | ||
| .getCommandListeners() | ||
| .get(0) | ||
| .getClass() | ||
| .getSimpleName()) | ||
| .isEqualTo("DDTracingCommandListener"); | ||
|
|
||
| mongoClient.close(); | ||
| public void insertOperation() throws UnknownHostException { | ||
| MongoDatabase db = client.getDatabase(MONGO_DB_NAME); | ||
| final String collectionName = "testCollection"; | ||
| db.createCollection(collectionName); | ||
| MongoCollection<Document> collection = db.getCollection(collectionName); | ||
|
|
||
| collection.insertOne(new Document("foo", "bar")); | ||
|
|
||
| Assert.assertEquals(1, collection.count()); | ||
|
|
||
| Assert.assertEquals(3, writer.getList().size()); | ||
|
|
||
| final String createCollectionQuery = | ||
| "{ \"create\" : \"testCollection\", \"autoIndexId\" : \"?\", \"capped\" : \"?\" }"; | ||
| final DDBaseSpan<?> 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( | ||
| ByteBuffer.wrap(InetAddress.getByName("127.0.0.1").getAddress()).getInt(), | ||
| 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())); | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
dd-java-agent-ittests/src/test/java/com/datadoghq/agent/integration/TestUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.datadoghq.agent.integration; | ||
|
|
||
| import io.opentracing.Tracer; | ||
| import io.opentracing.util.GlobalTracer; | ||
| import java.lang.reflect.Field; | ||
|
|
||
| public class TestUtils { | ||
| public static void registerOrReplaceGlobalTracer(Tracer tracer) throws Exception { | ||
| try { | ||
| GlobalTracer.register(tracer); | ||
| } catch (final Exception e) { | ||
| // Force it anyway using reflection | ||
| final Field field = GlobalTracer.class.getDeclaredField("tracer"); | ||
| field.setAccessible(true); | ||
| field.set(null, tracer); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we could take advantage of actual Cassandra and mongo instances running in CI… rather than having to start them up on the fly. (obviously using this as a fallback for non-CI execution)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For mongo, I don't see a substantial performance hit running a local instance.