refactor(metrics): remove external library

pull/33307/head
TheFox0x7 1 month ago
parent eddef36435
commit 0b1e3254dd
  1. 2
      go.mod
  2. 11
      routers/common/middleware.go
  3. 30
      routers/common/middleware_test.go

@ -46,7 +46,6 @@ require (
github.com/emirpasic/gods v1.18.1 github.com/emirpasic/gods v1.18.1
github.com/ethantkoenig/rupture v1.0.1 github.com/ethantkoenig/rupture v1.0.1
github.com/felixge/fgprof v0.9.5 github.com/felixge/fgprof v0.9.5
github.com/felixge/httpsnoop v1.0.4
github.com/fsnotify/fsnotify v1.7.0 github.com/fsnotify/fsnotify v1.7.0
github.com/gliderlabs/ssh v0.3.8 github.com/gliderlabs/ssh v0.3.8
github.com/go-ap/activitypub v0.0.0-20240910141749-b4b8c8aa484c 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/dlclark/regexp2 v1.11.4 // indirect
github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6 // indirect github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6 // indirect
github.com/fatih/color v1.18.0 // 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/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/git-lfs/pktline v0.0.0-20230103162542-ca444d533ef1 // indirect github.com/git-lfs/pktline v0.0.0-20230103162542-ca444d533ef1 // indirect
github.com/go-ap/errors v0.0.0-20240910140019-1e9d33cc1568 // indirect github.com/go-ap/errors v0.0.0-20240910140019-1e9d33cc1568 // indirect

@ -8,6 +8,7 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"time"
"code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/gtprof" "code.gitea.io/gitea/modules/gtprof"
@ -19,7 +20,6 @@ import (
"gitea.com/go-chi/session" "gitea.com/go-chi/session"
"github.com/chi-middleware/proxy" "github.com/chi-middleware/proxy"
"github.com/felixge/httpsnoop"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto" "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 := reqInflightGauge.WithLabelValues(req.Method)
inflight.Inc() inflight.Inc()
defer inflight.Dec() defer inflight.Dec()
start := time.Now()
m := httpsnoop.CaptureMetrics(next, resp, req)
next.ServeHTTP(resp, req) next.ServeHTTP(resp, req)
m := context.WrapResponseWriter(resp)
route := chi.RouteContext(req.Context()).RoutePattern() route := chi.RouteContext(req.Context()).RoutePattern()
code := strconv.Itoa(m.Code) code := strconv.Itoa(m.WrittenStatus())
reqDurationHistogram.WithLabelValues(req.Method, code, route).Observe(m.Duration.Seconds()) reqDurationHistogram.WithLabelValues(req.Method, code, route).Observe(time.Since(start).Seconds())
respSizeHistogram.WithLabelValues(req.Method, code, route).Observe(float64(m.Written)) respSizeHistogram.WithLabelValues(req.Method, code, route).Observe(float64(m.Size()))
size := req.ContentLength size := req.ContentLength
if size < 0 { if size < 0 {
size = 0 size = 0

@ -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)
}
Loading…
Cancel
Save