Improving instrumentation. (#1042)

This commit is contained in:
Alberto 2017-01-17 18:14:13 +01:00 committed by Vincent Demeester
parent 483ef486af
commit fa1090b6eb
5 changed files with 19 additions and 10 deletions

View file

@ -331,7 +331,7 @@ func NewTraefikDefaultPointersConfiguration() *TraefikConfiguration {
// default Metrics // default Metrics
defaultWeb.Metrics = &types.Metrics{ defaultWeb.Metrics = &types.Metrics{
Prometheus: &types.Prometheus{ Prometheus: &types.Prometheus{
Buckets: types.Buckets{100, 300, 1200, 5000}, Buckets: types.Buckets{0.1, 0.3, 1.2, 5},
}, },
} }

View file

@ -544,6 +544,10 @@ address = ":8080"
# [web.statistics] # [web.statistics]
# RecentErrors = 10 # 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 # To enable basic auth on the webui
# with 2 user/pass: test:test and test2:test2 # 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 # 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). - `/metrics`: You can enable Traefik to export internal metrics to different monitoring systems (Only Prometheus is supported at the moment).
```bash ```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 ## Docker backend

View file

@ -37,7 +37,7 @@ func (m *MetricsWrapper) ServeHTTP(rw http.ResponseWriter, r *http.Request, next
next(prw, r) next(prw, r)
labels := []string{"code", strconv.Itoa(prw.StatusCode()), "method", r.Method} labels := []string{"code", strconv.Itoa(prw.StatusCode()), "method", r.Method}
m.Impl.getReqsCounter().With(labels...).Add(1) 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 { func (rw *responseRecorder) StatusCode() int {

View file

@ -10,12 +10,12 @@ import (
) )
const ( const (
reqsName = "requests_total" reqsName = "traefik_requests_total"
latencyName = "request_duration_milliseconds" latencyName = "traefik_request_duration_seconds"
) )
// Prometheus is an Implementation for Metrics that exposes prometheus metrics for the number of requests, // Prometheus is an Implementation for Metrics that exposes prometheus metrics for the latency
// the latency and the response size, partitioned by status code and method. // and the number of requests partitioned by status code and method.
type Prometheus struct { type Prometheus struct {
reqsCounter metrics.Counter reqsCounter metrics.Counter
latencyHistogram metrics.Histogram latencyHistogram metrics.Histogram
@ -45,17 +45,17 @@ func NewPrometheus(name string, config *types.Prometheus) *Prometheus {
if config.Buckets != nil { if config.Buckets != nil {
buckets = config.Buckets buckets = config.Buckets
} else { } else {
buckets = []float64{100, 300, 1200, 5000} buckets = []float64{0.1, 0.3, 1.2, 5}
} }
m.latencyHistogram = prometheus.NewHistogramFrom( m.latencyHistogram = prometheus.NewHistogramFrom(
stdprometheus.HistogramOpts{ stdprometheus.HistogramOpts{
Name: latencyName, 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}, ConstLabels: stdprometheus.Labels{"service": name},
Buckets: buckets, Buckets: buckets,
}, },
[]string{"code", "method"}, []string{},
) )
return &m return &m
} }

View file

@ -326,6 +326,11 @@
# Enable more detailed statistics # Enable more detailed statistics
# [web.statistics] # [web.statistics]
# RecentErrors = 10 # 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 # To enable basic auth on the webui
# with 2 user/pass: test:test and test2:test2 # with 2 user/pass: test:test and test2:test2