pub async fn http_metrics_middleware(req: Request, next: Next) -> ResponseExpand description
Tower middleware that emits the three HTTP-metric families per request.
Operationally:
- Resolve the route template via
route_label(one extension lookup). - Snapshot
Instant::nowas the request-start time. - Construct an
InFlightGuardfor(route, method)(increments the in-flight gauge; itsDropdecrements it on every exit path, including cancellation/timeout). - Run the inner service.
- Drop the guard, decrementing the in-flight gauge.
- Compute elapsed seconds via
Instant::elapsed(saturating). - Increment the request counter and observe the duration histogram for
(route, method, status).
If the HttpMetricsLayerConfig extension is absent (e.g. a test
driver that bypassed the production router), the middleware degrades to
a no-op pass-through — no panics, no metric emission. This mirrors the
audit-log middleware’s behaviour.