Skip to content

Commit 3f4096a

Browse files
authored
Fix cache metrics path (#89)
* Fix cache metrics path * test
1 parent 0a658d6 commit 3f4096a

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

pkg/zrouter/zmiddlewares/cache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ func tryServeFromCache(w http.ResponseWriter, r *http.Request, cache zcache.ZCac
7171
w.Header().Set(domain.ContentTypeHeader, domain.ContentTypeApplicationJSON)
7272
_, _ = w.Write(cachedResponse)
7373

74-
if err = metricServer.IncrementMetric(cacheHitsMetric, r.URL.Path); err != nil {
74+
if err = metricServer.IncrementMetric(cacheHitsMetric, GetRoutePattern(r)); err != nil {
7575
logger.GetLoggerFromContext(r.Context()).Errorf("Error incrementing cache_hits metric: %v", err)
7676
}
7777

7878
return true
7979
}
8080

81-
if err = metricServer.IncrementMetric(cacheMissesMetric, r.URL.Path); err != nil {
81+
if err = metricServer.IncrementMetric(cacheMissesMetric, GetRoutePattern(r)); err != nil {
8282
logger.GetLoggerFromContext(r.Context()).Errorf("Error incrementing cache_misses metric: %v", err)
8383
}
8484

@@ -96,7 +96,7 @@ func cacheResponseIfNeeded(rw *responseWriter, r *http.Request, cache zcache.ZCa
9696
return
9797
}
9898

99-
if err := metricServer.IncrementMetric(cacheSetsMetric, r.URL.Path); err != nil {
99+
if err := metricServer.IncrementMetric(cacheSetsMetric, GetRoutePattern(r)); err != nil {
100100
logger.GetLoggerFromContext(r.Context()).Errorf("Error incrementing cache_sets metric: %v", err)
101101
}
102102
}

pkg/zrouter/zmiddlewares/common.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package zmiddlewares
22

33
import (
4+
"github.com/go-chi/chi/v5"
5+
"net/http"
46
"regexp"
57
"strings"
68
)
@@ -13,3 +15,7 @@ func PathToRegexp(path string) *regexp.Regexp {
1315
pattern := regexp.MustCompile(`\{[^}]*\}`).ReplaceAllString(escapedPath, "[^/]+")
1416
return regexp.MustCompile("^" + pattern + "$")
1517
}
18+
19+
func GetRoutePattern(r *http.Request) string {
20+
return chi.RouteContext(r.Context()).RoutePattern()
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package zmiddlewares
2+
3+
import (
4+
"github.com/go-chi/chi/v5"
5+
"github.com/stretchr/testify/assert"
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
9+
)
10+
11+
func TestGetRoutePattern(t *testing.T) {
12+
r := chi.NewRouter()
13+
14+
routePattern := "/test/{param}"
15+
r.Get(routePattern, func(w http.ResponseWriter, r *http.Request) {
16+
routePattern := GetRoutePattern(r)
17+
18+
assert.Equal(t, routePattern, "/test/{param}", "The returned route pattern should match the one configured in the router.")
19+
})
20+
21+
req := httptest.NewRequest("GET", "/test/123", nil)
22+
w := httptest.NewRecorder()
23+
24+
r.ServeHTTP(w, req)
25+
26+
assert.Equal(t, http.StatusOK, w.Code, "The expected status code should be 200 OK.")
27+
}

pkg/zrouter/zmiddlewares/metrics.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package zmiddlewares
22

33
import (
4-
"github.com/go-chi/chi/v5"
54
"github.com/zondax/golem/pkg/logger"
65
"github.com/zondax/golem/pkg/metrics"
76
"github.com/zondax/golem/pkg/metrics/collectors"
@@ -51,7 +50,7 @@ func RequestMetrics(metricsServer metrics.TaskMetrics) Middleware {
5150

5251
return func(next http.Handler) http.Handler {
5352
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
54-
path := chi.RouteContext(r.Context()).RoutePattern()
53+
path := GetRoutePattern(r)
5554
startTime := time.Now()
5655

5756
mu.Lock()

0 commit comments

Comments
 (0)