Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions packages/tracing/src/browser/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { browserPerformanceTimeOrigin, getGlobalObject, htmlTreeAsString, isNode
import { Span } from '../span';
import { Transaction } from '../transaction';
import { msToSec } from '../utils';
import { getCLS } from './web-vitals/getCLS';
import { getCLS, LayoutShift } from './web-vitals/getCLS';
import { getFID } from './web-vitals/getFID';
import { getLCP, LargestContentfulPaint } from './web-vitals/getLCP';
import { getFirstHidden } from './web-vitals/lib/getFirstHidden';
Expand All @@ -20,6 +20,7 @@ export class MetricsInstrumentation {

private _performanceCursor: number = 0;
private _lcpEntry: LargestContentfulPaint | undefined;
private _clsEntry: LayoutShift | undefined;

public constructor() {
if (!isNodeEnv() && global?.performance) {
Expand Down Expand Up @@ -187,26 +188,37 @@ export class MetricsInstrumentation {
}

transaction.setMeasurements(this._measurements);
this._tagMetricInfo(transaction);
}
}

if (this._lcpEntry) {
logger.log('[Measurements] Adding LCP Data');
// Capture Properties of the LCP element that contributes to the LCP.

if (this._lcpEntry.element) {
transaction.setTag('lcp.element', htmlTreeAsString(this._lcpEntry.element));
}
/** Add LCP / CLS data to transaction to allow debugging */
private _tagMetricInfo(transaction: Transaction): void {
if (this._lcpEntry) {
logger.log('[Measurements] Adding LCP Data');
// Capture Properties of the LCP element that contributes to the LCP.

if (this._lcpEntry.id) {
transaction.setTag('lcp.id', this._lcpEntry.id);
}
if (this._lcpEntry.element) {
transaction.setTag('lcp.element', htmlTreeAsString(this._lcpEntry.element));
}

if (this._lcpEntry.url) {
// Trim URL to the first 200 characters.
transaction.setTag('lcp.url', this._lcpEntry.url.trim().slice(0, 200));
}
if (this._lcpEntry.id) {
transaction.setTag('lcp.id', this._lcpEntry.id);
}

transaction.setTag('lcp.size', this._lcpEntry.size);
if (this._lcpEntry.url) {
// Trim URL to the first 200 characters.
transaction.setTag('lcp.url', this._lcpEntry.url.trim().slice(0, 200));
}

transaction.setTag('lcp.size', this._lcpEntry.size);
}

if (this._clsEntry) {
logger.log('[Measurements] Adding CLS Data');
this._clsEntry.sources.map((source, index) =>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're seeing some error when running Cypress tests with Chrome in headless mode which I am pretty sure relates to this map. Can sources be undefined?

image

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

transaction.setTag(`cls.source.${index + 1}`, htmlTreeAsString(source.node)),
);
}
}

Expand All @@ -221,6 +233,7 @@ export class MetricsInstrumentation {

logger.log('[Measurements] Adding CLS');
this._measurements['cls'] = { value: metric.value };
this._clsEntry = entry as LayoutShift;
});
}

Expand Down
10 changes: 9 additions & 1 deletion packages/tracing/src/browser/web-vitals/getCLS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ import { onHidden } from './lib/onHidden';
import { ReportHandler } from './types';

// https://wicg.github.io/layout-instability/#sec-layout-shift
interface LayoutShift extends PerformanceEntry {
export interface LayoutShift extends PerformanceEntry {
value: number;
hadRecentInput: boolean;
sources: Array<LayoutShiftAttribution>;
toJSON(): Record<string, unknown>;
}

export interface LayoutShiftAttribution {
node?: Node;
previousRect: DOMRectReadOnly;
currentRect: DOMRectReadOnly;
}

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