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/go-kit/kit/metrics/statsd"
|
2024-07-19 12:52:04 +00:00
|
|
|
kitlog "github.com/go-kit/log"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/safe"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/types"
|
2017-08-23 18:46:03 +00:00
|
|
|
)
|
|
|
|
|
2020-07-07 12:42:03 +00:00
|
|
|
var (
|
|
|
|
statsdClient *statsd.Statsd
|
|
|
|
statsdTicker *time.Ticker
|
|
|
|
)
|
2017-08-23 18:46:03 +00:00
|
|
|
|
2017-11-08 14:14:03 +00:00
|
|
|
const (
|
2021-04-30 08:22:04 +00:00
|
|
|
statsdConfigReloadsName = "config.reload.total"
|
|
|
|
statsdConfigReloadsFailureName = statsdConfigReloadsName + ".failure"
|
|
|
|
statsdLastConfigReloadSuccessName = "config.reload.lastSuccessTimestamp"
|
|
|
|
statsdLastConfigReloadFailureName = "config.reload.lastFailureTimestamp"
|
|
|
|
|
2020-12-18 17:44:03 +00:00
|
|
|
statsdTLSCertsNotAfterTimestampName = "tls.certs.notAfterTimestamp"
|
2021-04-30 08:22:04 +00:00
|
|
|
|
|
|
|
statsdEntryPointReqsName = "entrypoint.request.total"
|
|
|
|
statsdEntryPointReqsTLSName = "entrypoint.request.tls.total"
|
|
|
|
statsdEntryPointReqDurationName = "entrypoint.request.duration"
|
|
|
|
statsdEntryPointOpenConnsName = "entrypoint.connections.open"
|
2022-09-12 15:10:09 +00:00
|
|
|
statsdEntryPointReqsBytesName = "entrypoint.requests.bytes.total"
|
|
|
|
statsdEntryPointRespsBytesName = "entrypoint.responses.bytes.total"
|
2021-04-30 08:22:04 +00:00
|
|
|
|
|
|
|
statsdRouterReqsName = "router.request.total"
|
|
|
|
statsdRouterReqsTLSName = "router.request.tls.total"
|
|
|
|
statsdRouterReqsDurationName = "router.request.duration"
|
|
|
|
statsdRouterOpenConnsName = "router.connections.open"
|
2022-09-12 15:10:09 +00:00
|
|
|
statsdRouterReqsBytesName = "router.requests.bytes.total"
|
|
|
|
statsdRouterRespsBytesName = "router.responses.bytes.total"
|
2021-04-30 08:22:04 +00:00
|
|
|
|
|
|
|
statsdServiceReqsName = "service.request.total"
|
|
|
|
statsdServiceReqsTLSName = "service.request.tls.total"
|
|
|
|
statsdServiceReqsDurationName = "service.request.duration"
|
|
|
|
statsdServiceRetriesTotalName = "service.retries.total"
|
|
|
|
statsdServiceServerUpName = "service.server.up"
|
|
|
|
statsdServiceOpenConnsName = "service.connections.open"
|
2022-09-12 15:10:09 +00:00
|
|
|
statsdServiceReqsBytesName = "service.requests.bytes.total"
|
|
|
|
statsdServiceRespsBytesName = "service.responses.bytes.total"
|
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 {
|
2019-11-12 17:18:04 +00:00
|
|
|
// just to be sure there is a prefix defined
|
|
|
|
if config.Prefix == "" {
|
2021-10-06 15:34:07 +00:00
|
|
|
config.Prefix = defaultMetricsPrefix
|
2019-11-12 17:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
statsdClient = statsd.New(config.Prefix+".", kitlog.LoggerFunc(func(keyvals ...interface{}) error {
|
2022-07-19 16:38:09 +00:00
|
|
|
log.WithoutContext().WithField(log.MetricsProviderName, "statsd").Info(keyvals...)
|
2019-11-12 17:18:04 +00:00
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-18 19:36:05 +00:00
|
|
|
registry := &standardRegistry{
|
2020-12-18 17:44:03 +00:00
|
|
|
configReloadsCounter: statsdClient.NewCounter(statsdConfigReloadsName, 1.0),
|
|
|
|
configReloadsFailureCounter: statsdClient.NewCounter(statsdConfigReloadsFailureName, 1.0),
|
|
|
|
lastConfigReloadSuccessGauge: statsdClient.NewGauge(statsdLastConfigReloadSuccessName),
|
|
|
|
lastConfigReloadFailureGauge: statsdClient.NewGauge(statsdLastConfigReloadFailureName),
|
|
|
|
tlsCertsNotAfterTimestampGauge: statsdClient.NewGauge(statsdTLSCertsNotAfterTimestampName),
|
2017-08-23 18:46:03 +00:00
|
|
|
}
|
2019-07-18 19:36:05 +00:00
|
|
|
|
|
|
|
if config.AddEntryPointsLabels {
|
|
|
|
registry.epEnabled = config.AddEntryPointsLabels
|
2023-03-20 17:06:07 +00:00
|
|
|
registry.entryPointReqsCounter = NewCounterWithNoopHeaders(statsdClient.NewCounter(statsdEntryPointReqsName, 1.0))
|
2021-04-30 08:22:04 +00:00
|
|
|
registry.entryPointReqsTLSCounter = statsdClient.NewCounter(statsdEntryPointReqsTLSName, 1.0)
|
2020-03-05 14:10:07 +00:00
|
|
|
registry.entryPointReqDurationHistogram, _ = NewHistogramWithScale(statsdClient.NewTiming(statsdEntryPointReqDurationName, 1.0), time.Millisecond)
|
2019-07-18 19:36:05 +00:00
|
|
|
registry.entryPointOpenConnsGauge = statsdClient.NewGauge(statsdEntryPointOpenConnsName)
|
2022-09-12 15:10:09 +00:00
|
|
|
registry.entryPointReqsBytesCounter = statsdClient.NewCounter(statsdEntryPointReqsBytesName, 1.0)
|
|
|
|
registry.entryPointRespsBytesCounter = statsdClient.NewCounter(statsdEntryPointRespsBytesName, 1.0)
|
2019-07-18 19:36:05 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
if config.AddRoutersLabels {
|
|
|
|
registry.routerEnabled = config.AddRoutersLabels
|
2023-03-20 17:06:07 +00:00
|
|
|
registry.routerReqsCounter = NewCounterWithNoopHeaders(statsdClient.NewCounter(statsdRouterReqsName, 1.0))
|
2021-04-30 08:22:04 +00:00
|
|
|
registry.routerReqsTLSCounter = statsdClient.NewCounter(statsdRouterReqsTLSName, 1.0)
|
|
|
|
registry.routerReqDurationHistogram, _ = NewHistogramWithScale(statsdClient.NewTiming(statsdRouterReqsDurationName, 1.0), time.Millisecond)
|
|
|
|
registry.routerOpenConnsGauge = statsdClient.NewGauge(statsdRouterOpenConnsName)
|
2022-09-12 15:10:09 +00:00
|
|
|
registry.routerReqsBytesCounter = statsdClient.NewCounter(statsdRouterReqsBytesName, 1.0)
|
|
|
|
registry.routerRespsBytesCounter = statsdClient.NewCounter(statsdRouterRespsBytesName, 1.0)
|
2021-04-30 08:22:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 19:36:05 +00:00
|
|
|
if config.AddServicesLabels {
|
|
|
|
registry.svcEnabled = config.AddServicesLabels
|
2023-03-20 17:06:07 +00:00
|
|
|
registry.serviceReqsCounter = NewCounterWithNoopHeaders(statsdClient.NewCounter(statsdServiceReqsName, 1.0))
|
2021-04-30 08:22:04 +00:00
|
|
|
registry.serviceReqsTLSCounter = statsdClient.NewCounter(statsdServiceReqsTLSName, 1.0)
|
|
|
|
registry.serviceReqDurationHistogram, _ = NewHistogramWithScale(statsdClient.NewTiming(statsdServiceReqsDurationName, 1.0), time.Millisecond)
|
|
|
|
registry.serviceRetriesCounter = statsdClient.NewCounter(statsdServiceRetriesTotalName, 1.0)
|
|
|
|
registry.serviceOpenConnsGauge = statsdClient.NewGauge(statsdServiceOpenConnsName)
|
|
|
|
registry.serviceServerUpGauge = statsdClient.NewGauge(statsdServiceServerUpName)
|
2022-09-12 15:10:09 +00:00
|
|
|
registry.serviceReqsBytesCounter = statsdClient.NewCounter(statsdServiceReqsBytesName, 1.0)
|
|
|
|
registry.serviceRespsBytesCounter = statsdClient.NewCounter(statsdServiceRespsBytesName, 1.0)
|
2019-07-18 19:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return registry
|
2017-08-23 18:46:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +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"
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
report := time.NewTicker(time.Duration(config.PushInterval))
|
2017-08-23 18:46:03 +00:00
|
|
|
|
|
|
|
safe.Go(func() {
|
2019-07-18 19:36:05 +00:00
|
|
|
statsdClient.SendLoop(ctx, report.C, "udp", address)
|
2017-08-23 18:46:03 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return report
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// StopStatsd stops internal statsdTicker which controls the pushing of metrics to StatsD Agent and resets it to `nil`.
|
2017-08-23 18:46:03 +00:00
|
|
|
func StopStatsd() {
|
|
|
|
if statsdTicker != nil {
|
|
|
|
statsdTicker.Stop()
|
|
|
|
}
|
|
|
|
statsdTicker = nil
|
|
|
|
}
|