HTTP Response metrics

This commit is contained in:
Alex Gleason
2024-07-05 00:00:57 +01:00
parent d4713cae01
commit 96a8ccb2e6
3 changed files with 18 additions and 9 deletions

View File

@@ -1,10 +1,14 @@
import { MiddlewareHandler } from '@hono/hono';
import { httpRequestCounter } from '@/metrics.ts';
import { httpRequestCounter, httpResponseCounter } from '@/metrics.ts';
export const metricsMiddleware: MiddlewareHandler = async (c, next) => {
const { method } = c.req;
httpRequestCounter.inc({ method });
await next();
const { status } = c.res;
const path = c.req.matchedRoutes.find((r) => r.method !== 'ALL')?.path ?? c.req.routePath;
httpResponseCounter.inc({ status, path });
};