mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-01-27 07:09:04 -05:00
29 lines
718 B
TypeScript
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()
|
|
}
|