@@ -44,7 +44,7 @@ export class MetricsInstrumentation {
44
44
logger . log ( '[Tracing] Adding & adjusting spans using Performance API' ) ;
45
45
46
46
const timeOrigin = msToSec ( browserPerformanceTimeOrigin ) ;
47
- let entryScriptSrc : string | undefined ;
47
+ let entryScriptSrc = '' ;
48
48
49
49
if ( global . document && global . document . scripts ) {
50
50
// eslint-disable-next-line @typescript-eslint/prefer-for-of
@@ -114,7 +114,7 @@ export class MetricsInstrumentation {
114
114
const resourceName = ( entry . name as string ) . replace ( global . location . origin , '' ) ;
115
115
const endTimestamp = addResourceSpans ( transaction , entry , resourceName , startTime , duration , timeOrigin ) ;
116
116
// We remember the entry script end time to calculate the difference to the first init mark
117
- if ( entryScriptStartTimestamp === undefined && ( entryScriptSrc || '' ) . indexOf ( resourceName ) > - 1 ) {
117
+ if ( entryScriptStartTimestamp === undefined && entryScriptSrc . indexOf ( resourceName ) > - 1 ) {
118
118
entryScriptStartTimestamp = endTimestamp ;
119
119
}
120
120
break ;
@@ -194,60 +194,11 @@ export class MetricsInstrumentation {
194
194
}
195
195
196
196
transaction . setMeasurements ( this . _measurements ) ;
197
- this . _tagMetricInfo ( transaction ) ;
198
-
197
+ tagMetricInfo ( transaction , this . _lcpEntry , this . _clsEntry ) ;
199
198
transaction . setTag ( 'sentry_reportAllChanges' , this . _reportAllChanges ) ;
200
199
}
201
200
}
202
201
203
- /** Add LCP / CLS data to transaction to allow debugging */
204
- private _tagMetricInfo ( transaction : Transaction ) : void {
205
- if ( this . _lcpEntry ) {
206
- logger . log ( '[Measurements] Adding LCP Data' ) ;
207
- // Capture Properties of the LCP element that contributes to the LCP.
208
-
209
- if ( this . _lcpEntry . element ) {
210
- transaction . setTag ( 'lcp.element' , htmlTreeAsString ( this . _lcpEntry . element ) ) ;
211
- }
212
-
213
- if ( this . _lcpEntry . id ) {
214
- transaction . setTag ( 'lcp.id' , this . _lcpEntry . id ) ;
215
- }
216
-
217
- if ( this . _lcpEntry . url ) {
218
- // Trim URL to the first 200 characters.
219
- transaction . setTag ( 'lcp.url' , this . _lcpEntry . url . trim ( ) . slice ( 0 , 200 ) ) ;
220
- }
221
-
222
- transaction . setTag ( 'lcp.size' , this . _lcpEntry . size ) ;
223
- }
224
-
225
- // See: https://developer.mozilla.org/en-US/docs/Web/API/LayoutShift
226
- if ( this . _clsEntry && this . _clsEntry . sources ) {
227
- logger . log ( '[Measurements] Adding CLS Data' ) ;
228
- this . _clsEntry . sources . forEach ( ( source , index ) =>
229
- transaction . setTag ( `cls.source.${ index + 1 } ` , htmlTreeAsString ( source . node ) ) ,
230
- ) ;
231
- }
232
- }
233
-
234
- /** Starts tracking the Cumulative Layout Shift on the current page. */
235
- private _trackCLS ( ) : void {
236
- // See:
237
- // https://web.dev/evolving-cls/
238
- // https://web.dev/cls-web-tooling/
239
- getCLS ( metric => {
240
- const entry = metric . entries . pop ( ) ;
241
- if ( ! entry ) {
242
- return ;
243
- }
244
-
245
- logger . log ( '[Measurements] Adding CLS' ) ;
246
- this . _measurements [ 'cls' ] = { value : metric . value } ;
247
- this . _clsEntry = entry as LayoutShift ;
248
- } ) ;
249
- }
250
-
251
202
/**
252
203
* Capture the information of the user agent.
253
204
*/
@@ -286,11 +237,27 @@ export class MetricsInstrumentation {
286
237
}
287
238
}
288
239
240
+ /** Starts tracking the Cumulative Layout Shift on the current page. */
241
+ private _trackCLS ( ) : void {
242
+ // See:
243
+ // https://web.dev/evolving-cls/
244
+ // https://web.dev/cls-web-tooling/
245
+ getCLS ( metric => {
246
+ const entry = metric . entries . pop ( ) ;
247
+ if ( ! entry ) {
248
+ return ;
249
+ }
250
+
251
+ logger . log ( '[Measurements] Adding CLS' ) ;
252
+ this . _measurements [ 'cls' ] = { value : metric . value } ;
253
+ this . _clsEntry = entry as LayoutShift ;
254
+ } ) ;
255
+ }
256
+
289
257
/** Starts tracking the Largest Contentful Paint on the current page. */
290
258
private _trackLCP ( ) : void {
291
259
getLCP ( metric => {
292
260
const entry = metric . entries . pop ( ) ;
293
-
294
261
if ( ! entry ) {
295
262
return ;
296
263
}
@@ -308,7 +275,6 @@ export class MetricsInstrumentation {
308
275
private _trackFID ( ) : void {
309
276
getFID ( metric => {
310
277
const entry = metric . entries . pop ( ) ;
311
-
312
278
if ( ! entry ) {
313
279
return ;
314
280
}
@@ -324,28 +290,12 @@ export class MetricsInstrumentation {
324
290
325
291
/** Instrument navigation entries */
326
292
function addNavigationSpans ( transaction : Transaction , entry : Record < string , any > , timeOrigin : number ) : void {
327
- addPerformanceNavigationTiming ( { transaction, entry, event : 'unloadEvent' , timeOrigin } ) ;
328
- addPerformanceNavigationTiming ( { transaction, entry, event : 'redirect' , timeOrigin } ) ;
329
- addPerformanceNavigationTiming ( { transaction, entry, event : 'domContentLoadedEvent' , timeOrigin } ) ;
330
- addPerformanceNavigationTiming ( { transaction, entry, event : 'loadEvent' , timeOrigin } ) ;
331
- addPerformanceNavigationTiming ( { transaction, entry, event : 'connect' , timeOrigin } ) ;
332
- addPerformanceNavigationTiming ( {
333
- transaction,
334
- entry,
335
- event : 'secureConnection' ,
336
- timeOrigin,
337
- eventEnd : 'connectEnd' ,
338
- description : 'TLS/SSL' ,
293
+ [ 'unloadEvent' , 'redirect' , 'domContentLoadedEvent' , 'loadEvent' , 'connect' ] . forEach ( event => {
294
+ addPerformanceNavigationTiming ( transaction , entry , event , timeOrigin ) ;
339
295
} ) ;
340
- addPerformanceNavigationTiming ( {
341
- transaction,
342
- entry,
343
- event : 'fetch' ,
344
- timeOrigin,
345
- eventEnd : 'domainLookupStart' ,
346
- description : 'cache' ,
347
- } ) ;
348
- addPerformanceNavigationTiming ( { transaction, entry, event : 'domainLookup' , timeOrigin, description : 'DNS' } ) ;
296
+ addPerformanceNavigationTiming ( transaction , entry , 'secureConnection' , timeOrigin , 'TLS/SSL' , 'connectEnd' ) ;
297
+ addPerformanceNavigationTiming ( transaction , entry , 'fetch' , timeOrigin , 'cache' , 'domainLookupStart' ) ;
298
+ addPerformanceNavigationTiming ( transaction , entry , 'domainLookup' , timeOrigin , 'DNS' ) ;
349
299
addRequest ( transaction , entry , timeOrigin ) ;
350
300
}
351
301
@@ -418,16 +368,14 @@ export function addResourceSpans(
418
368
}
419
369
420
370
/** Create performance navigation related spans */
421
- function addPerformanceNavigationTiming ( props : {
422
- transaction : Transaction ;
423
- entry : Record < string , any > ;
424
- event : string ;
425
- timeOrigin : number ;
426
- eventEnd ?: string ;
427
- description ?: string ;
428
- } ) : void {
429
- const { transaction, entry, event, timeOrigin, eventEnd, description } = props ;
430
-
371
+ function addPerformanceNavigationTiming (
372
+ transaction : Transaction ,
373
+ entry : Record < string , any > ,
374
+ event : string ,
375
+ timeOrigin : number ,
376
+ description ?: string ,
377
+ eventEnd ?: string ,
378
+ ) : void {
431
379
const end = eventEnd ? ( entry [ eventEnd ] as number | undefined ) : ( entry [ `${ event } End` ] as number | undefined ) ;
432
380
const start = entry [ `${ event } Start` ] as number | undefined ;
433
381
if ( ! start || ! end ) {
@@ -480,3 +428,39 @@ export function _startChild(transaction: Transaction, { startTimestamp, ...ctx }
480
428
function isMeasurementValue ( value : any ) : boolean {
481
429
return typeof value === 'number' && isFinite ( value ) ;
482
430
}
431
+
432
+ /** Add LCP / CLS data to transaction to allow debugging */
433
+ function tagMetricInfo (
434
+ transaction : Transaction ,
435
+ lcpEntry : MetricsInstrumentation [ '_lcpEntry' ] ,
436
+ clsEntry : MetricsInstrumentation [ '_clsEntry' ] ,
437
+ ) : void {
438
+ if ( lcpEntry ) {
439
+ logger . log ( '[Measurements] Adding LCP Data' ) ;
440
+
441
+ // Capture Properties of the LCP element that contributes to the LCP.
442
+
443
+ if ( lcpEntry . element ) {
444
+ transaction . setTag ( 'lcp.element' , htmlTreeAsString ( lcpEntry . element ) ) ;
445
+ }
446
+
447
+ if ( lcpEntry . id ) {
448
+ transaction . setTag ( 'lcp.id' , lcpEntry . id ) ;
449
+ }
450
+
451
+ if ( lcpEntry . url ) {
452
+ // Trim URL to the first 200 characters.
453
+ transaction . setTag ( 'lcp.url' , lcpEntry . url . trim ( ) . slice ( 0 , 200 ) ) ;
454
+ }
455
+
456
+ transaction . setTag ( 'lcp.size' , lcpEntry . size ) ;
457
+ }
458
+
459
+ // See: https://developer.mozilla.org/en-US/docs/Web/API/LayoutShift
460
+ if ( clsEntry && clsEntry . sources ) {
461
+ logger . log ( '[Measurements] Adding CLS Data' ) ;
462
+ clsEntry . sources . forEach ( ( source , index ) =>
463
+ transaction . setTag ( `cls.source.${ index + 1 } ` , htmlTreeAsString ( source . node ) ) ,
464
+ ) ;
465
+ }
466
+ }
0 commit comments