mirror of
https://github.com/aljazceru/ditto.git
synced 2026-02-23 05:56:03 +01:00
Record HTTP response time metrics
This commit is contained in:
@@ -12,6 +12,12 @@ export const httpResponsesCounter = new Counter({
|
||||
labelNames: ['method', 'path', 'status'],
|
||||
});
|
||||
|
||||
export const httpResponseDurationHistogram = new Histogram({
|
||||
name: 'ditto_http_response_duration_seconds',
|
||||
help: 'Histogram of HTTP response times in seconds',
|
||||
labelNames: ['method', 'path', 'status'],
|
||||
});
|
||||
|
||||
export const streamingConnectionsGauge = new Gauge({
|
||||
name: 'ditto_streaming_connections',
|
||||
help: 'Number of active connections to the streaming API',
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
import { ScopedPerformance } from '@esroyo/scoped-performance';
|
||||
import { MiddlewareHandler } from '@hono/hono';
|
||||
|
||||
import { httpRequestsCounter, httpResponsesCounter } from '@/metrics.ts';
|
||||
import { httpRequestsCounter, httpResponseDurationHistogram, 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) => {
|
||||
// Start a timer to measure the duration of the response.
|
||||
using perf = new ScopedPerformance();
|
||||
perf.mark('start');
|
||||
|
||||
// HTTP Request.
|
||||
const { method } = c.req;
|
||||
httpRequestsCounter.inc({ method });
|
||||
@@ -17,4 +22,8 @@ export const metricsMiddleware: MiddlewareHandler = async (c, next) => {
|
||||
// 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;
|
||||
httpResponsesCounter.inc({ method, status, path });
|
||||
|
||||
// Measure the duration of the response.
|
||||
const { duration } = perf.measure('total', 'start');
|
||||
httpResponseDurationHistogram.observe({ method, status, path }, duration / 1000);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user