Skip to content

Commit 2f86823

Browse files
committed
refactor(metrics): remove external library
1 parent 28615e2 commit 2f86823

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ require (
4646
github.com/emirpasic/gods v1.18.1
4747
github.com/ethantkoenig/rupture v1.0.1
4848
github.com/felixge/fgprof v0.9.5
49-
github.com/felixge/httpsnoop v1.0.4
5049
github.com/fsnotify/fsnotify v1.7.0
5150
github.com/gliderlabs/ssh v0.3.8
5251
github.com/go-ap/activitypub v0.0.0-20240910141749-b4b8c8aa484c
@@ -195,6 +194,7 @@ require (
195194
github.com/dlclark/regexp2 v1.11.4 // indirect
196195
github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6 // indirect
197196
github.com/fatih/color v1.18.0 // indirect
197+
github.com/felixge/httpsnoop v1.0.4 // indirect
198198
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
199199
github.com/git-lfs/pktline v0.0.0-20230103162542-ca444d533ef1 // indirect
200200
github.com/go-ap/errors v0.0.0-20240910140019-1e9d33cc1568 // indirect

routers/common/middleware.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"strconv"
1010
"strings"
11+
"time"
1112

1213
"code.gitea.io/gitea/modules/cache"
1314
"code.gitea.io/gitea/modules/httplib"
@@ -18,7 +19,6 @@ import (
1819

1920
"gitea.com/go-chi/session"
2021
"github.com/chi-middleware/proxy"
21-
"github.com/felixge/httpsnoop"
2222
"github.com/go-chi/chi/v5"
2323
"github.com/prometheus/client_golang/prometheus"
2424
"github.com/prometheus/client_golang/prometheus/promauto"
@@ -145,13 +145,14 @@ func RouteMetrics() func(h http.Handler) http.Handler {
145145
inflight := reqInflightGauge.WithLabelValues(req.Method)
146146
inflight.Inc()
147147
defer inflight.Dec()
148+
start := time.Now()
148149

149-
m := httpsnoop.CaptureMetrics(next, resp, req)
150150
next.ServeHTTP(resp, req)
151+
m := context.WrapResponseWriter(resp)
151152
route := chi.RouteContext(req.Context()).RoutePattern()
152-
code := strconv.Itoa(m.Code)
153-
reqDurationHistogram.WithLabelValues(req.Method, code, route).Observe(m.Duration.Seconds())
154-
respSizeHistogram.WithLabelValues(req.Method, code, route).Observe(float64(m.Written))
153+
code := strconv.Itoa(m.WrittenStatus())
154+
reqDurationHistogram.WithLabelValues(req.Method, code, route).Observe(time.Since(start).Seconds())
155+
respSizeHistogram.WithLabelValues(req.Method, code, route).Observe(float64(m.Size()))
155156
size := req.ContentLength
156157
if size < 0 {
157158
size = 0

routers/common/middleware_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package common_test
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
"time"
8+
9+
"code.gitea.io/gitea/routers/common"
10+
11+
"github.com/go-chi/chi/v5"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestMetricsMiddlewere(t *testing.T) {
16+
17+
middleware := common.RouteMetrics()
18+
r := chi.NewRouter()
19+
r.Use(middleware)
20+
r.Get("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
21+
w.Write([]byte("test"))
22+
time.Sleep(5 * time.Millisecond)
23+
}))
24+
25+
testServer := httptest.NewServer(r)
26+
27+
_, err := http.Get(testServer.URL)
28+
require.NoError(t, err)
29+
30+
}

0 commit comments

Comments
 (0)