[claude] API observability — structured logging + /api/metrics endpoint (#57) (#87)

This commit was merged in pull request #87.
This commit is contained in:
2026-03-23 20:10:40 +00:00
parent 113095d2f0
commit 5dc71e1257
6 changed files with 75 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
import crypto from "crypto";
import type { Request, Response, NextFunction } from "express";
const HEADER = "X-Request-Id";
/**
* Assigns a unique request ID to every incoming request.
* If the client (or a reverse proxy) already sent X-Request-Id, reuse it;
* otherwise generate a short random hex string.
* The ID is stored on `res.locals.requestId` for downstream middleware/routes
* and echoed back via the X-Request-Id response header.
*/
export function requestIdMiddleware(req: Request, res: Response, next: NextFunction): void {
const id = (req.headers[HEADER.toLowerCase()] as string | undefined) ?? crypto.randomUUID();
res.locals["requestId"] = id;
res.setHeader(HEADER, id);
next();
}