Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
onurtemizkan committed Jan 22, 2025
1 parent b3a6402 commit 5d7f5c1
Showing 1 changed file with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,8 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
callback?: Function
) {
const currentSpan = trace.getSpan(context.active());

const hasNoParentSpan = currentSpan === undefined;
const requireParentSpan = instrumentation.getConfig().requireParentSpan;
const skipInstrumentation =
requireParentSpan === true && hasNoParentSpan;
instrumentation._checkSkipInstrumentation(currentSpan);

Check warning on line 450 in plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L450 was not covered by tests

const resultHandler =
typeof options === 'function' ? options : callback;
Expand Down Expand Up @@ -504,11 +501,8 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
callback?: Function
) {
const currentSpan = trace.getSpan(context.active());

const hasNoParentSpan = currentSpan === undefined;
const requireParentSpan = instrumentation.getConfig().requireParentSpan;
const skipInstrumentation =
requireParentSpan === true && hasNoParentSpan;
instrumentation._checkSkipInstrumentation(currentSpan);

Check warning on line 505 in plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L505 was not covered by tests

const resultHandler =
typeof options === 'function' ? options : callback;
Expand Down Expand Up @@ -555,10 +549,13 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
callback: any
) {
const currentSpan = trace.getSpan(context.active());
const skipInstrumentation =
instrumentation._checkSkipInstrumentation(currentSpan);

Check warning on line 553 in plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L553 was not covered by tests
const resultHandler = callback;
const commandType = Object.keys(cmd)[0];

if (
skipInstrumentation ||

Check warning on line 558 in plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L558 was not covered by tests
typeof resultHandler !== 'function' ||
typeof cmd !== 'object' ||
cmd.ismaster ||
Expand Down Expand Up @@ -601,10 +598,18 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
) {
const [ns, cmd] = args;
const currentSpan = trace.getSpan(context.active());
const skipInstrumentation =
instrumentation._checkSkipInstrumentation(currentSpan);

const commandType = Object.keys(cmd)[0];
const resultHandler = () => undefined;

if (typeof cmd !== 'object' || cmd.ismaster || cmd.hello) {
if (
skipInstrumentation ||
typeof cmd !== 'object' ||
cmd.ismaster ||
cmd.hello
) {
return original.apply(this, args);
}

Expand Down Expand Up @@ -654,12 +659,8 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
callback?: Function
) {
const currentSpan = trace.getSpan(context.active());

const hasNoParentSpan = currentSpan === undefined;
const requireParentSpan = instrumentation.getConfig().requireParentSpan;
const skipInstrumentation =
requireParentSpan === true && hasNoParentSpan;

instrumentation._checkSkipInstrumentation(currentSpan);

Check warning on line 663 in plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L663 was not covered by tests
const resultHandler =
typeof options === 'function' ? options : callback;
if (
Expand Down Expand Up @@ -725,11 +726,8 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
callback?: Function
) {
const currentSpan = trace.getSpan(context.active());

const hasNoParentSpan = currentSpan === undefined;
const requireParentSpan = instrumentation.getConfig().requireParentSpan;
const skipInstrumentation =
requireParentSpan === true && hasNoParentSpan;
instrumentation._checkSkipInstrumentation(currentSpan);

Check warning on line 730 in plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L730 was not covered by tests

const resultHandler =
typeof options === 'function' ? options : callback;
Expand Down Expand Up @@ -1053,4 +1051,10 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
const poolName = `mongodb://${host}:${port}/${database}`;
this._poolName = poolName;
}

private _checkSkipInstrumentation(currentSpan: Span | undefined) {
const requireParentSpan = this.getConfig().requireParentSpan;
const hasNoParentSpan = currentSpan === undefined;
return requireParentSpan === true && hasNoParentSpan;
}
}

0 comments on commit 5d7f5c1

Please sign in to comment.