| 1 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | |
| 3 | package middleware |
| 4 | |
| 5 | import ( |
| 6 | "net/http" |
| 7 | "strconv" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/go-chi/chi/v5" |
| 11 | |
| 12 | "github.com/tenseleyFlow/shithub/internal/infra/metrics" |
| 13 | ) |
| 14 | |
| 15 | // Metrics returns middleware that records HTTP-level metrics via the |
| 16 | // project-wide Prometheus registry. |
| 17 | // |
| 18 | // Route labels are extracted from chi when available so we get |
| 19 | // "/owner/{repo}" instead of the per-repo concrete path — keeping |
| 20 | // cardinality bounded. |
| 21 | func Metrics(next http.Handler) http.Handler { |
| 22 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 23 | metrics.HTTPInFlight.Inc() |
| 24 | defer metrics.HTTPInFlight.Dec() |
| 25 | |
| 26 | start := time.Now() |
| 27 | rec := newStatusRecorder(w) |
| 28 | next.ServeHTTP(rec, r) |
| 29 | |
| 30 | route := r.URL.Path |
| 31 | if rctx := chi.RouteContext(r.Context()); rctx != nil && rctx.RoutePattern() != "" { |
| 32 | route = rctx.RoutePattern() |
| 33 | } |
| 34 | method := r.Method |
| 35 | status := strconv.Itoa(rec.status) |
| 36 | metrics.HTTPRequestsTotal.WithLabelValues(route, method, status).Inc() |
| 37 | metrics.HTTPRequestDuration.WithLabelValues(route, method).Observe(time.Since(start).Seconds()) |
| 38 | }) |
| 39 | } |
| 40 |