2017-08-23 18:46:03 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
2018-11-14 09:18:03 +00:00
|
|
|
"context"
|
2017-08-23 18:46:03 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/containous/traefik/log"
|
|
|
|
"github.com/containous/traefik/safe"
|
|
|
|
"github.com/containous/traefik/types"
|
|
|
|
kitlog "github.com/go-kit/kit/log"
|
|
|
|
"github.com/go-kit/kit/metrics/statsd"
|
|
|
|
)
|
|
|
|
|
|
|
|
var statsdClient = statsd.New("traefik.", kitlog.LoggerFunc(func(keyvals ...interface{}) error {
|
2018-11-14 09:18:03 +00:00
|
|
|
log.WithoutContext().WithField(log.MetricsProviderName, "statsd").Info(keyvals)
|
2017-08-23 18:46:03 +00:00
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
|
|
|
|
var statsdTicker *time.Ticker
|
|
|
|
|
2017-11-08 14:14:03 +00:00
|
|
|
const (
|
2018-02-21 09:04:03 +00:00
|
|
|
statsdMetricsBackendReqsName = "backend.request.total"
|
|
|
|
statsdMetricsBackendLatencyName = "backend.request.duration"
|
|
|
|
statsdRetriesTotalName = "backend.retries.total"
|
|
|
|
statsdConfigReloadsName = "config.reload.total"
|
|
|
|
statsdConfigReloadsFailureName = statsdConfigReloadsName + ".failure"
|
|
|
|
statsdLastConfigReloadSuccessName = "config.reload.lastSuccessTimestamp"
|
|
|
|
statsdLastConfigReloadFailureName = "config.reload.lastFailureTimestamp"
|
|
|
|
statsdEntrypointReqsName = "entrypoint.request.total"
|
|
|
|
statsdEntrypointReqDurationName = "entrypoint.request.duration"
|
|
|
|
statsdEntrypointOpenConnsName = "entrypoint.connections.open"
|
|
|
|
statsdOpenConnsName = "backend.connections.open"
|
|
|
|
statsdServerUpName = "backend.server.up"
|
2017-11-08 14:14:03 +00:00
|
|
|
)
|
|
|
|
|
2017-08-23 18:46:03 +00:00
|
|
|
// RegisterStatsd registers the metrics pusher if this didn't happen yet and creates a statsd Registry instance.
|
2018-11-14 09:18:03 +00:00
|
|
|
func RegisterStatsd(ctx context.Context, config *types.Statsd) Registry {
|
2017-08-23 18:46:03 +00:00
|
|
|
if statsdTicker == nil {
|
2018-11-14 09:18:03 +00:00
|
|
|
statsdTicker = initStatsdTicker(ctx, config)
|
2017-08-23 18:46:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &standardRegistry{
|
2018-02-21 09:04:03 +00:00
|
|
|
enabled: true,
|
|
|
|
configReloadsCounter: statsdClient.NewCounter(statsdConfigReloadsName, 1.0),
|
|
|
|
configReloadsFailureCounter: statsdClient.NewCounter(statsdConfigReloadsFailureName, 1.0),
|
|
|
|
lastConfigReloadSuccessGauge: statsdClient.NewGauge(statsdLastConfigReloadSuccessName),
|
|
|
|
lastConfigReloadFailureGauge: statsdClient.NewGauge(statsdLastConfigReloadFailureName),
|
|
|
|
entrypointReqsCounter: statsdClient.NewCounter(statsdEntrypointReqsName, 1.0),
|
|
|
|
entrypointReqDurationHistogram: statsdClient.NewTiming(statsdEntrypointReqDurationName, 1.0),
|
|
|
|
entrypointOpenConnsGauge: statsdClient.NewGauge(statsdEntrypointOpenConnsName),
|
|
|
|
backendReqsCounter: statsdClient.NewCounter(statsdMetricsBackendReqsName, 1.0),
|
|
|
|
backendReqDurationHistogram: statsdClient.NewTiming(statsdMetricsBackendLatencyName, 1.0),
|
|
|
|
backendRetriesCounter: statsdClient.NewCounter(statsdRetriesTotalName, 1.0),
|
|
|
|
backendOpenConnsGauge: statsdClient.NewGauge(statsdOpenConnsName),
|
|
|
|
backendServerUpGauge: statsdClient.NewGauge(statsdServerUpName),
|
2017-08-23 18:46:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// initStatsdTicker initializes metrics pusher and creates a statsdClient if not created already
|
2018-11-14 09:18:03 +00:00
|
|
|
func initStatsdTicker(ctx context.Context, config *types.Statsd) *time.Ticker {
|
2017-08-23 18:46:03 +00:00
|
|
|
address := config.Address
|
|
|
|
if len(address) == 0 {
|
|
|
|
address = "localhost:8125"
|
|
|
|
}
|
|
|
|
pushInterval, err := time.ParseDuration(config.PushInterval)
|
|
|
|
if err != nil {
|
2018-11-14 09:18:03 +00:00
|
|
|
log.FromContext(ctx).Warnf("Unable to parse %s from config.PushInterval: using 10s as the default value", config.PushInterval)
|
2017-08-23 18:46:03 +00:00
|
|
|
pushInterval = 10 * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
report := time.NewTicker(pushInterval)
|
|
|
|
|
|
|
|
safe.Go(func() {
|
|
|
|
statsdClient.SendLoop(report.C, "udp", address)
|
|
|
|
})
|
|
|
|
|
|
|
|
return report
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopStatsd stops internal statsdTicker which controls the pushing of metrics to StatsD Agent and resets it to `nil`
|
|
|
|
func StopStatsd() {
|
|
|
|
if statsdTicker != nil {
|
|
|
|
statsdTicker.Stop()
|
|
|
|
}
|
|
|
|
statsdTicker = nil
|
|
|
|
}
|