Prometheus metrics middleware capturing status + duration

5116
0

I like to start observability with two metrics: request duration and response codes. The wrapper below intercepts WriteHeader to capture status codes and then records both a histogram observation and a counter increment. The biggest gotcha is label cardinality: never label on raw URLs; label on a normalized route like /v1/orders/:id. In routers like chi, you can get the route pattern from context; otherwise you can pass a route string into the handler when wiring routes. Once this is in, you can build SLO dashboards for p95 latency and error rate. It also makes regressions obvious during deploys, because you can compare latency distributions before and after.