Skip to main content

http_metrics_middleware

Function http_metrics_middleware 

Source
pub async fn http_metrics_middleware(req: Request, next: Next) -> Response
Expand description

Tower middleware that emits the three HTTP-metric families per request.

Operationally:

  1. Resolve the route template via route_label (one extension lookup).
  2. Snapshot Instant::now as the request-start time.
  3. Construct an InFlightGuard for (route, method) (increments the in-flight gauge; its Drop decrements it on every exit path, including cancellation/timeout).
  4. Run the inner service.
  5. Drop the guard, decrementing the in-flight gauge.
  6. Compute elapsed seconds via Instant::elapsed (saturating).
  7. 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.