@@ -581,7 +581,7 @@ export class Tracing implements Integration {
581581
582582 // tslint:disable-next-line: completed-docs
583583 function addPerformanceNavigationTiming ( parent : Span , entry : { [ key : string ] : number } , event : string ) : void {
584- parent . startChild ( {
584+ _startChild ( parent , {
585585 description : event ,
586586 endTimestamp : timeOrigin + Tracing . _msToSec ( entry [ `${ event } End` ] ) ,
587587 op : 'browser' ,
@@ -591,14 +591,14 @@ export class Tracing implements Integration {
591591
592592 // tslint:disable-next-line: completed-docs
593593 function addRequest ( parent : Span , entry : { [ key : string ] : number } ) : void {
594- parent . startChild ( {
594+ _startChild ( parent , {
595595 description : 'request' ,
596596 endTimestamp : timeOrigin + Tracing . _msToSec ( entry . responseEnd ) ,
597597 op : 'browser' ,
598598 startTimestamp : timeOrigin + Tracing . _msToSec ( entry . requestStart ) ,
599599 } ) ;
600600
601- parent . startChild ( {
601+ _startChild ( parent , {
602602 description : 'response' ,
603603 endTimestamp : timeOrigin + Tracing . _msToSec ( entry . responseEnd ) ,
604604 op : 'browser' ,
@@ -648,35 +648,27 @@ export class Tracing implements Integration {
648648 case 'mark' :
649649 case 'paint' :
650650 case 'measure' :
651- const mark = transactionSpan . startChild ( {
651+ const mark = _startChild ( transactionSpan , {
652652 description : entry . name ,
653+ endTimestamp : timeOrigin + startTime + duration ,
653654 op : entry . entryType ,
655+ startTimestamp : timeOrigin + startTime ,
654656 } ) ;
655- mark . startTimestamp = timeOrigin + startTime ;
656- mark . endTimestamp = mark . startTimestamp + duration ;
657657 if ( tracingInitMarkStartTime === undefined && entry . name === 'sentry-tracing-init' ) {
658658 tracingInitMarkStartTime = mark . startTimestamp ;
659659 }
660660 break ;
661661 case 'resource' :
662662 const resourceName = entry . name . replace ( window . location . origin , '' ) ;
663- if ( entry . initiatorType === 'xmlhttprequest' || entry . initiatorType === 'fetch' ) {
664- // We need to update existing spans with new timing info
665- if ( transactionSpan . spanRecorder ) {
666- transactionSpan . spanRecorder . spans . map ( ( finishedSpan : Span ) => {
667- if ( finishedSpan . description && finishedSpan . description . indexOf ( resourceName ) !== - 1 ) {
668- finishedSpan . startTimestamp = timeOrigin + startTime ;
669- finishedSpan . endTimestamp = finishedSpan . startTimestamp + duration ;
670- }
671- } ) ;
672- }
673- } else {
674- const resource = transactionSpan . startChild ( {
663+ // we already instrument based on fetch and xhr, so we don't need to
664+ // duplicate spans here.
665+ if ( entry . initiatorType !== 'xmlhttprequest' && entry . initiatorType !== 'fetch' ) {
666+ const resource = _startChild ( transactionSpan , {
675667 description : `${ entry . initiatorType } ${ resourceName } ` ,
668+ endTimestamp : timeOrigin + startTime + duration ,
676669 op : `resource` ,
670+ startTimestamp : timeOrigin + startTime ,
677671 } ) ;
678- resource . startTimestamp = timeOrigin + startTime ;
679- resource . endTimestamp = resource . startTimestamp + duration ;
680672 // We remember the entry script end time to calculate the difference to the first init mark
681673 if ( entryScriptStartEndTime === undefined && ( entryScriptSrc || '' ) . indexOf ( resourceName ) > - 1 ) {
682674 entryScriptStartEndTime = resource . endTimestamp ;
@@ -689,7 +681,7 @@ export class Tracing implements Integration {
689681 } ) ;
690682
691683 if ( entryScriptStartEndTime !== undefined && tracingInitMarkStartTime !== undefined ) {
692- transactionSpan . startChild ( {
684+ _startChild ( transactionSpan , {
693685 description : 'evaluation' ,
694686 endTimestamp : tracingInitMarkStartTime ,
695687 op : `script` ,
@@ -1054,3 +1046,19 @@ function historyCallback(_: { [key: string]: any }): void {
10541046 } ) ;
10551047 }
10561048}
1049+
1050+ /**
1051+ * Helper function to start child on transactions. This function will make sure that the transaction will
1052+ * use the start timestamp of the created child span if it is earlier than the transactions actual
1053+ * start timestamp.
1054+ */
1055+ export function _startChild ( parent : Span , { startTimestamp, ...ctx } : SpanContext ) : Span {
1056+ if ( startTimestamp && parent . startTimestamp > startTimestamp ) {
1057+ parent . startTimestamp = startTimestamp ;
1058+ }
1059+
1060+ return parent . startChild ( {
1061+ startTimestamp,
1062+ ...ctx ,
1063+ } ) ;
1064+ }
0 commit comments