feat(bus): give an unmatched request a bounded route to be labelled by

`route:unmatched` was the one terminal request event carrying no `route`, so the only
subject a consumer could reach was `name` — and on that event `name` is the concrete,
caller-controlled path.

The cardinality doctrine is already written into the codebase and it is right: 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.
That reasoning applies hardest to exactly this event. A matched route's path is bounded by
the route table; an unmatched one is bounded by nothing at all, and a scanner walking
/aaa, /aab, /aac is a memory leak with a metrics backend attached.

The framework did not create the trap knowingly. It just left the one field that avoids
it empty, precisely where it mattered most.

`route:unmatched` and the `request:end` that follows it now both carry
`route: '<unmatched>'`, exported as `UNMATCHED_ROUTE` so nobody has to hardcode the
string to compare against it. A single series for every path that was never a route,
rather than a fallback each consumer invents and some get wrong. It cannot collide with a
pattern a user can declare, since those start with `/`.

Written down alongside it, because a field alone does not teach the rule: `name` is
caller-controlled and must not be a metric label — true of `request:start` too, which is
not terminal and so was never the one anyone counted. And `route:unmatched` is not a
synonym for failure: a static file served by `createApp({ static })` matched no route
either and answers 200, so the status comes from `request:end`, like everything else.

Closes #65.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-09-01 10:37:41 -05:00
1 parent 5294e62a5a
commit c00f714aa8
4 files changed
+76 -2

No files matched your search

+35
View File
@@ -1,6 +1,7 @@
import { describe, it, expect } from 'vitest';
import { createApp, Provider, Step, Route, Get, Module, needs } from '../src/index';
import type { EventPayload } from '../src/bus';
import { UNMATCHED_ROUTE } from '../src/index';
@Provider({ provides: 'db' })
class Db {
@@ -180,6 +181,40 @@ describe('failure reporting', () => {
expect(requestFailures[0].status).toBe(500);
});
// The cardinality trap: `name` carries the path that arrived, which is bounded by nothing at all.
// A consumer labelling on `route` gets one bounded series for every path that was never a route;
// one labelling on `name` gets a series per scanner probe.
it('labels an unmatched request with a bounded route, on both events', async () => {
const app = createApp({ modules: [BadModule] });
const unmatched: EventPayload[] = [];
const ends: EventPayload[] = [];
app.bus.on('route:unmatched', (p) => unmatched.push(p));
app.bus.on('request:end', (p) => ends.push(p));
for (const probe of ['/aaa', '/aab', '/aac']) await app.fetch(new Request(`http://x${probe}`));
expect(unmatched).toHaveLength(3);
expect(ends).toHaveLength(3);
// Three distinct paths, one series.
expect(new Set(unmatched.map((e) => e.route))).toEqual(new Set([UNMATCHED_ROUTE]));
expect(new Set(ends.map((e) => e.route))).toEqual(new Set([UNMATCHED_ROUTE]));
// `name` still carries the concrete path, which is what makes it useful in a log and unusable
// as a label.
expect(new Set(unmatched.map((e) => e.name)).size).toBe(3);
// It cannot collide with a pattern a user can declare.
expect(UNMATCHED_ROUTE.startsWith('/')).toBe(false);
});
it('leaves a matched route labelled with its pattern, not the marker', async () => {
const app = createApp({ modules: [BadModule] });
const ends: EventPayload[] = [];
app.bus.on('request:end', (p) => ends.push(p));
await app.fetch(new Request('http://x/bad/go'));
expect(ends[0].route).toBe('/bad/go');
});
// The three-event overlap, asserted rather than described: one failing request, three events,
// one requestId. A consumer that counts `request:failed` and `request:end` as separate outcomes
// counts this request twice — which is the mistake the payload docs now name.