Skip to content

feat(browser): Emit web vitals as streamed spans#19827

Merged
Lms24 merged 22 commits intodevelopfrom
awad/js-17931-webvitals-v2-spans
Apr 16, 2026
Merged

feat(browser): Emit web vitals as streamed spans#19827
Lms24 merged 22 commits intodevelopfrom
awad/js-17931-webvitals-v2-spans

Conversation

@logaretm
Copy link
Copy Markdown
Member

@logaretm logaretm commented Mar 16, 2026

Summary

(Updated from @Lms24)

Closes #17931

When span streaming is enabled (traceLifecycle: 'stream'), emit web vital values as non-standalone spans that flow through the v2 pipeline (afterSpanEndcaptureSpan()SpanBuffer).

  • Emit LCP, CLS, INP as streamed spans when hasSpanStreamingEnabled(client) is true
  • LCP, CLS, INP v2 spans exclusively send new browser.web_vital.<vitalName>.* attributes
  • Disable standalone CLS/LCP spans when span streaming is enabled (!spanStreamingEnabled && enableStandaloneClsSpans)
  • Add MAX_PLAUSIBLE_INP_DURATION (60s) sanity check to streamed INP path, matching the existing standalone handler
  • TTFB, FCP, FP are not emitted as spans — they stay as pageload span attributes but get sent with the new browser.web_vital.(ttfb|fcp|fp).value attributes
  • Performance meta attributes are sent with better names (feat(attribute): Add browser performance attributes sentry-conventions#321)
  • Added integration tests
  • LCP v2 spans now have a duration and are no longer point in time

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 16, 2026

size-limit report 📦

Path Size % Change Change
@sentry/browser 25.78 kB - -
@sentry/browser - with treeshaking flags 24.27 kB - -
@sentry/browser (incl. Tracing) 43.61 kB +1.98% +843 B 🔺
@sentry/browser (incl. Tracing, Profiling) 48.51 kB +2.36% +1.11 kB 🔺
@sentry/browser (incl. Tracing, Replay) 82.74 kB +1.29% +1.05 kB 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 72.25 kB +1.45% +1.03 kB 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 87.42 kB +1.2% +1.03 kB 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 99.67 kB +1.1% +1.07 kB 🔺
@sentry/browser (incl. Feedback) 42.59 kB - -
@sentry/browser (incl. sendFeedback) 30.45 kB - -
@sentry/browser (incl. FeedbackAsync) 35.45 kB - -
@sentry/browser (incl. Metrics) 27.07 kB - -
@sentry/browser (incl. Logs) 27.2 kB - -
@sentry/browser (incl. Metrics & Logs) 27.89 kB - -
@sentry/react 27.53 kB - -
@sentry/react (incl. Tracing) 45.88 kB +1.76% +791 B 🔺
@sentry/vue 30.61 kB - -
@sentry/vue (incl. Tracing) 45.45 kB +1.86% +826 B 🔺
@sentry/svelte 25.8 kB - -
CDN Bundle 28.46 kB - -
CDN Bundle (incl. Tracing) 44.69 kB +1.99% +871 B 🔺
CDN Bundle (incl. Logs, Metrics) 29.83 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 45.77 kB +1.97% +882 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) 68.73 kB - -
CDN Bundle (incl. Tracing, Replay) 81.65 kB +1.08% +867 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 82.73 kB +1.11% +901 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 87.17 kB +1% +857 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 88.23 kB +1.03% +895 B 🔺
CDN Bundle - uncompressed 83.12 kB - -
CDN Bundle (incl. Tracing) - uncompressed 133.64 kB +2.85% +3.69 kB 🔺
CDN Bundle (incl. Logs, Metrics) - uncompressed 87.27 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 137.05 kB +2.77% +3.69 kB 🔺
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 210.63 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 250.87 kB +1.49% +3.66 kB 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 254.27 kB +1.47% +3.66 kB 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 263.78 kB +1.41% +3.66 kB 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 267.17 kB +1.4% +3.66 kB 🔺
@sentry/nextjs (client) 48.42 kB +1.89% +894 B 🔺
@sentry/sveltekit (client) 44.06 kB +1.89% +813 B 🔺
@sentry/node-core 57.94 kB +0.01% +5 B 🔺
@sentry/node 174.8 kB +0.01% +10 B 🔺
@sentry/node - without tracing 97.88 kB +0.03% +23 B 🔺
@sentry/aws-serverless 115.13 kB +0.01% +8 B 🔺
@sentry/browser (incl. Tracing + Span Streaming) 45.32 kB added added

