Skip to content

Commit

Permalink
feat(tracing): Improve data collection for mongodb spans (#8774)
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiPrasad committed Aug 10, 2023
1 parent 0bade5d commit fdd17e7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
9 changes: 5 additions & 4 deletions packages/tracing-internal/src/node/integrations/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,14 @@ export class Mongo implements LazyLoadedIntegration<MongoModule> {
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,
};
Expand All @@ -259,7 +260,7 @@ export class Mongo implements LazyLoadedIntegration<MongoModule> {
data[signature[1]] = typeof reduce === 'string' ? reduce : reduce.name || '<anonymous>';
} 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) {
Expand Down
22 changes: 11 additions & 11 deletions packages/tracing/test/integrations/node/mongo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down

0 comments on commit fdd17e7

Please sign in to comment.