Skip to content

Commit e69c3ea

Browse files
committed
feat(perf): Add experimental option to improve LCP collection
This will add an experimental option (options._metricOptions._reportAllChanges) that will set 'getLCP' to always report. Now that we are recording LCP element, it should be sufficient to collection the additional information and let the user pick the appropriate LCP element in the ui to improve accuracy dynamically instead. We'd like to test this out on Sentry first.
1 parent a27c0fa commit e69c3ea

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

packages/tracing/src/browser/browsertracing.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ export interface BrowserTracingOptions extends RequestInstrumentationOptions {
6060
*/
6161
markBackgroundTransactions: boolean;
6262

63+
/**
64+
* _metricOptions allows the user to send options to change how metrics are collected.
65+
*
66+
* _metricOptions is currently experimental.
67+
*
68+
* Default: undefined
69+
*/
70+
_metricOptions?: BrowserMetricOptions;
71+
6372
/**
6473
* beforeNavigate is called before a pageload/navigation transaction is created and allows users to modify transaction
6574
* context data, or drop the transaction entirely (by setting `sampled = false` in the context).
@@ -83,6 +92,13 @@ export interface BrowserTracingOptions extends RequestInstrumentationOptions {
8392
): void;
8493
}
8594

95+
/**
96+
* Exports a way to add options to our metric collection. Currently experimental.
97+
*/
98+
export interface BrowserMetricOptions {
99+
_reportAllChanges?: boolean;
100+
}
101+
86102
const DEFAULT_BROWSER_TRACING_OPTIONS = {
87103
idleTimeout: DEFAULT_IDLE_TIMEOUT,
88104
markBackgroundTransactions: true,
@@ -116,7 +132,7 @@ export class BrowserTracing implements Integration {
116132

117133
private _getCurrentHub?: () => Hub;
118134

119-
private readonly _metrics: MetricsInstrumentation = new MetricsInstrumentation();
135+
private readonly _metrics: MetricsInstrumentation;
120136

121137
private readonly _emitOptionsWarning: boolean = false;
122138

@@ -139,6 +155,8 @@ export class BrowserTracing implements Integration {
139155
..._options,
140156
tracingOrigins,
141157
};
158+
159+
this._metrics = new MetricsInstrumentation(this.options._metricOptions);
142160
}
143161

144162
/**

packages/tracing/src/browser/metrics.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { browserPerformanceTimeOrigin, getGlobalObject, htmlTreeAsString, isNode
66
import { Span } from '../span';
77
import { Transaction } from '../transaction';
88
import { msToSec } from '../utils';
9+
import { BrowserMetricOptions } from './browsertracing';
910
import { getCLS, LayoutShift } from './web-vitals/getCLS';
1011
import { getFID } from './web-vitals/getFID';
1112
import { getLCP, LargestContentfulPaint } from './web-vitals/getLCP';
@@ -22,15 +23,15 @@ export class MetricsInstrumentation {
2223
private _lcpEntry: LargestContentfulPaint | undefined;
2324
private _clsEntry: LayoutShift | undefined;
2425

25-
public constructor() {
26+
public constructor(_options?: BrowserMetricOptions) {
2627
if (!isNodeEnv() && global?.performance) {
2728
if (global.performance.mark) {
2829
global.performance.mark('sentry-tracing-init');
2930
}
3031

31-
this._trackCLS();
32-
this._trackLCP();
33-
this._trackFID();
32+
this._trackCLS(_options);
33+
this._trackLCP(_options);
34+
this._trackFID(_options);
3435
}
3536
}
3637

@@ -230,7 +231,7 @@ export class MetricsInstrumentation {
230231
}
231232

232233
/** Starts tracking the Cumulative Layout Shift on the current page. */
233-
private _trackCLS(): void {
234+
private _trackCLS(_options?: BrowserMetricOptions): void {
234235
// See:
235236
// https://web.dev/evolving-cls/
236237
// https://web.dev/cls-web-tooling/
@@ -285,7 +286,7 @@ export class MetricsInstrumentation {
285286
}
286287

287288
/** Starts tracking the Largest Contentful Paint on the current page. */
288-
private _trackLCP(): void {
289+
private _trackLCP(_options?: BrowserMetricOptions): void {
289290
getLCP(metric => {
290291
const entry = metric.entries.pop();
291292

@@ -299,11 +300,11 @@ export class MetricsInstrumentation {
299300
this._measurements['lcp'] = { value: metric.value };
300301
this._measurements['mark.lcp'] = { value: timeOrigin + startTime };
301302
this._lcpEntry = entry as LargestContentfulPaint;
302-
});
303+
}, _options?._reportAllChanges);
303304
}
304305

305306
/** Starts tracking the First Input Delay on the current page. */
306-
private _trackFID(): void {
307+
private _trackFID(_options?: BrowserMetricOptions): void {
307308
getFID(metric => {
308309
const entry = metric.entries.pop();
309310

0 commit comments

Comments
 (0)