This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / appview / metrics / middleware.go
935 B 40 lines
1package metrics 2 3import ( 4 "fmt" 5 "net/http" 6 "time" 7 8 "github.com/go-chi/chi/v5" 9) 10 11type statusRecorder struct { 12 http.ResponseWriter 13 status int 14} 15 16func (r *statusRecorder) WriteHeader(status int) { 17 r.status = status 18 r.ResponseWriter.WriteHeader(status) 19} 20 21func Middleware(next http.Handler) http.Handler { 22 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 23 rec := &statusRecorder{ResponseWriter: w, status: http.StatusOK} 24 start := time.Now() 25 26 next.ServeHTTP(rec, r) 27 28 // use the matched route pattern to avoid high cardinality 29 routePattern := chi.RouteContext(r.Context()).RoutePattern() 30 if routePattern == "" { 31 routePattern = "unknown" 32 } 33 34 status := fmt.Sprintf("%d", rec.status) 35 duration := time.Since(start).Seconds() 36 37 HttpRequestsTotal.WithLabelValues(r.Method, routePattern, status).Inc() 38 HttpRequestDuration.WithLabelValues(r.Method, routePattern, status).Observe(duration) 39 }) 40}