Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 19 additions & 0 deletions echoprometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,22 @@ func unregisterDefaults(subsystem string) {
Help: "The HTTP request sizes in bytes.",
})
}

func TestToMiddleware_RawPath(t *testing.T) {
middleware, err := MiddlewareConfig{}.ToMiddleware()
assert.NoError(t, err)

next := echo.HandlerFunc(func(c echo.Context) error {
return nil
})

handler := middleware(next)

e := echo.New()

req := httptest.NewRequest(http.MethodGet, "/v1/%a1", http.NoBody)
rec := httptest.NewRecorder()
echoContext := e.NewContext(req, rec)

assert.NoError(t, handler(echoContext))
}