node: add initial standard checks

This commit is contained in:
Elijah Duffy
2025-12-24 18:29:28 -08:00
parent ae664da0e4
commit a6d9d72322

26
node-health/src/checks.ts Normal file
View File

@@ -0,0 +1,26 @@
import { IntervalHistogram, monitorEventLoopDelay } from 'node:perf_hooks';
import { ReadinessDetail, ReadinessFunction, ReadinessStatus } from './readiness';
let hist: IntervalHistogram | null = null;
/**
* Builds a readiness check function that monitors event loop lag.
* @param degradedMs - The threshold in milliseconds above which the status is 'degraded' (default 200 ms).
* @returns A ReadinessFunction that checks event loop lag.
*/
export const buildEventLoopLagCheck = (degradedMs: number = 200): ReadinessFunction => {
if (!hist) {
hist = monitorEventLoopDelay({ resolution: 10 });
hist.enable();
}
return (): ReadinessDetail => {
const lag = hist!.mean / 1e6; // Convert from nanoseconds to milliseconds
const status: ReadinessStatus = lag < degradedMs ? 'ok' : 'degraded';
return {
name: 'event-loop-lag',
status,
message: `Event loop lag is ${lag.toFixed(2)} ms`,
};
};
};