1- import { Event , EventProcessor , Hub , Integration , Span , SpanContext , SpanStatus } from '@sentry/types' ;
1+ import { Hub , Scope } from '@sentry/hub' ;
2+ import { Event , EventProcessor , Integration , Span , SpanContext , SpanStatus } from '@sentry/types' ;
23import {
34 addInstrumentationHandler ,
45 getGlobalObject ,
@@ -272,6 +273,19 @@ export class Tracing implements Integration {
272273 * Unsets the current active transaction + activities
273274 */
274275 private static _resetActiveTransaction ( ) : void {
276+ // We want to clean up after ourselves
277+ // If there is still the active transaction on the scope we remove it
278+ const _getCurrentHub = Tracing . _getCurrentHub ;
279+ if ( _getCurrentHub ) {
280+ const hub = _getCurrentHub ( ) ;
281+ const scope = hub . getScope ( ) ;
282+ if ( scope ) {
283+ if ( scope . getSpan ( ) === Tracing . _activeTransaction ) {
284+ scope . setSpan ( undefined ) ;
285+ }
286+ }
287+ }
288+ // ------------------------------------------------------------------
275289 Tracing . _activeTransaction = undefined ;
276290 Tracing . _activities = { } ;
277291 }
@@ -365,6 +379,13 @@ export class Tracing implements Integration {
365379 true ,
366380 ) ;
367381
382+ // We set the transaction on the scope so if there are any other spans started outside of this integration
383+ // we also add them to this transaction.
384+ // Once the idle transaction is finished, we make sure to remove it again.
385+ hub . configureScope ( ( scope : Scope ) => {
386+ scope . setSpan ( Tracing . _activeTransaction ) ;
387+ } ) ;
388+
368389 // The reason we do this here is because of cached responses
369390 // If we start and transaction without an activity it would never finish since there is no activity
370391 const id = Tracing . pushActivity ( 'idleTransactionStarted' ) ;
@@ -405,13 +426,6 @@ export class Tracing implements Integration {
405426
406427 const timeOrigin = Tracing . _msToSec ( performance . timeOrigin ) ;
407428
408- // tslint:disable-next-line: completed-docs
409- function addSpan ( span : SpanClass ) : void {
410- if ( transactionSpan . spanRecorder ) {
411- transactionSpan . spanRecorder . finishSpan ( span ) ;
412- }
413- }
414-
415429 // tslint:disable-next-line: completed-docs
416430 function addPerformanceNavigationTiming ( parent : SpanClass , entry : { [ key : string ] : number } , event : string ) : void {
417431 const span = parent . child ( {
@@ -420,7 +434,6 @@ export class Tracing implements Integration {
420434 } ) ;
421435 span . startTimestamp = timeOrigin + Tracing . _msToSec ( entry [ `${ event } Start` ] ) ;
422436 span . timestamp = timeOrigin + Tracing . _msToSec ( entry [ `${ event } End` ] ) ;
423- addSpan ( span ) ;
424437 }
425438
426439 // tslint:disable-next-line: completed-docs
@@ -431,14 +444,13 @@ export class Tracing implements Integration {
431444 } ) ;
432445 request . startTimestamp = timeOrigin + Tracing . _msToSec ( entry . requestStart ) ;
433446 request . timestamp = timeOrigin + Tracing . _msToSec ( entry . responseEnd ) ;
434- addSpan ( request ) ;
447+
435448 const response = parent . child ( {
436449 description : 'response' ,
437450 op : 'browser' ,
438451 } ) ;
439452 response . startTimestamp = timeOrigin + Tracing . _msToSec ( entry . responseStart ) ;
440453 response . timestamp = timeOrigin + Tracing . _msToSec ( entry . responseEnd ) ;
441- addSpan ( response ) ;
442454 }
443455
444456 let entryScriptSrc : string | undefined ;
@@ -492,14 +504,13 @@ export class Tracing implements Integration {
492504 if ( tracingInitMarkStartTime === undefined && entry . name === 'sentry-tracing-init' ) {
493505 tracingInitMarkStartTime = mark . startTimestamp ;
494506 }
495- addSpan ( mark ) ;
496507 break ;
497508 case 'resource' :
498509 const resourceName = entry . name . replace ( window . location . origin , '' ) ;
499510 if ( entry . initiatorType === 'xmlhttprequest' || entry . initiatorType === 'fetch' ) {
500511 // We need to update existing spans with new timing info
501512 if ( transactionSpan . spanRecorder ) {
502- transactionSpan . spanRecorder . finishedSpans . map ( ( finishedSpan : SpanClass ) => {
513+ transactionSpan . spanRecorder . spans . map ( ( finishedSpan : SpanClass ) => {
503514 if ( finishedSpan . description && finishedSpan . description . indexOf ( resourceName ) !== - 1 ) {
504515 finishedSpan . startTimestamp = timeOrigin + startTime ;
505516 finishedSpan . timestamp = finishedSpan . startTimestamp + duration ;
@@ -517,7 +528,6 @@ export class Tracing implements Integration {
517528 if ( entryScriptStartEndTime === undefined && ( entryScriptSrc || '' ) . includes ( resourceName ) ) {
518529 entryScriptStartEndTime = resource . timestamp ;
519530 }
520- addSpan ( resource ) ;
521531 }
522532 break ;
523533 default :
@@ -532,7 +542,6 @@ export class Tracing implements Integration {
532542 } ) ;
533543 evaluation . startTimestamp = entryScriptStartEndTime ;
534544 evaluation . timestamp = tracingInitMarkStartTime ;
535- addSpan ( evaluation ) ;
536545 }
537546
538547 Tracing . _performanceCursor = Math . max ( performance . getEntries ( ) . length - 1 , 0 ) ;
0 commit comments