Skip to content

Commit

Permalink
chore(mongo): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
haddasbronfman committed Dec 6, 2022
1 parent 49eeb3b commit b87d8ac
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ describe('MongoDBInstrumentation', () => {
.insertMany(insertData)
.then(() => {
span.end();
assertSpans(getTestSpans(), 'mongodb.insert', SpanKind.CLIENT);
assertSpans(
getTestSpans(),
'mongodb.insert',
SpanKind.CLIENT,
'insert'
);
done();
})
.catch(err => {
Expand All @@ -128,7 +133,12 @@ describe('MongoDBInstrumentation', () => {
.updateOne({ a: 2 }, { $set: { b: 1 } })
.then(() => {
span.end();
assertSpans(getTestSpans(), 'mongodb.update', SpanKind.CLIENT);
assertSpans(
getTestSpans(),
'mongodb.update',
SpanKind.CLIENT,
'update'
);
done();
})
.catch(err => {
Expand All @@ -144,7 +154,12 @@ describe('MongoDBInstrumentation', () => {
.deleteOne({ a: 3 })
.then(() => {
span.end();
assertSpans(getTestSpans(), 'mongodb.remove', SpanKind.CLIENT);
assertSpans(
getTestSpans(),
'mongodb.remove',
SpanKind.CLIENT,
'remove'
);
done();
})
.catch(err => {
Expand All @@ -164,7 +179,12 @@ describe('MongoDBInstrumentation', () => {
.toArray()
.then(() => {
span.end();
assertSpans(getTestSpans(), 'mongodb.find', SpanKind.CLIENT);
assertSpans(
getTestSpans(),
'mongodb.find',
SpanKind.CLIENT,
'find'
);
done();
})
.catch(err => {
Expand All @@ -190,15 +210,17 @@ describe('MongoDBInstrumentation', () => {
span => !span.name.includes('mongodb.getMore')
),
'mongodb.find',
SpanKind.CLIENT
SpanKind.CLIENT,
'find'
);
// assert that we correctly got the first as a find
assertSpans(
getTestSpans().filter(
span => !span.name.includes('mongodb.find')
),
'mongodb.getMore',
SpanKind.CLIENT
SpanKind.CLIENT,
'getMore'
);
done();
})
Expand All @@ -222,7 +244,8 @@ describe('MongoDBInstrumentation', () => {
assertSpans(
getTestSpans(),
'mongodb.createIndexes',
SpanKind.CLIENT
SpanKind.CLIENT,
'createIndexes'
);
done();
})
Expand Down Expand Up @@ -253,7 +276,14 @@ describe('MongoDBInstrumentation', () => {
span.end();
const spans = getTestSpans();
const operationName = 'mongodb.insert';
assertSpans(spans, operationName, SpanKind.CLIENT, false, false);
assertSpans(
spans,
operationName,
SpanKind.CLIENT,
'insert',
false,
false
);
const mongoSpan = spans.find(s => s.name === operationName);
const dbStatement = JSON.parse(
mongoSpan!.attributes[SemanticAttributes.DB_STATEMENT] as string
Expand Down Expand Up @@ -291,7 +321,14 @@ describe('MongoDBInstrumentation', () => {
span.end();
const spans = getTestSpans();
const operationName = 'mongodb.insert';
assertSpans(spans, operationName, SpanKind.CLIENT, false, true);
assertSpans(
spans,
operationName,
SpanKind.CLIENT,
'insert',
false,
true
);
const mongoSpan = spans.find(s => s.name === operationName);
const dbStatement = JSON.parse(
mongoSpan!.attributes[SemanticAttributes.DB_STATEMENT] as string
Expand Down Expand Up @@ -324,7 +361,7 @@ describe('MongoDBInstrumentation', () => {
.then(() => {
span.end();
const spans = getTestSpans();
assertSpans(spans, 'mongodb.insert', SpanKind.CLIENT);
assertSpans(spans, 'mongodb.insert', SpanKind.CLIENT, 'insert');
done();
})
.catch(err => {
Expand Down Expand Up @@ -421,7 +458,7 @@ describe('MongoDBInstrumentation', () => {
.then(() => {
span.end();
const spans = getTestSpans();
assertSpans(spans, 'mongodb.find', SpanKind.CLIENT);
assertSpans(spans, 'mongodb.find', SpanKind.CLIENT, 'find');
done();
})
.catch(err => {
Expand All @@ -443,7 +480,7 @@ describe('MongoDBInstrumentation', () => {
span.end();
const spans = getTestSpans();
const mainSpan = spans[spans.length - 1];
assertSpans(spans, 'mongodb.insert', SpanKind.CLIENT);
assertSpans(spans, 'mongodb.insert', SpanKind.CLIENT, 'insert');
resetMemoryExporter();

collection
Expand All @@ -453,7 +490,7 @@ describe('MongoDBInstrumentation', () => {
const spans2 = getTestSpans();
spans2.push(mainSpan);

assertSpans(spans2, 'mongodb.find', SpanKind.CLIENT);
assertSpans(spans2, 'mongodb.find', SpanKind.CLIENT, 'find');
assert.strictEqual(
mainSpan.spanContext().spanId,
spans2[0].parentSpanId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ describe('MongoDBInstrumentation', () => {
.insertMany(insertData)
.then(() => {
span.end();
assertSpans(getTestSpans(), 'mongodb.insert', SpanKind.CLIENT);
assertSpans(
getTestSpans(),
'mongodb.insert',
SpanKind.CLIENT,
'insert'
);
done();
})
.catch(err => {
Expand All @@ -127,7 +132,12 @@ describe('MongoDBInstrumentation', () => {
.updateOne({ a: 2 }, { $set: { b: 1 } })
.then(() => {
span.end();
assertSpans(getTestSpans(), 'mongodb.update', SpanKind.CLIENT);
assertSpans(
getTestSpans(),
'mongodb.update',
SpanKind.CLIENT,
'update'
);
done();
})
.catch(err => {
Expand All @@ -143,7 +153,12 @@ describe('MongoDBInstrumentation', () => {
.deleteOne({ a: 3 })
.then(() => {
span.end();
assertSpans(getTestSpans(), 'mongodb.delete', SpanKind.CLIENT);
assertSpans(
getTestSpans(),
'mongodb.delete',
SpanKind.CLIENT,
'delete'
);
done();
})
.catch(err => {
Expand All @@ -163,7 +178,12 @@ describe('MongoDBInstrumentation', () => {
.toArray()
.then(() => {
span.end();
assertSpans(getTestSpans(), 'mongodb.find', SpanKind.CLIENT);
assertSpans(
getTestSpans(),
'mongodb.find',
SpanKind.CLIENT,
'find'
);
done();
})
.catch(err => {
Expand All @@ -189,15 +209,17 @@ describe('MongoDBInstrumentation', () => {
span => !span.name.includes('mongodb.getMore')
),
'mongodb.find',
SpanKind.CLIENT
SpanKind.CLIENT,
'find'
);
// assert that we correctly got the first as a find
assertSpans(
getTestSpans().filter(
span => !span.name.includes('mongodb.find')
),
'mongodb.getMore',
SpanKind.CLIENT
SpanKind.CLIENT,
'getMore'
);
done();
})
Expand All @@ -221,7 +243,8 @@ describe('MongoDBInstrumentation', () => {
assertSpans(
getTestSpans(),
'mongodb.createIndexes',
SpanKind.CLIENT
SpanKind.CLIENT,
'createIndexes'
);
done();
})
Expand Down Expand Up @@ -252,7 +275,14 @@ describe('MongoDBInstrumentation', () => {
span.end();
const spans = getTestSpans();
const operationName = 'mongodb.insert';
assertSpans(spans, operationName, SpanKind.CLIENT, false, false);
assertSpans(
spans,
operationName,
SpanKind.CLIENT,
'insert',
false,
false
);
const mongoSpan = spans.find(s => s.name === operationName);
const dbStatement = JSON.parse(
mongoSpan!.attributes[SemanticAttributes.DB_STATEMENT] as string
Expand Down Expand Up @@ -290,7 +320,14 @@ describe('MongoDBInstrumentation', () => {
span.end();
const spans = getTestSpans();
const operationName = 'mongodb.insert';
assertSpans(spans, operationName, SpanKind.CLIENT, false, true);
assertSpans(
spans,
operationName,
SpanKind.CLIENT,
'insert',
false,
true
);
const mongoSpan = spans.find(s => s.name === operationName);
const dbStatement = JSON.parse(
mongoSpan!.attributes[SemanticAttributes.DB_STATEMENT] as string
Expand Down Expand Up @@ -323,7 +360,7 @@ describe('MongoDBInstrumentation', () => {
.then(() => {
span.end();
const spans = getTestSpans();
assertSpans(spans, 'mongodb.insert', SpanKind.CLIENT);
assertSpans(spans, 'mongodb.insert', SpanKind.CLIENT, 'insert');
done();
})
.catch(err => {
Expand Down Expand Up @@ -417,7 +454,7 @@ describe('MongoDBInstrumentation', () => {
.then(() => {
span.end();
const spans = getTestSpans();
assertSpans(spans, 'mongodb.find', SpanKind.CLIENT);
assertSpans(spans, 'mongodb.find', SpanKind.CLIENT, 'find');
done();
})
.catch(err => {
Expand All @@ -439,7 +476,7 @@ describe('MongoDBInstrumentation', () => {
span.end();
const spans = getTestSpans();
const mainSpan = spans[spans.length - 1];
assertSpans(spans, 'mongodb.insert', SpanKind.CLIENT);
assertSpans(spans, 'mongodb.insert', SpanKind.CLIENT, 'insert');
resetMemoryExporter();

collection
Expand All @@ -448,7 +485,7 @@ describe('MongoDBInstrumentation', () => {
.then(() => {
const spans2 = getTestSpans();
spans2.push(mainSpan);
assertSpans(spans2, 'mongodb.find', SpanKind.CLIENT);
assertSpans(spans2, 'mongodb.find', SpanKind.CLIENT, 'find');
assert.strictEqual(
mainSpan.spanContext().spanId,
spans2[0].parentSpanId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function assertSpans(
spans: ReadableSpan[],
expectedName: string,
expectedKind: SpanKind,
expectedOperation: string,
log = false,
isEnhancedDatabaseReportingEnabled = false
) {
Expand All @@ -79,6 +80,10 @@ export function assertSpans(
const [mongoSpan] = spans;
assert.strictEqual(mongoSpan.name, expectedName);
assert.strictEqual(mongoSpan.kind, expectedKind);
assert.strictEqual(
mongoSpan.attributes[SemanticAttributes.DB_OPERATION],
expectedOperation
);
assert.strictEqual(
mongoSpan.attributes[SemanticAttributes.DB_SYSTEM],
'mongodb'
Expand Down

0 comments on commit b87d8ac

Please sign in to comment.