node: add initial standard checks
This commit is contained in:
26
node-health/src/checks.ts
Normal file
26
node-health/src/checks.ts
Normal 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`,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user