Skip to content

Commit 886f0f5

Browse files
committed
feat: enrich health-check response
1 parent 63c407e commit 886f0f5

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

src/transports/monitoringServer.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type express from "express";
22
import { LogId } from "../common/logging/loggingDefinitions.js";
3+
import { packageInfo } from "../common/packageInfo.js";
34
import type {
45
DefaultMetrics,
56
MonitoringServerFeature,
@@ -53,7 +54,14 @@ export class MonitoringServer<TMetrics extends DefaultMetrics = DefaultMetrics>
5354
protected override setupRoutes(): Promise<void> {
5455
if (this.features.includes("health-check")) {
5556
this.app.get("/health", (_req: express.Request, res: express.Response) => {
56-
res.json({ status: "ok" });
57+
// Health responses should never be cached by proxies or load balancers.
58+
res.set("Cache-Control", "no-store");
59+
res.json({
60+
status: "ok",
61+
version: packageInfo.version,
62+
uptimeSeconds: Math.floor(process.uptime()),
63+
timestamp: new Date().toISOString(),
64+
});
5765
});
5866
}
5967

tests/unit/transports/monitoringServer.test.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,20 @@ describe("MonitoringServer", () => {
4747

4848
const response = await fetch(`${server.serverAddress}/health`);
4949
expect(response.status).toBe(200);
50-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
51-
const body = await response.json();
52-
expect(body).toEqual({ status: "ok" });
50+
expect(response.headers.get("cache-control")).toBe("no-store");
51+
const body = (await response.json()) as {
52+
status: string;
53+
version: string;
54+
uptimeSeconds: number;
55+
timestamp: string;
56+
};
57+
// status remains "ok" for backward compatibility with existing probes
58+
expect(body.status).toBe("ok");
59+
expect(typeof body.version).toBe("string");
60+
expect(typeof body.uptimeSeconds).toBe("number");
61+
expect(body.uptimeSeconds).toBeGreaterThanOrEqual(0);
62+
// timestamp is a valid ISO date string
63+
expect(Number.isNaN(Date.parse(body.timestamp))).toBe(false);
5364
});
5465

5566
it("does not expose health endpoint when health-check feature is disabled", async () => {

0 commit comments

Comments
 (0)