Skip to content

Commit c7c6bb2

Browse files
authored
ref(browser): Store browser metrics as attributes instead of tags (#10823)
Extracted this out from #10808, as this is a bit more impactful, possibly.
1 parent f51e826 commit c7c6bb2

File tree

4 files changed

+22
-40
lines changed
  • dev-packages/browser-integration-tests/suites/tracing/metrics
  • packages/tracing-internal/src/browser/metrics

4 files changed

+22
-40
lines changed

dev-packages/browser-integration-tests/suites/tracing/metrics/handlers-lcp/test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ sentryTest(
2929
expect(eventData.measurements).toBeDefined();
3030
expect(eventData.measurements?.lcp?.value).toBeDefined();
3131

32-
expect(eventData.tags?.['lcp.element']).toBe('body > img');
33-
expect(eventData.tags?.['lcp.size']).toBe(107400);
34-
expect(eventData.tags?.['lcp.url']).toBe('https://example.com/path/to/image.png');
32+
expect(eventData.contexts?.trace?.data?.['lcp.element']).toBe('body > img');
33+
expect(eventData.contexts?.trace?.data?.['lcp.size']).toBe(107400);
34+
expect(eventData.contexts?.trace?.data?.['lcp.url']).toBe('https://example.com/path/to/image.png');
3535

3636
const lcp = await (await page.waitForFunction('window._LCP')).jsonValue();
3737
const lcp2 = await (await page.waitForFunction('window._LCP2')).jsonValue();

dev-packages/browser-integration-tests/suites/tracing/metrics/web-vitals-cls/test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ sentryTest('should capture a "GOOD" CLS vital with its source(s).', async ({ get
2323
expect(eventData.measurements?.cls?.value).toBeGreaterThan(0.03);
2424
expect(eventData.measurements?.cls?.value).toBeLessThan(0.07);
2525

26-
expect(eventData.tags?.['cls.source.1']).toBe('body > div#content > p#partial');
26+
expect(eventData.contexts?.trace?.data?.['cls.source.1']).toBe('body > div#content > p#partial');
2727
});
2828

2929
sentryTest('should capture a "MEH" CLS vital with its source(s).', async ({ getLocalTestPath, page }) => {
@@ -37,7 +37,7 @@ sentryTest('should capture a "MEH" CLS vital with its source(s).', async ({ getL
3737
expect(eventData.measurements?.cls?.value).toBeGreaterThan(0.18);
3838
expect(eventData.measurements?.cls?.value).toBeLessThan(0.23);
3939

40-
expect(eventData.tags?.['cls.source.1']).toBe('body > div#content > p');
40+
expect(eventData.contexts?.trace?.data?.['cls.source.1']).toBe('body > div#content > p');
4141
});
4242

4343
sentryTest('should capture a "POOR" CLS vital with its source(s).', async ({ getLocalTestPath, page }) => {
@@ -50,5 +50,5 @@ sentryTest('should capture a "POOR" CLS vital with its source(s).', async ({ get
5050
// Flakey value dependent on timings -> we check for a range
5151
expect(eventData.measurements?.cls?.value).toBeGreaterThan(0.34);
5252
expect(eventData.measurements?.cls?.value).toBeLessThan(0.36);
53-
expect(eventData.tags?.['cls.source.1']).toBe('body > div#content > p');
53+
expect(eventData.contexts?.trace?.data?.['cls.source.1']).toBe('body > div#content > p');
5454
});

dev-packages/browser-integration-tests/suites/tracing/metrics/web-vitals-lcp/test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ sentryTest('should capture a LCP vital with element details.', async ({ browserN
1010
sentryTest.skip();
1111
}
1212

13-
await page.route('**/path/to/image.png', (route: Route) =>
14-
route.fulfill({ path: `${__dirname}/assets/sentry-logo-600x179.png` }),
15-
);
13+
page.route('**', route => route.continue());
14+
page.route('**/path/to/image.png', async (route: Route) => {
15+
return route.fulfill({ path: `${__dirname}/assets/sentry-logo-600x179.png` });
16+
});
1617

1718
const url = await getLocalTestPath({ testDir: __dirname });
1819
const [eventData] = await Promise.all([
@@ -24,7 +25,7 @@ sentryTest('should capture a LCP vital with element details.', async ({ browserN
2425
expect(eventData.measurements).toBeDefined();
2526
expect(eventData.measurements?.lcp?.value).toBeDefined();
2627

27-
expect(eventData.tags?.['lcp.element']).toBe('body > img');
28-
expect(eventData.tags?.['lcp.size']).toBe(107400);
29-
expect(eventData.tags?.['lcp.url']).toBe('https://example.com/path/to/image.png');
28+
expect(eventData.contexts?.trace?.data?.['lcp.element']).toBe('body > img');
29+
expect(eventData.contexts?.trace?.data?.['lcp.size']).toBe(107400);
30+
expect(eventData.contexts?.trace?.data?.['lcp.url']).toBe('https://example.com/path/to/image.png');
3031
});

packages/tracing-internal/src/browser/metrics/index.ts

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* eslint-disable max-lines */
2-
import type { SentrySpan } from '@sentry/core';
32
import { getActiveSpan, startInactiveSpan } from '@sentry/core';
43
import { setMeasurement } from '@sentry/core';
54
import type { Measurements, Span, StartSpanOptions } from '@sentry/types';
@@ -445,15 +444,11 @@ function _trackNavigator(span: Span): void {
445444
const connection = navigator.connection;
446445
if (connection) {
447446
if (connection.effectiveType) {
448-
// TODO: Can we rewrite this to an attribute?
449-
// eslint-disable-next-line deprecation/deprecation
450-
(span as SentrySpan).setTag('effectiveConnectionType', connection.effectiveType);
447+
span.setAttribute('effectiveConnectionType', connection.effectiveType);
451448
}
452449

453450
if (connection.type) {
454-
// TODO: Can we rewrite this to an attribute?
455-
// eslint-disable-next-line deprecation/deprecation
456-
(span as SentrySpan).setTag('connectionType', connection.type);
451+
span.setAttribute('connectionType', connection.type);
457452
}
458453

459454
if (isMeasurementValue(connection.rtt)) {
@@ -462,15 +457,11 @@ function _trackNavigator(span: Span): void {
462457
}
463458

464459
if (isMeasurementValue(navigator.deviceMemory)) {
465-
// TODO: Can we rewrite this to an attribute?
466-
// eslint-disable-next-line deprecation/deprecation
467-
(span as SentrySpan).setTag('deviceMemory', `${navigator.deviceMemory} GB`);
460+
span.setAttribute('deviceMemory', `${navigator.deviceMemory} GB`);
468461
}
469462

470463
if (isMeasurementValue(navigator.hardwareConcurrency)) {
471-
// TODO: Can we rewrite this to an attribute?
472-
// eslint-disable-next-line deprecation/deprecation
473-
(span as SentrySpan).setTag('hardwareConcurrency', String(navigator.hardwareConcurrency));
464+
span.setAttribute('hardwareConcurrency', String(navigator.hardwareConcurrency));
474465
}
475466
}
476467

@@ -482,36 +473,26 @@ function _tagMetricInfo(span: Span): void {
482473
// Capture Properties of the LCP element that contributes to the LCP.
483474

484475
if (_lcpEntry.element) {
485-
// TODO: Can we rewrite this to an attribute?
486-
// eslint-disable-next-line deprecation/deprecation
487-
(span as SentrySpan).setTag('lcp.element', htmlTreeAsString(_lcpEntry.element));
476+
span.setAttribute('lcp.element', htmlTreeAsString(_lcpEntry.element));
488477
}
489478

490479
if (_lcpEntry.id) {
491-
// TODO: Can we rewrite this to an attribute?
492-
// eslint-disable-next-line deprecation/deprecation
493-
(span as SentrySpan).setTag('lcp.id', _lcpEntry.id);
480+
span.setAttribute('lcp.id', _lcpEntry.id);
494481
}
495482

496483
if (_lcpEntry.url) {
497484
// Trim URL to the first 200 characters.
498-
// TODO: Can we rewrite this to an attribute?
499-
// eslint-disable-next-line deprecation/deprecation
500-
(span as SentrySpan).setTag('lcp.url', _lcpEntry.url.trim().slice(0, 200));
485+
span.setAttribute('lcp.url', _lcpEntry.url.trim().slice(0, 200));
501486
}
502487

503-
// TODO: Can we rewrite this to an attribute?
504-
// eslint-disable-next-line deprecation/deprecation
505-
(span as SentrySpan).setTag('lcp.size', _lcpEntry.size);
488+
span.setAttribute('lcp.size', _lcpEntry.size);
506489
}
507490

508491
// See: https://developer.mozilla.org/en-US/docs/Web/API/LayoutShift
509492
if (_clsEntry && _clsEntry.sources) {
510493
DEBUG_BUILD && logger.log('[Measurements] Adding CLS Data');
511494
_clsEntry.sources.forEach((source, index) =>
512-
// TODO: Can we rewrite this to an attribute?
513-
// eslint-disable-next-line deprecation/deprecation
514-
(span as SentrySpan).setTag(`cls.source.${index + 1}`, htmlTreeAsString(source.node)),
495+
span.setAttribute(`cls.source.${index + 1}`, htmlTreeAsString(source.node)),
515496
);
516497
}
517498
}

0 commit comments

Comments
 (0)