diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts index dc35acd533..b627a3d0be 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts @@ -462,6 +462,7 @@ export class MongoDBInstrumentation extends InstrumentationBase undefined; - if ( - skipInstrumentation || - typeof cmd !== 'object' || - cmd.ismaster || - cmd.hello - ) { + if (typeof cmd !== 'object' || cmd.ismaster || cmd.hello) { return original.apply(this, args); } let span = undefined; - if (currentSpan) { + if (!skipInstrumentation) { span = instrumentation.tracer.startSpan(`mongodb.${commandType}`, { kind: SpanKind.CLIENT, }); @@ -663,6 +655,7 @@ export class MongoDBInstrumentation extends InstrumentationBase { describe('requireParentSpan', () => { // Resetting the behavior to default to avoid flakes in other tests - afterEach(() => { - instrumentation.setConfig({ - requireParentSpan: true, - }); + beforeEach(() => { + instrumentation.setConfig(); }); it('should not create spans without parent span when requireParentSpan is explicitly set to true', done => { - create({ - requireParentSpan: true, + context.with(trace.deleteSpan(context.active()), () => { + collection + .insertOne({ a: 1 }) + .then(() => { + assert.strictEqual(getTestSpans().length, 0); + done(); + }) + .catch(err => { + done(err); + }); }); - - collection - .insertOne({ a: 1 }) - .then(() => { - assert.strictEqual(getTestSpans().length, 0); - done(); - }) - .catch(err => { - done(err); - }); }); it('should create spans without parent span when requireParentSpan is false', done => { - create({ - requireParentSpan: false, - }); - - collection - .insertOne({ a: 1 }) - .then(() => { - assert.strictEqual(getTestSpans().length, 1); - done(); - }) - .catch(err => { - done(err); - }); - }); - - it('should create spans without parent span when requireParentSpan is set to false by setConfig', done => { - create(); - instrumentation.setConfig({ requireParentSpan: false, }); - collection - .insertOne({ a: 1 }) - .then(() => { - assert.strictEqual(getTestSpans().length, 1); - done(); - }) - .catch(err => { - done(err); - }); + context.with(trace.deleteSpan(context.active()), () => { + collection + .insertOne({ a: 1 }) + .then(() => { + assert.strictEqual(getTestSpans().length, 1); + done(); + }) + .catch(err => { + done(err); + }); + }); }); }); @@ -731,6 +711,9 @@ describe('MongoDBInstrumentation-Tracing-v5', () => { }) .catch(err => { done(err); + }) + .finally(() => { + span.end(); }); });