mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
metricsMiddleware: add comments
This commit is contained in:
parent
24d6ae0ce9
commit
9b521e9f36
1 changed files with 6 additions and 0 deletions
|
|
@ -2,13 +2,19 @@ import { MiddlewareHandler } from '@hono/hono';
|
|||
|
||||
import { httpRequestCounter, httpResponseCounter } from '@/metrics.ts';
|
||||
|
||||
/** Prometheus metrics middleware that tracks HTTP requests by methods and responses by status code. */
|
||||
export const metricsMiddleware: MiddlewareHandler = async (c, next) => {
|
||||
// HTTP Request.
|
||||
const { method } = c.req;
|
||||
httpRequestCounter.inc({ method });
|
||||
|
||||
// Wait for other handlers to run.
|
||||
await next();
|
||||
|
||||
// HTTP Response.
|
||||
const { status } = c.res;
|
||||
// Get a parameterized path name like `/posts/:id` instead of `/posts/1234`.
|
||||
// Tries to find actual route names first before falling back on potential middleware handlers like `app.use('*')`.
|
||||
const path = c.req.matchedRoutes.find((r) => r.method !== 'ALL')?.path ?? c.req.routePath;
|
||||
httpResponseCounter.inc({ status, path });
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue