Files
Compass/backend/shared/src/monitoring/context.ts
2025-08-27 21:30:05 +02:00

29 lines
718 B
TypeScript

// Shared contextual information that metrics and logging can use, e.g.
// the scheduler job or HTTP request endpoint currently running.
import { AsyncLocalStorage } from 'node:async_hooks'
export type ContextDetails = Record<string, string>
export type JobContext = ContextDetails & {
job: string
traceId: string
}
export type RequestContext = ContextDetails & {
endpoint: string
traceId: string
}
export type MonitoringContext = JobContext | RequestContext
export const STORE = new AsyncLocalStorage<MonitoringContext>()
export function withMonitoringContext<R>(ctx: MonitoringContext, fn: () => R) {
return STORE.run(ctx, fn)
}
export function getMonitoringContext() {
return STORE.getStore()
}