Skip to content

Commit

Permalink
Check RawPath before checking Path on Prometheus middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
d6o committed Oct 18, 2023
1 parent 1b66cfb commit abcb163
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion echoprometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,15 @@ func (conf MiddlewareConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
if url == "" {
// as of Echo v4.10.1 path is empty for 404 cases (when router did not find any matching routes)
// in this case we use actual path from request to have some distinction in Prometheus
url = c.Request().URL.Path

// Referencing Go documentation (https://cs.opensource.google/go/go/+/refs/tags/go1.21.3:src/net/url/url.go;l=357-359):
// We first check the RawPath, which is the original, escaped form. It's important to check RawPath first because
// it preserves the original encoding of the URL. Using Path (decoded form) can sometimes result invalid UTF-8 characters.
if c.Request().URL.RawPath != "" {
url = c.Request().URL.RawPath
} else {
url = c.Request().URL.Path
}
}

status := c.Response().Status
Expand Down

0 comments on commit abcb163

Please sign in to comment.