@@ -410,34 +410,52 @@ export class Hub implements HubInterface {
410410 /**
411411 * @inheritDoc
412412 */
413- public finishSpan ( span : Span ) : string | undefined {
414- if ( ! span . timestamp ) {
415- span . finish ( ) ;
413+ public finishSpan ( span ?: Span ) : string | undefined {
414+ const top = this . getStackTop ( ) ;
415+ let passedSpan = span ;
416+
417+ // If the passed span is undefined we try to get the span from the scope and finish it.
418+ if ( passedSpan === undefined ) {
419+ if ( top . scope && top . client ) {
420+ const scopeSpan = top . scope . getSpan ( ) ;
421+ if ( scopeSpan ) {
422+ passedSpan = scopeSpan ;
423+ }
424+ }
425+ }
426+
427+ if ( passedSpan === undefined ) {
428+ // We will do nothing since nothing was passed and there is no Span on the scope.
429+ return undefined ;
430+ }
431+
432+ if ( ! passedSpan . timestamp ) {
433+ passedSpan . finish ( ) ;
416434 }
417435
418- if ( ! span . transaction ) {
436+ if ( ! passedSpan . transaction ) {
419437 return undefined ;
420438 }
421439
422- if ( ! this . getClient ( ) ) {
440+ if ( ! top . client ) {
423441 return undefined ;
424442 }
425443
426444 // TODO: if sampled do what?
427445
428- const finishedSpans = span . finishedSpans . filter ( s => s !== span ) ;
446+ const finishedSpans = passedSpan . finishedSpans . filter ( s => s !== span ) ;
429447
430448 const eventId = this . captureEvent ( {
431- contexts : { trace : span . getTraceContext ( ) } ,
449+ contexts : { trace : passedSpan . getTraceContext ( ) } ,
432450 spans : finishedSpans ,
433- start_timestamp : span . startTimestamp ,
434- timestamp : span . timestamp ,
435- transaction : span . transaction ,
451+ start_timestamp : passedSpan . startTimestamp ,
452+ timestamp : passedSpan . timestamp ,
453+ transaction : passedSpan . transaction ,
436454 type : 'transaction' ,
437455 } ) ;
438456
439457 // After sending we reset the finishedSpans array
440- span . finishedSpans = [ ] ;
458+ passedSpan . finishedSpans = [ ] ;
441459 return eventId ;
442460 }
443461}
0 commit comments