Tasks 5 and 6, in that order rather than the plan's: request logging is a
subscriber, so the events it subscribes to had to exist first.
handle() now emits request:start, request:end, route:matched and
route:unmatched. request:end fires when the handler returns, not when a stream
it produced finishes — a streaming route is done here in milliseconds while its
connection may live for hours, and ending the span at close would put hour-long
SSE connections and 2ms buffered replies in one latency distribution. The
stream events already describe the connection and now share the requestId.
route:unmatched covers 404 and 405 both. Naming it after one outcome, as the
report's route.not_found does, would misreport the other.
logRequests(bus, logger) is the consumer: info per completed request, error per
failed one, and it returns an unsubscribe so it is removable rather than merely
optional. createApp({ logRequests: true }) turns it on and is off by default,
because a framework that writes to stdout uninvited is one you have to
configure before you can use it. withConsoleFallback wraps it, since Bus.emit
isolates listener failures by design and a user logger that throws would
otherwise stop writing with nothing to say so. A test proves both halves: the
request still succeeds, and the failure still surfaces.
The bus reaches handle() through HttpOptions so the Node and Fetch adapters
share one channel instead of growing separate ones.
Cost, measured: nothing. First attempt wrapped dispatch in a second async frame
and that alone cost 0.43us per request — a promise and two microtask ticks,
an order of magnitude more than the four Map lookups it was there to arrange.
handle() is no longer async and hands back dispatch's own promise when nothing
subscribes; 4.716-4.791us against 4.736-4.793 before, which is the same number.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
95 lines
3.8 KiB
TypeScript
95 lines
3.8 KiB
TypeScript
/** Names of every lifecycle event the framework emits over the {@link Bus}. */
|
|
export type LifecycleEvent =
|
|
| 'boot:provider:start'
|
|
| 'boot:provider:ok'
|
|
| 'boot:provider:fail'
|
|
| 'request:step:enter'
|
|
| 'request:step:leave'
|
|
| 'request:step:error'
|
|
| 'stream:open'
|
|
| 'stream:close'
|
|
| 'stream:error'
|
|
| 'mesh:connect'
|
|
| 'mesh:disconnect'
|
|
| 'mesh:rpc:error'
|
|
| 'plugin:mounted'
|
|
| 'request:start'
|
|
| 'request:end'
|
|
| 'request:failed'
|
|
| 'route:matched'
|
|
// Covers a 404 *and* a 405 — the report's `route.not_found` names one outcome and would
|
|
// misreport the other, and "no route ran" is the fact a consumer actually wants.
|
|
| 'route:unmatched';
|
|
|
|
/**
|
|
* 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>>();
|
|
|
|
/** Subscribe `listener` to `event`; returns an unsubscribe function. */
|
|
on(event: LifecycleEvent, listener: (p: EventPayload) => void): () => void {
|
|
const listenerSet = this.listeners.get(event) ?? new Set();
|
|
listenerSet.add(listener);
|
|
this.listeners.set(event, listenerSet);
|
|
return () => listenerSet.delete(listener);
|
|
}
|
|
|
|
/**
|
|
* Whether anything is listening to `event` — for skipping the *construction* of a payload nobody
|
|
* will read.
|
|
*
|
|
* Not a micro-optimisation looking for a problem. A correlated payload is built by spreading the
|
|
* request's identity into it, and that spread was measured at ~197 ns per request on a two-step
|
|
* pipeline — against a ~4.5 µs in-process request, on a path where the overwhelmingly common
|
|
* case is an application that subscribes to nothing at all. `emit` cannot help: its argument is
|
|
* already built by the time it is called. Only the caller can decline to build it.
|
|
*/
|
|
hasListeners(event: LifecycleEvent): boolean {
|
|
const listenerSet = this.listeners.get(event);
|
|
return listenerSet !== undefined && listenerSet.size > 0;
|
|
}
|
|
|
|
/** Dispatch `payload` to every listener of `event`; a throwing listener is isolated and does not affect the others. */
|
|
emit(event: LifecycleEvent, payload: EventPayload): void {
|
|
for (const listener of this.listeners.get(event) ?? []) {
|
|
try {
|
|
listener(payload);
|
|
} catch {
|
|
/* observers must never break the pipeline */
|
|
}
|
|
}
|
|
}
|
|
}
|