File tree Expand file tree Collapse file tree 1 file changed +21
-2
lines changed
packages/tracing/src/integrations/node Expand file tree Collapse file tree 1 file changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -88,6 +88,19 @@ interface MongoOptions {
88
88
useMongoose ?: boolean ;
89
89
}
90
90
91
+ interface MongoCursor {
92
+ once ( event : 'close' , listener : ( ) => void ) : void ;
93
+ }
94
+
95
+ function isCursor ( value : unknown ) : value is MongoCursor {
96
+ return (
97
+ typeof value === 'function' &&
98
+ value ?. constructor ?. name . indexOf ( 'Cursor' ) !== - 1 &&
99
+ 'once' in value &&
100
+ typeof ( value as MongoCursor ) . once === 'function'
101
+ ) ;
102
+ }
103
+
91
104
/** Tracing integration for mongo package */
92
105
export class Mongo implements Integration {
93
106
/**
@@ -153,15 +166,21 @@ export class Mongo implements Integration {
153
166
// its (non-callback) arguments can also be functions.)
154
167
if ( typeof lastArg !== 'function' || ( operation === 'mapReduce' && args . length === 2 ) ) {
155
168
const span = parentSpan ?. startChild ( getSpanContext ( this , operation , args ) ) ;
156
- const maybePromise = orig . call ( this , ...args ) as Promise < unknown > ;
169
+ const maybePromise = orig . call ( this , ...args ) as Promise < unknown > | MongoCursor ;
157
170
158
171
if ( isThenable ( maybePromise ) ) {
159
172
return maybePromise . then ( ( res : unknown ) => {
160
173
span ?. finish ( ) ;
161
174
return res ;
162
175
} ) ;
163
176
} else {
164
- span ?. finish ( ) ;
177
+ if ( isCursor ( maybePromise ) ) {
178
+ maybePromise . once ( 'close' , ( ) => {
179
+ span ?. finish ( ) ;
180
+ } ) ;
181
+ } else {
182
+ span ?. finish ( ) ;
183
+ }
165
184
return maybePromise ;
166
185
}
167
186
}
You can’t perform that action at this time.
0 commit comments