Skip to content

Commit 5ea90da

Browse files
committed
added path matching to prometheus middleware
1 parent 77ea50b commit 5ea90da

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+
func getRoutePattern(r *http.Request) string {
104+
rctx := chi.RouteContext(r.Context())
105+
if pattern := rctx.RoutePattern(); pattern != "" {
106+
// Pattern is already available
107+
return pattern
108+
}
109+
110+
routePath := r.URL.Path
111+
if r.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+
return tctx.RoutePattern()
124+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy