Skip to content

Commit 9ec5e6c

Browse files
authored
Move metrics from macaron to chi (#13601)
1 parent 75ebf7c commit 9ec5e6c

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

routers/metrics.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,29 @@ package routers
66

77
import (
88
"crypto/subtle"
9+
"net/http"
910

10-
"code.gitea.io/gitea/modules/context"
1111
"code.gitea.io/gitea/modules/setting"
1212

1313
"github.com/prometheus/client_golang/prometheus/promhttp"
1414
)
1515

1616
// Metrics validate auth token and render prometheus metrics
17-
func Metrics(ctx *context.Context) {
17+
func Metrics(resp http.ResponseWriter, req *http.Request) {
1818
if setting.Metrics.Token == "" {
19-
promhttp.Handler().ServeHTTP(ctx.Resp, ctx.Req.Request)
19+
promhttp.Handler().ServeHTTP(resp, req)
2020
return
2121
}
22-
header := ctx.Req.Header.Get("Authorization")
22+
header := req.Header.Get("Authorization")
2323
if header == "" {
24-
ctx.Error(401)
24+
http.Error(resp, "", 401)
2525
return
2626
}
2727
got := []byte(header)
2828
want := []byte("Bearer " + setting.Metrics.Token)
2929
if subtle.ConstantTimeCompare(got, want) != 1 {
30-
ctx.Error(401)
30+
http.Error(resp, "", 401)
3131
return
3232
}
33-
promhttp.Handler().ServeHTTP(ctx.Resp, ctx.Req.Request)
33+
promhttp.Handler().ServeHTTP(resp, req)
3434
}

routers/routes/chi.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ import (
1717
"time"
1818

1919
"code.gitea.io/gitea/modules/log"
20+
"code.gitea.io/gitea/modules/metrics"
2021
"code.gitea.io/gitea/modules/public"
2122
"code.gitea.io/gitea/modules/setting"
2223
"code.gitea.io/gitea/modules/storage"
24+
"code.gitea.io/gitea/routers"
2325

2426
"github.com/go-chi/chi"
2527
"github.com/go-chi/chi/middleware"
28+
"github.com/prometheus/client_golang/prometheus"
2629
)
2730

2831
type routerLoggerOptions struct {
@@ -251,6 +254,18 @@ func NormalRoutes() http.Handler {
251254
})
252255
}
253256

257+
r.Get("/apple-touch-icon.png", func(w http.ResponseWriter, req *http.Request) {
258+
http.Redirect(w, req, path.Join(setting.StaticURLPrefix, "img/apple-touch-icon.png"), 301)
259+
})
260+
261+
// prometheus metrics endpoint
262+
if setting.Metrics.Enabled {
263+
c := metrics.NewCollector()
264+
prometheus.MustRegister(c)
265+
266+
r.Get("/metrics", routers.Metrics)
267+
}
268+
254269
return r
255270
}
256271

routers/routes/macaron.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ package routes
66

77
import (
88
"encoding/gob"
9-
"path"
109

1110
"code.gitea.io/gitea/models"
1211
"code.gitea.io/gitea/modules/auth"
1312
"code.gitea.io/gitea/modules/context"
1413
"code.gitea.io/gitea/modules/lfs"
1514
"code.gitea.io/gitea/modules/log"
16-
"code.gitea.io/gitea/modules/metrics"
1715
"code.gitea.io/gitea/modules/options"
1816
"code.gitea.io/gitea/modules/setting"
1917
"code.gitea.io/gitea/modules/templates"
@@ -43,7 +41,6 @@ import (
4341
"gitea.com/macaron/macaron"
4442
"gitea.com/macaron/session"
4543
"gitea.com/macaron/toolbox"
46-
"github.com/prometheus/client_golang/prometheus"
4744
"github.com/tstranex/u2f"
4845
)
4946

@@ -978,23 +975,11 @@ func RegisterMacaronRoutes(m *macaron.Macaron) {
978975
private.RegisterRoutes(m)
979976
})
980977

981-
m.Get("/apple-touch-icon.png", func(ctx *context.Context) {
982-
ctx.Redirect(path.Join(setting.StaticURLPrefix, "img/apple-touch-icon.png"), 301)
983-
})
984-
985978
// Progressive Web App
986979
m.Get("/manifest.json", templates.JSONRenderer(), func(ctx *context.Context) {
987980
ctx.HTML(200, "pwa/manifest_json")
988981
})
989982

990-
// prometheus metrics endpoint
991-
if setting.Metrics.Enabled {
992-
c := metrics.NewCollector()
993-
prometheus.MustRegister(c)
994-
995-
m.Get("/metrics", routers.Metrics)
996-
}
997-
998983
// Not found handler.
999984
m.NotFound(routers.NotFound)
1000985
}

0 commit comments

Comments
 (0)