8000 Check the existence of while matching Cursors. · getsentry/sentry-javascript@6a821ba · GitHub
[go: up one dir, main page]

Skip to content

Commit 6a821ba

Browse files
committed
Check the existence of while matching Cursors.
1 parent a89e597 commit 6a821ba

File tree

1 file changed

+12
-5
lines changed
  • packages/tracing/src/integrations/node

1 file changed

+12
-5
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Hub } from '@sentry/core';
22
import { EventProcessor, Integration, SpanContext } from '@sentry/types';
3-
import { fill, isInstanceOf, isThenable, loadModule, logger } from '@sentry/utils';
4-
import { EventEmitter } from 'events';
3+
import { fill, isThenable, loadModule, logger } from '@sentry/utils';
54

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

@@ -91,6 +90,14 @@ interface MongoOptions {
9190
useMongoose?: boolean;
9291
}
9392

93+
interface MongoCursor {
94+
once(event: 'close', listener: () => void): void;
95+
}
96+
97+
function isCursor(maybeCursor: MongoCursor): maybeCursor is MongoCursor {
98+
return maybeCursor && typeof maybeCursor === 'object' && maybeCursor.once && typeof maybeCursor.once === 'function';
99+
}
100+
94101
/** Tracing integration for mongo package */
95102
export class Mongo implements Integration {
96103
/**
@@ -169,10 +176,10 @@ export class Mongo implements Integration {
169176
return res;
170177
});
171178
}
172-
// If the operation returns a cursor (which is an EventEmitter),
179+
// If the operation returns a Cursor
173180
// we need to attach a listener to it to finish the span when the cursor is closed.
174-
else if (isInstanceOf(maybePromiseOrCursor, EventEmitter)) {
175-
const cursor = maybePromiseOrCursor as EventEmitter;
181+
else if (isCursor(maybePromiseOrCursor)) {
182+
const cursor = maybePromiseOrCursor as MongoCursor;
176183

177184
try {
178185
cursor.once('close', () => {

0 commit comments

Comments
 (0)
0