Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 3 additions & 7 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions middleware.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Port https://github.com/zbindenren/negroni-prometheus for chi router
package chiprometheus

import (
"net/http"
"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"
)

Expand Down Expand Up @@ -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)

Expand Down
10 changes: 5 additions & 5 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
7 changes: 3 additions & 4 deletions pattern_example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
Expand Down