feat(observability): correlate and time the lifecycle event stream

Tasks 1-3 of the observability plan. The Bus already had 19 emission points;
what it lacked was any way to tell which request an event belonged to, which
made the whole stream unusable under concurrency.

Every per-request event now carries requestId, traceId, route, method and
transport, and request:step:leave carries durationMs — a field that has been
declared in EventPayload since the beginning with nothing ever writing it.

Identity is derived in the adapters rather than in handle(), because both read
and size-check the body first: a 413 is answered without handle() ever seeing
the request, so generating there would leave exactly the failures an operator
most wants to correlate with nothing to correlate them by. An incoming
x-request-id is adopted rather than replaced, and traceparent is carried
verbatim — core implements no propagation spec.

Timing takes N+1 readings for N steps, since one step's end is the next one's
start. Measured at 25.2ns per step against a ~10.19us request.

request:step:error now names the step that actually failed. It reported
name: 'pipeline' before, discarding the one fact it exists to carry, because it
was emitted from the outer catch where that is all that is known. The outer
catch emits request:failed instead: one marks the span, the other the trace.

route carries the matched pattern and never the concrete path. Labelling a
metric on raw URLs gives unbounded cardinality and takes down the metrics
backend rather than the app, so it is a contract, with a test.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-08-16 14:20:44 -05:00
1 parent 7bd11bff27
commit 2c47ee8896
8 files changed
+305 -13

No files matched your search

+28 -2
View File
@@ -12,16 +12,42 @@ export type LifecycleEvent =
| 'mesh:connect'
| 'mesh:disconnect'
| 'mesh:rpc:error'
| 'plugin:mounted';
| 'plugin:mounted'
| 'request:failed';
/** Data carried by a lifecycle event: the subject's name plus optional scope, error and timing. */
/**
* Data carried by a lifecycle event: the subject's name plus optional scope, error, timing and
* the fields that say which request it belongs to.
*
* Everything past `name` is optional and stays that way. Boot and mesh events have no request to
* name, and requiring a shape they cannot fill would only mean inventing values for it.
*/
export interface EventPayload {
name: string;
scope?: string;
error?: unknown;
durationMs?: number;
/** Correlates every event of one request. Adopted from `x-request-id` when a gateway sent one. */
requestId?: string;
/** A `traceparent` header carried verbatim. Core parses nothing — that is the exporter's job. */
traceId?: string;
/** The matched *pattern* (`/users/:id`), never the concrete path — see {@link Correlation}. */
route?: string;
method?: string;
transport?: string;
status?: number;
}
/**
* The subset of {@link EventPayload} that identifies a request, spread into each of its events.
*
* `route` carries the matched pattern rather than the URL that arrived, and that is load-bearing
* rather than cosmetic: 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.
*/
export type Correlation = Pick<EventPayload, 'requestId' | 'traceId' | 'route' | 'method' | 'transport'>;
/** In-process pub/sub for framework lifecycle events; observer failures are swallowed so they never break the pipeline. */
export class Bus {
private readonly listeners = new Map<LifecycleEvent, Set<(p: EventPayload) => void>>();