Skip to content

Commit 7cba82a

Browse files
committed
Update command span creation behaviour.
1 parent 5d7f5c1 commit 7cba82a

File tree

2 files changed

+37
-58
lines changed

2 files changed

+37
-58
lines changed

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

Lines changed: 11 additions & 15 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;
@@ -554,18 +557,12 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
554557
const resultHandler = callback;
555558
const commandType = Object.keys(cmd)[0];
556559

557-
if (
558-
skipInstrumentation ||
559-
typeof resultHandler !== 'function' ||
560-
typeof cmd !== 'object' ||
561-
cmd.ismaster ||
562-
cmd.hello
563-
) {
560+
if (typeof cmd !== 'object' || cmd.ismaster || cmd.hello) {
564561
return original.call(this, ns, cmd, options, callback);
565562
}
566563

567564
let span = undefined;
568-
if (currentSpan) {
565+
if (!skipInstrumentation) {
569566
span = instrumentation.tracer.startSpan(`mongodb.${commandType}`, {
570567
kind: SpanKind.CLIENT,
571568
});
@@ -604,17 +601,12 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
604601
const commandType = Object.keys(cmd)[0];
605602
const resultHandler = () => undefined;
606603

607-
if (
608-
skipInstrumentation ||
609-
typeof cmd !== 'object' ||
610-
cmd.ismaster ||
611-
cmd.hello
612-
) {
604+
if (typeof cmd !== 'object' || cmd.ismaster || cmd.hello) {
613605
return original.apply(this, args);
614606
}
615607

616608
let span = undefined;
617-
if (currentSpan) {
609+
if (!skipInstrumentation) {
618610
span = instrumentation.tracer.startSpan(`mongodb.${commandType}`, {
619611
kind: SpanKind.CLIENT,
620612
});
@@ -663,6 +655,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
663655
instrumentation._checkSkipInstrumentation(currentSpan);
664656
const resultHandler =
665657
typeof options === 'function' ? options : callback;
658+
666659
if (
667660
skipInstrumentation ||
668661
typeof resultHandler !== 'function' ||
@@ -682,6 +675,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
682675
);
683676
}
684677
}
678+
685679
const span = instrumentation.tracer.startSpan('mongodb.find', {
686680
kind: SpanKind.CLIENT,
687681
});
@@ -731,6 +725,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
731725

732726
const resultHandler =
733727
typeof options === 'function' ? options : callback;
728+
734729
if (skipInstrumentation || typeof resultHandler !== 'function') {
735730
if (typeof options === 'function') {
736731
return original.call(
@@ -753,6 +748,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
753748
);
754749
}
755750
}
751+
756752
const span = instrumentation.tracer.startSpan('mongodb.getMore', {
757753
kind: SpanKind.CLIENT,
758754
});

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)