@@ -6,7 +6,7 @@ import { browserPerformanceTimeOrigin, getGlobalObject, htmlTreeAsString, isNode
66import { Span } from '../span' ;
77import { Transaction } from '../transaction' ;
88import { msToSec } from '../utils' ;
9- import { getCLS } from './web-vitals/getCLS' ;
9+ import { getCLS , LayoutShift } from './web-vitals/getCLS' ;
1010import { getFID } from './web-vitals/getFID' ;
1111import { getLCP , LargestContentfulPaint } from './web-vitals/getLCP' ;
1212import { getFirstHidden } from './web-vitals/lib/getFirstHidden' ;
@@ -20,6 +20,7 @@ export class MetricsInstrumentation {
2020
2121 private _performanceCursor : number = 0 ;
2222 private _lcpEntry : LargestContentfulPaint | undefined ;
23+ private _clsEntry : LayoutShift | undefined ;
2324
2425 public constructor ( ) {
2526 if ( ! isNodeEnv ( ) && global ?. performance ) {
@@ -187,26 +188,37 @@ export class MetricsInstrumentation {
187188 }
188189
189190 transaction . setMeasurements ( this . _measurements ) ;
191+ this . _tagMetricInfo ( transaction ) ;
192+ }
193+ }
190194
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.
198200
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+ }
202204
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+ }
207208
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 ) ) ;
209212 }
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+ ) ;
210222 }
211223 }
212224
@@ -221,6 +233,7 @@ export class MetricsInstrumentation {
221233
222234 logger . log ( '[Measurements] Adding CLS' ) ;
223235 this . _measurements [ 'cls' ] = { value : metric . value } ;
236+ this . _clsEntry = entry as LayoutShift ;
224237 } ) ;
225238 }
226239
0 commit comments