Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(instrumentation-mongodb): Add requireParentSpan config option. #2658

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Mongodb instrumentation has few options available to choose from. You can set th
| [`enhancedDatabaseReporting`](./src/types.ts#L32) | `string` | If true, additional information about query parameters and results will be attached (as `attributes`) to spans representing database operations |
| `responseHook` | `MongoDBInstrumentationExecutionResponseHook` (function) | Function for adding custom attributes from db response |
| `dbStatementSerializer` | `DbStatementSerializer` (function) | Custom serializer function for the db.statement tag |
| `requireParentSpan` | `boolean` | Require a parent span in order to create mongodb spans, default when unset is `true` |

## Semantic Conventions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,21 @@
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
import { UpDownCounter } from '@opentelemetry/api';

const DEFAULT_CONFIG: MongoDBInstrumentationConfig = {
requireParentSpan: true,
};

/** mongodb instrumentation plugin for OpenTelemetry */
export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumentationConfig> {
private _connectionsUsage!: UpDownCounter;
private _poolName!: string;

constructor(config: MongoDBInstrumentationConfig = {}) {
super(PACKAGE_NAME, PACKAGE_VERSION, config);
super(PACKAGE_NAME, PACKAGE_VERSION, { ...DEFAULT_CONFIG, ...config });
}

override setConfig(config: MongoDBInstrumentationConfig = {}) {
super.setConfig({ ...DEFAULT_CONFIG, ...config });
}

override _updateMetricInstruments() {
Expand Down Expand Up @@ -438,10 +446,16 @@
callback?: Function
) {
const currentSpan = trace.getSpan(context.active());

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

Check warning on line 451 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-L451

Added lines #L450 - L451 were not covered by tests
const skipInstrumentation =
requireParentSpan === true && hasNoParentSpan;

Check warning on line 453 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#L453

Added line #L453 was not covered by tests

const resultHandler =
typeof options === 'function' ? options : callback;
if (
!currentSpan ||
skipInstrumentation ||

Check warning on line 458 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#L458

Added line #L458 was not covered by tests
typeof resultHandler !== 'function' ||
typeof ops !== 'object'
) {
Expand Down Expand Up @@ -490,10 +504,16 @@
callback?: Function
) {
const currentSpan = trace.getSpan(context.active());

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

Check warning on line 509 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#L508-L509

Added lines #L508 - L509 were not covered by tests
const skipInstrumentation =
requireParentSpan === true && hasNoParentSpan;

Check warning on line 511 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#L511

Added line #L511 was not covered by tests

const resultHandler =
typeof options === 'function' ? options : callback;
if (
!currentSpan ||
skipInstrumentation ||

Check warning on line 516 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#L516

Added line #L516 was not covered by tests
typeof resultHandler !== 'function' ||
typeof cmd !== 'object'
) {
Expand Down Expand Up @@ -634,10 +654,16 @@
callback?: Function
) {
const currentSpan = trace.getSpan(context.active());

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

Check warning on line 659 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#L658-L659

Added lines #L658 - L659 were not covered by tests
const skipInstrumentation =
requireParentSpan === true && hasNoParentSpan;

Check warning on line 661 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#L661

Added line #L661 was not covered by tests

const resultHandler =
typeof options === 'function' ? options : callback;
if (
!currentSpan ||
skipInstrumentation ||

Check warning on line 666 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#L666

Added line #L666 was not covered by tests
typeof resultHandler !== 'function' ||
typeof cmd !== 'object'
) {
Expand Down Expand Up @@ -699,9 +725,15 @@
callback?: Function
) {
const currentSpan = trace.getSpan(context.active());

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

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#L729-L730

Added lines #L729 - L730 were not covered by tests
const skipInstrumentation =
requireParentSpan === true && hasNoParentSpan;

Check warning on line 732 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#L732

Added line #L732 was not covered by tests

const resultHandler =
typeof options === 'function' ? options : callback;
if (!currentSpan || typeof resultHandler !== 'function') {
if (skipInstrumentation || typeof resultHandler !== 'function') {

Check warning on line 736 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#L736

Added line #L736 was not covered by tests
if (typeof options === 'function') {
return original.call(
this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export interface MongoDBInstrumentationConfig extends InstrumentationConfig {
* Custom serializer function for the db.statement tag
*/
dbStatementSerializer?: DbStatementSerializer;

/**
* Require parent to create mongodb span, default when unset is true
*/
requireParentSpan?: boolean;
}

export interface MongoResponseHookInformation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,65 @@ describe('MongoDBInstrumentation-Tracing-v5', () => {
});
});

describe('requireParentSpan', () => {
// Resetting the behavior to default to avoid flakes in other tests
afterEach(() => {
instrumentation.setConfig({
requireParentSpan: true,
});
});

it('should not create spans without parent span when requireParentSpan is explicitly set to true', done => {
create({
requireParentSpan: true,
});

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

/** Should intercept command */
describe('Removing Instrumentation', () => {
it('should unpatch plugin', () => {
Expand Down
Loading