1
1
/* eslint-disable max-lines */
2
2
/* eslint-disable @typescript-eslint/no-explicit-any */
3
3
import { Measurements , SpanContext } from '@sentry/types' ;
4
- import { browserPerformanceTimeOrigin , getGlobalObject , logger } from '@sentry/utils' ;
4
+ import { browserPerformanceTimeOrigin , getGlobalObject , htmlTreeAsString , logger } from '@sentry/utils' ;
5
5
6
6
import { Span } from '../span' ;
7
7
import { Transaction } from '../transaction' ;
8
8
import { msToSec } from '../utils' ;
9
- import { getCLS } from './web-vitals/getCLS' ;
9
+ import { getCLS , LayoutShift } from './web-vitals/getCLS' ;
10
10
import { getFID } from './web-vitals/getFID' ;
11
- import { getLCP } from './web-vitals/getLCP' ;
11
+ import { getLCP , LargestContentfulPaint } from './web-vitals/getLCP' ;
12
12
import { getTTFB } from './web-vitals/getTTFB' ;
13
13
import { getFirstHidden } from './web-vitals/lib/getFirstHidden' ;
14
14
import { NavigatorDeviceMemory , NavigatorNetworkInformation } from './web-vitals/types' ;
@@ -20,6 +20,8 @@ export class MetricsInstrumentation {
20
20
private _measurements : Measurements = { } ;
21
21
22
22
private _performanceCursor : number = 0 ;
23
+ private _lcpEntry : LargestContentfulPaint | undefined ;
24
+ private _clsEntry : LayoutShift | undefined ;
23
25
24
26
public constructor ( ) {
25
27
if ( global && global . performance ) {
@@ -135,6 +137,24 @@ export class MetricsInstrumentation {
135
137
// Measurements are only available for pageload transactions
136
138
if ( transaction . op === 'pageload' ) {
137
139
transaction . setMeasurements ( this . _measurements ) ;
140
+
141
+ if ( this . _lcpEntry ) {
142
+ logger . log ( '[Measurements] Adding LCP Data' ) ;
143
+ transaction . setTag ( 'measurements.lcp.url' , this . _lcpEntry . url ) ;
144
+ transaction . setData ( 'measurements.lcp' , {
145
+ size : this . _lcpEntry . size ,
146
+ id : this . _lcpEntry . id ,
147
+ url : this . _lcpEntry . url ,
148
+ element : this . _lcpEntry . element ? htmlTreeAsString ( this . _lcpEntry . element ) : undefined ,
149
+ } ) ;
150
+ }
151
+
152
+ if ( this . _clsEntry ) {
153
+ logger . log ( '[Measurements] Adding CLS Data' ) ;
154
+ transaction . setData ( 'measurements.cls' , {
155
+ sources : this . _clsEntry . sources . map ( source => htmlTreeAsString ( source . node ) ) ,
156
+ } ) ;
157
+ }
138
158
}
139
159
}
140
160
@@ -149,6 +169,7 @@ export class MetricsInstrumentation {
149
169
150
170
logger . log ( '[Measurements] Adding CLS' ) ;
151
171
this . _measurements [ 'cls' ] = { value : metric . value } ;
172
+ this . _clsEntry = entry as LayoutShift ;
152
173
} ) ;
153
174
}
154
175
@@ -206,6 +227,7 @@ export class MetricsInstrumentation {
206
227
logger . log ( '[Measurements] Adding LCP' ) ;
207
228
this . _measurements [ 'lcp' ] = { value : metric . value } ;
208
229
this . _measurements [ 'mark.lcp' ] = { value : timeOrigin + startTime } ;
230
+ this . _lcpEntry = entry as LargestContentfulPaint ;
209
231
} ) ;
210
232
}
211
233
0 commit comments