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(mongodb): add db.operation span attribute #1321

Merged
merged 8 commits into from
Dec 11, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ Setup [Jaeger Tracing](https://www.jaegertracing.io/docs/latest/getting-started/
npm run zipkin:server
```

- Run the zipkin:client
- Run the client

```sh
# from this directory
npm run client
npm run zipkin:client
```

#### Zipkin UI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ export class MongoDBInstrumentation extends InstrumentationBase {
ns,
server,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ops[0] as any
ops[0] as any,
operationName
);
const patchedCallback = instrumentation._patchEnd(span, resultHandler);
// handle when options is the callback to send the correct number of args
Expand Down Expand Up @@ -255,7 +256,9 @@ export class MongoDBInstrumentation extends InstrumentationBase {
const span = instrumentation.tracer.startSpan(`mongodb.${type}`, {
kind: SpanKind.CLIENT,
});
instrumentation._populateV3Attributes(span, ns, server, cmd);
const operation =
commandType === MongodbCommandType.UNKNOWN ? undefined : commandType;
instrumentation._populateV3Attributes(span, ns, server, cmd, operation);
const patchedCallback = instrumentation._patchEnd(span, resultHandler);
// handle when options is the callback to send the correct number of args
if (typeof options === 'function') {
Expand Down Expand Up @@ -296,7 +299,7 @@ export class MongoDBInstrumentation extends InstrumentationBase {
kind: SpanKind.CLIENT,
}
);
instrumentation._populateV4Attributes(span, this, ns, cmd);
instrumentation._populateV4Attributes(span, this, ns, cmd, commandType);
const patchedCallback = instrumentation._patchEnd(span, resultHandler);
return original.call(this, ns, cmd, options, patchedCallback);
};
Expand Down Expand Up @@ -341,7 +344,7 @@ export class MongoDBInstrumentation extends InstrumentationBase {
const span = instrumentation.tracer.startSpan('mongodb.find', {
kind: SpanKind.CLIENT,
});
instrumentation._populateV3Attributes(span, ns, server, cmd);
instrumentation._populateV3Attributes(span, ns, server, cmd, 'find');
const patchedCallback = instrumentation._patchEnd(span, resultHandler);
// handle when options is the callback to send the correct number of args
if (typeof options === 'function') {
Expand Down Expand Up @@ -413,7 +416,8 @@ export class MongoDBInstrumentation extends InstrumentationBase {
span,
ns,
server,
cursorState.cmd
cursorState.cmd,
'getMore'
);
const patchedCallback = instrumentation._patchEnd(span, resultHandler);
// handle when options is the callback to send the correct number of args
Expand Down Expand Up @@ -472,7 +476,8 @@ export class MongoDBInstrumentation extends InstrumentationBase {
span: Span,
connectionCtx: any,
ns: any,
command?: any
command?: any,
operation?: string
) {
let host, port: undefined | string;
if (connectionCtx) {
Expand Down Expand Up @@ -501,7 +506,8 @@ export class MongoDBInstrumentation extends InstrumentationBase {
ns.collection,
host,
port,
commandObj
commandObj,
operation
);
}

Expand All @@ -516,7 +522,8 @@ export class MongoDBInstrumentation extends InstrumentationBase {
span: Span,
ns: string,
topology: MongoInternalTopology,
command?: MongoInternalCommand
command?: MongoInternalCommand,
operation?: string | undefined
) {
// add network attributes to determine the remote server
let host: undefined | string;
Expand Down Expand Up @@ -548,7 +555,8 @@ export class MongoDBInstrumentation extends InstrumentationBase {
dbCollection,
host,
port,
commandObj
commandObj,
operation
);
}

Expand All @@ -558,13 +566,15 @@ export class MongoDBInstrumentation extends InstrumentationBase {
dbCollection?: string,
host?: undefined | string,
port?: undefined | string,
commandObj?: any
commandObj?: any,
operation?: string | undefined
) {
// add database related attributes
span.setAttributes({
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.MONGODB,
[SemanticAttributes.DB_NAME]: dbName,
[SemanticAttributes.DB_MONGODB_COLLECTION]: dbCollection,
[SemanticAttributes.DB_OPERATION]: operation,
});

if (host && port) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ describe('MongoDBInstrumentation', () => {
.insertMany(insertData)
.then(() => {
span.end();
assertSpans(getTestSpans(), 'mongodb.insert', SpanKind.CLIENT);
assertSpans(
getTestSpans(),
'mongodb.insert',
SpanKind.CLIENT,
'insert'
);
done();
})
.catch(err => {
Expand All @@ -128,7 +133,12 @@ describe('MongoDBInstrumentation', () => {
.updateOne({ a: 2 }, { $set: { b: 1 } })
.then(() => {
span.end();
assertSpans(getTestSpans(), 'mongodb.update', SpanKind.CLIENT);
assertSpans(
getTestSpans(),
'mongodb.update',
SpanKind.CLIENT,
'update'
);
done();
})
.catch(err => {
Expand All @@ -144,7 +154,12 @@ describe('MongoDBInstrumentation', () => {
.deleteOne({ a: 3 })
.then(() => {
span.end();
assertSpans(getTestSpans(), 'mongodb.remove', SpanKind.CLIENT);
assertSpans(
getTestSpans(),
'mongodb.remove',
SpanKind.CLIENT,
'remove'
);
done();
})
.catch(err => {
Expand All @@ -164,7 +179,12 @@ describe('MongoDBInstrumentation', () => {
.toArray()
.then(() => {
span.end();
assertSpans(getTestSpans(), 'mongodb.find', SpanKind.CLIENT);
assertSpans(
getTestSpans(),
'mongodb.find',
SpanKind.CLIENT,
'find'
);
done();
})
.catch(err => {
Expand All @@ -190,15 +210,17 @@ describe('MongoDBInstrumentation', () => {
span => !span.name.includes('mongodb.getMore')
),
'mongodb.find',
SpanKind.CLIENT
SpanKind.CLIENT,
'find'
);
// assert that we correctly got the first as a find
assertSpans(
getTestSpans().filter(
span => !span.name.includes('mongodb.find')
),
'mongodb.getMore',
SpanKind.CLIENT
SpanKind.CLIENT,
'getMore'
);
done();
})
Expand All @@ -222,7 +244,8 @@ describe('MongoDBInstrumentation', () => {
assertSpans(
getTestSpans(),
'mongodb.createIndexes',
SpanKind.CLIENT
SpanKind.CLIENT,
'createIndexes'
);
done();
})
Expand Down Expand Up @@ -253,7 +276,14 @@ describe('MongoDBInstrumentation', () => {
span.end();
const spans = getTestSpans();
const operationName = 'mongodb.insert';
assertSpans(spans, operationName, SpanKind.CLIENT, false, false);
assertSpans(
spans,
operationName,
SpanKind.CLIENT,
'insert',
false,
false
);
const mongoSpan = spans.find(s => s.name === operationName);
const dbStatement = JSON.parse(
mongoSpan!.attributes[SemanticAttributes.DB_STATEMENT] as string
Expand Down Expand Up @@ -291,7 +321,14 @@ describe('MongoDBInstrumentation', () => {
span.end();
const spans = getTestSpans();
const operationName = 'mongodb.insert';
assertSpans(spans, operationName, SpanKind.CLIENT, false, true);
assertSpans(
spans,
operationName,
SpanKind.CLIENT,
'insert',
false,
true
);
const mongoSpan = spans.find(s => s.name === operationName);
const dbStatement = JSON.parse(
mongoSpan!.attributes[SemanticAttributes.DB_STATEMENT] as string
Expand Down Expand Up @@ -324,7 +361,7 @@ describe('MongoDBInstrumentation', () => {
.then(() => {
span.end();
const spans = getTestSpans();
assertSpans(spans, 'mongodb.insert', SpanKind.CLIENT);
assertSpans(spans, 'mongodb.insert', SpanKind.CLIENT, 'insert');
done();
})
.catch(err => {
Expand Down Expand Up @@ -421,7 +458,7 @@ describe('MongoDBInstrumentation', () => {
.then(() => {
span.end();
const spans = getTestSpans();
assertSpans(spans, 'mongodb.find', SpanKind.CLIENT);
assertSpans(spans, 'mongodb.find', SpanKind.CLIENT, 'find');
done();
})
.catch(err => {
Expand All @@ -443,7 +480,7 @@ describe('MongoDBInstrumentation', () => {
span.end();
const spans = getTestSpans();
const mainSpan = spans[spans.length - 1];
assertSpans(spans, 'mongodb.insert', SpanKind.CLIENT);
assertSpans(spans, 'mongodb.insert', SpanKind.CLIENT, 'insert');
resetMemoryExporter();

collection
Expand All @@ -453,7 +490,7 @@ describe('MongoDBInstrumentation', () => {
const spans2 = getTestSpans();
spans2.push(mainSpan);

assertSpans(spans2, 'mongodb.find', SpanKind.CLIENT);
assertSpans(spans2, 'mongodb.find', SpanKind.CLIENT, 'find');
assert.strictEqual(
mainSpan.spanContext().spanId,
spans2[0].parentSpanId
Expand Down
Loading