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 89f7318
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 48 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 ||
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 @@ -555,8 +558,6 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
const commandType = Object.keys(cmd)[0];

if (
skipInstrumentation ||
typeof resultHandler !== 'function' ||
typeof cmd !== 'object' ||
cmd.ismaster ||
cmd.hello
Expand All @@ -565,7 +566,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
}

let span = undefined;
if (currentSpan) {
if (!skipInstrumentation) {
span = instrumentation.tracer.startSpan(`mongodb.${commandType}`, {
kind: SpanKind.CLIENT,
});
Expand Down Expand Up @@ -605,7 +606,6 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
const resultHandler = () => undefined;

if (
skipInstrumentation ||
typeof cmd !== 'object' ||
cmd.ismaster ||
cmd.hello
Expand All @@ -614,7 +614,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
}

let span = undefined;
if (currentSpan) {
if (!skipInstrumentation) {
span = instrumentation.tracer.startSpan(`mongodb.${commandType}`, {
kind: SpanKind.CLIENT,
});
Expand Down Expand Up @@ -663,6 +663,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
instrumentation._checkSkipInstrumentation(currentSpan);
const resultHandler =
typeof options === 'function' ? options : callback;

if (
skipInstrumentation ||
typeof resultHandler !== 'function' ||
Expand All @@ -682,6 +683,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
);
}
}

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

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

if (skipInstrumentation || typeof resultHandler !== 'function') {
if (typeof options === 'function') {
return original.call(
Expand All @@ -753,6 +756,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 89f7318

Please sign in to comment.