Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

services/horizon: Fix route value for rate limited requests #3658

Merged
merged 5 commits into from
Jun 7, 2021
Merged
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
26 changes: 25 additions & 1 deletion services/horizon/internal/httpx/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,32 @@ func sanitizeMetricRoute(routePattern string) string {
return route
}

// Author: https://github.com/rliebz
// From: https://github.com/go-chi/chi/issues/270#issuecomment-479184559
// https://github.com/go-chi/chi/blob/master/LICENSE
func getRoutePattern(r *http.Request) string {
rctx := chi.RouteContext(r.Context())
if pattern := rctx.RoutePattern(); pattern != "" {
// Pattern is already available
return pattern
}

routePath := r.URL.Path
if r.URL.RawPath != "" {
routePath = r.URL.RawPath
}

tctx := chi.NewRouteContext()
if !rctx.Routes.Match(tctx, r.Method, routePath) {
return ""
}

// tctx has the updated pattern, since Match mutates it
return tctx.RoutePattern()
}

func logEndOfRequest(ctx context.Context, r *http.Request, requestDurationSummary *prometheus.SummaryVec, duration time.Duration, mw middleware.WrapResponseWriter, streaming bool) {
route := sanitizeMetricRoute(chi.RouteContext(r.Context()).RoutePattern())
route := sanitizeMetricRoute(getRoutePattern(r))

referer := r.Referer()
if referer == "" {
Expand Down