Skip to content

Commit 08f3e00

Browse files
committed
fix(tracing): Gate mongo operation span data behind sendDefaultPii
1 parent ba9999f commit 08f3e00

File tree

4 files changed

+33
-19
lines changed
  • dev-packages/node-integration-tests/suites
    • tracing-new/auto-instrument/mongodb
    • tracing/auto-instrument/mongodb
  • packages

4 files changed

+33
-19
lines changed

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ conditionalTest({ min: 12 })('MongoDB Test', () => {
3434
'db.name': 'admin',
3535
'db.operation': 'insertOne',
3636
'db.mongodb.collection': 'movies',
37-
'db.mongodb.doc': '{"title":"Rick and Morty"}',
3837
},
3938
description: 'insertOne',
4039
op: 'db',
@@ -45,7 +44,6 @@ conditionalTest({ min: 12 })('MongoDB Test', () => {
4544
'db.name': 'admin',
4645
'db.operation': 'findOne',
4746
'db.mongodb.collection': 'movies',
48-
'db.mongodb.query': '{"title":"Back to the Future"}',
4947
},
5048
description: 'findOne',
5149
op: 'db',
@@ -56,8 +54,6 @@ conditionalTest({ min: 12 })('MongoDB Test', () => {
5654
'db.name': 'admin',
5755
'db.operation': 'updateOne',
5856
'db.mongodb.collection': 'movies',
59-
'db.mongodb.filter': '{"title":"Back to the Future"}',
60-
'db.mongodb.update': '{"$set":{"title":"South Park"}}',
6157
},
6258
description: 'updateOne',
6359
op: 'db',
@@ -68,7 +64,6 @@ conditionalTest({ min: 12 })('MongoDB Test', () => {
6864
'db.name': 'admin',
6965
'db.operation': 'findOne',
7066
'db.mongodb.collection': 'movies',
71-
'db.mongodb.query': '{"title":"South Park"}',
7267
},
7368
description: 'findOne',
7469
op: 'db',
@@ -79,7 +74,6 @@ conditionalTest({ min: 12 })('MongoDB Test', () => {
7974
'db.name': 'admin',
8075
'db.operation': 'find',
8176
'db.mongodb.collection': 'movies',
82-
'db.mongodb.query': '{"title":"South Park"}',
8377
},
8478
description: 'find',
8579
op: 'db',

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ conditionalTest({ min: 12 })('MongoDB Test', () => {
3434
'db.name': 'admin',
3535
'db.operation': 'insertOne',
3636
'db.mongodb.collection': 'movies',
37-
'db.mongodb.doc': '{"title":"Rick and Morty"}',
3837
},
3938
description: 'insertOne',
4039
op: 'db',
@@ -45,7 +44,6 @@ conditionalTest({ min: 12 })('MongoDB Test', () => {
4544
'db.name': 'admin',
4645
'db.operation': 'findOne',
4746
'db.mongodb.collection': 'movies',
48-
'db.mongodb.query': '{"title":"Back to the Future"}',
4947
},
5048
description: 'findOne',
5149
op: 'db',
@@ -56,8 +54,6 @@ conditionalTest({ min: 12 })('MongoDB Test', () => {
5654
'db.name': 'admin',
5755
'db.operation': 'updateOne',
5856
'db.mongodb.collection': 'movies',
59-
'db.mongodb.filter': '{"title":"Back to the Future"}',
60-
'db.mongodb.update': '{"$set":{"title":"South Park"}}',
6157
},
6258
description: 'updateOne',
6359
op: 'db',
@@ -68,7 +64,6 @@ conditionalTest({ min: 12 })('MongoDB Test', () => {
6864
'db.name': 'admin',
6965
'db.operation': 'findOne',
7066
'db.mongodb.collection': 'movies',
71-
'db.mongodb.query': '{"title":"South Park"}',
7267
},
7368
description: 'findOne',
7469
op: 'db',
@@ -79,7 +74,6 @@ conditionalTest({ min: 12 })('MongoDB Test', () => {
7974
'db.name': 'admin',
8075
'db.operation': 'find',
8176
'db.mongodb.collection': 'movies',
82-
'db.mongodb.query': '{"title":"South Park"}',
8377
},
8478
description: 'find',
8579
op: 'db',

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Hub } from '@sentry/core';
1+
import { Hub, getClient } from '@sentry/core';
22
import type { EventProcessor, SpanContext } from '@sentry/types';
33
import { fill, isThenable, loadModule, logger } from '@sentry/utils';
44

@@ -175,15 +175,21 @@ export class Mongo implements LazyLoadedIntegration<MongoModule> {
175175
return function (this: unknown, ...args: unknown[]) {
176176
const lastArg = args[args.length - 1];
177177
// eslint-disable-next-line deprecation/deprecation
178-
const scope = getCurrentHub().getScope();
178+
const hub = getCurrentHub();
179+
// eslint-disable-next-line deprecation/deprecation
180+
const scope = hub.getScope();
181+
// eslint-disable-next-line deprecation/deprecation
182+
const client = hub.getClient();
179183
// eslint-disable-next-line deprecation/deprecation
180184
const parentSpan = scope.getSpan();
181185

186+
const sendDefaultPii = client?.getOptions().sendDefaultPii;
187+
182188
// Check if the operation was passed a callback. (mapReduce requires a different check, as
183189
// its (non-callback) arguments can also be functions.)
184190
if (typeof lastArg !== 'function' || (operation === 'mapReduce' && args.length === 2)) {
185191
// eslint-disable-next-line deprecation/deprecation
186-
const span = parentSpan?.startChild(getSpanContext(this, operation, args));
192+
const span = parentSpan?.startChild(getSpanContext(this, operation, args, sendDefaultPii));
187193
const maybePromiseOrCursor = orig.call(this, ...args);
188194

189195
if (isThenable(maybePromiseOrCursor)) {
@@ -232,6 +238,7 @@ export class Mongo implements LazyLoadedIntegration<MongoModule> {
232238
collection: MongoCollection,
233239
operation: Operation,
234240
args: unknown[],
241+
sendDefaultPii: boolean | undefined = false,
235242
): SpanContext {
236243
const data: { [key: string]: string } = {
237244
'db.system': 'mongodb',
@@ -254,7 +261,7 @@ export class Mongo implements LazyLoadedIntegration<MongoModule> {
254261
? this._describeOperations.includes(operation)
255262
: this._describeOperations;
256263

257-
if (!signature || !shouldDescribe) {
264+
if (!signature || !shouldDescribe || !sendDefaultPii) {
258265
return spanContext;
259266
}
260267

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,22 @@ describe('patchOperation()', () => {
5050
let scope = new Scope();
5151
let parentSpan: Span;
5252
let childSpan: Span;
53+
let testClient = getTestClient({});
5354

5455
beforeAll(() => {
5556
new Integrations.Mongo({
5657
operations: ['insertOne', 'initializeOrderedBulkOp'],
5758
}).setupOnce(
5859
() => undefined,
59-
() => new Hub(undefined, scope),
60+
() => new Hub(testClient, scope),
6061
);
6162
});
6263

6364
beforeEach(() => {
6465
scope = new Scope();
6566
parentSpan = new Span();
6667
childSpan = parentSpan.startChild();
68+
testClient = getTestClient({});
6769
jest.spyOn(scope, 'getSpan').mockReturnValueOnce(parentSpan);
6870
jest.spyOn(parentSpan, 'startChild').mockReturnValueOnce(childSpan);
6971
jest.spyOn(childSpan, 'end');
@@ -77,7 +79,6 @@ describe('patchOperation()', () => {
7779
'db.mongodb.collection': 'mockedCollectionName',
7880
'db.name': 'mockedDbName',
7981
'db.operation': 'insertOne',
80-
'db.mongodb.doc': JSON.stringify(doc),
8182
'db.system': 'mongodb',
8283
},
8384
op: 'db',
@@ -97,7 +98,25 @@ describe('patchOperation()', () => {
9798
'db.mongodb.collection': 'mockedCollectionName',
9899
'db.name': 'mockedDbName',
99100
'db.operation': 'insertOne',
100-
'db.mongodb.doc': JSON.stringify(doc),
101+
'db.system': 'mongodb',
102+
},
103+
op: 'db',
104+
origin: 'auto.db.mongo',
105+
description: 'insertOne',
106+
});
107+
expect(childSpan.end).toBeCalled();
108+
});
109+
110+
it('attaches mongodb operation spans if sendDefaultPii is enabled', async () => {
111+
testClient.getOptions().sendDefaultPii = true;
112+
await collection.insertOne(doc, {});
113+
expect(scope.getSpan).toBeCalled();
114+
expect(parentSpan.startChild).toBeCalledWith({
115+
data: {
116+
'db.mongodb.collection': 'mockedCollectionName',
117+
'db.mongodb.doc': '{"name":"PickleRick","answer":42}',
118+
'db.name': 'mockedDbName',
119+
'db.operation': 'insertOne',
101120
'db.system': 'mongodb',
102121
},
103122
op: 'db',

0 commit comments

Comments
 (0)