All checks were successful
CI / verification (pull_request) Successful in 59s
GET /health only gave a snapshot, so the monitoring missed the 2026-11-03
saturation and the nights before it. A one-minute tick, armed in the
require.main block, now records host load, CPU by /proc/stat difference
and, when TLS_PROBE_HOSTS is set, the TLS handshake time of each listed
host into an in-memory buffer of 1440 samples (load-history.js, pure).
- /health gains `saturation` ({state: ok|saturated|unknown, since,
coverageMinutes, load1, cores}) and `tls`, appended after the existing
keys. Saturated = the last five samples of an unbroken series all have
load1 > cores; unknown under five samples, after a gap over 3 minutes,
or when the last sample is over 3 minutes old. No sample at start-up,
so the state stays unknown for the first five minutes after a deploy.
- GET /health/history (read token) serves {since, coverageMinutes, cores,
samples}. The buffer restarts from zero at every deploy.
- probeTls() verifies the certificate, times out after 3 s and reports an
error code only. TLS_PROBE_HOSTS is empty by default: no network call in
the tests, the CI or boot-from-copy.sh.
- Dockerfile COPY, REVIEW.md Haut table (and review-levels.py), CLAUDE.md,
docs and .env.example updated.
Refs #73
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
114 lines
4.3 KiB
JavaScript
114 lines
4.3 KiB
JavaScript
// In-memory load history for /health and /health/history (#73).
|
|
//
|
|
// Pure module: no I/O, no timer, no clock of its own. The caller (the
|
|
// one-minute tick in index.js) reads the host and hands each sample in; every
|
|
// time-dependent answer takes `now` as an argument so the tests drive the
|
|
// clock. The buffer lives in the process: every deploy (every merge on main)
|
|
// starts it again from zero, which is why snapshot() and saturation() always
|
|
// say how far back they reach.
|
|
|
|
const DEFAULT_CAPACITY = 1440; // 24 h at one sample a minute
|
|
const SATURATION_WINDOW = 5; // consecutive samples above the core count
|
|
const MAX_GAP_MS = 3 * 60 * 1000; // a longer gap breaks the series
|
|
|
|
function isFiniteNumber(value) {
|
|
return typeof value === "number" && Number.isFinite(value);
|
|
}
|
|
|
|
function numberOrNull(value) {
|
|
return isFiniteNumber(value) ? value : null;
|
|
}
|
|
|
|
function createHistory({ capacity = DEFAULT_CAPACITY } = {}) {
|
|
if (!Number.isInteger(capacity) || capacity < 1) {
|
|
throw new Error("capacity must be a positive integer");
|
|
}
|
|
const samples = [];
|
|
|
|
// Rebuilt field by field: nothing the caller adds rides along into the
|
|
// response. A sample without a usable timestamp or load1 is dropped.
|
|
function record({ at, load1, load5, cpuPercent, tls } = {}) {
|
|
if (!isFiniteNumber(at) || !isFiniteNumber(load1)) return false;
|
|
const last = samples[samples.length - 1];
|
|
if (last && at <= last.at) return false;
|
|
samples.push({
|
|
at,
|
|
load1,
|
|
load5: numberOrNull(load5),
|
|
cpuPercent: numberOrNull(cpuPercent),
|
|
tls: Array.isArray(tls) ? tls : null,
|
|
});
|
|
if (samples.length > capacity) samples.splice(0, samples.length - capacity);
|
|
return true;
|
|
}
|
|
|
|
function coverageMinutes() {
|
|
if (samples.length < 2) return 0;
|
|
return Math.round((samples[samples.length - 1].at - samples[0].at) / 60000);
|
|
}
|
|
|
|
function snapshot() {
|
|
return {
|
|
since: samples.length ? new Date(samples[0].at).toISOString() : null,
|
|
coverageMinutes: coverageMinutes(),
|
|
samples: samples.map((s) => ({
|
|
at: new Date(s.at).toISOString(),
|
|
load1: s.load1,
|
|
load5: s.load5,
|
|
cpuPercent: s.cpuPercent,
|
|
tls: s.tls,
|
|
})),
|
|
};
|
|
}
|
|
|
|
// The most recent unbroken run: walking back from the last sample, stop at
|
|
// the first gap longer than MAX_GAP_MS.
|
|
function trailingRun() {
|
|
let start = samples.length - 1;
|
|
while (start > 0 && samples[start].at - samples[start - 1].at <= MAX_GAP_MS) start--;
|
|
return samples.slice(Math.max(start, 0));
|
|
}
|
|
|
|
// "saturated" when the last five samples of an unbroken run all have
|
|
// load1 > cores; "unknown" under five samples, after a gap, or when the
|
|
// latest sample is older than three minutes (the tick has stopped).
|
|
// `since` is when the current state began, within the run.
|
|
function saturation(now, cores) {
|
|
const last = samples[samples.length - 1];
|
|
const base = {
|
|
coverageMinutes: coverageMinutes(),
|
|
load1: last ? last.load1 : null,
|
|
cores,
|
|
};
|
|
const run = trailingRun();
|
|
if (!last || now - last.at > MAX_GAP_MS || run.length < SATURATION_WINDOW || !(cores > 0)) {
|
|
return { state: "unknown", since: null, ...base };
|
|
}
|
|
|
|
const window = run.slice(-SATURATION_WINDOW);
|
|
const saturated = window.every((s) => s.load1 > cores);
|
|
let since;
|
|
if (saturated) {
|
|
// First sample of the current streak above cores.
|
|
let i = run.length - 1;
|
|
while (i > 0 && run[i - 1].load1 > cores) i--;
|
|
since = run[i].at;
|
|
} else {
|
|
// Back to ok at the sample that closed the last saturated window; since
|
|
// the run began if no window of the run was ever saturated.
|
|
let lastSaturatedEnd = -1;
|
|
for (let end = SATURATION_WINDOW - 1; end < run.length; end++) {
|
|
const w = run.slice(end - SATURATION_WINDOW + 1, end + 1);
|
|
if (w.every((s) => s.load1 > cores)) lastSaturatedEnd = end;
|
|
}
|
|
// The current window is not saturated, so lastSaturatedEnd is at most
|
|
// run.length - 2; -1 (never saturated) lands on the start of the run.
|
|
since = run[lastSaturatedEnd + 1].at;
|
|
}
|
|
return { state: saturated ? "saturated" : "ok", since: new Date(since).toISOString(), ...base };
|
|
}
|
|
|
|
return { record, snapshot, saturation, size: () => samples.length };
|
|
}
|
|
|
|
module.exports = { createHistory, DEFAULT_CAPACITY, SATURATION_WINDOW, MAX_GAP_MS };
|