Skip to content

Commit abcb163

Browse files
committed
Check RawPath before checking Path on Prometheus middleware
1 parent 1b66cfb commit abcb163

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

echoprometheus/prometheus.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,15 @@ func (conf MiddlewareConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
249249
if url == "" {
250250
// as of Echo v4.10.1 path is empty for 404 cases (when router did not find any matching routes)
251251
// in this case we use actual path from request to have some distinction in Prometheus
252-
url = c.Request().URL.Path
252+
253+
// Referencing Go documentation (https://cs.opensource.google/go/go/+/refs/tags/go1.21.3:src/net/url/url.go;l=357-359):
254+
// We first check the RawPath, which is the original, escaped form. It's important to check RawPath first because
255+
// it preserves the original encoding of the URL. Using Path (decoded form) can sometimes result invalid UTF-8 characters.
256+
if c.Request().URL.RawPath != "" {
257+
url = c.Request().URL.RawPath
258+
} else {
259+
url = c.Request().URL.Path
260+
}
253261
}
254262

255263
status := c.Response().Status

0 commit comments

Comments
 (0)