@@ -100,9 +100,11 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
100
100
idleTimeout = TRACING_DEFAULTS . idleTimeout ,
101
101
finalTimeout = TRACING_DEFAULTS . finalTimeout ,
102
102
childSpanTimeout = TRACING_DEFAULTS . childSpanTimeout ,
103
- beforeSpanEnd,
104
103
} = options ;
105
104
105
+ // We reset this to undefined when this is called the first time
106
+ let beforeSpanEnd = options . beforeSpanEnd ;
107
+
106
108
const client = getClient ( ) ;
107
109
108
110
if ( ! client || ! hasTracingEnabled ( ) ) {
@@ -114,6 +116,12 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
114
116
const span = _startIdleSpan ( startSpanOptions ) ;
115
117
116
118
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
+
117
125
// Ensure we end with the last span timestamp, if possible
118
126
const spans = getSpanDescendants ( span ) . filter ( child => child !== span ) ;
119
127
@@ -227,8 +235,13 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
227
235
_finished = true ;
228
236
activities . clear ( ) ;
229
237
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!
230
242
if ( beforeSpanEnd ) {
231
243
beforeSpanEnd ( span ) ;
244
+ beforeSpanEnd = undefined ;
232
245
}
233
246
234
247
_setSpanForScope ( scope , previousActiveSpan ) ;
0 commit comments