Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ async function run() {
await collection.findOne({ title: 'South Park' });

await collection.find({ title: 'South Park' }).toArray();

// Issue a query the server rejects to exercise the error-status span path.
await collection
.find({ $thisOperatorDoesNotExist: 1 })
.toArray()
.catch(() => {});
} finally {
await client.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ describe('MongoDB auto-instrumentation', () => {
origin: 'auto.db.otel.mongo',
});

// A query the server rejects: same attributes as a successful find, but with an error status.
const SPAN_FIND_ERROR_MATCHER = expect.objectContaining({
data: expect.objectContaining({
'sentry.origin': 'auto.db.otel.mongo',
'sentry.op': 'db',
'db.system': 'mongodb',
'db.operation': 'find',
'db.statement': '{"$thisOperatorDoesNotExist":"?"}',
'otel.kind': 'CLIENT',
}),
description: '{"$thisOperatorDoesNotExist":"?"}',
op: 'db',
origin: 'auto.db.otel.mongo',
status: 'internal_error',
});

const SPAN_ENDSESSIONS_MATCHER = expect.objectContaining({
data: {
'sentry.origin': 'auto.db.otel.mongo',
Expand Down Expand Up @@ -147,7 +163,7 @@ describe('MongoDB auto-instrumentation', () => {
}, {});

expect(operationCounts).toEqual({
find: 3,
find: 4,
isMaster: 2,
insert: 1,
update: 1,
Expand All @@ -158,6 +174,7 @@ describe('MongoDB auto-instrumentation', () => {
expect(spans).toContainEqual(SPAN_INSERT_MATCHER);
expect(spans).toContainEqual(SPAN_ISMASTER_MATCHER);
expect(spans).toContainEqual(SPAN_UPDATE_MATCHER);
expect(spans).toContainEqual(SPAN_FIND_ERROR_MATCHER);
expect(spans).toContainEqual(SPAN_ENDSESSIONS_MATCHER);
},
})
Expand Down
58 changes: 2 additions & 56 deletions packages/node/src/integrations/tracing/mongo/index.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,11 @@
import { MongoDBInstrumentation } from './vendored/instrumentation';
import type { IntegrationFn } from '@sentry/core';
import { defineIntegration } from '@sentry/core';
import { addOriginToSpan, generateInstrumentOnce } from '@sentry/node-core';
import { generateInstrumentOnce } from '@sentry/node-core';

const INTEGRATION_NAME = 'Mongo';

export const instrumentMongo = generateInstrumentOnce(
INTEGRATION_NAME,
() =>
new MongoDBInstrumentation({
dbStatementSerializer: _defaultDbStatementSerializer,
responseHook(span) {
addOriginToSpan(span, 'auto.db.otel.mongo');
},
}),
);

/**
* Replaces values in document with '?', hiding PII and helping grouping.
*/
export function _defaultDbStatementSerializer(commandObj: Record<string, unknown>): string {
const resultObj = _scrubStatement(commandObj);
return JSON.stringify(resultObj);
}

function _scrubStatement(value: unknown): unknown {
if (Array.isArray(value)) {
return value.map(element => _scrubStatement(element));
}

if (isCommandObj(value)) {
const initial: Record<string, unknown> = {};
return Object.entries(value)
.map(([key, element]) => [key, _scrubStatement(element)])
.reduce((prev, current) => {
if (isCommandEntry(current)) {
prev[current[0]] = current[1];
}
return prev;
}, initial);
}

// A value like string or number, possible contains PII, scrub it
return '?';
}

function isCommandObj(value: Record<string, unknown> | unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !isBuffer(value);
}

function isBuffer(value: unknown): boolean {
let isBuffer = false;
if (typeof Buffer !== 'undefined') {
isBuffer = Buffer.isBuffer(value);
}
return isBuffer;
}

function isCommandEntry(value: [string, unknown] | unknown): value is [string, unknown] {
return Array.isArray(value);
}
export const instrumentMongo = generateInstrumentOnce(INTEGRATION_NAME, () => new MongoDBInstrumentation());

const _mongoIntegration = (() => {
return {
Expand Down
Loading
Loading