MentraOS / cloud-v2 /packages /core /src /api /middleware /context.middleware.ts
Anirudh1950's picture
Add files using upload-large-folder tool
17c0464 verified
Raw History Blame Contribute Delete
1.79 kB
/**
* @fileoverview Per-request context middleware.
*
* Runs first on every request inside `/api/*`. Two responsibilities:
*
* 1. Assign a request ID. Use the inbound `X-Request-Id` header if the
* caller (frontend, proxy, another service) already chose one so traces
* chain across hops; otherwise mint a fresh ULID. Echoed back in the
* response header so the client can correlate too.
*
* 2. Attach a per-request child logger pre-bound with `reqId`, method, and
* path. Handlers should log via `c.var.logger` rather than the root
* logger so every log line in a request is automatically correlated.
* The path never includes the query string: sign-in callbacks
* (`/api/admin/auth/callback?code=…&state=…`) carry authorization codes there.
*
* 3. Run the rest of the request with that id as the current request id
* (`services/request-context.ts`), so services without the Hono context,
* such as the workspace audit trail, can record it.
*/
import { createMiddleware } from "hono/factory";
import { ulid } from "ulid";
import { createLogger } from "@mentra/cloud-shared";
import { runWithRequestId } from "../../services/request-context";
import type { AppEnv } from "../../types/hono.types";
const rootLogger = createLogger("core");
export const requestContext = createMiddleware<AppEnv>(async (c, next) => {
const incoming = c.req.header("x-request-id");
const reqId = incoming && incoming.length <= 64 ? incoming : ulid();
c.set("reqId", reqId);
c.set(
"logger",
rootLogger.child({
reqId,
method: c.req.method,
path: c.req.path,
}),
);
c.header("x-request-id", reqId);
await runWithRequestId(reqId, next);
});