Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/added-healthz-request-metrics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Record `/api/healthz` request count, status class, and duration in the existing bounded `/api/metrics` registry so readiness failures can be alerted on.
15 changes: 15 additions & 0 deletions src/__tests__/healthz-route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from "fs";
import { docsContentPath } from "@/app/docs/page-map";
import { GET } from "@/app/api/healthz/route";
import { logger } from "@/lib/logger";
import { metricsRegistry } from "@/lib/metrics";

/**
* Coverage for src/app/api/healthz/route.ts — the readiness probe that
Expand Down Expand Up @@ -154,4 +155,18 @@ describe("/api/healthz route", () => {
error: "EACCES: permission denied",
});
});

it("records healthz requests by status class in the metrics registry", async () => {
statSyncSpy.mockImplementationOnce(() => {
throw new Error("boom");
});

const res = await GET();

expect(res.status).toBe(503);
const body = await metricsRegistry.metrics();
expect(body).toContain(
'docs_api_requests_total{route="healthz",method="GET",status_class="5xx"}'
);
});
});
13 changes: 13 additions & 0 deletions src/app/api/healthz/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NextResponse } from "next/server";
import fs from "fs";
import { docsContentPath } from "../../docs/page-map";
import { logger } from "@/lib/logger";
import { recordApiRequest } from "@/lib/metrics";

// Readiness check for the docs app.
//
Expand All @@ -15,6 +16,18 @@ import { logger } from "@/lib/logger";
// orchestrator or deploy pipeline can detect that condition before routing
// traffic to this instance.
export async function GET() {
const startedAt = performance.now();
const response = check();
recordApiRequest(
"healthz",
"GET",
response.status,
performance.now() - startedAt
);
return response;
}

function check(): NextResponse {
try {
const stat = fs.statSync(docsContentPath);
if (!stat.isDirectory()) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
import { Counter, Histogram, Registry } from "prom-client";

export const ApiRoutes = ["search", "docs-image"] as const;
export const ApiRoutes = ["search", "docs-image", "healthz"] as const;
export type ApiRoute = (typeof ApiRoutes)[number];

const HTTP_METHODS = new Set([
Expand Down
Loading