@@ -96,6 +96,8 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
96
96
97
97
let _autoFinishAllowed : boolean = ! options . disableAutoFinish ;
98
98
99
+ const _cleanupHooks : ( ( ) => void ) [ ] = [ ] ;
100
+
99
101
const {
100
102
idleTimeout = TRACING_DEFAULTS . idleTimeout ,
101
103
finalTimeout = TRACING_DEFAULTS . finalTimeout ,
@@ -237,6 +239,8 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
237
239
}
238
240
239
241
function onIdleSpanEnded ( endTimestamp : number ) : void {
242
+ _cleanupHooks . forEach ( cleanup => cleanup ( ) ) ;
243
+
240
244
_finished = true ;
241
245
activities . clear ( ) ;
242
246
@@ -298,41 +302,47 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
298
302
}
299
303
}
300
304
301
- client . on ( 'spanStart' , startedSpan => {
302
- // If we already finished the idle span,
303
- // or if this is the idle span itself being started,
304
- // or if the started span has already been closed,
305
- // we don't care about it for activity
306
- if ( _finished || startedSpan === span || ! ! spanToJSON ( startedSpan ) . timestamp ) {
307
- return ;
308
- }
305
+ _cleanupHooks . push (
306
+ client . on ( 'spanStart' , startedSpan => {
307
+ // If we already finished the idle span,
308
+ // or if this is the idle span itself being started,
309
+ // or if the started span has already been closed,
310
+ // we don't care about it for activity
311
+ if ( _finished || startedSpan === span || ! ! spanToJSON ( startedSpan ) . timestamp ) {
312
+ return ;
313
+ }
309
314
310
- const allSpans = getSpanDescendants ( span ) ;
315
+ const allSpans = getSpanDescendants ( span ) ;
311
316
312
- // If the span that was just started is a child of the idle span, we should track it
313
- if ( allSpans . includes ( startedSpan ) ) {
314
- _pushActivity ( startedSpan . spanContext ( ) . spanId ) ;
315
- }
316
- } ) ;
317
+ // If the span that was just started is a child of the idle span, we should track it
318
+ if ( allSpans . includes ( startedSpan ) ) {
319
+ _pushActivity ( startedSpan . spanContext ( ) . spanId ) ;
320
+ }
321
+ } ) ,
322
+ ) ;
317
323
318
- client . on ( 'spanEnd' , endedSpan => {
319
- if ( _finished ) {
320
- return ;
321
- }
324
+ _cleanupHooks . push (
325
+ client . on ( 'spanEnd' , endedSpan => {
326
+ if ( _finished ) {
327
+ return ;
328
+ }
322
329
323
- _popActivity ( endedSpan . spanContext ( ) . spanId ) ;
324
- } ) ;
330
+ _popActivity ( endedSpan . spanContext ( ) . spanId ) ;
331
+ } ) ,
332
+ ) ;
325
333
326
- client . on ( 'idleSpanEnableAutoFinish' , spanToAllowAutoFinish => {
327
- if ( spanToAllowAutoFinish === span ) {
328
- _autoFinishAllowed = true ;
329
- _restartIdleTimeout ( ) ;
334
+ _cleanupHooks . push (
335
+ client . on ( 'idleSpanEnableAutoFinish' , spanToAllowAutoFinish => {
336
+ if ( spanToAllowAutoFinish === span ) {
337
+ _autoFinishAllowed = true ;
338
+ _restartIdleTimeout ( ) ;
330
339
331
- if ( activities . size ) {
332
- _restartChildSpanTimeout ( ) ;
340
+ if ( activities . size ) {
341
+ _restartChildSpanTimeout ( ) ;
342
+ }
333
343
}
334
- }
335
- } ) ;
344
+ } ) ,
345
+ ) ;
336
346
337
347
// We only start the initial idle timeout if we are not delaying the auto finish
338
348
if ( ! options . disableAutoFinish ) {
0 commit comments