@@ -418,34 +418,52 @@ export class Hub implements HubInterface {
418
418
/**
419
419
* @inheritDoc
420
420
*/
421
- public finishSpan ( span : Span ) : string | undefined {
422
- if ( ! span . timestamp ) {
423
- span . finish ( ) ;
421
+ public finishSpan ( span ?: Span ) : string | undefined {
422
+ const top = this . getStackTop ( ) ;
423
+ let passedSpan = span ;
424
+
425
+ // If the passed span is undefined we try to get the span from the scope and finish it.
426
+ if ( passedSpan === undefined ) {
427
+ if ( top . scope && top . client ) {
428
+ const scopeSpan = top . scope . getSpan ( ) ;
429
+ if ( scopeSpan ) {
430
+ passedSpan = scopeSpan ;
431
+ }
432
+ }
433
+ }
434
+
435
+ if ( passedSpan === undefined ) {
436
+ // We will do nothing since nothing was passed and there is no Span on the scope.
437
+ return undefined ;
438
+ }
439
+
440
+ if ( ! passedSpan . timestamp ) {
441
+ passedSpan . finish ( ) ;
424
442
}
425
443
426
- if ( ! span . transaction ) {
444
+ if ( ! passedSpan . transaction ) {
427
445
return undefined ;
428
446
}
429
447
430
- if ( ! this . getClient ( ) ) {
448
+ if ( ! top . client ) {
431
449
return undefined ;
432
450
}
433
451
434
452
// TODO: if sampled do what?
435
453
436
- const finishedSpans = span . finishedSpans . filter ( s => s !== span ) ;
454
+ const finishedSpans = passedSpan . finishedSpans . filter ( s => s !== span ) ;
437
455
438
456
const eventId = this . captureEvent ( {
439
- contexts : { trace : span . getTraceContext ( ) } ,
457
+ contexts : { trace : passedSpan . getTraceContext ( ) } ,
440
458
spans : finishedSpans ,
441
- start_timestamp : span . startTimestamp ,
442
- timestamp : span . timestamp ,
443
- transaction : span . transaction ,
459
+ start_timestamp : passedSpan . startTimestamp ,
460
+ timestamp : passedSpan . timestamp ,
461
+ transaction : passedSpan . transaction ,
444
462
type : 'transaction' ,
445
463
} ) ;
446
464
447
465
// After sending we reset the finishedSpans array
448
- span . finishedSpans = [ ] ;
466
+ passedSpan . finishedSpans = [ ] ;
449
467
return eventId ;
450
468
}
451
469
}
0 commit comments