AsyncLocalStorage Correlation Logging
Tag every log line with a request correlation id using AsyncLocalStorage, letting deeply nested async code log without passing context.
Code
import { AsyncLocalStorage } from 'node:async_hooks';
import pino from 'pino';
interface RequestScope {
requestId: string;
userId?: string;
}
const storage = new AsyncLocalStorage<RequestScope>();
const logger = pino({
level: 'info',
mixin: () => {
const scope = storage.getStore();
return scope ? { requestId: scope.requestId, userId: scope.userId } : {};
},
});
async function chargeCard(orderId: string) {
logger.info({ orderId }, 'charging card'); // 自动带上请求上下文
await new Promise((r) => setTimeout(r, 10));
}
async function handleCheckout(req) {
await chargeCard('ord-5');
await refundIfDuplicate('ord-5');
}
async function refundIfDuplicate(orderId: string) {
logger.warn({ orderId }, 'duplicate detected');
}
async function entry(request) {
const scope: RequestScope = {
requestId: request.headers['x-request-id'] ?? crypto.randomUUID(),
userId: request.headers['x-user-id'],
};
// 后续整条异步链路都在同一 scope 内执行
return storage.run(scope, () => handleCheckout(request));
}
await entry({ headers: {} });How the snippet works
The code’s whole job belongs to the functions chargeCard, handleCheckout, refundIfDuplicate and entry.
Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed. From pino it pulls AsyncLocalStorage — the imports name exactly which library surface the snippet touches.
Points of care
- External dependency:
pino— install it before running the snippet. cryptoexist only in browsers — a Node runtime has no shims for them out of the box.
Using it
The primary signature is async function chargeCard(orderId: string)
Key statements
await new Promise((r) => setTimeout(r, 10));
return storage.run(scope, () => handleCheckout(request));
return scope ? { requestId: scope.requestId, userId: scope.userId } : {};
Interactive Tools for This Topic
FAQ
- Are there third-party dependencies?
- `pino` — install it with the package manager before running.
Report a problem with this exampleOpens a pre-filled issue on GitHub.