diff --git a/example/main.go b/example/main.go index 11d7c97..beb1b3c 100644 --- a/example/main.go +++ b/example/main.go @@ -6,21 +6,17 @@ import ( "net/http" "time" - "github.com/go-chi/chi" - - "github.com/766b/chi-prometheus" - "github.com/prometheus/client_golang/prometheus" + chiprometheus "github.com/766b/chi-prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" ) func main() { n := chi.NewRouter() m := chiprometheus.NewMiddleware("serviceName") - // if you want to use other buckets than the default (300, 1200, 5000) you can run: - // m := negroniprometheus.NewMiddleware("serviceName", 400, 1600, 700) n.Use(m) - n.Handle("/metrics", prometheus.Handler()) + n.Handle("/metrics", promhttp.Handler()) n.Get("/ok", func(w http.ResponseWriter, r *http.Request) { sleep := rand.Intn(4999) + 1 time.Sleep(time.Duration(sleep) * time.Millisecond) diff --git a/middleware.go b/middleware.go index a40159c..8f384d1 100644 --- a/middleware.go +++ b/middleware.go @@ -1,4 +1,3 @@ -// Port https://github.com/zbindenren/negroni-prometheus for chi router package chiprometheus import ( @@ -6,8 +5,8 @@ import ( "strings" "time" - "github.com/go-chi/chi" - "github.com/go-chi/chi/middleware" + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" "github.com/prometheus/client_golang/prometheus" ) @@ -104,6 +103,10 @@ func (c Middleware) patternHandler(next http.Handler) http.Handler { next.ServeHTTP(ww, r) rctx := chi.RouteContext(r.Context()) + if rctx == nil { + // avoid nil panic + return + } routePattern := strings.Join(rctx.RoutePatterns, "") routePattern = strings.Replace(routePattern, "/*/", "/", -1) diff --git a/middleware_test.go b/middleware_test.go index 3d45bde..b77f42b 100644 --- a/middleware_test.go +++ b/middleware_test.go @@ -7,8 +7,8 @@ import ( "strings" "testing" - "github.com/go-chi/chi" - "github.com/prometheus/client_golang/prometheus" + "github.com/go-chi/chi/v5" + "github.com/prometheus/client_golang/prometheus/promhttp" ) func Test_Logger(t *testing.T) { @@ -18,7 +18,7 @@ func Test_Logger(t *testing.T) { m := NewMiddleware("test") n.Use(m) - n.Handle("/metrics", prometheus.Handler()) + n.Handle("/metrics", promhttp.Handler()) n.Get(`/ok`, func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) fmt.Fprintln(w, "ok") @@ -80,7 +80,7 @@ func Test_PatternLogger(t *testing.T) { m := NewPatternMiddleware("patternOnlyTest") n.Use(m) - n.Handle("/metrics", prometheus.Handler()) + n.Handle("/metrics", promhttp.Handler()) n.Get(`/ok`, func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) fmt.Fprintln(w, "ok") @@ -151,7 +151,7 @@ func Test_MultipleLoggers(t *testing.T) { n.Use(mid) n.Use(m) - n.Handle("/metrics", prometheus.Handler()) + n.Handle("/metrics", promhttp.Handler()) n.Get(`/ok`, func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) fmt.Fprintln(w, "ok") diff --git a/pattern_example/main.go b/pattern_example/main.go index 1322979..7f4af39 100644 --- a/pattern_example/main.go +++ b/pattern_example/main.go @@ -6,9 +6,8 @@ import ( "net/http" "time" - "github.com/edjumacator/chi-prometheus" - "github.com/go-chi/chi" - "github.com/prometheus/client_golang/prometheus" + chiprometheus "github.com/766b/chi-prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" ) func main() { @@ -17,7 +16,7 @@ func main() { n.Use(m) - n.Handle("/metrics", prometheus.Handler()) + n.Handle("/metrics", promhttp.Handler()) n.Get("/ok", func(w http.ResponseWriter, r *http.Request) { sleep := rand.Intn(4999) + 1 time.Sleep(time.Duration(sleep) * time.Millisecond)