Label request metrics by route, not by path (closes #254) #260

Merged
clawbot merged 8 commits from issue-254-metrics-route-pattern into next 2026-10-15 16:37:21 +02:00
Showing only changes of commit cdfca2fa03 - Show all commits
+12
View File
@@ -8,6 +8,7 @@ import (
"net/http"
"net/netip"
"regexp"
"strings"
"time"
basicauth "github.com/99designs/basicauth-go"
@@ -250,6 +251,17 @@ func routePattern(r *http.Request) string {
}
pattern = rctx.Routes.Find(chi.NewRouteContext(), r.Method, path)
// For the path a subrouter is mounted at, such as /v1 or /v1/, Find
// answers that path without asking the subrouter, which chi hands
// the request as /.
for _, route := range rctx.Routes.Routes() {
if route.SubRoutes != nil &&
route.Pattern == strings.TrimSuffix(pattern, "/")+"/*" &&
!route.SubRoutes.Match(chi.NewRouteContext(), r.Method, "/") {
pattern = ""
}
}
}
if pattern == "" {