@@ -3,6 +3,7 @@ import { getGlobalObject, logger } from '@sentry/utils';
33import { Transaction } from '../transaction' ;
44
55import { msToSec } from './utils' ;
6+ import { TransactionContext , SpanContext } from '@sentry/types' ;
67
78const global = getGlobalObject < Window > ( ) ;
89
@@ -162,7 +163,7 @@ export class MetricsInstrumentation {
162163 } ) ;
163164
164165 if ( entryScriptStartTimestamp !== undefined && tracingInitMarkStartTime !== undefined ) {
165- transaction . startChild ( {
166+ _startChild ( transaction , {
166167 description : 'evaluation' ,
167168 endTimestamp : tracingInitMarkStartTime ,
168169 op : 'script' ,
@@ -195,7 +196,7 @@ function addMeasureSpans(
195196 const measureStartTimestamp = timeOrigin + startTime ;
196197 const measureEndTimestamp = measureStartTimestamp + duration ;
197198
198- transaction . startChild ( {
199+ _startChild ( transaction , {
199200 description : entry . name as string ,
200201 endTimestamp : measureEndTimestamp ,
201202 op : entry . entryType as string ,
@@ -223,7 +224,7 @@ function addResourceSpans(
223224 const startTimestamp = timeOrigin + startTime ;
224225 const endTimestamp = startTimestamp + duration ;
225226
226- transaction . startChild ( {
227+ _startChild ( transaction , {
227228 description : `${ entry . initiatorType } ${ resourceName } ` ,
228229 endTimestamp,
229230 op : 'resource' ,
@@ -245,7 +246,7 @@ function addPerformanceNavigationTiming(
245246 if ( ! start || ! end ) {
246247 return ;
247248 }
248- transaction . startChild ( {
249+ _startChild ( transaction , {
249250 description : event ,
250251 endTimestamp : timeOrigin + msToSec ( end ) ,
251252 op : 'browser' ,
@@ -255,17 +256,33 @@ function addPerformanceNavigationTiming(
255256
256257/** Create request and response related spans */
257258function addRequest ( transaction : Transaction , entry : Record < string , any > , timeOrigin : number ) : void {
258- transaction . startChild ( {
259+ _startChild ( transaction , {
259260 description : 'request' ,
260261 endTimestamp : timeOrigin + msToSec ( entry . responseEnd as number ) ,
261262 op : 'browser' ,
262263 startTimestamp : timeOrigin + msToSec ( entry . requestStart as number ) ,
263264 } ) ;
264265
265- transaction . startChild ( {
266+ _startChild ( transaction , {
266267 description : 'response' ,
267268 endTimestamp : timeOrigin + msToSec ( entry . responseEnd as number ) ,
268269 op : 'browser' ,
269270 startTimestamp : timeOrigin + msToSec ( entry . responseStart as number ) ,
270271 } ) ;
271272}
273+
274+ /**
275+ * Helper function to start child on transactions. This function will make sure that the transaction will
276+ * will use the start timestamp of the created child span if it is earlier than the transactions actual
277+ * start timestamp.
278+ */
279+ export function _startChild ( transaction : Transaction , { startTimestamp, ...ctx } : SpanContext ) : Span {
280+ if ( startTimestamp && transaction . startTimestamp > startTimestamp ) {
281+ transaction . startTimestamp = startTimestamp ;
282+ }
283+
284+ return transaction . startChild ( {
285+ startTimestamp,
286+ ...ctx ,
287+ } ) ;
288+ }
0 commit comments