From eaea7226eaa8305bc0b1841b92808c37343a4ce3 Mon Sep 17 00:00:00 2001 From: Patrick Christopher Date: Wed, 26 Sep 2018 15:10:30 -0700 Subject: [PATCH] fix tests, add module files --- go.mod | 16 ++++++++++++++++ go.sum | 24 ++++++++++++++++++++++++ middleware.go | 9 ++------- middleware_test.go | 8 ++++---- 4 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..60a5b45 --- /dev/null +++ b/go.mod @@ -0,0 +1,16 @@ +module github.com/Remitly/chi-prometheus + +require ( + github.com/766b/chi-prometheus v0.0.0-20180509160047-46ac2b31aa30 + github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 // indirect + github.com/go-chi/chi v3.3.3+incompatible + github.com/golang/protobuf v1.2.0 // indirect + github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect + github.com/prometheus/client_golang v0.8.0 + github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 // indirect + github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e // indirect + github.com/prometheus/procfs v0.0.0-20180920065004-418d78d0b9a7 // indirect + golang.org/x/net v0.0.0-20180926154720-4dfa2610cdf3 // indirect + golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect + golang.org/x/text v0.3.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..672dc42 --- /dev/null +++ b/go.sum @@ -0,0 +1,24 @@ +github.com/766b/chi-prometheus v0.0.0-20180509160047-46ac2b31aa30 h1:bNHbCMKiQxpRNe4Pk2W09N1aXXc4ICOawQFKIDEicqc= +github.com/766b/chi-prometheus v0.0.0-20180509160047-46ac2b31aa30/go.mod h1:X/LhbmoBoRu8TxoGIOIraVNhfz3hhikJoaelrOuhdPY= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/go-chi/chi v3.3.3+incompatible h1:KHkmBEMNkwKuK4FdQL7N2wOeB9jnIx7jR5wsuSBEFI8= +github.com/go-chi/chi v3.3.3+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= +github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/prometheus/client_golang v0.8.0 h1:1921Yw9Gc3iSc4VQh3PIoOqgPCZS7G/4xQNVUp8Mda8= +github.com/prometheus/client_golang v0.8.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 h1:idejC8f05m9MGOsuEi1ATq9shN03HrxNkD/luQvxCv8= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e h1:n/3MEhJQjQxrOUCzh1Y3Re6aJUUWRp2M9+Oc3eVn/54= +github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= +github.com/prometheus/procfs v0.0.0-20180920065004-418d78d0b9a7 h1:NgR6WN8nQ4SmFC1sSUHY8SriLuWCZ6cCIQtH4vDZN3c= +github.com/prometheus/procfs v0.0.0-20180920065004-418d78d0b9a7/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +golang.org/x/net v0.0.0-20180926154720-4dfa2610cdf3 h1:dgd4x4kJt7G4k4m93AYLzM8Ni6h2qLTfh9n9vXJT3/0= +golang.org/x/net v0.0.0-20180926154720-4dfa2610cdf3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/middleware.go b/middleware.go index 442e57a..570c5e7 100644 --- a/middleware.go +++ b/middleware.go @@ -13,11 +13,6 @@ var ( dflBuckets = []float64{300, 1200, 5000} ) -const ( - reqsName = "chi_requests_total" - latencyName = "chi_request_duration_milliseconds" -) - // Middleware is a handler that exposes prometheus metrics for the number of requests, // the latency and the response size, partitioned by status code, method and HTTP path. type Middleware struct { @@ -30,7 +25,7 @@ func NewMiddleware(name string, buckets ...float64) func(next http.Handler) http var m Middleware m.reqs = prometheus.NewCounterVec( prometheus.CounterOpts{ - Name: reqsName, + Name: name + "_requests_total", Help: "How many HTTP requests processed, partitioned by status code, method and HTTP path.", ConstLabels: prometheus.Labels{"service": name}, }, @@ -42,7 +37,7 @@ func NewMiddleware(name string, buckets ...float64) func(next http.Handler) http buckets = dflBuckets } m.latency = prometheus.NewHistogramVec(prometheus.HistogramOpts{ - Name: latencyName, + Name: name + "_duration_milliseconds", Help: "How long it took to process the request, partitioned by status code, method and HTTP path.", ConstLabels: prometheus.Labels{"service": name}, Buckets: buckets, diff --git a/middleware_test.go b/middleware_test.go index 40b4f66..d379137 100644 --- a/middleware_test.go +++ b/middleware_test.go @@ -36,10 +36,10 @@ func Test_Logger(t *testing.T) { n.ServeHTTP(recorder, req1) n.ServeHTTP(recorder, req2) body := recorder.Body.String() - if !strings.Contains(body, reqsName) { - t.Errorf("body does not contain request total entry '%s'", reqsName) + if !strings.Contains(body, "test_requests_total") { + t.Errorf("body does not contain request total entry '%s'", "test_requests_total") } - if !strings.Contains(body, latencyName) { - t.Errorf("body does not contain request duration entry '%s'", reqsName) + if !strings.Contains(body, "test_duration_milliseconds") { + t.Errorf("body does not contain request duration entry '%s'", "test_duration_milliseconds") } }