Healthz Lag Monitor (lib/healthzLag.ts)

omniroute11 · src/lib/healthzLag.ts · typescript · MIT
infra, fw:node, tag:performance, tag:lag

Monitors Node.js event loop lag for health checks, warns when lag exceeds threshold, and provides formatted warnings.

// Source: https://github.com/muhhabat00786-design/omniroute11 | License: MIT | Copyright 2026 diegosouzapw
// Provided via justclaw.it.com; keep this notice with the code.
import { monitorEventLoopDelay } from "node:perf_hooks";

/** `/healthz` returning 200 after this much event-loop lag is already sick (#10303). */
export const HEALTHZ_SLOW_LAG_MS = 200;
const WARN_EVERY_MS = 10_000;

let lastWarnAt = 0;
let histogram: ReturnType<typeof monitorEventLoopDelay> | null = null;

export function resetHealthzLagWarnStateForTests(): void {
  lastWarnAt = 0;
}

export function shouldWarnHealthzLag(lagMs: number, now = Date.now()): boolean {
  if (!Number.isFinite(lagMs) || lagMs < HEALTHZ_SLOW_LAG_MS) return false;
  if (now - lastWarnAt < WARN_EVERY_MS) return false;
  lastWarnAt = now;
  retu

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key