Skip to content

ref(browser-tests): Add waitForMetricRequest helper - #20002

Merged
logaretm merged 2 commits into
developfrom
feat/refactor-wait-for-metrics
Mar 30, 2026
Merged

ref(browser-tests): Add waitForMetricRequest helper#20002
logaretm merged 2 commits into
developfrom
feat/refactor-wait-for-metrics

Conversation

@logaretm

@logaretmlogaretm commented Mar 26, 2026

Copy link
Copy Markdown
Member

Summary

  • Adds a shared waitForMetricRequest helper to browser integration test utils, following the same page.waitForRequest pattern as waitForErrorRequest, waitForTransactionRequest, etc.
  • Refactors element timing tests to use waitForMetricRequest instead of a custom createMetricCollector with polling-based waitForIdentifiers
  • The new helper accumulates SerializedMetric[] across envelope requests and resolves when the callback returns true for the full collected set

Closes#20005 (added automatically)

…ent timing tests
Replace custom createMetricCollector/waitForIdentifiers polling pattern
with a shared waitForMetricRequest helper that matches existing waitFor
patterns (waitForErrorRequest, waitForTransactionRequest, etc.).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
CopilotAI review requested due to automatic review settings March 26, 2026 19:38
@github-actions

github-actionsBot commented Mar 26, 2026

Copy link
Copy Markdown
Contributor

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 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 ✨

Deps

  • Bump babel-loader from 10.0.0 to 10.1.1 by dependabot in #19997
  • Bump handlebars from 4.7.7 to 4.7.9 by dependabot in #20008

Other

  • (browser) Replace element timing spans with metrics by logaretm in #19869
  • (bun) Add bunRuntimeMetricsIntegration by chargome in #19979
  • (core) Support embedding APIs in google-genai by nicohrubec in #19797
  • (node) Add nodeRuntimeMetricsIntegration by chargome in #19923
  • (nuxt) Support parametrized SSR routes in Nuxt 5 by s1gr1d in #19977

Bug Fixes 🐛

  • (e2e) Pin @opentelemetry/api to 1.9.0 in ts3.8 test app by logaretm in #19992
  • (node) Ensure startNewTrace propagates traceId in OTel environments by logaretm in #19963
  • (opentelemetry) Convert seconds timestamps in span.end() to milliseconds by logaretm in #19958

Documentation 📚

  • (release) Update publishing-a-release.md by nicohrubec in #19982

Internal Changes 🔧

Core

  • Introduce instrumented method registry for AI integrations by nicohrubec in #19981
  • Consolidate getOperationName into one shared utility by nicohrubec in #19971

Deps

  • Bump amqplib from 0.10.7 to 0.10.9 by dependabot in #20000
  • Bump actions/upload-artifact from 6 to 7 by dependabot in #19569
  • Bump srvx from 0.11.12 to 0.11.13 by dependabot in #20001
  • Bump @apollo/server from 5.4.0 to 5.5.0 by dependabot in #20007

Deps Dev

  • Remove esbuild override in astro-5-cf-workers E2E test by isaacs in #20024
  • Bump node-forge from 1.3.2 to 1.4.0 by dependabot in #20012
  • Bump yaml from 2.8.2 to 2.8.3 by dependabot in #19985

Other

  • (browser-tests) Add waitForMetricRequest helper by logaretm in #20002
  • (deno) Expand Deno E2E test coverage by chargome in #19957
  • (e2e) Add e2e tests for nodeRuntimeMetricsIntegration by chargome in #19989

🤖 This preview updates automatically when you update the PR.

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a reusable Playwright helper to await and collect metrics from Sentry envelope requests, and refactors the element timing browser integration tests to use this helper instead of custom request listeners + polling.

Changes:

  • Added waitForMetricRequest(page, callback?) to accumulate SerializedMetric[] across trace_metric envelope requests and resolve once the callback condition is met.
  • Refactored element timing metrics tests to use waitForMetricRequest (removing the bespoke collector/polling loop).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

FileDescription
dev-packages/browser-integration-tests/utils/helpers.tsIntroduces waitForMetricRequest to parse metric containers from envelopes and accumulate metrics until a condition is satisfied.
dev-packages/browser-integration-tests/suites/tracing/metrics/element-timing/test.tsReplaces custom metric collection + polling with waitForMetricRequest in pageload and navigation scenarios.

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

Comment on lines +86 to +91
await page.goto(url);

// Wait for pageload element timing metrics to arrive before navigating
await collector.waitForIdentifiers(['image-fast', 'text1']);

// Reset so we only capture post-navigation metrics
collector.reset();
await waitForMetricRequest(page, metrics =>
metrics.some(m => m.name === 'ui.element.render_time' && getIdentifier(m) === 'image-fast'),
);

CopilotAIMar 26, 2026

Copy link

Choose a reason for hiding this comment

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

waitForMetricRequest only observes future requests. In this test it’s awaited afterpage.goto(url), so the pageload metric envelope could already have been sent during navigation, leading to a hang/timeout. Create the waitForMetricRequest(...) promise before calling page.goto, and await them together (similar to the first test).

Copilot uses AI. Check for mistakes.
Comment on lines +93 to +100
// Trigger navigation
await page.locator('#button1').click();

