Skip to content

Commit

Permalink
Merge pull request #18 from toshi0607/feature/change-status-code-str-…
Browse files Browse the repository at this point in the history
…to-int

Feature/change status code str to int
  • Loading branch information
toshi0607 authored Aug 15, 2022
2 parents 6017788 + 83abc4f commit 3285dd1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func (m Middleware) Handler(next http.Handler) http.Handler {

rp := chi.RouteContext(r.Context()).RoutePattern()
since := float64(time.Since(start).Milliseconds())
m.requests.WithLabelValues(http.StatusText(ww.Status()), r.Method, rp).Inc()
m.latency.WithLabelValues(http.StatusText(ww.Status()), r.Method, rp).Observe(since)
m.requests.WithLabelValues(strconv.Itoa(ww.Status()), r.Method, rp).Inc()
m.latency.WithLabelValues(strconv.Itoa(ww.Status()), r.Method, rp).Observe(since)
}
return http.HandlerFunc(fn)
}
Expand Down
25 changes: 16 additions & 9 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
const testHost = "http://localhost"

func TestMiddleware_MustRegisterDefault(t *testing.T) {
t.Parallel()

t.Run("without collectors", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
Expand Down Expand Up @@ -48,6 +50,8 @@ func TestMiddleware_MustRegisterDefault(t *testing.T) {
}

func TestMiddleware_Collectors(t *testing.T) {
t.Parallel()

m := chiprometheus.New("test")
want := 2
got := len(m.Collectors())
Expand All @@ -56,11 +60,11 @@ func TestMiddleware_Collectors(t *testing.T) {
}
}

func testHandler(t *testing.T) http.HandlerFunc {
func testHandler(t *testing.T, code int) http.HandlerFunc {
t.Helper()
return func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Duration(rand.Intn(5)) * time.Millisecond)
w.WriteHeader(http.StatusOK)
w.WriteHeader(code)
}
}

Expand Down Expand Up @@ -88,10 +92,11 @@ func TestMiddleware_Handler(t *testing.T) {
}{
"request header": {chiprometheus.RequestsCollectorName, true},
"latency header": {chiprometheus.LatencyCollectorName, true},
"path variable": {`chi_request_duration_milliseconds_count{code="OK",method="GET",path="/users/{firstName}",service="test"} 2`, true},
"path variable": {`chi_request_duration_milliseconds_count{code="200",method="GET",path="/users/{firstName}",service="test"} 2`, true},
"404": {`chi_requests_total{code="404",method="GET",path="/healthz",service="test"} 1`, true},
// specific path values should be omitted
"bob": {`chi_request_duration_milliseconds_count{code="OK",method="GET",path="/users/bob",service="test"} 1`, false},
"alice": {`chi_request_duration_milliseconds_count{code="OK",method="GET",path="/users/alice",service="test"} 1`, false},
"bob": {`chi_request_duration_milliseconds_count{code="200",method="GET",path="/users/bob",service="test"} 1`, false},
"alice": {`chi_request_duration_milliseconds_count{code="200",method="GET",path="/users/alice",service="test"} 1`, false},
}

r := chi.NewRouter()
Expand All @@ -104,8 +109,8 @@ func TestMiddleware_Handler(t *testing.T) {
})
r.Use(m.Handler)
r.Handle("/metrics", promhttp.Handler())
r.Get("/healthz", testHandler(t))
r.Get("/users/{firstName}", testHandler(t))
r.Get("/healthz", testHandler(t, http.StatusNotFound))
r.Get("/users/{firstName}", testHandler(t, http.StatusOK))
paths := [][]string{
{"healthz"},
{"users", "bob"},
Expand All @@ -128,6 +133,8 @@ func TestMiddleware_Handler(t *testing.T) {
}

func TestMiddleware_HandlerWithCustomRegistry(t *testing.T) {
t.Parallel()

tests := map[string]struct {
want string
}{
Expand Down Expand Up @@ -157,7 +164,7 @@ func TestMiddleware_HandlerWithCustomRegistry(t *testing.T) {
)
r.Use(m.Handler)
r.Handle("/metrics", promh)
r.Get("/healthz", testHandler(t))
r.Get("/healthz", testHandler(t, http.StatusOK))
paths := [][]string{
{"healthz"},
{"metrics"},
Expand Down Expand Up @@ -226,7 +233,7 @@ func TestMiddleware_HandlerWithBucketEnv(t *testing.T) {
})
r.Use(m.Handler)
r.Handle("/metrics", promhttp.Handler())
r.Get("/healthz", testHandler(t))
r.Get("/healthz", testHandler(t, http.StatusOK))
paths := [][]string{
{"healthz"},
{"metrics"},
Expand Down

0 comments on commit 3285dd1

Please sign in to comment.