From 7f7a53361a076201bc04980f0cbe81e1cd068bcb Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 9 Aug 2023 12:24:26 -0400 Subject: [PATCH 1/2] feat(tracing): Improve data collection for mongodb spans --- .../tracing/auto-instrument/mongodb/test.ts | 42 +++++++++---------- .../src/node/integrations/mongo.ts | 9 ++-- .../test/integrations/node/mongo.test.ts | 22 +++++----- 3 files changed, 37 insertions(+), 36 deletions(-) diff --git a/packages/node-integration-tests/suites/tracing/auto-instrument/mongodb/test.ts b/packages/node-integration-tests/suites/tracing/auto-instrument/mongodb/test.ts index 8511841c6da8..2d2ec05553e3 100644 --- a/packages/node-integration-tests/suites/tracing/auto-instrument/mongodb/test.ts +++ b/packages/node-integration-tests/suites/tracing/auto-instrument/mongodb/test.ts @@ -30,56 +30,56 @@ conditionalTest({ min: 12 })('MongoDB Test', () => { spans: [ { data: { - collectionName: 'movies', - dbName: 'admin', - namespace: 'admin.movies', - doc: '{"title":"Rick and Morty"}', 'db.system': 'mongodb', + 'db.name': 'admin', + 'db.operation': 'insertOne', + 'db.mongodb.collection': 'movies', + 'db.mongodb.doc': '{"title":"Rick and Morty"}', }, description: 'insertOne', op: 'db', }, { data: { - collectionName: 'movies', - dbName: 'admin', - namespace: 'admin.movies', - query: '{"title":"Back to the Future"}', 'db.system': 'mongodb', + 'db.name': 'admin', + 'db.operation': 'findOne', + 'db.mongodb.collection': 'movies', + 'db.mongodb.query': '{"title":"Back to the Future"}', }, description: 'findOne', op: 'db', }, { data: { - collectionName: 'movies', - dbName: 'admin', - namespace: 'admin.movies', - filter: '{"title":"Back to the Future"}', - update: '{"$set":{"title":"South Park"}}', 'db.system': 'mongodb', + 'db.name': 'admin', + 'db.operation': 'updateOne', + 'db.mongodb.collection': 'movies', + 'db.mongodb.filter': '{"title":"Back to the Future"}', + 'db.mongodb.update': '{"$set":{"title":"South Park"}}', }, description: 'updateOne', op: 'db', }, { data: { - collectionName: 'movies', - dbName: 'admin', - namespace: 'admin.movies', - query: '{"title":"South Park"}', 'db.system': 'mongodb', + 'db.name': 'admin', + 'db.operation': 'findOne', + 'db.mongodb.collection': 'movies', + 'db.mongodb.query': '{"title":"South Park"}', }, description: 'findOne', op: 'db', }, { data: { - collectionName: 'movies', - dbName: 'admin', - namespace: 'admin.movies', - query: '{"title":"South Park"}', 'db.system': 'mongodb', + 'db.name': 'admin', + 'db.operation': 'find', + 'db.mongodb.collection': 'movies', + 'db.mongodb.query': '{"title":"South Park"}', }, description: 'find', op: 'db', diff --git a/packages/tracing-internal/src/node/integrations/mongo.ts b/packages/tracing-internal/src/node/integrations/mongo.ts index 637b7d124b33..c462ea4dcdf2 100644 --- a/packages/tracing-internal/src/node/integrations/mongo.ts +++ b/packages/tracing-internal/src/node/integrations/mongo.ts @@ -229,13 +229,14 @@ export class Mongo implements LazyLoadedIntegration { args: unknown[], ): SpanContext { const data: { [key: string]: string } = { - collectionName: collection.collectionName, - dbName: collection.dbName, - namespace: collection.namespace, 'db.system': 'mongodb', + 'db.name': collection.dbName, + 'db.operation': operation, + 'db.mongodb.collection': collection.collectionName, }; const spanContext: SpanContext = { op: 'db', + // TODO v8: Use `${collection.collectionName}.${operation}` description: operation, data, }; @@ -259,7 +260,7 @@ export class Mongo implements LazyLoadedIntegration { data[signature[1]] = typeof reduce === 'string' ? reduce : reduce.name || ''; } else { for (let i = 0; i < signature.length; i++) { - data[signature[i]] = JSON.stringify(args[i]); + data[`db.mongodb.${signature[i]}`] = JSON.stringify(args[i]); } } } catch (_oO) { diff --git a/packages/tracing/test/integrations/node/mongo.test.ts b/packages/tracing/test/integrations/node/mongo.test.ts index 547b7708b55e..4e866f608cb8 100644 --- a/packages/tracing/test/integrations/node/mongo.test.ts +++ b/packages/tracing/test/integrations/node/mongo.test.ts @@ -74,10 +74,10 @@ describe('patchOperation()', () => { expect(scope.getSpan).toBeCalled(); expect(parentSpan.startChild).toBeCalledWith({ data: { - collectionName: 'mockedCollectionName', - dbName: 'mockedDbName', - doc: JSON.stringify(doc), - namespace: 'mockedNamespace', + 'db.mongodb.collection': 'mockedCollectionName', + 'db.name': 'mockedDbName', + 'db.operation': 'insertOne', + 'db.mongodb.doc': JSON.stringify(doc), 'db.system': 'mongodb', }, op: 'db', @@ -93,10 +93,10 @@ describe('patchOperation()', () => { expect(scope.getSpan).toBeCalled(); expect(parentSpan.startChild).toBeCalledWith({ data: { - collectionName: 'mockedCollectionName', - dbName: 'mockedDbName', - doc: JSON.stringify(doc), - namespace: 'mockedNamespace', + 'db.mongodb.collection': 'mockedCollectionName', + 'db.name': 'mockedDbName', + 'db.operation': 'insertOne', + 'db.mongodb.doc': JSON.stringify(doc), 'db.system': 'mongodb', }, op: 'db', @@ -110,9 +110,9 @@ describe('patchOperation()', () => { expect(scope.getSpan).toBeCalled(); expect(parentSpan.startChild).toBeCalledWith({ data: { - collectionName: 'mockedCollectionName', - dbName: 'mockedDbName', - namespace: 'mockedNamespace', + 'db.mongodb.collection': 'mockedCollectionName', + 'db.name': 'mockedDbName', + 'db.operation': 'initializeOrderedBulkOp', 'db.system': 'mongodb', }, op: 'db', From 9d07c2545abe23b42ddc9f7c7113d811fc5d49c5 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 9 Aug 2023 13:09:14 -0400 Subject: [PATCH 2/2] fix new tracing tests --- .../auto-instrument/mongodb/test.ts | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/packages/node-integration-tests/suites/tracing-new/auto-instrument/mongodb/test.ts b/packages/node-integration-tests/suites/tracing-new/auto-instrument/mongodb/test.ts index 8511841c6da8..2d2ec05553e3 100644 --- a/packages/node-integration-tests/suites/tracing-new/auto-instrument/mongodb/test.ts +++ b/packages/node-integration-tests/suites/tracing-new/auto-instrument/mongodb/test.ts @@ -30,56 +30,56 @@ conditionalTest({ min: 12 })('MongoDB Test', () => { spans: [ { data: { - collectionName: 'movies', - dbName: 'admin', - namespace: 'admin.movies', - doc: '{"title":"Rick and Morty"}', 'db.system': 'mongodb', + 'db.name': 'admin', + 'db.operation': 'insertOne', + 'db.mongodb.collection': 'movies', + 'db.mongodb.doc': '{"title":"Rick and Morty"}', }, description: 'insertOne', op: 'db', }, { data: { - collectionName: 'movies', - dbName: 'admin', - namespace: 'admin.movies', - query: '{"title":"Back to the Future"}', 'db.system': 'mongodb', + 'db.name': 'admin', + 'db.operation': 'findOne', + 'db.mongodb.collection': 'movies', + 'db.mongodb.query': '{"title":"Back to the Future"}', }, description: 'findOne', op: 'db', }, { data: { - collectionName: 'movies', - dbName: 'admin', - namespace: 'admin.movies', - filter: '{"title":"Back to the Future"}', - update: '{"$set":{"title":"South Park"}}', 'db.system': 'mongodb', + 'db.name': 'admin', + 'db.operation': 'updateOne', + 'db.mongodb.collection': 'movies', + 'db.mongodb.filter': '{"title":"Back to the Future"}', + 'db.mongodb.update': '{"$set":{"title":"South Park"}}', }, description: 'updateOne', op: 'db', }, { data: { - collectionName: 'movies', - dbName: 'admin', - namespace: 'admin.movies', - query: '{"title":"South Park"}', 'db.system': 'mongodb', + 'db.name': 'admin', + 'db.operation': 'findOne', + 'db.mongodb.collection': 'movies', + 'db.mongodb.query': '{"title":"South Park"}', }, description: 'findOne', op: 'db', }, { data: { - collectionName: 'movies', - dbName: 'admin', - namespace: 'admin.movies', - query: '{"title":"South Park"}', 'db.system': 'mongodb', + 'db.name': 'admin', + 'db.operation': 'find', + 'db.mongodb.collection': 'movies', + 'db.mongodb.query': '{"title":"South Park"}', }, description: 'find', op: 'db',