Skip to content

Node.js — alert on rising event-loop lag

Node app stalls under CPU-blocking operations; user latency creeps up. Want an endpoint that exposes the live event-loop-lag value.

Recipe

js
// /healthz endpoint that exposes event-loop lag in ms
const express = require('express');
const { monitorEventLoopDelay } = require('perf_hooks');
const h = monitorEventLoopDelay({ resolution: 20 });
h.enable();

const THRESHOLD_MS = parseInt(process.env.LAG_THRESHOLD_MS || '50', 10);

const app = express();
app.get('/healthz', (_req, res) => {
  const p99 = Math.round(h.percentile(99) / 1e6); // ns → ms
  h.reset();
  res.type('text/plain');
  res.send(p99 >= THRESHOLD_MS ? `lag ${p99}` : `ok ${p99}`);
});
app.listen(8080);

Same thing in Enterno.io

Point an Enterno HTTP monitor with body-keyword "ok". Multi-region check (Pro+) tells you whether it is a global stall or a single-node problem.

Set up HTTP monitor → ← All recipes

Related recipes

long_query_time = 1, slow_query_log enabled. You need to know when the slow-query rate per minute suddenly jumps (a deploy broke an index, ORM went N+1).

Spring app is sluggish — long GC pauses (>500ms) every few minutes. Heap size was fine but the new-gen ratio is misconfigured. Want an endpoint with p99 pause.