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: Strip regexes from routes before sending to prometheus #3459

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 8 additions & 4 deletions services/horizon/internal/httpx/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"net/http"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -129,12 +130,15 @@ func getClientData(r *http.Request, headerName string) string {
return value
}

var routeRegexp = regexp.MustCompile("{(([^:}]*)?:[^}]*|().*)}")

func logEndOfRequest(ctx context.Context, r *http.Request, requestDurationSummary *prometheus.SummaryVec, duration time.Duration, mw middleware.WrapResponseWriter, streaming bool) {
routePattern := chi.RouteContext(r.Context()).RoutePattern()
route := routeRegexp.ReplaceAllString(routePattern, "$2")
// Can be empty when request did not reached the final route (ex. blocked by
// a middleware). More info: https://github.com/go-chi/chi/issues/270
if routePattern == "" {
routePattern = "undefined"
if route == "" {
route = "undefined"
}

referer := r.Referer()
Expand All @@ -155,15 +159,15 @@ func logEndOfRequest(ctx context.Context, r *http.Request, requestDurationSummar
"ip_port": r.RemoteAddr,
"method": r.Method,
"path": r.URL.String(),
"route": routePattern,
"route": route,
"status": mw.Status(),
"streaming": streaming,
"referer": referer,
}).Info("Finished request")

requestDurationSummary.With(prometheus.Labels{
"status": strconv.FormatInt(int64(mw.Status()), 10),
"route": routePattern,
"route": route,
"streaming": strconv.FormatBool(streaming),
"method": r.Method,
}).Observe(float64(duration.Seconds()))
Expand Down
37 changes: 37 additions & 0 deletions services/horizon/internal/httpx/middleware_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package httpx

import (
"testing"
)

func TestMiddlewareSanitizesRoutesForPrometheus(t *testing.T) {
for _, setup := range []struct {
name string
route string
expected string
}{
{
"normal routes are unaffected",
"/accounts",
"/accounts",
},
{
"named regexes are replaced with their name",
"/accounts/{account_id:\\w+}/effects",
"/accounts/account_id/effects",
paulbellamy marked this conversation as resolved.
Show resolved Hide resolved
},
{
"unnamed regexes are removed",
"/accounts/{\\w+}/effects",
paulbellamy marked this conversation as resolved.
Show resolved Hide resolved
"/accounts//effects",
},
} {
t.Run(setup.name, func(t *testing.T) {
result := routeRegexp.ReplaceAllString(setup.route, "$2")
if result != setup.expected {
t.Errorf("Expected %q to be sanitized to %q, but got %q", setup.route, setup.expected, result)
}
})
}

}