Skip to content

Commit fdd17e7

Browse files
authored
feat(tracing): Improve data collection for mongodb spans (#8774)
Align mongodb spans with https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/mongodb.md needed for starfish views.
1 parent 0bade5d commit fdd17e7

File tree

4 files changed

+58
-57
lines changed
  • packages
    • node-integration-tests/suites
      • tracing/auto-instrument/mongodb
      • tracing-new/auto-instrument/mongodb
    • tracing/test/integrations/node
    • tracing-internal/src/node/integrations

4 files changed

+58
-57
lines changed

packages/node-integration-tests/suites/tracing-new/auto-instrument/mongodb/test.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,56 +30,56 @@ conditionalTest({ min: 12 })('MongoDB Test', () => {
3030
spans: [
3131
{
3232
data: {
33-
collectionName: 'movies',
34-
dbName: 'admin',
35-
namespace: 'admin.movies',
36-
doc: '{"title":"Rick and Morty"}',
3733
'db.system': 'mongodb',
34+
'db.name': 'admin',
35+
'db.operation': 'insertOne',
36+
'db.mongodb.collection': 'movies',
37+
'db.mongodb.doc': '{"title":"Rick and Morty"}',
3838
},
3939
description: 'insertOne',
4040
op: 'db',
4141
},
4242
{
4343
data: {
44-
collectionName: 'movies',
45-
dbName: 'admin',
46-
namespace: 'admin.movies',
47-
query: '{"title":"Back to the Future"}',
4844
'db.system': 'mongodb',
45+
'db.name': 'admin',
46+
'db.operation': 'findOne',
47+
'db.mongodb.collection': 'movies',
48+
'db.mongodb.query': '{"title":"Back to the Future"}',
4949
},
5050
description: 'findOne',
5151
op: 'db',
5252
},
5353
{
5454
data: {
55-
collectionName: 'movies',
56-
dbName: 'admin',
57-
namespace: 'admin.movies',
58-
filter: '{"title":"Back to the Future"}',
59-
update: '{"$set":{"title":"South Park"}}',
6055
'db.system': 'mongodb',
56+
'db.name': 'admin',
57+
'db.operation': 'updateOne',
58+
'db.mongodb.collection': 'movies',
59+
'db.mongodb.filter': '{"title":"Back to the Future"}',
60+
'db.mongodb.update': '{"$set":{"title":"South Park"}}',
6161
},
6262
description: 'updateOne',
6363
op: 'db',
6464
},
6565
{
6666
data: {
67-
collectionName: 'movies',
68-
dbName: 'admin',
69-
namespace: 'admin.movies',
70-
query: '{"title":"South Park"}',
7167
'db.system': 'mongodb',
68+
'db.name': 'admin',
69+
'db.operation': 'findOne',
70+
'db.mongodb.collection': 'movies',
71+
'db.mongodb.query': '{"title":"South Park"}',
7272
},
7373
description: 'findOne',
7474
op: 'db',
7575
},
7676
{
7777
data: {
78-
collectionName: 'movies',
79-
dbName: 'admin',
80-
namespace: 'admin.movies',
81-
query: '{"title":"South Park"}',
8278
'db.system': 'mongodb',
79+
'db.name': 'admin',
80+
'db.operation': 'find',
81+
'db.mongodb.collection': 'movies',
82+
'db.mongodb.query': '{"title":"South Park"}',
8383
},
8484
description: 'find',
8585
op: 'db',

packages/node-integration-tests/suites/tracing/auto-instrument/mongodb/test.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,56 +30,56 @@ conditionalTest({ min: 12 })('MongoDB Test', () => {
3030
spans: [
3131
{
3232
data: {
33-
collectionName: 'movies',
34-
dbName: 'admin',
35-
namespace: 'admin.movies',
36-
doc: '{"title":"Rick and Morty"}',
3733
'db.system': 'mongodb',
34+
'db.name': 'admin',
35+
'db.operation': 'insertOne',
36+
'db.mongodb.collection': 'movies',
37+
'db.mongodb.doc': '{"title":"Rick and Morty"}',
3838
},
3939
description: 'insertOne',
4040
op: 'db',
4141
},
4242
{
4343
data: {
44-
collectionName: 'movies',
45-
dbName: 'admin',
46-
namespace: 'admin.movies',
47-
query: '{"title":"Back to the Future"}',
4844
'db.system': 'mongodb',
45+
'db.name': 'admin',
46+
'db.operation': 'findOne',
47+
'db.mongodb.collection': 'movies',
48+
'db.mongodb.query': '{"title":"Back to the Future"}',
4949
},
5050
description: 'findOne',
5151
op: 'db',
5252
},
5353
{
5454
data: {
55-
collectionName: 'movies',
56-
dbName: 'admin',
57-
namespace: 'admin.movies',
58-
filter: '{"title":"Back to the Future"}',
59-
update: '{"$set":{"title":"South Park"}}',
6055
'db.system': 'mongodb',
56+
'db.name': 'admin',
57+
'db.operation': 'updateOne',
58+
'db.mongodb.collection': 'movies',
59+
'db.mongodb.filter': '{"title":"Back to the Future"}',
60+
'db.mongodb.update': '{"$set":{"title":"South Park"}}',
6161
},
6262
description: 'updateOne',
6363
op: 'db',
6464
},
6565
{
6666
data: {
67-
collectionName: 'movies',
68-
dbName: 'admin',
69-
namespace: 'admin.movies',
70-
query: '{"title":"South Park"}',
7167
'db.system': 'mongodb',
68+
'db.name': 'admin',
69+
'db.operation': 'findOne',
70+
'db.mongodb.collection': 'movies',
71+
'db.mongodb.query': '{"title":"South Park"}',
7272
},
7373
description: 'findOne',
7474
op: 'db',
7575
},
7676
{
7777
data: {
78-
collectionName: 'movies',
79-
dbName: 'admin',
80-
namespace: 'admin.movies',
81-
query: '{"title":"South Park"}',
8278
'db.system': 'mongodb',
79+
'db.name': 'admin',
80+
'db.operation': 'find',
81+
'db.mongodb.collection': 'movies',
82+
'db.mongodb.query': '{"title":"South Park"}',
8383
},
8484
description: 'find',
8585
op: 'db',

packages/tracing-internal/src/node/integrations/mongo.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,14 @@ export class Mongo implements LazyLoadedIntegration<MongoModule> {
229229
args: unknown[],
230230
): SpanContext {
231231
const data: { [key: string]: string } = {
232-
collectionName: collection.collectionName,
233-
dbName: collection.dbName,
234-
namespace: collection.namespace,
235232
'db.system': 'mongodb',
233+
'db.name': collection.dbName,
234+
'db.operation': operation,
235+
'db.mongodb.collection': collection.collectionName,
236236
};
237237
const spanContext: SpanContext = {
238238
op: 'db',
239+
// TODO v8: Use `${collection.collectionName}.${operation}`
239240
description: operation,
240241
data,
241242
};
@@ -259,7 +260,7 @@ export class Mongo implements LazyLoadedIntegration<MongoModule> {
259260
data[signature[1]] = typeof reduce === 'string' ? reduce : reduce.name || '<anonymous>';
260261
} else {
261262
for (let i = 0; i < signature.length; i++) {
262-
data[signature[i]] = JSON.stringify(args[i]);
263+
data[`db.mongodb.${signature[i]}`] = JSON.stringify(args[i]);
263264
}
264265
}
265266
} catch (_oO) {

packages/tracing/test/integrations/node/mongo.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ describe('patchOperation()', () => {
7474
expect(scope.getSpan).toBeCalled();
7575
expect(parentSpan.startChild).toBeCalledWith({
7676
data: {
77-
collectionName: 'mockedCollectionName',
78-
dbName: 'mockedDbName',
79-
doc: JSON.stringify(doc),
80-
namespace: 'mockedNamespace',
77+
'db.mongodb.collection': 'mockedCollectionName',
78+
'db.name': 'mockedDbName',
79+
'db.operation': 'insertOne',
80+
'db.mongodb.doc': JSON.stringify(doc),
8181
'db.system': 'mongodb',
8282
},
8383
op: 'db',
@@ -93,10 +93,10 @@ describe('patchOperation()', () => {
9393
expect(scope.getSpan).toBeCalled();
9494
expect(parentSpan.startChild).toBeCalledWith({
9595
data: {
96-
collectionName: 'mockedCollectionName',
97-
dbName: 'mockedDbName',
98-
doc: JSON.stringify(doc),
99-
namespace: 'mockedNamespace',
96+
'db.mongodb.collection': 'mockedCollectionName',
97+
'db.name': 'mockedDbName',
98+
'db.operation': 'insertOne',
99+
'db.mongodb.doc': JSON.stringify(doc),
100100
'db.system': 'mongodb',
101101
},
102102
op: 'db',
@@ -110,9 +110,9 @@ describe('patchOperation()', () => {
110110
expect(scope.getSpan).toBeCalled();
111111
expect(parentSpan.startChild).toBeCalledWith({
112112
data: {
113-
collectionName: 'mockedCollectionName',
114-
dbName: 'mockedDbName',
115-
namespace: 'mockedNamespace',
113+
'db.mongodb.collection': 'mockedCollectionName',
114+
'db.name': 'mockedDbName',
115+
'db.operation': 'initializeOrderedBulkOp',
116116
'db.system': 'mongodb',
117117
},
118118
op: 'db',

0 commit comments

Comments
 (0)