1
1
import com.mongodb.client.MongoClients
2
- import datadog.trace.agent.test.InstrumentationSpecification
3
2
import datadog.trace.api.config.TraceInstrumentationConfig
4
3
import datadog.trace.bootstrap.instrumentation.api.Tags
5
- import datadog.trace.core.DDSpan
6
4
import org.bson.Document
7
- import org.testcontainers.containers.GenericContainer
8
- import spock.lang.Shared
9
5
import spock.lang.Unroll
10
6
11
7
import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
12
8
import static datadog.trace.bootstrap.instrumentation.api.InstrumentationTags.DBM_TRACE_INJECTED
13
9
14
- class MongoDBMInjectionTest extends InstrumentationSpecification {
15
- @Shared
16
- GenericContainer mongo
10
+ class MongoDBMInjectionTest extends MongoBaseTest {
17
11
18
- def setupSpec () {
19
- mongo = new GenericContainer ( " mongo:4.4.29 " ) . withExposedPorts( 27017 )
20
- mongo . start()
12
+ @Override
13
+ int version () {
14
+ return 0
21
15
}
22
16
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"
27
30
}
28
31
29
32
def " MongoDB find command includes comment" () {
30
33
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)
36
38
37
39
when :
38
40
runUnderTrace(" test" ) {
@@ -42,11 +44,11 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
42
44
then :
43
45
def traces = TEST_WRITER . waitForTraces(1 )
44
46
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" }
46
48
mongoSpan != null
47
49
// Verify span has trace injected tag
48
50
mongoSpan. getTag(DBM_TRACE_INJECTED ) == true
49
- mongoSpan. getTag(Tags . DB_TYPE ) in [ " mongo" , " mongodb " ]
51
+ mongoSpan. getTag(Tags . DB_TYPE ) == " mongo"
50
52
mongoSpan. getTag(Tags . DB_OPERATION ) != null
51
53
52
54
cleanup :
@@ -55,10 +57,8 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
55
57
56
58
def " MongoDB aggregate command includes comment" () {
57
59
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)
62
62
def collection = database. getCollection(" testCollection" )
63
63
64
64
when :
@@ -69,11 +69,11 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
69
69
then :
70
70
def traces = TEST_WRITER . waitForTraces(1 )
71
71
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" }
73
73
mongoSpan != null
74
74
// Verify span has trace injected tag
75
75
mongoSpan. getTag(DBM_TRACE_INJECTED ) == true
76
- mongoSpan. getTag(Tags . DB_TYPE ) in [ " mongo" , " mongodb " ]
76
+ mongoSpan. getTag(Tags . DB_TYPE ) == " mongo"
77
77
mongoSpan. getTag(Tags . DB_OPERATION ) != null
78
78
79
79
cleanup :
@@ -82,10 +82,8 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
82
82
83
83
def " MongoDB insert command includes comment" () {
84
84
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)
89
87
def collection = database. getCollection(" testCollection" )
90
88
91
89
when :
@@ -96,11 +94,11 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
96
94
then :
97
95
def traces = TEST_WRITER . waitForTraces(1 )
98
96
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" }
100
98
mongoSpan != null
101
99
// Verify span has trace injected tag
102
100
mongoSpan. getTag(DBM_TRACE_INJECTED ) == true
103
- mongoSpan. getTag(Tags . DB_TYPE ) in [ " mongo" , " mongodb " ]
101
+ mongoSpan. getTag(Tags . DB_TYPE ) == " mongo"
104
102
mongoSpan. getTag(Tags . DB_OPERATION ) != null
105
103
106
104
cleanup :
@@ -109,10 +107,8 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
109
107
110
108
def " Comment format matches expected pattern" () {
111
109
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)
116
112
def collection = database. getCollection(" testCollection" )
117
113
118
114
when :
@@ -123,7 +119,7 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
123
119
then :
124
120
def spans = TEST_WRITER . waitForTraces(1 )
125
121
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" }
127
123
mongoSpan != null
128
124
mongoSpan. getTag(DBM_TRACE_INJECTED ) == true
129
125
@@ -136,23 +132,12 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
136
132
client?. close()
137
133
}
138
134
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
-
148
135
@Unroll
149
136
def " Comment injection works for different propagation modes: #mode" () {
150
137
setup :
151
138
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)
156
141
def collection = database. getCollection(" testCollection" )
157
142
158
143
when :
@@ -163,7 +148,7 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
163
148
then :
164
149
def traces = TEST_WRITER . waitForTraces(1 )
165
150
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" }
167
152
mongoSpan != null
168
153
169
154
if (mode == " disabled" ) {
@@ -192,10 +177,8 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
192
177
setup :
193
178
injectSysConfig(TraceInstrumentationConfig . DB_DBM_PROPAGATION_MODE_MODE , " service" )
194
179
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)
199
182
def collection = database. getCollection(" testCollection" )
200
183
201
184
when :
@@ -222,10 +205,10 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
222
205
then :
223
206
def traces = TEST_WRITER . waitForTraces(1 )
224
207
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" }
226
209
mongoSpan != null
227
210
mongoSpan. getTag(DBM_TRACE_INJECTED ) == true
228
- mongoSpan. getTag(Tags . DB_TYPE ) in [ " mongo" , " mongodb " ]
211
+ mongoSpan. getTag(Tags . DB_TYPE ) == " mongo"
229
212
230
213
cleanup :
231
214
client?. close()
@@ -239,10 +222,8 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
239
222
injectSysConfig(TraceInstrumentationConfig . DB_DBM_PROPAGATION_MODE_MODE , " service" )
240
223
injectSysConfig(" service.name" , " original-service" )
241
224
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)
246
227
def collection = database. getCollection(" testCollection" )
247
228
248
229
when :
@@ -253,7 +234,7 @@ class MongoDBMInjectionTest extends InstrumentationSpecification {
253
234
then :
254
235
def traces = TEST_WRITER . waitForTraces(1 )
255
236
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" }
257
238
mongoSpan != null
258
239
mongoSpan. getTag(DBM_TRACE_INJECTED ) == true
259
240
// The exact service name used in comment is tested in unit tests
0 commit comments