Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5ea90da

Browse files
committed
added path matching to prometheus middleware
1 parent77ea50b commit5ea90da

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

‎coderd/httpmw/prometheus.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ func Prometheus(register prometheus.Registerer) func(http.Handler) http.Handler
6161
var (
6262
start=time.Now()
6363
method=r.Method
64-
rctx=chi.RouteContext(r.Context())
64+
//rctx = chi.RouteContext(r.Context())
6565
)
6666

6767
sw,ok:=w.(*tracing.StatusWriter)
6868
if!ok {
6969
panic("dev error: http.ResponseWriter is not *tracing.StatusWriter")
7070
}
7171

72-
path:=rctx.RoutePattern()
72+
path:=getRoutePattern(r)
7373

7474
var (
7575
dist*prometheus.HistogramVec
@@ -99,3 +99,26 @@ func Prometheus(register prometheus.Registerer) func(http.Handler) http.Handler
9999
})
100100
}
101101
}
102+
103+
funcgetRoutePattern(r*http.Request)string {
104+
rctx:=chi.RouteContext(r.Context())
105+
ifpattern:=rctx.RoutePattern();pattern!="" {
106+
// Pattern is already available
107+
returnpattern
108+
}
109+
110+
routePath:=r.URL.Path
111+
ifr.URL.RawPath!="" {
112+
routePath=r.URL.RawPath
113+
}
114+
115+
tctx:=chi.NewRouteContext()
116+
if!rctx.Routes.Match(tctx,r.Method,routePath) {
117+
// No matching pattern, so just return an empty string.
118+
// It is done to avoid returning a static path for frontend requests.
119+
return""
120+
}
121+
122+
// tctx has the updated pattern, since Match mutates it
123+
returntctx.RoutePattern()
124+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp