Skip to content

feat(react-router): Include middleware function names and indices#19109

Open
onurtemizkan wants to merge 5 commits intodevelopfrom
onur/react-router-middleware-names
Open

feat(react-router): Include middleware function names and indices#19109
onurtemizkan wants to merge 5 commits intodevelopfrom
onur/react-router-middleware-names

Conversation

@onurtemizkan
Copy link
Collaborator

@onurtemizkan onurtemizkan commented Jan 30, 2026

Resolves: #19097

Middleware spans created by the instrumentation API were using the route pattern as the span name (same as loaders and actions), which doesn't help when a route has multiple middlewares.

This PR adds function name resolution so middleware spans are named middleware authMiddleware instead of just the route pattern. Each span also gets a react_router.middleware.index attribute. When names are not available (anonymous arrow functions), it falls back to the route ID.

Function names are resolved from the ServerBuild object, which is captured through two complementary paths:

  • The OTEL instrumentation patches createRequestHandler at runtime, capturing the ServerBuild from its arguments. This handles both static builds and factory functions (dev mode HMR).

  • New Vite plugin (makeServerBuildCapturePlugin) that injects into the virtual react-router/server-build module during SSR builds, providing early capture at module initialization. This is needed because virtual modules are not reachable by OTEL's module hooks.

The isInstrumentationApiUsed() check in the OTEL instrumentation was moved from the per-request handler to createRequestHandler itself. The OTEL hook now always needs to run to capture the ServerBuild reference for middleware name lookup; per-request wrapping is gated at the handler level instead. The Node version gating for the OTEL hook was also removed since it needs to run unconditionally now.

Limitation:

  • Anonymous middlewares won't have function names regardless of build mode.

@github-actions
Copy link
Contributor

github-actions bot commented Jan 30, 2026

Codecov Results 📊


Generated by Codecov Action

@onurtemizkan onurtemizkan force-pushed the onur/react-router-middleware-names branch from de6be28 to 4c6cef9 Compare February 18, 2026 13:07
@github-actions
Copy link
Contributor

github-actions bot commented Feb 18, 2026

size-limit report 📦

Path Size % Change Change
@sentry/browser 25.61 kB - -
@sentry/browser - with treeshaking flags 24.12 kB - -
@sentry/browser (incl. Tracing) 42.42 kB - -
@sentry/browser (incl. Tracing, Profiling) 47.08 kB - -
@sentry/browser (incl. Tracing, Replay) 81.24 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 70.86 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 85.93 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 98.09 kB - -
@sentry/browser (incl. Feedback) 42.33 kB - -
@sentry/browser (incl. sendFeedback) 30.28 kB - -
@sentry/browser (incl. FeedbackAsync) 35.28 kB - -
@sentry/browser (incl. Metrics) 26.78 kB - -
@sentry/browser (incl. Logs) 26.92 kB - -
@sentry/browser (incl. Metrics & Logs) 27.6 kB - -
@sentry/react 27.37 kB - -
@sentry/react (incl. Tracing) 44.76 kB - -
@sentry/vue 30.06 kB - -
@sentry/vue (incl. Tracing) 44.26 kB - -
@sentry/svelte 25.64 kB - -
CDN Bundle 28.16 kB - -
CDN Bundle (incl. Tracing) 43.25 kB - -
CDN Bundle (incl. Logs, Metrics) 29 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 44.09 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 68.08 kB - -
CDN Bundle (incl. Tracing, Replay) 80.12 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 80.99 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 85.56 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 86.46 kB - -
CDN Bundle - uncompressed 82.33 kB - -
CDN Bundle (incl. Tracing) - uncompressed 128.05 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 85.17 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 130.88 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 208.83 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 244.93 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 247.75 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 257.73 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 260.54 kB - -
@sentry/nextjs (client) 47.17 kB - -
@sentry/sveltekit (client) 42.88 kB - -
@sentry/node-core 52.18 kB +0.02% +8 B 🔺
@sentry/node 166.49 kB -0.03% -46 B 🔽
@sentry/node - without tracing 93.97 kB +0.02% +10 B 🔺
@sentry/aws-serverless 109.47 kB +0.01% +8 B 🔺

View base workflow run

@github-actions
Copy link
Contributor

github-actions bot commented Feb 18, 2026

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.

Scenario Requests/s % of Baseline Prev. Requests/s Change %
GET Baseline 9,014 - 9,110 -1%
GET With Sentry 1,584 18% 1,694 -6%
GET With Sentry (error only) 6,024 67% 6,094 -1%
POST Baseline 1,184 - 1,195 -1%
POST With Sentry 578 49% 589 -2%
POST With Sentry (error only) 1,035 87% 1,065 -3%
MYSQL Baseline 3,250 - 3,339 -3%
MYSQL With Sentry 413 13% 470 -12%
MYSQL With Sentry (error only) 2,618 81% 2,710 -3%

View base workflow run

@onurtemizkan onurtemizkan force-pushed the onur/react-router-middleware-names branch from b2182e1 to d47508b Compare February 19, 2026 04:19
@onurtemizkan onurtemizkan force-pushed the onur/react-router-middleware-names branch from 851f1e1 to 7484acb Compare February 19, 2026 14:03
@onurtemizkan onurtemizkan marked this pull request as ready for review February 19, 2026 14:04
Copy link

@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.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

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.

React router middleware instrumentation span's missing context and thus not useful

1 participant

Comments