A simple Caddy HTTP middleware that records request duration metrics per route and exposes them to Prometheus.
Build Caddy with this plugin using xcaddy:
xcaddy build \
--with github.com/onliner/caddy-route-metricsEnable route_metrics inside your Caddyfile.
By default, the header name is X-Route-Pattern.
{
metrics
admin localhost:2019
}
example.com {
route_metrics {
header X-Route-Pattern
}
handle_path /ok* {
header {
X-Route-Pattern "/users/{id}"
}
respond "ok"
}
handle_path /miss* {
respond "miss"
}
}The directive route_metrics supports the following option:
- header <name> (optional)
The HTTP header name from which to read the route pattern. This header must be set on the response by your application or middleware.
Defaults toX-Route-Pattern.
The middleware records one Prometheus histogram for request duration:
| Metric name | Type | Labels |
|---|---|---|
| caddy_route_request_duration_seconds | Histogram | method, route |
method— the HTTP method (e.g. GET, POST).route— the route pattern taken from the configured header (e.g./users/{id}).
- If the header is not present in the response, the request is skipped and no metric is recorded.