From b87d8acf99d398361ef2d1d4a1a4aa3cb538abbd Mon Sep 17 00:00:00 2001 From: haddasb Date: Tue, 6 Dec 2022 17:06:38 +0200 Subject: [PATCH] chore(mongo): add tests --- .../test/mongodb-v3.test.ts | 63 +++++++++++++++---- .../test/mongodb-v4.test.ts | 63 +++++++++++++++---- .../test/utils.ts | 5 ++ 3 files changed, 105 insertions(+), 26 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts index 3a663957f6..5a0b39e773 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts @@ -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 => { @@ -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 => { @@ -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 => { @@ -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 => { @@ -190,7 +210,8 @@ 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( @@ -198,7 +219,8 @@ describe('MongoDBInstrumentation', () => { span => !span.name.includes('mongodb.find') ), 'mongodb.getMore', - SpanKind.CLIENT + SpanKind.CLIENT, + 'getMore' ); done(); }) @@ -222,7 +244,8 @@ describe('MongoDBInstrumentation', () => { assertSpans( getTestSpans(), 'mongodb.createIndexes', - SpanKind.CLIENT + SpanKind.CLIENT, + 'createIndexes' ); done(); }) @@ -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 @@ -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 @@ -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 => { @@ -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 => { @@ -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 @@ -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 diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts index f42be4b03b..3101a947d2 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts @@ -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 => { @@ -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 => { @@ -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 => { @@ -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 => { @@ -189,7 +209,8 @@ 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( @@ -197,7 +218,8 @@ describe('MongoDBInstrumentation', () => { span => !span.name.includes('mongodb.find') ), 'mongodb.getMore', - SpanKind.CLIENT + SpanKind.CLIENT, + 'getMore' ); done(); }) @@ -221,7 +243,8 @@ describe('MongoDBInstrumentation', () => { assertSpans( getTestSpans(), 'mongodb.createIndexes', - SpanKind.CLIENT + SpanKind.CLIENT, + 'createIndexes' ); done(); }) @@ -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 @@ -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 @@ -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 => { @@ -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 => { @@ -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 @@ -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 diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts index fde0af56d8..3da0a1833c 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts @@ -65,6 +65,7 @@ export function assertSpans( spans: ReadableSpan[], expectedName: string, expectedKind: SpanKind, + expectedOperation: string, log = false, isEnhancedDatabaseReportingEnabled = false ) { @@ -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'