-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(browser): split web vitals integration #21210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
logaretm
wants to merge
4
commits into
develop
Choose a base branch
from
awad/js-2628-split-web-vitals-into-their-own-integration
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
433f30a
feat(browser): split web vitals integration
logaretm 301fee7
refactor(browser): rename web vitals `disable` option to `ignore`
logaretm fb0e9d0
refactor(browser): move all web vital logic into webVitalsIntegration
logaretm 98a76d7
fix(browser): keep connection.rtt a pageload-only measurement
logaretm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
...ckages/browser-integration-tests/suites/tracing/metrics/connection-rtt-navigation/init.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import * as Sentry from '@sentry/browser'; | ||
|
|
||
| window.Sentry = Sentry; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| integrations: [ | ||
| Sentry.browserTracingIntegration({ | ||
| idleTimeout: 1000, | ||
| }), | ||
| ], | ||
| tracesSampleRate: 1, | ||
| }); |
9 changes: 9 additions & 0 deletions
9
.../browser-integration-tests/suites/tracing/metrics/connection-rtt-navigation/template.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <!doctype html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| </head> | ||
| <body> | ||
| <div>Rendered</div> | ||
| </body> | ||
| </html> |
28 changes: 28 additions & 0 deletions
28
...ckages/browser-integration-tests/suites/tracing/metrics/connection-rtt-navigation/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { expect } from '@playwright/test'; | ||
| import type { Event } from '@sentry/core'; | ||
| import { sentryTest } from '../../../../utils/fixtures'; | ||
| import { getFirstSentryEnvelopeRequest, shouldSkipTracingTest } from '../../../../utils/helpers'; | ||
|
|
||
| sentryTest.beforeEach(({ browserName }) => { | ||
| if (shouldSkipTracingTest() || browserName !== 'chromium') { | ||
| sentryTest.skip(); | ||
| } | ||
| }); | ||
|
|
||
| // `connection.rtt` is recorded as a measurement, which is only flushed on the pageload | ||
| // transaction. It must not leak onto navigation transactions. | ||
| sentryTest( | ||
| 'records `connection.rtt` as a measurement on pageload but not on navigation transactions', | ||
| async ({ getLocalTestUrl, page }) => { | ||
| const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
|
||
| const pageloadRequest = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
| const navigationRequest = await getFirstSentryEnvelopeRequest<Event>(page, `${url}#foo`); | ||
|
|
||
| expect(pageloadRequest.contexts?.trace?.op).toBe('pageload'); | ||
| expect(navigationRequest.contexts?.trace?.op).toBe('navigation'); | ||
|
|
||
| expect(pageloadRequest.measurements?.['connection.rtt']?.value).toBeDefined(); | ||
| expect(navigationRequest.measurements?.['connection.rtt']).toBeUndefined(); | ||
| }, | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import type { Client, IntegrationFn, Span } from '@sentry/core/browser'; | ||
| import { defineIntegration, hasSpanStreamingEnabled } from '@sentry/core/browser'; | ||
| import { | ||
| addWebVitalsToSpan, | ||
| registerInpInteractionListener, | ||
| startTrackingINP, | ||
| startTrackingWebVitals, | ||
| trackClsAsSpan, | ||
| trackInpAsSpan, | ||
| trackLcpAsSpan, | ||
| } from '@sentry-internal/browser-utils'; | ||
|
|
||
| export const WEB_VITALS_INTEGRATION_NAME = 'WebVitals'; | ||
|
|
||
| export type WebVitalName = 'cls' | 'inp' | 'lcp'; | ||
|
|
||
| export interface WebVitalsOptions { | ||
| /** | ||
| * Web vitals to skip. | ||
| */ | ||
| ignore?: WebVitalName[]; | ||
|
|
||
| /** | ||
| * @experimental | ||
| */ | ||
| _experiments?: Partial<{ | ||
| enableStandaloneClsSpans: boolean; | ||
| enableStandaloneLcpSpans: boolean; | ||
| }>; | ||
| } | ||
|
|
||
| const collectWebVitalsCallbacks = new WeakMap<Client, (span: Span) => void>(); | ||
|
|
||
| /** | ||
| * Finalizes the collected web vitals and writes them onto the given (pageload) span. | ||
| * Called by `browserTracingIntegration` when the pageload/navigation span ends. | ||
| */ | ||
| export function collectWebVitalsForClient(client: Client, span: Span): void { | ||
| collectWebVitalsCallbacks.get(client)?.(span); | ||
| } | ||
|
|
||
| /** | ||
| * Captures Core Web Vitals (LCP, CLS, INP) and related pageload vitals. | ||
| * | ||
| * `browserTracingIntegration` auto-registers this integration if no | ||
| * `webVitalsIntegration` is already present, so explicit registration is only | ||
| * needed to customize options or to use it without `browserTracingIntegration`. | ||
| */ | ||
| export const webVitalsIntegration = defineIntegration((options: WebVitalsOptions = {}) => { | ||
| const ignored = new Set(options.ignore ?? []); | ||
|
|
||
| return { | ||
| name: WEB_VITALS_INTEGRATION_NAME, | ||
| setup(client) { | ||
| const spanStreamingEnabled = hasSpanStreamingEnabled(client); | ||
| const { enableStandaloneClsSpans, enableStandaloneLcpSpans } = options._experiments ?? {}; | ||
|
|
||
| const recordClsStandaloneSpans = | ||
| spanStreamingEnabled || ignored.has('cls') ? undefined : enableStandaloneClsSpans || false; | ||
| const recordLcpStandaloneSpans = | ||
| spanStreamingEnabled || ignored.has('lcp') ? undefined : enableStandaloneLcpSpans || false; | ||
|
|
||
| const finalizeWebVitals = startTrackingWebVitals({ | ||
| recordClsStandaloneSpans, | ||
| recordLcpStandaloneSpans, | ||
| client, | ||
| }); | ||
|
|
||
| collectWebVitalsCallbacks.set(client, span => { | ||
| finalizeWebVitals(); | ||
| addWebVitalsToSpan(span, { | ||
| // CLS/LCP are recorded as pageload span measurements only when they're neither | ||
| // tracked as standalone spans nor handled by span streaming (and not ignored). | ||
| recordClsOnPageloadSpan: recordClsStandaloneSpans === false, | ||
| recordLcpOnPageloadSpan: recordLcpStandaloneSpans === false, | ||
| spanStreamingEnabled, | ||
| }); | ||
| }); | ||
|
|
||
| if (spanStreamingEnabled) { | ||
| if (!ignored.has('lcp')) { | ||
| trackLcpAsSpan(client); | ||
| } | ||
| if (!ignored.has('cls')) { | ||
| trackClsAsSpan(client); | ||
| } | ||
| if (!ignored.has('inp')) { | ||
| trackInpAsSpan(); | ||
| } | ||
| } else if (!ignored.has('inp')) { | ||
| startTrackingINP(); | ||
| } | ||
| }, | ||
| afterAllSetup() { | ||
| if (!ignored.has('inp')) { | ||
| registerInpInteractionListener(); | ||
| } | ||
| }, | ||
| }; | ||
| }) satisfies IntegrationFn; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E2E test uses unreliable
getFirstSentryEnvelopeRequesthelperMedium Severity
The new E2E test uses
getFirstSentryEnvelopeRequestwhich is flagged as unreliable by the project rules. The sequential pattern of awaiting the pageload request and then immediately navigating and awaiting the navigation request is prone to race conditions — stale or unrelated envelopes (e.g. session updates) could be captured instead of the expected transaction. Helpers likewaitForTransactionwith appropriate filtering would be more reliable.Triggered by project rule: PR Review Guidelines for Cursor Bot
Reviewed by Cursor Bugbot for commit 98a76d7. Configure here.