11import com.mongodb.client.MongoClients
2- import datadog.trace.agent.test.InstrumentationSpecification
32import datadog.trace.api.config.TraceInstrumentationConfig
43import datadog.trace.bootstrap.instrumentation.api.Tags
5- import datadog.trace.core.DDSpan
64import org.bson.Document
7- import org.testcontainers.containers.GenericContainer
8- import spock.lang.Shared
95import spock.lang.Unroll
106
117import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
128import static datadog.trace.bootstrap.instrumentation.api.InstrumentationTags.DBM_TRACE_INJECTED
139
14- class MongoDBMInjectionTest extends InstrumentationSpecification {
15- @Shared
16- GenericContainer mongo
10+ class MongoDBMInjectionTest extends MongoBaseTest {
1711
18- def setupSpec () {
19- mongo = new GenericContainer ( " mongo:4.4.29 " ) . withExposedPorts( 27017 )
20- mongo . start()
12+ @Override
13+ int version () {
14+ return 0
2115 }
2216
23- def cleanupSpec () {
24- if (mongo) {
25- mongo. stop()
26- }
17+ @Override
18+ String service () {
19+ return V0_SERVICE
20+ }
21+
22+ @Override
23+ String operation () {
24+ return V0_OPERATION
25+ }
26+
27+ @Override
28+ String dbType () {
29+ return " mongo"
2730 }
2831
2932 def " MongoDB find command includes comment" () {
3033 setup :
31- def mongoHost = mongo. getHost()
32- def mongoPort = mongo. getFirstMappedPort()
33- def client = MongoClients . create(" mongodb://${ mongoHost} :${ mongoPort} " )
34- def database = client. getDatabase(" test" )
35- def collection = database. getCollection(" testCollection" )
34+ def collectionName = randomCollectionName()
35+ def client = MongoClients . create(" mongodb://${ mongoDbContainer.getHost()} :${ port} " )
36+ def database = client. getDatabase(databaseName)
37+ def collection = database. getCollection(collectionName)
3638
3739 when :
3840 runUnderTrace(" test" ) {
@@ -42,11 +44,11 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
4244 then :
4345 def traces = TEST_WRITER . waitForTraces(1 )
4446 traces. size() == 1
45- def mongoSpan = traces[0 ]. find { it. operationName in [ " mongo.query" , " mongodb.query " ] }
47+ def mongoSpan = traces[0 ]. find { it. operationName == " mongo.query" }
4648 mongoSpan != null
4749 // Verify span has trace injected tag
4850 mongoSpan. getTag(DBM_TRACE_INJECTED ) == true
49- mongoSpan. getTag(Tags . DB_TYPE ) in [ " mongo" , " mongodb " ]
51+ mongoSpan. getTag(Tags . DB_TYPE ) == " mongo"
5052 mongoSpan. getTag(Tags . DB_OPERATION ) != null
5153
5254 cleanup :
@@ -55,10 +57,8 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
5557
5658 def " MongoDB aggregate command includes comment" () {
5759 setup :
58- def mongoHost = mongo. getHost()
59- def mongoPort = mongo. getFirstMappedPort()
60- def client = MongoClients . create(" mongodb://${ mongoHost} :${ mongoPort} " )
61- def database = client. getDatabase(" test" )
60+ def client = MongoClients . create(" mongodb://${ mongoDbContainer.getHost()} :${ port} " )
61+ def database = client. getDatabase(databaseName)
6262 def collection = database. getCollection(" testCollection" )
6363
6464 when :
@@ -69,11 +69,11 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
6969 then :
7070 def traces = TEST_WRITER . waitForTraces(1 )
7171 traces. size() == 1
72- def mongoSpan = traces[0 ]. find { it. operationName in [ " mongo.query" , " mongodb.query " ] }
72+ def mongoSpan = traces[0 ]. find { it. operationName == " mongo.query" }
7373 mongoSpan != null
7474 // Verify span has trace injected tag
7575 mongoSpan. getTag(DBM_TRACE_INJECTED ) == true
76- mongoSpan. getTag(Tags . DB_TYPE ) in [ " mongo" , " mongodb " ]
76+ mongoSpan. getTag(Tags . DB_TYPE ) == " mongo"
7777 mongoSpan. getTag(Tags . DB_OPERATION ) != null
7878
7979 cleanup :
@@ -82,10 +82,8 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
8282
8383 def " MongoDB insert command includes comment" () {
8484 setup :
85- def mongoHost = mongo. getHost()
86- def mongoPort = mongo. getFirstMappedPort()
87- def client = MongoClients . create(" mongodb://${ mongoHost} :${ mongoPort} " )
88- def database = client. getDatabase(" test" )
85+ def client = MongoClients . create(" mongodb://${ mongoDbContainer.getHost()} :${ port} " )
86+ def database = client. getDatabase(databaseName)
8987 def collection = database. getCollection(" testCollection" )
9088
9189 when :
@@ -96,11 +94,11 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
9694 then :
9795 def traces = TEST_WRITER . waitForTraces(1 )
9896 traces. size() == 1
99- def mongoSpan = traces[0 ]. find { it. operationName in [ " mongo.query" , " mongodb.query " ] }
97+ def mongoSpan = traces[0 ]. find { it. operationName == " mongo.query" }
10098 mongoSpan != null
10199 // Verify span has trace injected tag
102100 mongoSpan. getTag(DBM_TRACE_INJECTED ) == true
103- mongoSpan. getTag(Tags . DB_TYPE ) in [ " mongo" , " mongodb " ]
101+ mongoSpan. getTag(Tags . DB_TYPE ) == " mongo"
104102 mongoSpan. getTag(Tags . DB_OPERATION ) != null
105103
106104 cleanup :
@@ -109,10 +107,8 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
109107
110108 def " Comment format matches expected pattern" () {
111109 setup :
112- def mongoHost = mongo. getHost()
113- def mongoPort = mongo. getFirstMappedPort()
114- def client = MongoClients . create(" mongodb://${ mongoHost} :${ mongoPort} " )
115- def database = client. getDatabase(" test" )
110+ def client = MongoClients . create(" mongodb://${ mongoDbContainer.getHost()} :${ port} " )
111+ def database = client. getDatabase(databaseName)
116112 def collection = database. getCollection(" testCollection" )
117113
118114 when :
@@ -123,7 +119,7 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
123119 then :
124120 def spans = TEST_WRITER . waitForTraces(1 )
125121 spans. size() == 1
126- def mongoSpan = spans [0 ]. find { it. operationName in [ " mongo.query" , " mongodb.query " ] } as DDSpan
122+ def mongoSpan = traces [0 ]. find { it. operationName == " mongo.query" }
127123 mongoSpan != null
128124 mongoSpan. getTag(DBM_TRACE_INJECTED ) == true
129125
@@ -136,23 +132,12 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
136132 client?. close()
137133 }
138134
139- @Override
140- void configurePreAgent () {
141- super . configurePreAgent()
142- // Enable in service mode only
143- injectSysConfig(TraceInstrumentationConfig . DB_DBM_PROPAGATION_MODE_MODE , " service" )
144- injectSysConfig(" dd.service.name" , " test-mongo-service" )
145- injectSysConfig(" dd.env" , " test" )
146- }
147-
148135 @Unroll
149136 def " Comment injection works for different propagation modes: #mode" () {
150137 setup :
151138 injectSysConfig(TraceInstrumentationConfig . DB_DBM_PROPAGATION_MODE_MODE , mode)
152- def mongoHost = mongo. getHost()
153- def mongoPort = mongo. getFirstMappedPort()
154- def client = MongoClients . create(" mongodb://${ mongoHost} :${ mongoPort} " )
155- def database = client. getDatabase(" test" )
139+ def client = MongoClients . create(" mongodb://${ mongoDbContainer.getHost()} :${ port} " )
140+ def database = client. getDatabase(databaseName)
156141 def collection = database. getCollection(" testCollection" )
157142
158143 when :
@@ -163,7 +148,7 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
163148 then :
164149 def traces = TEST_WRITER . waitForTraces(1 )
165150 traces. size() == 1
166- def mongoSpan = traces[0 ]. find { it. operationName in [ " mongo.query" , " mongodb.query " ] } as DDSpan
151+ def mongoSpan = traces[0 ]. find { it. operationName == " mongo.query" }
167152 mongoSpan != null
168153
169154 if (mode == " disabled" ) {
@@ -192,10 +177,8 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
192177 setup :
193178 injectSysConfig(TraceInstrumentationConfig . DB_DBM_PROPAGATION_MODE_MODE , " service" )
194179 injectSysConfig(" dd.service.name" , " test-mongo-service" )
195- def mongoHost = mongo. getHost()
196- def mongoPort = mongo. getFirstMappedPort()
197- def client = MongoClients . create(" mongodb://${ mongoHost} :${ mongoPort} " )
198- def database = client. getDatabase(" test" )
180+ def client = MongoClients . create(" mongodb://${ mongoDbContainer.getHost()} :${ port} " )
181+ def database = client. getDatabase(databaseName)
199182 def collection = database. getCollection(" testCollection" )
200183
201184 when :
@@ -222,10 +205,10 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
222205 then :
223206 def traces = TEST_WRITER . waitForTraces(1 )
224207 traces. size() == 1
225- def mongoSpan = traces[0 ]. find { it. operationName in [ " mongo.query" , " mongodb.query " ] }
208+ def mongoSpan = traces[0 ]. find { it. operationName == " mongo.query" }
226209 mongoSpan != null
227210 mongoSpan. getTag(DBM_TRACE_INJECTED ) == true
228- mongoSpan. getTag(Tags . DB_TYPE ) in [ " mongo" , " mongodb " ]
211+ mongoSpan. getTag(Tags . DB_TYPE ) == " mongo"
229212
230213 cleanup :
231214 client?. close()
@@ -239,10 +222,8 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
239222 injectSysConfig(TraceInstrumentationConfig . DB_DBM_PROPAGATION_MODE_MODE , " service" )
240223 injectSysConfig(" service.name" , " original-service" )
241224 injectSysConfig(" dd.service.mapping" , " mongo:mapped-mongo-service" )
242- def mongoHost = mongo. getHost()
243- def mongoPort = mongo. getFirstMappedPort()
244- def client = MongoClients . create(" mongodb://${ mongoHost} :${ mongoPort} " )
245- def database = client. getDatabase(" test" )
225+ def client = MongoClients . create(" mongodb://${ mongoDbContainer.getHost()} :${ port} " )
226+ def database = client. getDatabase(databaseName)
246227 def collection = database. getCollection(" testCollection" )
247228
248229 when :
@@ -253,7 +234,7 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
253234 then :
254235 def traces = TEST_WRITER . waitForTraces(1 )
255236 traces. size() == 1
256- def mongoSpan = traces[0 ]. find { it. operationName in [ " mongo.query " , " mongodb.query" ] }
237+ def mongoSpan = traces[0 ]. find { it. operationName == " mongodb.query" }
257238 mongoSpan != null
258239 mongoSpan. getTag(DBM_TRACE_INJECTED ) == true
259240 // The exact service name used in comment is tested in unit tests
0 commit comments