@@ -4,6 +4,7 @@ import { Span } from '../span';
4
4
import { Transaction } from '../transaction' ;
5
5
6
6
import { msToSec } from './utils' ;
7
+ import { TransactionContext , SpanContext } from '@sentry/types' ;
7
8
8
9
const global = getGlobalObject < Window > ( ) ;
9
10
@@ -163,7 +164,7 @@ export class MetricsInstrumentation {
163
164
} ) ;
164
165
165
166
if ( entryScriptStartTimestamp !== undefined && tracingInitMarkStartTime !== undefined ) {
166
- transaction . startChild ( {
167
+ _startChild ( transaction , {
167
168
description : 'evaluation' ,
168
169
endTimestamp : tracingInitMarkStartTime ,
169
170
op : 'script' ,
@@ -196,7 +197,7 @@ function addMeasureSpans(
196
197
const measureStartTimestamp = timeOrigin + startTime ;
197
198
const measureEndTimestamp = measureStartTimestamp + duration ;
198
199
199
- transaction . startChild ( {
200
+ _startChild ( transaction , {
200
201
description : entry . name as string ,
201
202
endTimestamp : measureEndTimestamp ,
202
203
op : entry . entryType as string ,
@@ -229,7 +230,7 @@ function addResourceSpans(
229
230
const startTimestamp = timeOrigin + startTime ;
230
231
const endTimestamp = startTimestamp + duration ;
231
232
232
- transaction . startChild ( {
233
+ _startChild ( transaction , {
233
234
description : `${ entry . initiatorType } ${ resourceName } ` ,
234
235
endTimestamp,
235
236
op : 'resource' ,
@@ -254,7 +255,7 @@ function addPerformanceNavigationTiming(
254
255
if ( ! start || ! end ) {
255
256
return ;
256
257
}
257
- transaction . startChild ( {
258
+ _startChild ( transaction , {
258
259
description : event ,
259
260
endTimestamp : timeOrigin + msToSec ( end ) ,
260
261
op : 'browser' ,
@@ -264,17 +265,33 @@ function addPerformanceNavigationTiming(
264
265
265
266
/** Create request and response related spans */
266
267
function addRequest ( transaction : Transaction , entry : Record < string , any > , timeOrigin : number ) : void {
267
- transaction . startChild ( {
268
+ _startChild ( transaction , {
268
269
description : 'request' ,
269
270
endTimestamp : timeOrigin + msToSec ( entry . responseEnd as number ) ,
270
271
op : 'browser' ,
271
272
startTimestamp : timeOrigin + msToSec ( entry . requestStart as number ) ,
272
273
} ) ;
273
274
274
- transaction . startChild ( {
275
+ _startChild ( transaction , {
275
276
description : 'response' ,
276
277
endTimestamp : timeOrigin + msToSec ( entry . responseEnd as number ) ,
277
278
op : 'browser' ,
278
279
startTimestamp : timeOrigin + msToSec ( entry . responseStart as number ) ,
279
280
} ) ;
280
281
}
282
+
283
+ /**
284
+ * Helper function to start child on transactions. This function will make sure that the transaction will
285
+ * will use the start timestamp of the created child span if it is earlier than the transactions actual
286
+ * start timestamp.
287
+ */
288
+ export function _startChild ( transaction : Transaction , { startTimestamp, ...ctx } : SpanContext ) : Span {
289
+ if ( startTimestamp && transaction . startTimestamp > startTimestamp ) {
290
+ transaction . startTimestamp = startTimestamp ;
291
+ }
292
+
293
+ return transaction . startChild ( {
294
+ startTimestamp,
295
+ ...ctx ,
296
+ } ) ;
297
+ }
0 commit comments