route:unmatched hands a consumer only the caller-controlled path — the one value that must never become a metric label #65

Closed
opened 2026-08-20 17:53:13 +00:00 by chrnx · 0 comments
Owner

route:unmatched is the one terminal request event that carries no route, so the only subject a consumer can reach is name — and name is the concrete, caller-controlled path.

What happens

src/http/core.ts:297:

bus.emit('route:unmatched', {
  name: `${req.method} ${path}`,   // the path that arrived
  method: req.method,
  requestId: req.requestId,
  traceId: req.traceId,
});

No route, no status. Compare request:end, which carries route: trace.route — undefined for an unmatched request, so that one is no help either.

Why it matters

The cardinality doctrine is written into the codebase and it is right. Correlation's doc comment says it plainly:

a metrics consumer that labels a counter with a concrete path gets one label per distinct URL, and unbounded label cardinality takes down the metrics backend rather than the application. Handing anyone that shape by default would be the framework's fault.

That reasoning applies hardest to exactly this event. A matched route's path is bounded by the route table; an unmatched path is bounded by nothing at all. A scanner walking /aaa, /aab, /aac … is a memory leak with a metrics backend attached — and for this event, the concrete path is the only field a consumer is given.

The framework does not create the trap knowingly; it just leaves the one field that avoids it empty precisely where it matters most.

Scope

Deliberately small — this is a field, not a design:

  • Carry a bounded marker, e.g. route: '<unmatched>', on route:unmatched and on the request:end that follows it. It reads as a pattern, aggregates as one series, and needs no consumer to invent a fallback that some consumers will get wrong.
  • If that overloads route (it is documented as "the matched pattern", and nothing matched), an explicit matched: false works too. Either beats an empty field.
  • Either way, say in the doc comment that name is caller-controlled and must not be used as a metric label. request:start has the same property, but it is not a terminal event anyone counts.

Related

Found while writing metrics.plugin.ts for the green-desk demo. It collapses everything unmatched into a single <unmatched> bucket — the right behaviour, arrived at by reading src/http/core.ts, not by reading the payload type.

`route:unmatched` is the one terminal request event that carries no `route`, so the only subject a consumer can reach is `name` — and `name` is the concrete, caller-controlled path. ## What happens `src/http/core.ts:297`: ```ts bus.emit('route:unmatched', { name: `${req.method} ${path}`, // the path that arrived method: req.method, requestId: req.requestId, traceId: req.traceId, }); ``` No `route`, no `status`. Compare `request:end`, which carries `route: trace.route` — `undefined` for an unmatched request, so that one is no help either. ## Why it matters The cardinality doctrine is written into the codebase and it is right. `Correlation`'s doc comment says it plainly: > a metrics consumer that labels a counter with a concrete path gets one label per distinct URL, and unbounded label cardinality takes down the metrics backend rather than the application. Handing anyone that shape by default would be the framework's fault. That reasoning applies hardest to exactly this event. A matched route's path is bounded by the route table; an **unmatched** path is bounded by nothing at all. A scanner walking `/aaa`, `/aab`, `/aac` … is a memory leak with a metrics backend attached — and for this event, the concrete path is the only field a consumer is given. The framework does not create the trap knowingly; it just leaves the one field that avoids it empty precisely where it matters most. ## Scope Deliberately small — this is a field, not a design: - Carry a bounded marker, e.g. `route: '<unmatched>'`, on `route:unmatched` and on the `request:end` that follows it. It reads as a pattern, aggregates as one series, and needs no consumer to invent a fallback that some consumers will get wrong. - If that overloads `route` (it is documented as "the matched pattern", and nothing matched), an explicit `matched: false` works too. Either beats an empty field. - Either way, say in the doc comment that `name` is caller-controlled and must not be used as a metric label. `request:start` has the same property, but it is not a terminal event anyone counts. ## Related Found while writing `metrics.plugin.ts` for the `green-desk` demo. It collapses everything unmatched into a single `<unmatched>` bucket — the right behaviour, arrived at by reading `src/http/core.ts`, not by reading the payload type.
chrnx closed this issue 2026-09-04 13:32:29 +00:00
Sign in to join this conversation.