Label requests for a subrouter's own path unmatched in metrics
check / check (push) Waiting to run

For /v1 and /v1/, the path the image subrouter is mounted at, chi's
Find answers that path as the pattern without asking the subrouter,
which chi hands the request as / and which has no route there, so the
router answers 404. routePattern now asks the subrouter mounted there
whether it has a route for /, and labels the request "unmatched" when
it has none.

Model: opus-5-5
This commit is contained in:
2026-10-15 10:26:04 +00:00
parent 0ff6fdf00f
commit c9e2a22cca
+12
View File
@@ -8,6 +8,7 @@ import (
"net/http" "net/http"
"net/netip" "net/netip"
"regexp" "regexp"
"strings"
"time" "time"
basicauth "github.com/99designs/basicauth-go" 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) 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 == "" { if pattern == "" {