Skip to content

About

An example using latest perf_hooks features

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

Fastify sliding-window health example

Sample using the new Node.js perf_hooks APIs to monitor health with no dependencies.

  • Fastify response latency for every completed request.
  • Native event-loop delay, represented by one interval-p99 sample per second.
  • Event-loop utilization, sampled once per second.

Each signal is stored in a fixed-memory sliding window. The default window is six 10-second chunks, giving approximately one minute of recent data.

Run

npm install
npm start

npm start intentionally uses ../../node/out/Release/node, because a stock Node.js binary does not yet provide createSlidingWindowHistogram().

(you'll need to build nodejs/node locally and adjust the paths accordingly)

In another terminal:

curl -s http://127.0.0.1:3000/
curl -s 'http://127.0.0.1:3000/health?refresh=true' | jq

Generate ordinary traffic, then deliberately block the event loop. The /work route is disabled by default because it intentionally consumes CPU; restart the example with ENABLE_DEMO_WORK=true npm start before running:

for i in $(seq 1 100); do
  curl -s http://127.0.0.1:3000/ > /dev/null
done

curl -s 'http://127.0.0.1:3000/work?blockMs=150'
sleep 2
curl -s 'http://127.0.0.1:3000/health?refresh=true' | jq

The degraded response uses HTTP status 503. During initial sampling, the status is warming and the endpoint returns 200.

Reported analysis

The cached /health report includes:

  • Batched p50, p90, p95, and p99 values with percentilesAt().
  • Mean and p99 95% confidence intervals.
  • CDF and complementary CDF rates against configurable thresholds.
  • Skewness and excess kurtosis for tail-shape diagnostics.
  • QRDE quantiles and densities calculated from detached snapshots.
  • Periodic KS distance, Welch's t-test, Mann-Whitney U test, Cohen's d, and Cliff's delta against an approximately non-overlapping prior window.

QRDE work runs in the libuv thread pool during background report refreshes. /health normally serves the cached report; pass refresh=true to request an immediate analysis pass. Concurrent refreshes are coalesced. A report older than three refresh intervals is returned as degraded.

The event-loop delay SLO means that 99% of the per-interval p99 samples must be at or below the configured threshold. This intentionally makes sustained or repeated delay spikes visible across the window.

Configuration

Environment variable Default
PORT 3000
HOST 127.0.0.1
WINDOW_CHUNKS 6
WINDOW_CHUNK_MS 10000
SAMPLE_INTERVAL_MS 1000
REPORT_INTERVAL_MS 5000
LATENCY_SLO_MS 100
EVENT_LOOP_DELAY_SLO_MS 50
ELU_LIMIT 0.85
ENABLE_DEMO_WORK false

This is an observability example, not a universal production health policy. Tune thresholds and decide whether degraded telemetry should trigger a restart for the service being monitored. Do not expose the demonstration /work route to untrusted traffic.

About

An example using latest perf_hooks features

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages