@@ -6,7 +6,7 @@ import { browserPerformanceTimeOrigin, getGlobalObject, htmlTreeAsString, isNode
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
11
import { getLCP , LargestContentfulPaint } from './web-vitals/getLCP' ;
12
12
import { getFirstHidden } from './web-vitals/lib/getFirstHidden' ;
@@ -20,6 +20,7 @@ export class MetricsInstrumentation {
20
20
21
21
private _performanceCursor : number = 0 ;
22
22
private _lcpEntry : LargestContentfulPaint | undefined ;
23
+ private _clsEntry : LayoutShift | undefined ;
23
24
24
25
public constructor ( ) {
25
26
if ( ! isNodeEnv ( ) && global ?. performance ) {
@@ -187,26 +188,37 @@ export class MetricsInstrumentation {
187
188
}
188
189
189
190
transaction . setMeasurements ( this . _measurements ) ;
191
+ this . _tagMetricInfo ( transaction ) ;
192
+ }
193
+ }
190
194
191
- if ( this . _lcpEntry ) {
192
- logger . log ( '[Measurements] Adding LCP Data' ) ;
193
- // Capture Properties of the LCP element that contributes to the LCP.
194
-
195
- if ( this . _lcpEntry . element ) {
196
- transaction . setTag ( 'lcp.element' , htmlTreeAsString ( this . _lcpEntry . element ) ) ;
197
- }
195
+ /** Add LCP / CLS data to transaction to allow debugging */
196
+ private _tagMetricInfo ( transaction : Transaction ) : void {
197
+ if ( this . _lcpEntry ) {
198
+ logger . log ( '[Measurements] Adding LCP Data' ) ;
199
+ // Capture Properties of the LCP element that contributes to the LCP.
198
200
199
- if ( this . _lcpEntry . id ) {
200
- transaction . setTag ( 'lcp.id ' , this . _lcpEntry . id ) ;
201
- }
201
+ if ( this . _lcpEntry . element ) {
202
+ transaction . setTag ( 'lcp.element ' , htmlTreeAsString ( this . _lcpEntry . element ) ) ;
203
+ }
202
204
203
- if ( this . _lcpEntry . url ) {
204
- // Trim URL to the first 200 characters.
205
- transaction . setTag ( 'lcp.url' , this . _lcpEntry . url . trim ( ) . slice ( 0 , 200 ) ) ;
206
- }
205
+ if ( this . _lcpEntry . id ) {
206
+ transaction . setTag ( 'lcp.id' , this . _lcpEntry . id ) ;
207
+ }
207
208
208
- transaction . setTag ( 'lcp.size' , this . _lcpEntry . size ) ;
209
+ if ( this . _lcpEntry . url ) {
210
+ // Trim URL to the first 200 characters.
211
+ transaction . setTag ( 'lcp.url' , this . _lcpEntry . url . trim ( ) . slice ( 0 , 200 ) ) ;
209
212
}
213
+
214
+ transaction . setTag ( 'lcp.size' , this . _lcpEntry . size ) ;
215
+ }
216
+
217
+ if ( this . _clsEntry ) {
218
+ logger . log ( '[Measurements] Adding CLS Data' ) ;
219
+ this . _clsEntry . sources . map ( ( source , index ) =>
220
+ transaction . setTag ( `cls.source.${ index + 1 } ` , htmlTreeAsString ( source . node ) ) ,
221
+ ) ;
210
222
}
211
223
}
212
224
@@ -221,6 +233,7 @@ export class MetricsInstrumentation {
221
233
222
234
logger . log ( '[Measurements] Adding CLS' ) ;
223
235
this . _measurements [ 'cls' ] = { value : metric . value } ;
236
+ this . _clsEntry = entry as LayoutShift ;
224
237
} ) ;
225
238
}
226
239
0 commit comments