Skip to content

Commit 1219b75

Browse files
authored
ref(tracing): Reduce metrics bundle size (#4432)
- Extract out function `_tagMetricInfo` (rename to `tagMetricInfo`) - Update `addPerformanceNavigationTiming` function signature to avoid using object destructure - Small change to entryScriptSrc
1 parent ff923f9 commit 1219b75

File tree

1 file changed

+69
-85
lines changed

1 file changed

+69
-85
lines changed

packages/tracing/src/browser/metrics.ts

Lines changed: 69 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class MetricsInstrumentation {
4444
logger.log('[Tracing] Adding & adjusting spans using Performance API');
4545

4646
const timeOrigin = msToSec(browserPerformanceTimeOrigin);
47-
let entryScriptSrc: string | undefined;
47+
let entryScriptSrc = '';
4848

4949
if (global.document && global.document.scripts) {
5050
// eslint-disable-next-line @typescript-eslint/prefer-for-of
@@ -114,7 +114,7 @@ export class MetricsInstrumentation {
114114
const resourceName = (entry.name as string).replace(global.location.origin, '');
115115
const endTimestamp = addResourceSpans(transaction, entry, resourceName, startTime, duration, timeOrigin);
116116
// We remember the entry script end time to calculate the difference to the first init mark
117-
if (entryScriptStartTimestamp === undefined && (entryScriptSrc || '').indexOf(resourceName) > -1) {
117+
if (entryScriptStartTimestamp === undefined && entryScriptSrc.indexOf(resourceName) > -1) {
118118
entryScriptStartTimestamp = endTimestamp;
119119
}
120120
break;
@@ -194,60 +194,11 @@ export class MetricsInstrumentation {
194194
}
195195

196196
transaction.setMeasurements(this._measurements);
197-
this._tagMetricInfo(transaction);
198-
197+
tagMetricInfo(transaction, this._lcpEntry, this._clsEntry);
199198
transaction.setTag('sentry_reportAllChanges', this._reportAllChanges);
200199
}
201200
}
202201

203-
/** Add LCP / CLS data to transaction to allow debugging */
204-
private _tagMetricInfo(transaction: Transaction): void {
205-
if (this._lcpEntry) {
206-
logger.log('[Measurements] Adding LCP Data');
207-
// Capture Properties of the LCP element that contributes to the LCP.
208-
209-
if (this._lcpEntry.element) {
210-
transaction.setTag('lcp.element', htmlTreeAsString(this._lcpEntry.element));
211-
}
212-
213-
if (this._lcpEntry.id) {
214-
transaction.setTag('lcp.id', this._lcpEntry.id);
215-
}
216-
217-
if (this._lcpEntry.url) {
218-
// Trim URL to the first 200 characters.
219-
transaction.setTag('lcp.url', this._lcpEntry.url.trim().slice(0, 200));
220-
}
221-
222-
transaction.setTag('lcp.size', this._lcpEntry.size);
223-
}
224-
225-
// See: https://developer.mozilla.org/en-US/docs/Web/API/LayoutShift
226-
if (this._clsEntry && this._clsEntry.sources) {
227-
logger.log('[Measurements] Adding CLS Data');
228-
this._clsEntry.sources.forEach((source, index) =>
229-
transaction.setTag(`cls.source.${index + 1}`, htmlTreeAsString(source.node)),
230-
);
231-
}
232-
}
233-
234-
/** Starts tracking the Cumulative Layout Shift on the current page. */
235-
private _trackCLS(): void {
236-
// See:
237-
// https://web.dev/evolving-cls/
238-
// https://web.dev/cls-web-tooling/
239-
getCLS(metric => {
240-
const entry = metric.entries.pop();
241-
if (!entry) {
242-
return;
243-
}
244-
245-
logger.log('[Measurements] Adding CLS');
246-
this._measurements['cls'] = { value: metric.value };
247-
this._clsEntry = entry as LayoutShift;
248-
});
249-
}
250-
251202
/**
252203
* Capture the information of the user agent.
253204
*/
@@ -286,11 +237,27 @@ export class MetricsInstrumentation {
286237
}
287238
}
288239

240+
/** Starts tracking the Cumulative Layout Shift on the current page. */
241+
private _trackCLS(): void {
242+
// See:
243+
// https://web.dev/evolving-cls/
244+
// https://web.dev/cls-web-tooling/
245+
getCLS(metric => {
246+
const entry = metric.entries.pop();
247+
if (!entry) {
248+
return;
249+
}
250+
251+
logger.log('[Measurements] Adding CLS');
252+
this._measurements['cls'] = { value: metric.value };
253+
this._clsEntry = entry as LayoutShift;
254+
});
255+
}
256+
289257
/** Starts tracking the Largest Contentful Paint on the current page. */
290258
private _trackLCP(): void {
291259
getLCP(metric => {
292260
const entry = metric.entries.pop();
293-
294261
if (!entry) {
295262
return;
296263
}
@@ -308,7 +275,6 @@ export class MetricsInstrumentation {
308275
private _trackFID(): void {
309276
getFID(metric => {
310277
const entry = metric.entries.pop();
311-
312278
if (!entry) {
313279
return;
314280
}
@@ -324,28 +290,12 @@ export class MetricsInstrumentation {
324290

325291
/** Instrument navigation entries */
326292
function addNavigationSpans(transaction: Transaction, entry: Record<string, any>, timeOrigin: number): void {
327-
addPerformanceNavigationTiming({ transaction, entry, event: 'unloadEvent', timeOrigin });
328-
addPerformanceNavigationTiming({ transaction, entry, event: 'redirect', timeOrigin });
329-
addPerformanceNavigationTiming({ transaction, entry, event: 'domContentLoadedEvent', timeOrigin });
330-
addPerformanceNavigationTiming({ transaction, entry, event: 'loadEvent', timeOrigin });
331-
addPerformanceNavigationTiming({ transaction, entry, event: 'connect', timeOrigin });
332-
addPerformanceNavigationTiming({
333-
transaction,
334-
entry,
335-
event: 'secureConnection',
336-
timeOrigin,
337-
eventEnd: 'connectEnd',
338-
description: 'TLS/SSL',
293+
['unloadEvent', 'redirect', 'domContentLoadedEvent', 'loadEvent', 'connect'].forEach(event => {
294+
addPerformanceNavigationTiming(transaction, entry, event, timeOrigin);
339295
});
340-
addPerformanceNavigationTiming({
341-
transaction,
342-
entry,
343-
event: 'fetch',
344-
timeOrigin,
345-
eventEnd: 'domainLookupStart',
346-
description: 'cache',
347-
});
348-
addPerformanceNavigationTiming({ transaction, entry, event: 'domainLookup', timeOrigin, description: 'DNS' });
296+
addPerformanceNavigationTiming(transaction, entry, 'secureConnection', timeOrigin, 'TLS/SSL', 'connectEnd');
297+
addPerformanceNavigationTiming(transaction, entry, 'fetch', timeOrigin, 'cache', 'domainLookupStart');
298+
addPerformanceNavigationTiming(transaction, entry, 'domainLookup', timeOrigin, 'DNS');
349299
addRequest(transaction, entry, timeOrigin);
350300
}
351301

@@ -418,16 +368,14 @@ export function addResourceSpans(
418368
}
419369

420370
/** Create performance navigation related spans */
421-
function addPerformanceNavigationTiming(props: {
422-
transaction: Transaction;
423-
entry: Record<string, any>;
424-
event: string;
425-
timeOrigin: number;
426-
eventEnd?: string;
427-
description?: string;
428-
}): void {
429-
const { transaction, entry, event, timeOrigin, eventEnd, description } = props;
430-
371+
function addPerformanceNavigationTiming(
372+
transaction: Transaction,
373+
entry: Record<string, any>,
374+
event: string,
375+
timeOrigin: number,
376+
description?: string,
377+
eventEnd?: string,
378+
): void {
431379
const end = eventEnd ? (entry[eventEnd] as number | undefined) : (entry[`${event}End`] as number | undefined);
432380
const start = entry[`${event}Start`] as number | undefined;
433381
if (!start || !end) {
@@ -480,3 +428,39 @@ export function _startChild(transaction: Transaction, { startTimestamp, ...ctx }
480428
function isMeasurementValue(value: any): boolean {
481429
return typeof value === 'number' && isFinite(value);
482430
}
431+
432+
/** Add LCP / CLS data to transaction to allow debugging */
433+
function tagMetricInfo(
434+
transaction: Transaction,
435+
lcpEntry: MetricsInstrumentation['_lcpEntry'],
436+
clsEntry: MetricsInstrumentation['_clsEntry'],
437+
): void {
438+
if (lcpEntry) {
439+
logger.log('[Measurements] Adding LCP Data');
440+
441+
// Capture Properties of the LCP element that contributes to the LCP.
442+
443+
if (lcpEntry.element) {
444+
transaction.setTag('lcp.element', htmlTreeAsString(lcpEntry.element));
445+
}
446+
447+
if (lcpEntry.id) {
448+
transaction.setTag('lcp.id', lcpEntry.id);
449+
}
450+
451+
if (lcpEntry.url) {
452+
// Trim URL to the first 200 characters.
453+
transaction.setTag('lcp.url', lcpEntry.url.trim().slice(0, 200));
454+
}
455+
456+
transaction.setTag('lcp.size', lcpEntry.size);
457+
}
458+
459+
// See: https://developer.mozilla.org/en-US/docs/Web/API/LayoutShift
460+
if (clsEntry && clsEntry.sources) {
461+
logger.log('[Measurements] Adding CLS Data');
462+
clsEntry.sources.forEach((source, index) =>
463+
transaction.setTag(`cls.source.${index + 1}`, htmlTreeAsString(source.node)),
464+
);
465+
}
466+
}

0 commit comments

Comments
 (0)