@@ -3,6 +3,7 @@ import { getGlobalObject, logger } from '@sentry/utils';
3
3
import { Transaction } from '../transaction' ;
4
4
5
5
import { msToSec } from './utils' ;
6
+ import { TransactionContext , SpanContext } from '@sentry/types' ;
6
7
7
8
const global = getGlobalObject < Window > ( ) ;
8
9
@@ -162,7 +163,7 @@ export class MetricsInstrumentation {
162
163
} ) ;
163
164
164
165
if ( entryScriptStartTimestamp !== undefined && tracingInitMarkStartTime !== undefined ) {
165
- transaction . startChild ( {
166
+ _startChild ( transaction , {
166
167
description : 'evaluation' ,
167
168
endTimestamp : tracingInitMarkStartTime ,
168
169
op : 'script' ,
@@ -195,7 +196,7 @@ function addMeasureSpans(
195
196
const measureStartTimestamp = timeOrigin + startTime ;
196
197
const measureEndTimestamp = measureStartTimestamp + duration ;
197
198
198
- transaction . startChild ( {
199
+ _startChild ( transaction , {
199
200
description : entry . name as string ,
200
201
endTimestamp : measureEndTimestamp ,
201
202
op : entry . entryType as string ,
@@ -223,7 +224,7 @@ function addResourceSpans(
223
224
const startTimestamp = timeOrigin + startTime ;
224
225
const endTimestamp = startTimestamp + duration ;
225
226
226
- transaction . startChild ( {
227
+ _startChild ( transaction , {
227
228
description : `${ entry . initiatorType } ${ resourceName } ` ,
228
229
endTimestamp,
229
230
op : 'resource' ,
@@ -245,7 +246,7 @@ function addPerformanceNavigationTiming(
245
246
if ( ! start || ! end ) {
246
247
return ;
247
248
}
248
- transaction . startChild ( {
249
+ _startChild ( transaction , {
249
250
description : event ,
250
251
endTimestamp : timeOrigin + msToSec ( end ) ,
251
252
op : 'browser' ,
@@ -255,17 +256,33 @@ function addPerformanceNavigationTiming(
255
256
256
257
/** Create request and response related spans */
257
258
function addRequest ( transaction : Transaction , entry : Record < string , any > , timeOrigin : number ) : void {
258
- transaction . startChild ( {
259
+ _startChild ( transaction , {
259
260
description : 'request' ,
260
261
endTimestamp : timeOrigin + msToSec ( entry . responseEnd as number ) ,
261
262
op : 'browser' ,
262
263
startTimestamp : timeOrigin + msToSec ( entry . requestStart as number ) ,
263
264
} ) ;
264
265
265
- transaction . startChild ( {
266
+ _startChild ( transaction , {
266
267
description : 'response' ,
267
268
endTimestamp : timeOrigin + msToSec ( entry . responseEnd as number ) ,
268
269
op : 'browser' ,
269
270
startTimestamp : timeOrigin + msToSec ( entry . responseStart as number ) ,
270
271
} ) ;
271
272
}
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