@@ -96,6 +96,8 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
9696
9797 let _autoFinishAllowed : boolean = ! options . disableAutoFinish ;
9898
99+ const _cleanupHooks : ( ( ) => void ) [ ] = [ ] ;
100+
99101 const {
100102 idleTimeout = TRACING_DEFAULTS . idleTimeout ,
101103 finalTimeout = TRACING_DEFAULTS . finalTimeout ,
@@ -240,6 +242,8 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
240242 _finished = true ;
241243 activities . clear ( ) ;
242244
245+ _cleanupHooks . forEach ( cleanup => cleanup ( ) ) ;
246+
243247 _setSpanForScope ( scope , previousActiveSpan ) ;
244248
245249 const spanJSON = spanToJSON ( span ) ;
@@ -298,41 +302,47 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
298302 }
299303 }
300304
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+ }
309314
310- const allSpans = getSpanDescendants ( span ) ;
315+ const allSpans = getSpanDescendants ( span ) ;
311316
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+ ) ;
317323
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+ }
322329
323- _popActivity ( endedSpan . spanContext ( ) . spanId ) ;
324- } ) ;
330+ _popActivity ( endedSpan . spanContext ( ) . spanId ) ;
331+ } ) ,
332+ ) ;
325333
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 ( ) ;
330339
331- if ( activities . size ) {
332- _restartChildSpanTimeout ( ) ;
340+ if ( activities . size ) {
341+ _restartChildSpanTimeout ( ) ;
342+ }
333343 }
334- }
335- } ) ;
344+ } ) ,
345+ ) ;
336346
337347 // We only start the initial idle timeout if we are not delaying the auto finish
338348 if ( ! options . disableAutoFinish ) {
0 commit comments