@@ -100,9 +100,11 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
100100 idleTimeout = TRACING_DEFAULTS . idleTimeout ,
101101 finalTimeout = TRACING_DEFAULTS . finalTimeout ,
102102 childSpanTimeout = TRACING_DEFAULTS . childSpanTimeout ,
103- beforeSpanEnd,
104103 } = options ;
105104
105+ // We reset this to undefined when this is called the first time
106+ let beforeSpanEnd = options . beforeSpanEnd ;
107+
106108 const client = getClient ( ) ;
107109
108110 if ( ! client || ! hasTracingEnabled ( ) ) {
@@ -114,6 +116,12 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
114116 const span = _startIdleSpan ( startSpanOptions ) ;
115117
116118 function _endSpan ( timestamp : number = timestampInSeconds ( ) ) : void {
119+ // Ensure to call this before the span is actually ended
120+ if ( beforeSpanEnd ) {
121+ beforeSpanEnd ( span ) ;
122+ beforeSpanEnd = undefined ;
123+ }
124+
117125 // Ensure we end with the last span timestamp, if possible
118126 const spans = getSpanDescendants ( span ) . filter ( child => child !== span ) ;
119127
@@ -227,8 +235,13 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
227235 _finished = true ;
228236 activities . clear ( ) ;
229237
238+ // If the span is idle-finished, this was already called in _endSpan
239+ // But if the span is manually finished, we ensure to still call this here
240+ // this is a bit later, as at this point the span is already ended,
241+ // but better late than never!
230242 if ( beforeSpanEnd ) {
231243 beforeSpanEnd ( span ) ;
244+ beforeSpanEnd = undefined ;
232245 }
233246
234247 _setSpanForScope ( scope , previousActiveSpan ) ;
0 commit comments