/
mongodb.test.ts
417 lines (378 loc) · 13.2 KB
/
mongodb.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// for testing locally "npm run docker:start"
import { context, trace, SpanKind, Span } from '@opentelemetry/api';
import { BasicTracerProvider } from '@opentelemetry/tracing';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import {
InMemorySpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/tracing';
import * as assert from 'assert';
import { MongoDBInstrumentation, MongoDBInstrumentationConfig } from '../src';
import { CommandResult } from '../src/types';
const instrumentation = new MongoDBInstrumentation();
instrumentation.enable();
instrumentation.disable();
import * as mongodb from 'mongodb';
import { assertSpans, accessCollection } from './utils';
describe('MongoDBInstrumentation', () => {
function create(config: MongoDBInstrumentationConfig = {}) {
instrumentation.setConfig(config);
instrumentation.enable();
}
// For these tests, mongo must be running. Add RUN_MONGODB_TESTS to run
// these tests.
const RUN_MONGODB_TESTS = process.env.RUN_MONGODB_TESTS as string;
let shouldTest = true;
if (!RUN_MONGODB_TESTS) {
console.log('Skipping test-mongodb. Run MongoDB to test');
shouldTest = false;
}
// shouldTest = true
const URL = `mongodb://${process.env.MONGODB_HOST || 'localhost'}:${
process.env.MONGODB_PORT || '27017'
}`;
const DB_NAME = process.env.MONGODB_DB || 'opentelemetry-tests';
const COLLECTION_NAME = 'test';
let client: mongodb.MongoClient;
let collection: mongodb.Collection;
const provider = new BasicTracerProvider();
const contextManager = new AsyncHooksContextManager().enable();
const memoryExporter = new InMemorySpanExporter();
const spanProcessor = new SimpleSpanProcessor(memoryExporter);
before(done => {
instrumentation.enable();
instrumentation.setTracerProvider(provider);
provider.addSpanProcessor(spanProcessor);
context.setGlobalContextManager(contextManager);
shouldTest = true;
accessCollection(URL, DB_NAME, COLLECTION_NAME)
.then(result => {
client = result.client;
collection = result.collection;
done();
})
.catch((err: Error) => {
console.log(
'Skipping test-mongodb. Could not connect. Run MongoDB to test'
);
shouldTest = false;
done();
});
});
after(() => {
contextManager.disable();
instrumentation.disable();
});
beforeEach(function mongoBeforeEach(done) {
// Skipping all tests in beforeEach() is a workaround. Mocha does not work
// properly when skipping tests in before() on nested describe() calls.
// https://github.com/mochajs/mocha/issues/2819
if (!shouldTest) {
this.skip();
}
memoryExporter.reset();
// Non traced insertion of basic data to perform tests
const insertData = [{ a: 1 }, { a: 2 }, { a: 3 }];
collection.insertMany(insertData, (err, result) => {
done();
});
});
afterEach(done => {
memoryExporter.reset();
if (shouldTest) {
return collection.deleteMany({}, done);
}
done();
});
after(() => {
if (client) {
client.close();
}
});
/** Should intercept query */
describe('Instrumenting query operations', () => {
beforeEach(() => {
memoryExporter.reset();
});
it('should create a child span for insert', done => {
const insertData = [{ a: 1 }, { a: 2 }, { a: 3 }];
const span = provider.getTracer('default').startSpan('insertRootSpan');
context.with(trace.setSpan(context.active(), span), () => {
collection.insertMany(insertData, (err, result) => {
span.end();
assert.ifError(err);
assertSpans(
memoryExporter.getFinishedSpans(),
'mongodb.insert',
SpanKind.CLIENT
);
done();
});
});
});
it('should create a child span for update', done => {
const span = provider.getTracer('default').startSpan('updateRootSpan');
context.with(trace.setSpan(context.active(), span), () => {
collection.updateOne({ a: 2 }, { $set: { b: 1 } }, (err, result) => {
span.end();
assert.ifError(err);
assertSpans(
memoryExporter.getFinishedSpans(),
'mongodb.update',
SpanKind.CLIENT
);
done();
});
});
});
it('should create a child span for remove', done => {
const span = provider.getTracer('default').startSpan('removeRootSpan');
context.with(trace.setSpan(context.active(), span), () => {
collection.deleteOne({ a: 3 }, (err, result) => {
span.end();
assert.ifError(err);
assertSpans(
memoryExporter.getFinishedSpans(),
'mongodb.remove',
SpanKind.CLIENT
);
done();
});
});
});
});
/** Should intercept cursor */
describe('Instrumenting cursor operations', () => {
beforeEach(() => {
memoryExporter.reset();
});
it('should create a child span for find', done => {
const span = provider.getTracer('default').startSpan('findRootSpan');
context.with(trace.setSpan(context.active(), span), () => {
collection.find({ a: 1 }).toArray((err, result) => {
span.end();
assert.ifError(err);
assertSpans(
memoryExporter.getFinishedSpans(),
'mongodb.find',
SpanKind.CLIENT
);
done();
});
});
});
it('should create a child span for cursor operations', done => {
const span = provider.getTracer('default').startSpan('findRootSpan');
context.with(trace.setSpan(context.active(), span), () => {
const cursor = collection.find().batchSize(1);
cursor.next().then(firstElement => {
assert(firstElement !== null);
cursor.next().then(secondElement => {
span.end();
assert(secondElement !== null);
// assert that we correctly got the first as a find
assertSpans(
memoryExporter
.getFinishedSpans()
.filter(
span => span.name.includes('mongodb.getMore') === false
),
'mongodb.find',
SpanKind.CLIENT
);
// assert that we correctly got the first as a find
assertSpans(
memoryExporter
.getFinishedSpans()
.filter(span => span.name.includes('mongodb.find') === false),
'mongodb.getMore',
SpanKind.CLIENT
);
done();
});
});
});
});
});
/** Should intercept command */
describe('Instrumenting command operations', () => {
beforeEach(() => {
memoryExporter.reset();
});
it('should create a child span for create index', done => {
const span = provider.getTracer('default').startSpan('indexRootSpan');
context.with(trace.setSpan(context.active(), span), () => {
collection.createIndex({ a: 1 }, (err, result) => {
span.end();
assert.ifError(err);
assertSpans(
memoryExporter.getFinishedSpans(),
'mongodb.createIndexes',
SpanKind.CLIENT
);
done();
});
});
});
});
describe('when specifying a responseHook configuration', () => {
const dataAttributeName = 'mongodb_data';
beforeEach(() => {
memoryExporter.reset();
});
describe('with a valid function', () => {
beforeEach(() => {
create({
responseHook: (span: Span, result: CommandResult) => {
span.setAttribute(dataAttributeName, JSON.stringify(result.result));
},
});
});
it('should attach response hook data to the resulting span for insert function', done => {
const insertData = [{ a: 1 }, { a: 2 }, { a: 3 }];
const span = provider.getTracer('default').startSpan('insertRootSpan');
context.with(trace.setSpan(context.active(), span), () => {
collection.insertMany(insertData, (err, result) => {
span.end();
assert.ifError(err);
const spans = memoryExporter.getFinishedSpans();
const insertSpan = spans[0];
assert.strictEqual(
insertSpan.attributes[dataAttributeName],
JSON.stringify(result.result, Object.keys(result.result).sort())
);
memoryExporter.reset();
done();
});
});
});
it('should attach response hook data to the resulting span for find function', done => {
const span = provider.getTracer('default').startSpan('findRootSpan');
context.with(trace.setSpan(context.active(), span), () => {
collection.find({ a: 1 }).toArray((err, results) => {
span.end();
assert.ifError(err);
const spans = memoryExporter.getFinishedSpans();
const findSpan = spans[0];
const spanResult = JSON.parse(
findSpan.attributes[dataAttributeName]?.toString() || '{}'
);
assert.strictEqual(
spanResult.cursor.firstBatch[0]._id,
results[0]._id.toString()
);
memoryExporter.reset();
done();
});
});
});
});
describe('with an invalid function', () => {
beforeEach(() => {
create({
responseHook: (span: Span, result: CommandResult) => {
throw 'some error';
},
});
});
it('should not do any harm when throwing an exception', done => {
const span = provider.getTracer('default').startSpan('findRootSpan');
context.with(trace.setSpan(context.active(), span), () => {
collection.find({ a: 1 }).toArray((err, results) => {
span.end();
const spans = memoryExporter.getFinishedSpans();
assert.ifError(err);
assertSpans(spans, 'mongodb.find', SpanKind.CLIENT);
memoryExporter.reset();
done();
});
});
});
});
});
describe('Mixed operations with callback', () => {
beforeEach(() => {
memoryExporter.reset();
});
it('should create a span for find after callback insert', done => {
const insertData = [{ a: 1 }, { a: 2 }, { a: 3 }];
const span = provider.getTracer('default').startSpan('insertRootSpan');
context.with(trace.setSpan(context.active(), span), () => {
collection.insertMany(insertData, (err, result) => {
span.end();
assert.ifError(err);
const spans = memoryExporter.getFinishedSpans();
const mainSpan = spans[spans.length - 1];
assertSpans(spans, 'mongodb.insert', SpanKind.CLIENT);
memoryExporter.reset();
collection.find({ a: 1 }).toArray((err, result) => {
const spans2 = memoryExporter.getFinishedSpans();
spans2.push(mainSpan);
assert.ifError(err);
assertSpans(spans2, 'mongodb.find', SpanKind.CLIENT);
assert.strictEqual(
mainSpan.spanContext().spanId,
spans2[0].parentSpanId
);
memoryExporter.reset();
done();
});
});
});
});
});
/** Should intercept command */
describe('Removing Instrumentation', () => {
beforeEach(() => {
memoryExporter.reset();
});
it('should unpatch plugin', () => {
assert.doesNotThrow(() => {
instrumentation.disable();
});
});
it('should not create a child span for query', done => {
const insertData = [{ a: 1 }, { a: 2 }, { a: 3 }];
const span = provider.getTracer('default').startSpan('insertRootSpan');
collection.insertMany(insertData, (err, result) => {
span.end();
assert.ifError(err);
assert.strictEqual(memoryExporter.getFinishedSpans().length, 1);
done();
});
});
it('should not create a child span for cursor', done => {
const span = provider.getTracer('default').startSpan('findRootSpan');
collection.find({}).toArray((err, result) => {
span.end();
assert.ifError(err);
assert.strictEqual(memoryExporter.getFinishedSpans().length, 1);
done();
});
});
it('should not create a child span for command', done => {
const span = provider.getTracer('default').startSpan('indexRootSpan');
collection.createIndex({ a: 1 }, (err, result) => {
span.end();
assert.ifError(err);
assert.strictEqual(memoryExporter.getFinishedSpans().length, 1);
done();
});
});
});
});