mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-11-17 17:44:09 +01:00
12 lines
641 B
TypeScript
12 lines
641 B
TypeScript
import { AsyncLocalStorage } from "async_hooks";
|
|
import { randomUUID } from "crypto";
|
|
|
|
export interface LogContext { requestId?: string; jobId?: string; attempt?: number; service?: string; route?: string; method?: string; requestPath?: string }
|
|
export const logContext = new AsyncLocalStorage<LogContext>();
|
|
export const validTraceId = (value: unknown): value is string => typeof value === "string" &&
|
|
/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/.test(value);
|
|
export function jobTrace() {
|
|
const requestId = logContext.getStore()?.requestId;
|
|
return { logJobId: randomUUID(), ...(requestId ? { requestId } : {}) };
|
|
}
|