// Wait for navigation element timing metrics
await collector.waitForIdentifiers(['navigation-image', 'navigation-text']);
const navigationMetrics = await waitForMetricRequest(page, metrics => {
const seen = new Set(metrics.filter(m => m.name === 'ui.element.render_time').map(getIdentifier));
return seen.has('navigation-image') && seen.has('navigation-text');
});

CopilotAIMar 26, 2026

Copy link

Choose a reason for hiding this comment

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

waitForMetricRequest is started after the click. If the click-triggered navigation flushes metrics quickly (or if Playwright waits for navigation during click()), the relevant envelope request can happen before the waiter is registered, making this flaky. Start waitForMetricRequest(...) before click() and await both (e.g. via Promise.all).

Copilot uses AI. Check for mistakes.
@github-actions

github-actionsBot commented Mar 26, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

PathSize% ChangeChange
@sentry/browser25.69 kB--
@sentry/browser - with treeshaking flags24.17 kB--
@sentry/browser (incl. Tracing)42.17 kB--
@sentry/browser (incl. Tracing, Profiling)46.79 kB--
@sentry/browser (incl. Tracing, Replay)80.98 kB--
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags70.6 kB--
@sentry/browser (incl. Tracing, Replay with Canvas)85.7 kB--
@sentry/browser (incl. Tracing, Replay, Feedback)97.97 kB--
@sentry/browser (incl. Feedback)42.48 kB--
@sentry/browser (incl. sendFeedback)30.35 kB--
@sentry/browser (incl. FeedbackAsync)35.4 kB--
@sentry/browser (incl. Metrics)26.96 kB--
@sentry/browser (incl. Logs)27.1 kB--
@sentry/browser (incl. Metrics & Logs)27.78 kB--
@sentry/react27.45 kB--
@sentry/react (incl. Tracing)44.52 kB--
@sentry/vue30.13 kB--
@sentry/vue (incl. Tracing)44.08 kB--
@sentry/svelte25.7 kB--
CDN Bundle28.39 kB--
CDN Bundle (incl. Tracing)43.2 kB--
CDN Bundle (incl. Logs, Metrics)29.76 kB--
CDN Bundle (incl. Tracing, Logs, Metrics)44.25 kB--
CDN Bundle (incl. Replay, Logs, Metrics)68.56 kB--
CDN Bundle (incl. Tracing, Replay)80.08 kB--
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)81.16 kB--
CDN Bundle (incl. Tracing, Replay, Feedback)85.62 kB--
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)86.67 kB--
CDN Bundle - uncompressed82.93 kB--
CDN Bundle (incl. Tracing) - uncompressed128.07 kB--
CDN Bundle (incl. Logs, Metrics) - uncompressed87.07 kB--
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed131.48 kB--
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed210.06 kB--
CDN Bundle (incl. Tracing, Replay) - uncompressed244.95 kB--
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed248.34 kB--
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed257.86 kB--
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed261.25 kB--
@sentry/nextjs (client)46.93 kB--
@sentry/sveltekit (client)42.67 kB--
@sentry/node-core56.51 kB+0.02%+11 B 🔺
@sentry/node173.6 kB+0.01%+12 B 🔺
@sentry/node - without tracing96.54 kB+0.01%+5 B 🔺
@sentry/aws-serverless113.54 kB+0.01%+8 B 🔺

View base workflow run

* and resolves when the callback returns true for the full set of collected metrics.
* If no callback is provided, resolves on the first request containing metrics.
*/
export function waitForMetricRequest(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

super-l: given we return Promise<SerializedMetric[]>, wdyt about just calling this waitForMetrics? I went with this pattern for streamed spans (example, though your collection mechanism is much more sophisticated :D).

Besides, I think the waitForErrorRequest et al. helpers actually return a Promise<Request<Event>> which was a bit cumbersome to work with in most cases. So I personally prefer this pattern of directly returning the metrics much more.

No strong feelings, just a suggestion!

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

I think that is actually nicer, most of the time we want the actual thing we are waiting for/filtering on. We should do the same for others as well if the pattern holds there.

Good point, thanks!


// Reset so we only capture post-navigation metrics
collector.reset();
await waitForMetricRequest(page, metrics =>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

m: This is a valid catch from copilot: we should start listening for metrics before going to the URL (or clicking below) and after the respective action await the promise.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Rare copilot W

Rename waitForMetricRequest to waitForMetrics since it returns SerializedMetric[]
directly. Fix race conditions in navigation test by creating metric listener
promises before triggering page.goto() and click() actions.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.
⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

ScenarioRequests/s% of BaselinePrev. Requests/sChange %
GET Baseline11,478-9,471+21%
GET With Sentry1,98517%1,653+20%
GET With Sentry (error only)7,61566%5,960+28%
POST Baseline1,170-1,198-2%
POST With Sentry54046%583-7%
POST With Sentry (error only)98684%1,047-6%
MYSQL Baseline3,844-3,251+18%
MYSQL With Sentry47912%468+2%
MYSQL With Sentry (error only)3,03879%2,633+15%

View base workflow run

@logaretm
logaretm requested a review from Lms24March 27, 2026 20:26

@Lms24Lms24 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@logaretm
logaretm merged commit 9bfc682 into developMar 30, 2026
241 checks passed
@logaretm
logaretm deleted the feat/refactor-wait-for-metrics branch March 30, 2026 13:14
Sign up for freeto 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.

ref(browser-tests): Add waitForMetricRequest helper

3 participants

@logaretm@Lms24