Skip to content

Commit

Permalink
fix(metrics): remove query string from collapsed path segment (#1159)
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdimas authored Apr 15, 2024
1 parent 817943a commit 15ee438
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
4 changes: 3 additions & 1 deletion metrics/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ func (m *Middleware) getFirstPathSegment(requestURI string) string {
// Will split /my/example/uri in []string{"", "my", "example/uri"}
uriSegments := strings.SplitN(requestURI, "/", 3)
if len(uriSegments) > 1 {
return "/" + uriSegments[1]
// Remove any query string from the segment
// For example /my?query=string should return /my
return "/" + strings.SplitN(uriSegments[1], "?", 2)[0]
}
return "/"

Expand Down
20 changes: 13 additions & 7 deletions metrics/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ var (
`
metricsNotCollapsed string = metricMetadata + rootMetric + `
ory_oathkeeper_requests_total{method="GET",request="/hello/world",service="test",status_code="200"} 1
ory_oathkeeper_requests_total{method="GET",request="/hello/world?foo=bar",service="test",status_code="200"} 1
ory_oathkeeper_requests_total{method="GET",request="/hello?foo=bar",service="test",status_code="200"} 1
`
metricsCollapsed string = metricMetadata + rootMetric + `
ory_oathkeeper_requests_total{method="GET",request="/hello",service="test",status_code="200"} 1
ory_oathkeeper_requests_total{method="GET",request="/hello",service="test",status_code="200"} 3
`
metricsHidden string = metricMetadata + `
ory_oathkeeper_requests_total{method="GET",request="",service="test",status_code="200"} 2
ory_oathkeeper_requests_total{method="GET",request="",service="test",status_code="200"} 4
`
serverContextPaths []string = []string{"/", "/hello/world"}

serverConfigPaths []string = []string{"/", "/hello", "/hello/world"}
serverRequestPaths []string = []string{"/", "/hello?foo=bar", "/hello/world", "/hello/world?foo=bar"}

configurableMetricMetadata string = `
# HELP http_requests_total Total number of requests
Expand All @@ -49,10 +53,12 @@ var (
http_requests_total{method="GET",request="/",service="test",status_code="200"} 1
`
configurableMetricsNotCollapsed string = configurableMetricMetadata + configurableRootMetric + `
http_requests_total{method="GET",request="/hello?foo=bar",service="test",status_code="200"} 1
http_requests_total{method="GET",request="/hello/world",service="test",status_code="200"} 1
http_requests_total{method="GET",request="/hello/world?foo=bar",service="test",status_code="200"} 1
`
configurableMetricsCollapsed string = configurableMetricMetadata + configurableRootMetric + `
http_requests_total{method="GET",request="/hello",service="test",status_code="200"} 1
http_requests_total{method="GET",request="/hello",service="test",status_code="200"} 3
`
)

Expand All @@ -73,7 +79,7 @@ func PrometheusTestApp(middleware *Middleware) http.Handler {

r := httprouter.New()

for _, path := range serverContextPaths {
for _, path := range serverConfigPaths {
r.GET(path, func(res http.ResponseWriter, req *http.Request, p httprouter.Params) {
fmt.Fprint(res, "OK")
})
Expand Down Expand Up @@ -108,7 +114,7 @@ func TestPrometheusRequestTotalMetrics(t *testing.T) {
ts := httptest.NewServer(PrometheusTestApp(promMiddleware))
defer ts.Close()

for _, path := range serverContextPaths {
for _, path := range serverRequestPaths {
req, err := http.NewRequest("GET", ts.URL+path, nil)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -156,7 +162,7 @@ serve:
ts := httptest.NewServer(PrometheusTestApp(promMiddleware))
defer ts.Close()

for _, path := range serverContextPaths {
for _, path := range serverRequestPaths {
req, err := http.NewRequest("GET", ts.URL+path, nil)
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 15ee438

Please sign in to comment.