Skip to content

Commit

Permalink
Update command span creation behaviour.
Browse files Browse the repository at this point in the history
  • Loading branch information
onurtemizkan committed Jan 23, 2025
1 parent 5d7f5c1 commit 7cba82a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
return original.call(this, server, ns, ops, options, callback);
}
}

const span = instrumentation.tracer.startSpan(
`mongodb.${operationName}`,
{
Expand Down Expand Up @@ -506,6 +507,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen

const resultHandler =
typeof options === 'function' ? options : callback;

if (
skipInstrumentation ||

Check warning on line 512 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#L512

Added line #L512 was not covered by tests
typeof resultHandler !== 'function' ||
Expand All @@ -517,6 +519,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
return original.call(this, server, ns, cmd, options, callback);
}
}

const commandType = MongoDBInstrumentation._getCommandType(cmd);
const type =
commandType === MongodbCommandType.UNKNOWN ? 'command' : commandType;
Expand Down Expand Up @@ -554,18 +557,12 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
const resultHandler = callback;
const commandType = Object.keys(cmd)[0];

if (
skipInstrumentation ||
typeof resultHandler !== 'function' ||
typeof cmd !== 'object' ||
cmd.ismaster ||
cmd.hello
) {
if (typeof cmd !== 'object' || cmd.ismaster || cmd.hello) {

Check warning on line 560 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#L560

Added line #L560 was not covered by tests
return original.call(this, ns, cmd, options, callback);
}

let span = undefined;
if (currentSpan) {
if (!skipInstrumentation) {

Check warning on line 565 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#L565

Added line #L565 was not covered by tests
span = instrumentation.tracer.startSpan(`mongodb.${commandType}`, {
kind: SpanKind.CLIENT,
});
Expand Down Expand Up @@ -604,17 +601,12 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
const commandType = Object.keys(cmd)[0];
const resultHandler = () => 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,
});
Expand Down Expand Up @@ -663,6 +655,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
instrumentation._checkSkipInstrumentation(currentSpan);

Check warning on line 655 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#L655

Added line #L655 was not covered by tests
const resultHandler =
typeof options === 'function' ? options : callback;

if (
skipInstrumentation ||

Check warning on line 660 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#L660

Added line #L660 was not covered by tests
typeof resultHandler !== 'function' ||
Expand All @@ -682,6 +675,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
);
}
}

const span = instrumentation.tracer.startSpan('mongodb.find', {
kind: SpanKind.CLIENT,
});
Expand Down Expand Up @@ -731,6 +725,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen

const resultHandler =
typeof options === 'function' ? options : callback;

if (skipInstrumentation || typeof resultHandler !== 'function') {

Check warning on line 729 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#L729

Added line #L729 was not covered by tests
if (typeof options === 'function') {
return original.call(
Expand All @@ -753,6 +748,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
);
}
}

const span = instrumentation.tracer.startSpan('mongodb.getMore', {
kind: SpanKind.CLIENT,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -654,60 +654,40 @@ describe('MongoDBInstrumentation-Tracing-v5', () => {

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);
});
});
});
});

Expand All @@ -731,6 +711,9 @@ describe('MongoDBInstrumentation-Tracing-v5', () => {
})
.catch(err => {
done(err);
})
.finally(() => {
span.end();
});
});

Expand Down

0 comments on commit 7cba82a

Please sign in to comment.