10LOC

#observability

Node.jsadvanced

AsyncLocalStorage for request-scoped trace IDs across async calls

import { AsyncLocalStorage } from "node:async_hooks";
import { createServer } from "node:http";
import { randomUUID } from "node:crypto";

const requestContext = new AsyncLocalStorage<string>();

Thread a trace ID through every async call of a request, without passing it as an argument through every function in between.