From fa1090b6eb46568bd5609c0d01a3a372b2e25554 Mon Sep 17 00:00:00 2001 From: Alberto Date: Tue, 17 Jan 2017 18:14:13 +0100 Subject: [PATCH] Improving instrumentation. (#1042) --- configuration.go | 2 +- docs/toml.md | 6 +++++- middlewares/metrics.go | 2 +- middlewares/prometheus.go | 14 +++++++------- traefik.sample.toml | 5 +++++ 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/configuration.go b/configuration.go index 47410121d..7ae5d71ee 100644 --- a/configuration.go +++ b/configuration.go @@ -331,7 +331,7 @@ func NewTraefikDefaultPointersConfiguration() *TraefikConfiguration { // default Metrics defaultWeb.Metrics = &types.Metrics{ Prometheus: &types.Prometheus{ - Buckets: types.Buckets{100, 300, 1200, 5000}, + Buckets: types.Buckets{0.1, 0.3, 1.2, 5}, }, } diff --git a/docs/toml.md b/docs/toml.md index 48ee7a9ce..521c4f422 100644 --- a/docs/toml.md +++ b/docs/toml.md @@ -544,6 +544,10 @@ address = ":8080" # [web.statistics] # RecentErrors = 10 # +# To enable Traefik to export internal metrics to Prometheus +# [web.metrics.prometheus] +# Buckets=[0.1,0.3,1.2,5] +# # To enable basic auth on the webui # with 2 user/pass: test:test and test2:test2 # Passwords can be encoded in MD5, SHA1 and BCrypt: you can use htpasswd to generate those ones @@ -718,7 +722,7 @@ $ curl -s "http://localhost:8080/api" | jq . - `/metrics`: You can enable Traefik to export internal metrics to different monitoring systems (Only Prometheus is supported at the moment). ```bash -$ traefik --web.metrics.prometheus --web.metrics.prometheus.buckets="100,300" +$ traefik --web.metrics.prometheus --web.metrics.prometheus.buckets="0.1,0.3,1.2,5" ``` ## Docker backend diff --git a/middlewares/metrics.go b/middlewares/metrics.go index 422774a28..08c4a6350 100644 --- a/middlewares/metrics.go +++ b/middlewares/metrics.go @@ -37,7 +37,7 @@ func (m *MetricsWrapper) ServeHTTP(rw http.ResponseWriter, r *http.Request, next next(prw, r) labels := []string{"code", strconv.Itoa(prw.StatusCode()), "method", r.Method} m.Impl.getReqsCounter().With(labels...).Add(1) - m.Impl.getLatencyHistogram().With(labels...).Observe(float64(time.Since(start).Nanoseconds()) / 1000000) + m.Impl.getLatencyHistogram().Observe(float64(time.Since(start).Seconds())) } func (rw *responseRecorder) StatusCode() int { diff --git a/middlewares/prometheus.go b/middlewares/prometheus.go index 661e54918..366b96727 100644 --- a/middlewares/prometheus.go +++ b/middlewares/prometheus.go @@ -10,12 +10,12 @@ import ( ) const ( - reqsName = "requests_total" - latencyName = "request_duration_milliseconds" + reqsName = "traefik_requests_total" + latencyName = "traefik_request_duration_seconds" ) -// Prometheus is an Implementation for Metrics that exposes prometheus metrics for the number of requests, -// the latency and the response size, partitioned by status code and method. +// Prometheus is an Implementation for Metrics that exposes prometheus metrics for the latency +// and the number of requests partitioned by status code and method. type Prometheus struct { reqsCounter metrics.Counter latencyHistogram metrics.Histogram @@ -45,17 +45,17 @@ func NewPrometheus(name string, config *types.Prometheus) *Prometheus { if config.Buckets != nil { buckets = config.Buckets } else { - buckets = []float64{100, 300, 1200, 5000} + buckets = []float64{0.1, 0.3, 1.2, 5} } m.latencyHistogram = prometheus.NewHistogramFrom( stdprometheus.HistogramOpts{ Name: latencyName, - Help: "How long it took to process the request, partitioned by status code and method.", + Help: "How long it took to process the request.", ConstLabels: stdprometheus.Labels{"service": name}, Buckets: buckets, }, - []string{"code", "method"}, + []string{}, ) return &m } diff --git a/traefik.sample.toml b/traefik.sample.toml index f2c02da73..30c1c8c2b 100644 --- a/traefik.sample.toml +++ b/traefik.sample.toml @@ -326,6 +326,11 @@ # Enable more detailed statistics # [web.statistics] # RecentErrors = 10 +# +# To enable Traefik to export internal metrics to Prometheus +# [web.metrics.prometheus] +# Buckets=[0.1,0.3,1.2,5] +# # To enable basic auth on the webui # with 2 user/pass: test:test and test2:test2