diff --git a/appview/metrics/middleware.go b/appview/metrics/middleware.go index cce45ddb..18026b54 100644 --- a/appview/metrics/middleware.go +++ b/appview/metrics/middleware.go @@ -8,6 +8,7 @@ import ( "time" "github.com/go-chi/chi/v5" + "github.com/samber/lo" ) type statusRecorder struct { @@ -32,19 +33,23 @@ func Middleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { rec := &statusRecorder{ResponseWriter: w, status: http.StatusOK} start := time.Now() + returned := false - next.ServeHTTP(rec, r) + defer func() { + // use the matched route pattern to avoid high cardinality + routePattern := chi.RouteContext(r.Context()).RoutePattern() + if routePattern == "" { + routePattern = "unknown" + } - // use the matched route pattern to avoid high cardinality - routePattern := chi.RouteContext(r.Context()).RoutePattern() - if routePattern == "" { - routePattern = "unknown" - } + status := lo.Ternary(returned, fmt.Sprintf("%d", rec.status), "aborted") + duration := time.Since(start).Seconds() - status := fmt.Sprintf("%d", rec.status) - duration := time.Since(start).Seconds() + HttpRequestsTotal.WithLabelValues(r.Method, routePattern, status).Inc() + HttpRequestDuration.WithLabelValues(r.Method, routePattern, status).Observe(duration) + }() - HttpRequestsTotal.WithLabelValues(r.Method, routePattern, status).Inc() - HttpRequestDuration.WithLabelValues(r.Method, routePattern, status).Observe(duration) + next.ServeHTTP(rec, r) + returned = true }) } diff --git a/appview/pages/templates/repo/fragments/artifactList.html b/appview/pages/templates/repo/fragments/artifactList.html index eae6816f..f79d6154 100644 --- a/appview/pages/templates/repo/fragments/artifactList.html +++ b/appview/pages/templates/repo/fragments/artifactList.html @@ -13,7 +13,7 @@