Skip to content

Commit 2581f72

Browse files
authored
Metrics: Tag CLS elements (#3734)
1 parent 740443d commit 2581f72

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

packages/tracing/src/browser/metrics.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { browserPerformanceTimeOrigin, getGlobalObject, htmlTreeAsString, isNode
66
import { Span } from '../span';
77
import { Transaction } from '../transaction';
88
import { msToSec } from '../utils';
9-
import { getCLS } from './web-vitals/getCLS';
9+
import { getCLS, LayoutShift } from './web-vitals/getCLS';
1010
import { getFID } from './web-vitals/getFID';
1111
import { getLCP, LargestContentfulPaint } from './web-vitals/getLCP';
1212
import { 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

packages/tracing/src/browser/web-vitals/getCLS.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,17 @@ import { onHidden } from './lib/onHidden';
2121
import { ReportHandler } from './types';
2222

2323
// https://wicg.github.io/layout-instability/#sec-layout-shift
24-
interface LayoutShift extends PerformanceEntry {
24+
export interface LayoutShift extends PerformanceEntry {
2525
value: number;
2626
hadRecentInput: boolean;
27+
sources: Array<LayoutShiftAttribution>;
28+
toJSON(): Record<string, unknown>;
29+
}
30+
31+
export interface LayoutShiftAttribution {
32+
node?: Node;
33+
previousRect: DOMRectReadOnly;
34+
currentRect: DOMRectReadOnly;
2735
}
2836

2937
export const getCLS = (onReport: ReportHandler, reportAllChanges = false): void => {

0 commit comments

Comments
 (0)