diff --git a/go.mod b/go.mod index d4da8f6707b..69f5b2728d2 100644 --- a/go.mod +++ b/go.mod @@ -46,7 +46,6 @@ require ( github.com/emirpasic/gods v1.18.1 github.com/ethantkoenig/rupture v1.0.1 github.com/felixge/fgprof v0.9.5 - github.com/felixge/httpsnoop v1.0.4 github.com/fsnotify/fsnotify v1.7.0 github.com/gliderlabs/ssh v0.3.8 github.com/go-ap/activitypub v0.0.0-20240910141749-b4b8c8aa484c @@ -195,6 +194,7 @@ require ( github.com/dlclark/regexp2 v1.11.4 // indirect github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6 // indirect github.com/fatih/color v1.18.0 // indirect + github.com/felixge/httpsnoop v1.0.4 // indirect github.com/fxamacker/cbor/v2 v2.7.0 // indirect github.com/git-lfs/pktline v0.0.0-20230103162542-ca444d533ef1 // indirect github.com/go-ap/errors v0.0.0-20240910140019-1e9d33cc1568 // indirect diff --git a/routers/common/middleware.go b/routers/common/middleware.go index 7feadf7943d..30484583fd4 100644 --- a/routers/common/middleware.go +++ b/routers/common/middleware.go @@ -8,6 +8,7 @@ import ( "net/http" "strconv" "strings" + "time" "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/gtprof" @@ -19,7 +20,6 @@ import ( "gitea.com/go-chi/session" "github.com/chi-middleware/proxy" - "github.com/felixge/httpsnoop" "github.com/go-chi/chi/v5" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" @@ -158,13 +158,14 @@ func RouteMetrics() func(h http.Handler) http.Handler { inflight := reqInflightGauge.WithLabelValues(req.Method) inflight.Inc() defer inflight.Dec() + start := time.Now() - m := httpsnoop.CaptureMetrics(next, resp, req) next.ServeHTTP(resp, req) + m := context.WrapResponseWriter(resp) route := chi.RouteContext(req.Context()).RoutePattern() - code := strconv.Itoa(m.Code) - reqDurationHistogram.WithLabelValues(req.Method, code, route).Observe(m.Duration.Seconds()) - respSizeHistogram.WithLabelValues(req.Method, code, route).Observe(float64(m.Written)) + code := strconv.Itoa(m.WrittenStatus()) + reqDurationHistogram.WithLabelValues(req.Method, code, route).Observe(time.Since(start).Seconds()) + respSizeHistogram.WithLabelValues(req.Method, code, route).Observe(float64(m.Size())) size := req.ContentLength if size < 0 { size = 0 diff --git a/routers/common/middleware_test.go b/routers/common/middleware_test.go new file mode 100644 index 00000000000..03612faf223 --- /dev/null +++ b/routers/common/middleware_test.go @@ -0,0 +1,30 @@ +package common_test + +import ( + "net/http" + "net/http/httptest" + "testing" + "time" + + "code.gitea.io/gitea/routers/common" + + "github.com/go-chi/chi/v5" + "github.com/stretchr/testify/require" +) + +func TestMetricsMiddlewere(t *testing.T) { + + middleware := common.RouteMetrics() + r := chi.NewRouter() + r.Use(middleware) + r.Get("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("test")) + time.Sleep(5 * time.Millisecond) + })) + + testServer := httptest.NewServer(r) + + _, err := http.Get(testServer.URL) + require.NoError(t, err) + +}