Skip to content

fix(tracing): Instrument cursors returned from MongoDB operations. #6337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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 @@ -36,6 +36,8 @@ async function run(): Promise<void> {
await collection.findOne({ title: 'Back to the Future' });
await collection.updateOne({ title: 'Back to the Future' }, { $set: { title: 'South Park' } });
await collection.findOne({ title: 'South Park' });

await collection.find({ title: 'South Park' }).toArray();
} finally {
if (transaction) transaction.finish();
await client.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,6 @@ conditionalTest({ min: 12 })('MongoDB Test', () => {
description: 'findOne',
op: 'db',
},
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
query: '{"title":"Back to the Future"}',
},
description: 'find',
op: 'db',
},
{
data: {
collectionName: 'movies',
Expand Down
29 changes: 24 additions & 5 deletions packages/tracing/src/integrations/node/mongo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Hub } from '@sentry/core';
import { EventProcessor, Integration, SpanContext } from '@sentry/types';
import { fill, isThenable, loadModule, logger } from '@sentry/utils';
import { fill, isInstanceOf, isThenable, loadModule, logger } from '@sentry/utils';
import { EventEmitter } from 'events';

import { shouldDisableAutoInstrumentation } from './utils/node-utils';

Expand Down Expand Up @@ -160,20 +161,38 @@ export class Mongo implements Integration {
// its (non-callback) arguments can also be functions.)
if (typeof lastArg !== 'function' || (operation === 'mapReduce' && args.length === 2)) {
const span = parentSpan?.startChild(getSpanContext(this, operation, args));
const maybePromise = orig.call(this, ...args) as Promise<unknown>;
const maybePromiseOrCursor = orig.call(this, ...args);

if (isThenable(maybePromise)) {
return maybePromise.then((res: unknown) => {
if (isThenable(maybePromiseOrCursor)) {
return maybePromiseOrCursor.then((res: unknown) => {
span?.finish();
return res;
});
}
// If the operation returns a cursor (which is an EventEmitter),
// we need to attach a listener to it to finish the span when the cursor is closed.
else if (isInstanceOf(maybePromiseOrCursor, EventEmitter)) {
const cursor = maybePromiseOrCursor as EventEmitter;

try {
cursor.once('close', () => {
span?.finish();
});
} catch (e) {
// If the cursor is already closed, `once` will throw an error. In that case, we can
// finish the span immediately.
span?.finish();
}

return cursor;
} else {
span?.finish();
return maybePromise;
return maybePromiseOrCursor;
}
}

const span = parentSpan?.startChild(getSpanContext(this, operation, args.slice(0, -1)));

return orig.call(this, ...args.slice(0, -1), function (err: Error, result: unknown) {
span?.finish();
lastArg(err, result);
Expand Down