View base workflow run

@Lms24 Lms24 force-pushed the lms/feat-span-first branch from 8bf8eaf to c966a4a Compare March 18, 2026 17:14
@logaretm logaretm force-pushed the awad/js-17931-webvitals-v2-spans branch 2 times, most recently from 1a5cfb3 to 28c0d45 Compare March 18, 2026 18:28
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 18, 2026

Semver Impact of This PR

🟡 Minor (new features)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

Cloudflare

  • Split alarms into multiple traces and link them by JPeer264 in #19373
  • Propagate traceparent to RPC calls - via fetch by JPeer264 in #19991

Core

  • Automatically disable truncation when span streaming is enabled in LangGraph integration by andreiborza in #20231
  • Automatically disable truncation when span streaming is enabled in LangChain integration by andreiborza in #20230
  • Automatically disable truncation when span streaming is enabled in Google GenAI integration by andreiborza in #20229
  • Automatically disable truncation when span streaming is enabled in Anthropic AI integration by andreiborza in #20228
  • Automatically disable truncation when span streaming is enabled in Vercel AI integration by andreiborza in #20232
  • Automatically disable truncation when span streaming is enabled in OpenAI integration by andreiborza in #20227
  • Add enableTruncation option to Vercel AI integration by nicohrubec in #20195
  • Add enableTruncation option to Google GenAI integration by andreiborza in #20184
  • Add enableTruncation option to Anthropic AI integration by andreiborza in #20181
  • Add enableTruncation option to LangGraph integration by andreiborza in #20183
  • Add enableTruncation option to LangChain integration by andreiborza in #20182
  • Add enableTruncation option to OpenAI integration by andreiborza in #20167
  • Export a reusable function to add tracing headers by JPeer264 in #20076

Deps

  • Bump axios from 1.13.5 to 1.15.0 by dependabot in #20180
  • Bump hono from 4.12.7 to 4.12.12 by dependabot in #20118
  • Bump defu from 6.1.4 to 6.1.6 by dependabot in #20104

Other

  • (browser) Emit web vitals as streamed spans by logaretm in #19827
  • (node) Include global scope for eventLoopBlockIntegration by timfish in #20108
  • (node-native) Add support for V8 v14 (Node v25+) by timfish in #20125

Bug Fixes 🐛

Deno

  • Handle reader.closed rejection from releaseLock() in streaming by andreiborza in #20187
  • Avoid inferring invalid span op from Deno tracer by Lms24 in #20128

Other

  • (ci) Prevent command injection in ci-metadata workflow by fix-it-felix-sentry in #19899
  • (core, node) Support loading Express options lazily by isaacs in #20211
  • (e2e) Add op check to waitForTransaction in React Router e2e tests by copilot-swe-agent in #20193
  • (node-integration-tests) Fix flaky kafkajs test race condition by copilot-swe-agent in #20189

Internal Changes 🔧

Ci

  • Remove node-overhead GitHub Action by mydea in #20246
  • Bump dorny/paths-filter from v3.0.1 to v4.0.1 by mydea in #20251
  • Remove codecov steps from jobs that produce no coverage/JUnit data by mydea in #20244

Deps

  • Bump hono from 4.12.7 to 4.12.12 in /dev-packages/e2e-tests/test-applications/cloudflare-hono by dependabot in #20119
  • Bump axios from 1.13.5 to 1.15.0 in /dev-packages/e2e-tests/test-applications/nestjs-basic by dependabot in #20179

Other

  • (bugbot) Add rules to flag test-flake-provoking patterns by Lms24 in #20192
  • (deps-dev) Bump vite from 7.2.0 to 7.3.2 in /dev-packages/e2e-tests/test-applications/tanstackstart-react by dependabot in #20107
  • (react) Remove duplicated test mock by s1gr1d in #20200
  • (size-limit) Bump failing size limit scenario by Lms24 in #20186
  • Fix lint warnings by mydea in #20250
  • Fix flaky ANR test by increasing blocking duration by JPeer264 in #20239
  • Add automatic flaky test detector by nicohrubec in #18684

