Skip to content

Commit 14b6fca

Browse files
committed
Update command span creation behaviour.
1 parent 5d7f5c1 commit 14b6fca

File tree

2 files changed

+35
-45
lines changed

2 files changed

+35
-45
lines changed

plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
462462
return original.call(this, server, ns, ops, options, callback);
463463
}
464464
}
465+
465466
const span = instrumentation.tracer.startSpan(
466467
`mongodb.${operationName}`,
467468
{
@@ -506,6 +507,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
506507

507508
const resultHandler =
508509
typeof options === 'function' ? options : callback;
510+
509511
if (
510512
skipInstrumentation ||
511513
typeof resultHandler !== 'function' ||
@@ -517,6 +519,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
517519
return original.call(this, server, ns, cmd, options, callback);
518520
}
519521
}
522+
520523
const commandType = MongoDBInstrumentation._getCommandType(cmd);
521524
const type =
522525
commandType === MongodbCommandType.UNKNOWN ? 'command' : commandType;
@@ -565,7 +568,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
565568
}
566569

567570
let span = undefined;
568-
if (currentSpan) {
571+
if (!skipInstrumentation) {
569572
span = instrumentation.tracer.startSpan(`mongodb.${commandType}`, {
570573
kind: SpanKind.CLIENT,
571574
});
@@ -614,7 +617,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
614617
}
615618

616619
let span = undefined;
617-
if (currentSpan) {
620+
if (!skipInstrumentation) {
618621
span = instrumentation.tracer.startSpan(`mongodb.${commandType}`, {
619622
kind: SpanKind.CLIENT,
620623
});
@@ -663,6 +666,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
663666
instrumentation._checkSkipInstrumentation(currentSpan);
664667
const resultHandler =
665668
typeof options === 'function' ? options : callback;
669+
666670
if (
667671
skipInstrumentation ||
668672
typeof resultHandler !== 'function' ||
@@ -682,6 +686,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
682686
);
683687
}
684688
}
689+
685690
const span = instrumentation.tracer.startSpan('mongodb.find', {
686691
kind: SpanKind.CLIENT,
687692
});
@@ -731,6 +736,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
731736

732737
const resultHandler =
733738
typeof options === 'function' ? options : callback;
739+
734740
if (skipInstrumentation || typeof resultHandler !== 'function') {
735741
if (typeof options === 'function') {
736742
return original.call(
@@ -753,6 +759,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
753759
);
754760
}
755761
}
762+
756763
const span = instrumentation.tracer.startSpan('mongodb.getMore', {
757764
kind: SpanKind.CLIENT,
758765
});

plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v5-v6.test.ts

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -654,60 +654,40 @@ describe('MongoDBInstrumentation-Tracing-v5', () => {
654654

655655
describe('requireParentSpan', () => {
656656
// Resetting the behavior to default to avoid flakes in other tests
657-
afterEach(() => {
658-
instrumentation.setConfig({
659-
requireParentSpan: true,
660-
});
657+
beforeEach(() => {
658+
instrumentation.setConfig();
661659
});
662660

663661
it('should not create spans without parent span when requireParentSpan is explicitly set to true', done => {
664-
create({
665-
requireParentSpan: true,
662+
context.with(trace.deleteSpan(context.active()), () => {
663+
collection
664+
.insertOne({ a: 1 })
665+
.then(() => {
666+
assert.strictEqual(getTestSpans().length, 0);
667+
done();
668+
})
669+
.catch(err => {
670+
done(err);
671+
});
666672
});
667-
668-
collection
669-
.insertOne({ a: 1 })
670-
.then(() => {
671-
assert.strictEqual(getTestSpans().length, 0);
672-
done();
673-
})
674-
.catch(err => {
675-
done(err);
676-
});
677673
});
678674

679675
it('should create spans without parent span when requireParentSpan is false', done => {
680-
create({
681-
requireParentSpan: false,
682-
});
683-
684-
collection
685-
.insertOne({ a: 1 })
686-
.then(() => {
687-
assert.strictEqual(getTestSpans().length, 1);
688-
done();
689-
})
690-
.catch(err => {
691-
done(err);
692-
});
693-
});
694-
695-
it('should create spans without parent span when requireParentSpan is set to false by setConfig', done => {
696-
create();
697-
698676
instrumentation.setConfig({
699677
requireParentSpan: false,
700678
});
701679

702-
collection
703-
.insertOne({ a: 1 })
704-
.then(() => {
705-
assert.strictEqual(getTestSpans().length, 1);
706-
done();
707-
})
708-
.catch(err => {
709-
done(err);
710-
});
680+
context.with(trace.deleteSpan(context.active()), () => {
681+
collection
682+
.insertOne({ a: 1 })
683+
.then(() => {
684+
assert.strictEqual(getTestSpans().length, 1);
685+
done();
686+
})
687+
.catch(err => {
688+
done(err);
689+
});
690+
});
711691
});
712692
});
713693

@@ -731,6 +711,9 @@ describe('MongoDBInstrumentation-Tracing-v5', () => {
731711
})
732712
.catch(err => {
733713
done(err);
714+
})
715+
.finally(() => {
716+
span.end();
734717
});
735718
});
736719

0 commit comments

Comments
 (0)