PR Review Lab · Record Request Timing Metrics

20:00left of 20:00

Record Request Timing Metrics

We have no per-endpoint latency data, so slow routes only show up as customer complaints. This adds middleware that times every request and records it in a histogram labeled by method, route pattern, and status code. Unmatched paths share one "unmatched" label so raw URLs never become label values. The existing /metrics endpoint exposes it.

Aapi/middleware/requestTiming.ts1 of 3

Files Changed

api/middleware/

api/

api/middleware/requestTiming.ts

TypeScript · Click a line number to comment · Tap a line to comment
@@ -0,0 +1,25 @@
Added: import type { NextFunction, Request, Response } from "express";
Added: import { Histogram } from "prom-client";
Added:
Added: export const requestDuration = new Histogram({
Added: name: "http_request_duration_ms",
Added: help: "HTTP request duration in milliseconds",
Added: labelNames: ["method", "route", "status"],
Added: buckets: [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000],
Added: });
Added:
Added: export function requestTiming(req: Request, res: Response, next: NextFunction) {
Added: const start = process.hrtime.bigint();
Added: res.on("finish", () => {
Added: const ms = Number(process.hrtime.bigint() - start) / 1e6;
Added: requestDuration.observe(
Added: {
Added: method: req.method,
Added: route: req.route?.path ?? "unmatched",
Added: status: String(res.statusCode),
Added: },
Added: ms,
Added: );
Added: });
Added: next();
Added: }