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:
8 files changed
+305
-13
No files matched your search
+42
-9
@@ -1,4 +1,4 @@
|
||||
import { Bus } from './bus';
|
||||
import { Bus, type Correlation } from './bus';
|
||||
import { renderError, type ErrorRenderer } from './transformers';
|
||||
import { isAsyncIterable } from './channel';
|
||||
import type { TransformerFn, Transport } from './metadata';
|
||||
@@ -45,15 +45,44 @@ export function isStreamResult(result: PipelineResult): result is StreamResult {
|
||||
* `request:step:enter`/`leave` on the bus. The single place steps are executed — every
|
||||
* transport (HTTP, ws, mesh) goes through here so observability is uniform by construction.
|
||||
*/
|
||||
export async function runSteps(steps: PipelineStep[], seed: Record<string, unknown>, bus: Bus): Promise<any> {
|
||||
export async function runSteps(
|
||||
steps: PipelineStep[],
|
||||
seed: Record<string, unknown>,
|
||||
bus: Bus,
|
||||
correlation: Correlation = {},
|
||||
): Promise<any> {
|
||||
// context is intentionally `any`: each step merges arbitrary keys into the accumulator
|
||||
const context: any = seed;
|
||||
// One timestamp per boundary, not one pair per step: a step's end is the next one's start, so
|
||||
// N steps need N+1 readings rather than 2N. Measured at 25.2ns vs 45.3ns per step, against a
|
||||
// ~10.19us request — 0.25%, below the 0.3% run-to-run CV of the project's own benchmark, which
|
||||
// is why this is always on instead of hiding behind a flag nobody could tune with evidence.
|
||||
let mark = performance.now();
|
||||
|
||||
for (const step of steps) {
|
||||
bus.emit('request:step:enter', { name: step.name, scope: step.origin });
|
||||
const output = await step.run(context);
|
||||
Object.assign(context, output);
|
||||
bus.emit('request:step:leave', { name: step.name, scope: step.origin });
|
||||
bus.emit('request:step:enter', { name: step.name, scope: step.origin, ...correlation });
|
||||
|
||||
try {
|
||||
const output = await step.run(context);
|
||||
Object.assign(context, output);
|
||||
} catch (error) {
|
||||
// Emitted here, where the step that failed is still known. The outer catch in runPipeline
|
||||
// only sees "something in the pipeline threw" and used to report `name: 'pipeline'`, which
|
||||
// threw away the one fact the event exists to carry. Rethrown unchanged — this observes.
|
||||
const now = performance.now();
|
||||
bus.emit('request:step:error', {
|
||||
name: step.name,
|
||||
scope: step.origin,
|
||||
error,
|
||||
durationMs: now - mark,
|
||||
...correlation,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
|
||||
const now = performance.now();
|
||||
bus.emit('request:step:leave', { name: step.name, scope: step.origin, durationMs: now - mark, ...correlation });
|
||||
mark = now;
|
||||
}
|
||||
|
||||
return context;
|
||||
@@ -72,13 +101,14 @@ export async function runPipeline(args: {
|
||||
bus: Bus;
|
||||
transport: Transport;
|
||||
onError?: ErrorRenderer;
|
||||
correlation?: Correlation;
|
||||
}): Promise<PipelineResult> {
|
||||
const { steps, handler, transformer, seed, bus, transport, onError } = args;
|
||||
const { steps, handler, transformer, seed, bus, transport, onError, correlation = {} } = args;
|
||||
// context is intentionally `any`: each step merges arbitrary keys into the accumulator
|
||||
let context: any = { ...seed };
|
||||
|
||||
try {
|
||||
context = await runSteps(steps, context, bus);
|
||||
context = await runSteps(steps, context, bus, correlation);
|
||||
const result = await handler(context);
|
||||
const streaming = isAsyncIterable(result);
|
||||
const contract = TRANSPORT_RETURN[transport];
|
||||
@@ -95,7 +125,10 @@ export async function runPipeline(args: {
|
||||
const transformed = transformer(result);
|
||||
return { status: transformed.status ?? 200, headers: transformed.headers ?? {}, body: transformed.body };
|
||||
} catch (error) {
|
||||
bus.emit('request:step:error', { name: 'pipeline', error });
|
||||
// Both fire for a failing step, deliberately: `request:step:error` above marks the span,
|
||||
// this marks the trace. A tracing exporter needs each, and only emitting the outer one is
|
||||
// what produces a trace that says a request failed without saying where.
|
||||
bus.emit('request:failed', { name: correlation.route ?? 'pipeline', error, ...correlation });
|
||||
const req = (context.req ?? {}) as { method?: string; url?: string; headers?: Record<string, unknown> };
|
||||
return renderError(
|
||||
error,
|
||||
|
||||
Reference in new issue
Block a user