Skip to content

fetch-router: Expose matched route on request context#11373

Open
gyx1000 wants to merge 1 commit into
remix-run:mainfrom
gyx1000:feat/expose-matched-route-context
Open

fetch-router: Expose matched route on request context#11373
gyx1000 wants to merge 1 commit into
remix-run:mainfrom
gyx1000:feat/expose-matched-route-context

Conversation

@gyx1000
Copy link
Copy Markdown
Contributor

@gyx1000 gyx1000 commented May 8, 2026

This PR proposes a small fetch-router API addition that makes the matched route available from request context.

The motivation is future telemetry and observability work. I do not necessarily expect this exact PR to be accepted as-is; the goal is mostly to suggest a simple shape and ask whether Remix is already exploring telemetry support internally.

  • Adds a MatchedRoute context key exported from @remix-run/fetch-router
  • Stores the matched RouteEntry on the request context once a route and method have matched
  • Makes the matched route readable from route middleware, route handlers, and router middleware after await next()

One telemetry use case is enriching an active OpenTelemetry HTTP span with the low-cardinality route pattern. For example, an instrumentation layer could report /products/:productId instead of /products/123, avoiding high-cardinality span names and attributes.

import { MatchedRoute } from '@remix-run/fetch-router'

async function telemetryMiddleware(context, next) {
  let response = await next()
  let matchedRoute = context.get(MatchedRoute)

  if (matchedRoute) {
    span.setAttribute('http.route', matchedRoute.pattern.source)
    span.updateName(`${context.method} ${matchedRoute.pattern.source}`)
  }

  return response
}

Is telemetry something the Remix team is already working on or planning for? If so, would exposing the matched route through request context fit that direction, or would you prefer a different extension point?

@pcattori pcattori self-assigned this May 11, 2026
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.

2 participants