Skip to content

Commit 5d7f5c1

Browse files
committed
Refactor
1 parent b3a6402 commit 5d7f5c1

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

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

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,8 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
446446
callback?: Function
447447
) {
448448
const currentSpan = trace.getSpan(context.active());
449-
450-
const hasNoParentSpan = currentSpan === undefined;
451-
const requireParentSpan = instrumentation.getConfig().requireParentSpan;
452449
const skipInstrumentation =
453-
requireParentSpan === true && hasNoParentSpan;
450+
instrumentation._checkSkipInstrumentation(currentSpan);
454451

455452
const resultHandler =
456453
typeof options === 'function' ? options : callback;
@@ -504,11 +501,8 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
504501
callback?: Function
505502
) {
506503
const currentSpan = trace.getSpan(context.active());
507-
508-
const hasNoParentSpan = currentSpan === undefined;
509-
const requireParentSpan = instrumentation.getConfig().requireParentSpan;
510504
const skipInstrumentation =
511-
requireParentSpan === true && hasNoParentSpan;
505+
instrumentation._checkSkipInstrumentation(currentSpan);
512506

513507
const resultHandler =
514508
typeof options === 'function' ? options : callback;
@@ -555,10 +549,13 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
555549
callback: any
556550
) {
557551
const currentSpan = trace.getSpan(context.active());
552+
const skipInstrumentation =
553+
instrumentation._checkSkipInstrumentation(currentSpan);
558554
const resultHandler = callback;
559555
const commandType = Object.keys(cmd)[0];
560556

561557
if (
558+
skipInstrumentation ||
562559
typeof resultHandler !== 'function' ||
563560
typeof cmd !== 'object' ||
564561
cmd.ismaster ||
@@ -601,10 +598,18 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
601598
) {
602599
const [ns, cmd] = args;
603600
const currentSpan = trace.getSpan(context.active());
601+
const skipInstrumentation =
602+
instrumentation._checkSkipInstrumentation(currentSpan);
603+
604604
const commandType = Object.keys(cmd)[0];
605605
const resultHandler = () => undefined;
606606

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

@@ -654,12 +659,8 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
654659
callback?: Function
655660
) {
656661
const currentSpan = trace.getSpan(context.active());
657-
658-
const hasNoParentSpan = currentSpan === undefined;
659-
const requireParentSpan = instrumentation.getConfig().requireParentSpan;
660662
const skipInstrumentation =
661-
requireParentSpan === true && hasNoParentSpan;
662-
663+
instrumentation._checkSkipInstrumentation(currentSpan);
663664
const resultHandler =
664665
typeof options === 'function' ? options : callback;
665666
if (
@@ -725,11 +726,8 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
725726
callback?: Function
726727
) {
727728
const currentSpan = trace.getSpan(context.active());
728-
729-
const hasNoParentSpan = currentSpan === undefined;
730-
const requireParentSpan = instrumentation.getConfig().requireParentSpan;
731729
const skipInstrumentation =
732-
requireParentSpan === true && hasNoParentSpan;
730+
instrumentation._checkSkipInstrumentation(currentSpan);
733731

734732
const resultHandler =
735733
typeof options === 'function' ? options : callback;
@@ -1053,4 +1051,10 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
10531051
const poolName = `mongodb://${host}:${port}/${database}`;
10541052
this._poolName = poolName;
10551053
}
1054+
1055+
private _checkSkipInstrumentation(currentSpan: Span | undefined) {
1056+
const requireParentSpan = this.getConfig().requireParentSpan;
1057+
const hasNoParentSpan = currentSpan === undefined;
1058+
return requireParentSpan === true && hasNoParentSpan;
1059+
}
10561060
}

0 commit comments

Comments
 (0)