Update Prometheus metrics to conform to best practices

This commit is contained in:
Alex Gleason
2024-09-07 08:52:02 -05:00
parent 3fa97c0533
commit 5454942a2c
8 changed files with 42 additions and 48 deletions

View File

@@ -1,12 +1,12 @@
import { MiddlewareHandler } from '@hono/hono';
import { httpRequestCounter, httpResponseCounter } from '@/metrics.ts';
import { httpRequestsCounter, httpResponsesCounter } 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 });
httpRequestsCounter.inc({ method });
// Wait for other handlers to run.
await next();
@@ -16,5 +16,5 @@ export const metricsMiddleware: MiddlewareHandler = async (c, next) => {
// 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({ method, status, path });
httpResponsesCounter.inc({ method, status, path });
};