🤖 This preview updates automatically when you update the PR.

@logaretm logaretm marked this pull request as ready for review March 18, 2026 18:29
@logaretm logaretm requested review from Lms24 and Copilot and removed request for Copilot March 18, 2026 18:29
Comment thread packages/browser-utils/src/metrics/webVitalSpans.ts Outdated
Comment thread packages/browser-utils/src/metrics/webVitalSpans.ts
Comment thread packages/browser/src/tracing/browserTracingIntegration.ts Outdated
Comment thread packages/browser/src/tracing/browserTracingIntegration.ts
@Lms24 Lms24 force-pushed the lms/feat-span-first branch from c966a4a to 5963170 Compare March 23, 2026 09:41
@logaretm logaretm force-pushed the awad/js-17931-webvitals-v2-spans branch from 28c0d45 to af2969e Compare March 23, 2026 18:03
Comment thread packages/browser/src/tracing/browserTracingIntegration.ts Outdated
Copilot AI review requested due to automatic review settings March 23, 2026 18:14
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds support for emitting certain Web Vitals as streamed (v2 pipeline) spans when traceLifecycle: 'stream' / span streaming is enabled, while keeping existing pageload measurements in place.

Changes:

  • Gate standalone CLS/LCP spans off when span streaming is enabled, and wire up streamed LCP/CLS/INP emission from the browser tracing integration.
  • Introduce webVitalSpans.ts helpers + unit tests for emitting streamed Web Vital spans.
  • Add Playwright integration tests for streamed LCP and CLS spans; export INP_ENTRY_MAP; add (currently-unused) FCP metric instrumentation.

Reviewed changes

Copilot reviewed 13 out of 14 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
packages/browser/src/tracing/browserTracingIntegration.ts Enables streamed Web Vital span tracking when span streaming is enabled; disables standalone CLS/LCP in that mode
packages/browser-utils/src/metrics/webVitalSpans.ts Implements streamed span emitters for LCP/CLS/INP
packages/browser-utils/test/metrics/webVitalSpans.test.ts Unit tests for streamed web vital span emission helpers
packages/browser-utils/src/metrics/instrument.ts Adds FCP metric instrumentation plumbing (fcp observer + handler)
packages/browser-utils/src/metrics/inp.ts Exports INP_ENTRY_MAP for reuse by streamed INP span logic
packages/browser-utils/src/index.ts Re-exports streamed web vital span trackers from browser-utils
dev-packages/browser-integration-tests/suites/tracing/metrics/web-vitals-lcp-streamed-spans/test.ts Playwright test validating streamed LCP span + attributes
dev-packages/browser-integration-tests/suites/tracing/metrics/web-vitals-lcp-streamed-spans/template.html Test page for streamed LCP
dev-packages/browser-integration-tests/suites/tracing/metrics/web-vitals-lcp-streamed-spans/init.js Initializes SDK with span streaming enabled for LCP test
dev-packages/browser-integration-tests/suites/tracing/metrics/web-vitals-lcp-streamed-spans/assets/sentry-logo-600x179.png Asset used to trigger LCP
dev-packages/browser-integration-tests/suites/tracing/metrics/web-vitals-cls-streamed-spans/test.ts Playwright test validating streamed CLS span + attributes
dev-packages/browser-integration-tests/suites/tracing/metrics/web-vitals-cls-streamed-spans/template.html Test page for streamed CLS
dev-packages/browser-integration-tests/suites/tracing/metrics/web-vitals-cls-streamed-spans/subject.js Simulates CLS for the CLS streamed span test
dev-packages/browser-integration-tests/suites/tracing/metrics/web-vitals-cls-streamed-spans/init.js Initializes SDK with span streaming enabled for CLS test

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/browser/src/tracing/browserTracingIntegration.ts Outdated
Comment thread packages/browser/src/tracing/browserTracingIntegration.ts Outdated
Comment thread packages/browser-utils/src/metrics/webVitalSpans.ts
Comment thread packages/browser-utils/src/metrics/webVitalSpans.ts Outdated
Comment thread packages/browser-utils/src/metrics/instrument.ts Outdated
Comment thread packages/browser/src/tracing/browserTracingIntegration.ts Outdated
Comment thread packages/browser-utils/src/metrics/instrument.ts Outdated
Comment thread packages/browser/src/tracing/browserTracingIntegration.ts
@logaretm logaretm force-pushed the awad/js-17931-webvitals-v2-spans branch from d764f2e to b80ddd4 Compare March 24, 2026 14:28
Comment thread packages/browser-utils/src/metrics/webVitalSpans.ts Outdated
Comment thread packages/browser/src/tracing/browserTracingIntegration.ts Outdated
Comment thread packages/browser/src/tracing/browserTracingIntegration.ts Outdated
@logaretm logaretm force-pushed the awad/js-17931-webvitals-v2-spans branch from a74fec7 to 7206304 Compare March 24, 2026 15:00
Comment thread packages/browser/src/tracing/browserTracingIntegration.ts Outdated
@logaretm logaretm force-pushed the awad/js-17931-webvitals-v2-spans branch 2 times, most recently from 86d1929 to 4bf129b Compare March 24, 2026 15:38
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Autofix Details

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Redundant CLS/LCP tracking when streaming is enabled
    • Added recordClsOnPageloadSpan and recordLcpOnPageloadSpan parameters to startTrackingWebVitals to prevent redundant handler registration when span streaming is enabled, eliminating the double-handler issue where one handler did throwaway work.

Create PR

Or push these changes by commenting:

@cursor push d3ccbaa211
Preview (d3ccbaa211)
diff --git a/packages/browser-utils/src/metrics/browserMetrics.ts b/packages/browser-utils/src/metrics/browserMetrics.ts
--- a/packages/browser-utils/src/metrics/browserMetrics.ts
+++ b/packages/browser-utils/src/metrics/browserMetrics.ts
@@ -77,6 +77,8 @@
 interface StartTrackingWebVitalsOptions {
   recordClsStandaloneSpans: boolean;
   recordLcpStandaloneSpans: boolean;
+  recordClsOnPageloadSpan?: boolean;
+  recordLcpOnPageloadSpan?: boolean;
   client: Client;
 }
 
@@ -89,6 +91,8 @@
 export function startTrackingWebVitals({
   recordClsStandaloneSpans,
   recordLcpStandaloneSpans,
+  recordClsOnPageloadSpan = true,
+  recordLcpOnPageloadSpan = true,
   client,
 }: StartTrackingWebVitalsOptions): () => void {
   const performance = getBrowserPerformanceAPI();
@@ -97,10 +101,22 @@
     if (performance.mark) {
       WINDOW.performance.mark('sentry-tracing-init');
     }
-    const lcpCleanupCallback = recordLcpStandaloneSpans ? trackLcpAsStandaloneSpan(client) : _trackLCP();
+    let lcpCleanupCallback: (() => void) | undefined;
+    if (recordLcpStandaloneSpans) {
+      trackLcpAsStandaloneSpan(client);
+    } else if (recordLcpOnPageloadSpan) {
+      lcpCleanupCallback = _trackLCP();
+    }
+
     const ttfbCleanupCallback = _trackTtfb();
-    const clsCleanupCallback = recordClsStandaloneSpans ? trackClsAsStandaloneSpan(client) : _trackCLS();
 
+    let clsCleanupCallback: (() => void) | undefined;
+    if (recordClsStandaloneSpans) {
+      trackClsAsStandaloneSpan(client);
+    } else if (recordClsOnPageloadSpan) {
+      clsCleanupCallback = _trackCLS();
+    }
+
     return (): void => {
       lcpCleanupCallback?.();
       ttfbCleanupCallback();

diff --git a/packages/browser/src/tracing/browserTracingIntegration.ts b/packages/browser/src/tracing/browserTracingIntegration.ts
--- a/packages/browser/src/tracing/browserTracingIntegration.ts
+++ b/packages/browser/src/tracing/browserTracingIntegration.ts
@@ -519,6 +519,8 @@
       _collectWebVitals = startTrackingWebVitals({
         recordClsStandaloneSpans: !spanStreamingEnabled && (enableStandaloneClsSpans || false),
         recordLcpStandaloneSpans: !spanStreamingEnabled && (enableStandaloneLcpSpans || false),
+        recordClsOnPageloadSpan: !spanStreamingEnabled,
+        recordLcpOnPageloadSpan: !spanStreamingEnabled,
         client,
       });

This Bugbot Autofix run was free. To enable autofix for future PRs, go to the Cursor dashboard.

Comment thread packages/browser/src/tracing/browserTracingIntegration.ts Outdated
logaretm and others added 19 commits April 15, 2026 16:12
…NTRY_MAP

Add `addFcpInstrumentationHandler` using the existing `onFCP` web-vitals
library integration, following the same pattern as the other metric handlers.
Export `INP_ENTRY_MAP` from inp.ts for reuse in the new web vital spans module.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…is enabled

Add non-standalone web vital spans that flow through the v2 span streaming
pipeline (afterSpanEnd -> captureSpan -> SpanBuffer). Each web vital gets
`browser.web_vital.<metric>.value` attributes and span events for measurement
extraction. Spans have meaningful durations showing time from navigation start
to the web vital event (except CLS which is a score, not a duration).

New tracking functions: trackLcpAsSpan, trackClsAsSpan, trackInpAsSpan,
trackTtfbAsSpan, trackFcpAsSpan, trackFpAsSpan — wired up in
browserTracingIntegration.setup() when hasSpanStreamingEnabled(client).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add Playwright integration tests verifying CLS, LCP, FCP, FP, and TTFB
are emitted as streamed spans with correct attributes, value attributes,
and meaningful durations when span streaming is enabled.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…dalone spans when streaming

TTFB, FCP, and FP should remain as attributes on the pageload span rather than
separate streamed spans. Also ensures standalone CLS/LCP spans are disabled when
span streaming is enabled to prevent duplicate spans.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…an path

The standalone INP handler filters out unrealistically long INP values
(>60s) but the streamed span path was missing this sanity check.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Gate standalone INP (`startTrackingINP`) behind `!spanStreamingEnabled`
and gate streamed INP (`trackInpAsSpan`) behind `enableInp` so both
paths respect the user's preference and don't produce duplicate data.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Remove `addFcpInstrumentationHandler`, `instrumentFcp`, and
`_previousFcp` which were added to support FCP streamed spans but are
no longer called after FCP spans were removed from the implementation.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…_sendInpSpan

Use `|| 0` fallback instead of `as number` cast, consistent with the
LCP and CLS span handlers that already guard against undefined.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…cpSpan

Avoid calling browserPerformanceTimeOrigin() twice by caching the
result in a local variable.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…nabled

The streamed INP path does not use INTERACTIONS_SPAN_MAP or
ELEMENT_NAME_TIMESTAMP_MAP, so registering the listeners is unnecessary overhead.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
When span streaming is enabled, CLS and LCP are emitted as streamed
spans. Previously they were also recorded as measurements on the
pageload span because the flags only checked enableStandaloneClsSpans
and enableStandaloneLcpSpans, which default to undefined.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
… handlers

Export the constant from inp.ts and import it in webVitalSpans.ts to
avoid the two definitions drifting apart.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…Setup

spanStreamingEnabled was declared in setup() but referenced in
afterAllSetup(), a separate scope. Replace with inline
hasSpanStreamingEnabled(client) call.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…enabled

When span streaming handles CLS/LCP, `startTrackingWebVitals` no longer
registers throwaway `_trackCLS()`/`_trackLCP()` handlers. Instead of
adding a separate skip flag, the existing `recordClsStandaloneSpans` and
`recordLcpStandaloneSpans` options now accept `undefined` to mean "skip
entirely" — three states via two flags instead of three flags.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@Lms24 Lms24 force-pushed the awad/js-17931-webvitals-v2-spans branch from c30527a to a0bfb46 Compare April 15, 2026 14:12
Comment thread packages/browser-utils/src/metrics/utils.ts
@Lms24 Lms24 self-assigned this Apr 15, 2026
Comment thread packages/core/src/integrations/requestdata.ts Outdated
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 1aa3b6e. Configure here.

Comment thread packages/core/src/integrations/requestdata.ts Outdated
Copy link
Copy Markdown
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

semi self approval move lol
(I will ask for forgiveness later :) )

@Lms24 Lms24 merged commit 50438f9 into develop Apr 16, 2026
473 of 478 checks passed
@Lms24 Lms24 deleted the awad/js-17931-webvitals-v2-spans branch April 16, 2026 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Emit web vital spans as v2 spans

3 participants