Stable Middleware IDs for Instrumentation #15314
jellevanwezel
started this conversation in
Proposals
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Middleware instrumentation IDs
Status: Stage 0 — Proposal
Modes: Data and Framework (server and client middleware)
Summary
Allow route middleware to declare an ID that is passed to middleware
instrumentation. This gives observability integrations a stable way to identify
individual middleware on a route.
Motivation
Instrumentation can currently observe that a middleware is running, but it
cannot reliably tell which of several middleware functions on the same route is
being invoked. Using a function name is not reliable: names can be absent,
change during minification, or be shared by multiple middleware functions.
This makes it difficult for logging, tracing, and metrics integrations to name,
filter, or aggregate middleware spans. An array index would distinguish entries,
but changes whenever middleware is reordered.
Proposal
Middleware arrays continue to accept functions. They may additionally contain
an object with a
stringornumberID and a middleware function:The same shape is available for Data Mode route objects and Framework Mode
middlewareandclientMiddlewareexports. The generated route types exposeRoute.MiddlewareDefinitionandRoute.ClientMiddlewareDefinitionfor userswho want to annotate these arrays explicitly.
Middleware instrumentation receives the ID alongside the existing context:
For a function entry, React Router supplies its zero-based index in that
route's middleware array. For an object entry, it supplies the declared
id.IDs are route-local and need not be unique; instrumentation already has the
route identity, so the pair of route ID and middleware ID identifies an
instrumented middleware invocation. Applications that need stable telemetry
should use explicit IDs.
This is additive and does not alter middleware ordering, execution, or the
behavior of existing function entries.
Alternatives considered
middleware silently changes telemetry identity.
identifier and are especially unsuitable for production builds.
unmodified and makes the identifier visible at the call site.
property and implement the middleware behavior. This adds a class-based API
and changes the existing function-first middleware model for a small piece of
metadata.
[id, middleware]. Whilecompact, positional entries are less self-documenting and are easier to
misread than
{ id, middleware }, particularly when middleware definitionsare shared or generated.
Prior art
A working implementation and tests were previously submitted in
PR #15304.
All reactions