feat(observability): request-level events, and request logging that reads them
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>
This commit is contained in:
7 files changed
+303
-6
No files matched your search
+13
-1
@@ -1,6 +1,6 @@
|
||||
import type http from 'http';
|
||||
import { Bus } from '../bus';
|
||||
import { createDefaultLogger, type Logger } from '../logger';
|
||||
import { createDefaultLogger, logRequests, type Logger } from '../logger';
|
||||
import { Container } from '../container';
|
||||
import { topoSort, subgraphFor, GraphNode, nearest } from '../graph';
|
||||
import { toMermaid, toDOT, graphHtml, type GraphView } from '../graph-viz';
|
||||
@@ -121,6 +121,15 @@ export function createApp(opts: {
|
||||
* `@needs('logger')` — one object, two access paths.
|
||||
*/
|
||||
logger?: Logger;
|
||||
/**
|
||||
* Log one line per completed request, and one per failed one (default: off).
|
||||
*
|
||||
* Off by default because a framework that writes to stdout without being asked is a framework
|
||||
* you have to configure before you can use it. On, it is a subscriber to the lifecycle stream
|
||||
* like any other consumer — `logRequests(app.bus, app.logger)` does the same thing by hand and
|
||||
* hands back an unsubscribe, so nothing here is reachable only through this flag.
|
||||
*/
|
||||
logRequests?: boolean;
|
||||
/** Opt in to alpha features whose API may still change. Currently gates `mesh`. */
|
||||
experimental?: boolean;
|
||||
/**
|
||||
@@ -139,6 +148,7 @@ export function createApp(opts: {
|
||||
|
||||
const bus = new Bus();
|
||||
const logger = opts.logger ?? createDefaultLogger();
|
||||
if (opts.logRequests) logRequests(bus, logger);
|
||||
const container = new Container();
|
||||
let server: http.Server | undefined;
|
||||
const streams = new Set<() => void>();
|
||||
@@ -234,6 +244,7 @@ export function createApp(opts: {
|
||||
const staticResolver = opts.static ? buildStaticResolver(opts.static) : undefined;
|
||||
|
||||
const fetchOpts: HttpOptions = {
|
||||
bus,
|
||||
limits: opts.limits,
|
||||
tls: opts.tls,
|
||||
trustProxy: opts.trustProxy,
|
||||
@@ -264,6 +275,7 @@ export function createApp(opts: {
|
||||
const wsRoutes = buildWsRoutes(routePlans, deps);
|
||||
|
||||
server = createHttpServer(httpRoutes, wsRoutes, bus, meshControl, {
|
||||
bus,
|
||||
limits: opts.limits,
|
||||
streams,
|
||||
tls: opts.tls,
|
||||
|
||||
Reference in new issue
Block a user