@@ -61,15 +61,15 @@ func Prometheus(register prometheus.Registerer) func(http.Handler) http.Handler
61
61
var (
62
62
start = time .Now ()
63
63
method = r .Method
64
- rctx = chi .RouteContext (r .Context ())
64
+ // rctx = chi.RouteContext(r.Context())
65
65
)
66
66
67
67
sw , ok := w .(* tracing.StatusWriter )
68
68
if ! ok {
69
69
panic ("dev error: http.ResponseWriter is not *tracing.StatusWriter" )
70
70
}
71
71
72
- path := rctx . RoutePattern ( )
72
+ path := getRoutePattern ( r )
73
73
74
74
var (
75
75
dist * prometheus.HistogramVec
@@ -99,3 +99,26 @@ func Prometheus(register prometheus.Registerer) func(http.Handler) http.Handler
99
99
})
100
100
}
101
101
}
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