Skip to content

Conversation

@onurtemizkan
Copy link
Collaborator

@onurtemizkanonurtemizkan commented Dec 19, 2025

Adds support for React Router's unstable_instrumentations API for automatic performance monitoring.

Resolves: #18127
Resolves: #18121
Ref: remix-run/react-router#13749
Ref: remix-run/react-router#14412

React Router 7.9.5 introduced the unstable_instrumentations API, which provides hooks for instrumenting request handlers, loaders, actions, middleware, and lazy routes. This enables automatic span creation without requiring manual wrapper functions.

Server-side

Added createSentryServerInstrumentation():

  • handler.request - Creates root HTTP server span with parameterized route names
  • route.loader - Automatic loader span creation
  • route.action - Automatic action span creation
  • route.middleware - Automatic middleware span creation
  • route.lazy - Lazy route loading spans
// entry.server.tsximport*asSentryfrom'@sentry/react-router';exportdefaultSentry.createSentryHandleRequest({/* ... */});exportconsthandleError=Sentry.createSentryHandleError();exportconstunstable_instrumentations=[Sentry.createSentryServerInstrumentation()];

Client-side

Added client instrumentation via reactRouterTracingIntegration({useInstrumentationAPI: true }):

  • router.navigate - Navigation transaction creation
  • router.fetch - Fetcher span creation
  • route.loader/action/middleware/lazy - Client-side route handler spans
// entry.client.tsxconsttracing=Sentry.reactRouterTracingIntegration({useInstrumentationAPI: true});Sentry.init({integrations: [tracing],});<HydratedRouterunstable_instrumentations={[tracing.clientInstrumentation]}/>

Backward compatibility

When the instrumentation API is used:

  • wrapServerLoader and wrapServerAction skip creating spans and emit a warning in debug mode
  • Legacy navigation patching in instrumentHydratedRouter checks if instrumentation API handled it
  • Server-side OTEL instrumentation for loaders/actions is skipped

Also:

  • Created serverGlobals.ts to isolate flag handling (avoids pulling OpenTelemetry deps in Cloudflare builds)
  • Updated wrapSentryHandleRequest to explicitly set parameterized transaction names (improves Hydrogen parameterization)
  • Added E2E test application react-router-7-framework-instrumentation

Notes

  • Client-side limitation: As of React Router 7.x, HydratedRouter does not invoke client instrumentation hooks in Framework Mode. Client-side navigation is handled by existing instrumentHydratedRouter(). The client instrumentation is prepared for when React Router adds support. See Observability: Server-side instrumentation remix-run/react-router#13749.
  • Lazy routes limitation: In Framework Mode, route.lazy hooks are not triggered because the bundler handles code-splitting.

Needs docs PR

Copy link
Contributor

CopilotAI 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

This PR adds comprehensive support for React Router's instrumentation API, enabling automatic performance monitoring and error tracking for React Router v7.x applications. The implementation provides both server-side and client-side instrumentation that integrates with React Router's unstable_instrumentations API (planned to become stable in v8).

Key Changes:

  • Server and client instrumentation API implementations with automatic span creation for loaders, actions, middleware, and lazy routes
  • Backward compatibility mechanisms to prevent duplicate instrumentation when using both legacy wrappers and the new API
  • Comprehensive E2E test application demonstrating real-world usage

Reviewed changes

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

Show a summary per file
FileDescription
packages/react-router/src/server/createServerInstrumentation.tsNew server instrumentation implementation with handler and route hooks
packages/react-router/src/client/createClientInstrumentation.tsNew client instrumentation implementation for router navigation and route handlers
packages/react-router/src/common/utils.tsShared utility functions for URL parsing, pattern extraction, and error capture
packages/react-router/src/common/types.tsTypeScript type definitions for React Router's instrumentation API
packages/react-router/src/server/serverGlobals.tsGlobal flag management to detect instrumentation API usage
packages/react-router/src/server/wrapServerLoader.tsUpdated to skip instrumentation when API is active, with deprecation warning
packages/react-router/src/server/wrapServerAction.tsUpdated to skip instrumentation when API is active, with deprecation warning
packages/react-router/src/server/wrapSentryHandleRequest.tsEnhanced to work alongside instrumentation API, preserving origin attributes
packages/react-router/src/client/tracingIntegration.tsExtended with lazy-loaded clientInstrumentation getter and options support
packages/react-router/src/client/hydratedRouter.tsUpdated to avoid double-counting navigation when API handles it
packages/react-router/src/server/integration/reactRouterServer.tsUpdated to skip OTEL patching when instrumentation API is used
packages/react-router/src/server/instrumentation/reactRouter.tsUpdated to skip OTEL instrumentation for loaders/actions when API is active
packages/react-router/test/server/createServerInstrumentation.test.tsComprehensive unit tests for server instrumentation API
packages/react-router/test/client/createClientInstrumentation.test.tsComprehensive unit tests for client instrumentation API
packages/react-router/test/common/utils.test.tsUnit tests for shared utility functions
dev-packages/e2e-tests/test-applications/react-router-7-framework-instrumentation/*Complete E2E test application with performance and error tests

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

Copy link
Contributor

CopilotAI 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

Copilot reviewed 55 out of 55 changed files in this pull request and generated no new comments.


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

@onurtemizkanonurtemizkanforce-pushed the react-router-8-instrumentation-api branch from 883f798 to 75374f3CompareDecember 23, 2025 10:36
@onurtemizkanonurtemizkan marked this pull request as ready for review December 23, 2025 10:36
Comment on lines +45 to +49

return{
router(router: InstrumentableRouter){
router.instrument({
async navigate(callNavigate, info){

This comment was marked as outdated.

@github-actions
Copy link
Contributor

github-actionsbot commented Dec 23, 2025

size-limit report 📦

PathSize% ChangeChange
@sentry/browser24.82 kB--
@sentry/browser - with treeshaking flags23.32 kB--
@sentry/browser (incl. Tracing)41.6 kB--
@sentry/browser (incl. Tracing, Profiling)46.19 kB--
@sentry/browser (incl. Tracing, Replay)80.17 kB--
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags69.91 kB--
@sentry/browser (incl. Tracing, Replay with Canvas)84.85 kB--
@sentry/browser (incl. Tracing, Replay, Feedback)97.1 kB--
@sentry/browser (incl. Feedback)41.54 kB--
@sentry/browser (incl. sendFeedback)29.51 kB--
@sentry/browser (incl. FeedbackAsync)34.5 kB--
@sentry/browser (incl. Metrics)25.85 kB--
@sentry/browser (incl. Logs)26.07 kB--
@sentry/browser (incl. Metrics & Logs)26.73 kB--
@sentry/react26.54 kB--
@sentry/react (incl. Tracing)43.79 kB--
@sentry/vue29.29 kB--
@sentry/vue (incl. Tracing)43.41 kB--
@sentry/svelte24.84 kB--
CDN Bundle27.25 kB--
CDN Bundle (incl. Tracing)42.24 kB--
CDN Bundle (incl. Tracing, Replay)78.96 kB--
CDN Bundle (incl. Tracing, Replay, Feedback)84.41 kB--
CDN Bundle - uncompressed80.06 kB--
CDN Bundle (incl. Tracing) - uncompressed125.49 kB--
CDN Bundle (incl. Tracing, Replay) - uncompressed242.03 kB--
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed254.79 kB--
@sentry/nextjs (client)46.02 kB--
@sentry/sveltekit (client)41.97 kB--
@sentry/node-core51.62 kB+0.01%+1 B 🔺
@sentry/node161.52 kB--
@sentry/node - without tracing93.05 kB--
@sentry/aws-serverless108.57 kB+0.01%+1 B 🔺

View base workflow run

@github-actions
Copy link
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.

ScenarioRequests/s% of BaselinePrev. Requests/sChange %
GET Baseline9,365-11,145-16%
GET With Sentry1,80319%1,889-5%
GET With Sentry (error only)6,06765%7,659-21%
POST Baseline1,198-1,171+2%
POST With Sentry60350%582+4%
POST With Sentry (error only)1,05388%1,039+1%
MYSQL Baseline3,320-4,024-17%
MYSQL With Sentry50715%600-16%
MYSQL With Sentry (error only)2,71282%3,153-14%

View base workflow run

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.

Support Instrumentation API React Router Instrumentation

2 participants

@onurtemizkan