2017-08-23 21:46:03 +03:00
package metrics
import (
2018-11-14 12:18:03 +03:00
"context"
2017-08-23 21:46:03 +03:00
"time"
kitlog "github.com/go-kit/kit/log"
"github.com/go-kit/kit/metrics/dogstatsd"
2020-09-16 16:46:04 +03: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 21:46:03 +03:00
)
var datadogClient = dogstatsd . New ( "traefik." , kitlog . LoggerFunc ( func ( keyvals ... interface { } ) error {
2018-11-14 12:18:03 +03:00
log . WithoutContext ( ) . WithField ( log . MetricsProviderName , "datadog" ) . Info ( keyvals )
2017-08-23 21:46:03 +03:00
return nil
} ) )
var datadogTicker * time . Ticker
// Metric names consistent with https://github.com/DataDog/integrations-extras/pull/64
const (
2019-07-18 22:36:05 +03:00
ddMetricsServiceReqsName = "service.request.total"
ddMetricsServiceLatencyName = "service.request.duration"
ddRetriesTotalName = "service.retries.total"
2018-02-21 12:04:03 +03:00
ddConfigReloadsName = "config.reload.total"
ddConfigReloadsFailureTagName = "failure"
ddLastConfigReloadSuccessName = "config.reload.lastSuccessTimestamp"
ddLastConfigReloadFailureName = "config.reload.lastFailureTimestamp"
2019-07-18 22:36:05 +03:00
ddEntryPointReqsName = "entrypoint.request.total"
ddEntryPointReqDurationName = "entrypoint.request.duration"
ddEntryPointOpenConnsName = "entrypoint.connections.open"
ddOpenConnsName = "service.connections.open"
ddServerUpName = "service.server.up"
2017-08-23 21:46:03 +03:00
)
// RegisterDatadog registers the metrics pusher if this didn't happen yet and creates a datadog Registry instance.
2019-09-02 13:18:04 +03:00
func RegisterDatadog ( ctx context . Context , config * types . Datadog ) Registry {
2017-08-23 21:46:03 +03:00
if datadogTicker == nil {
2018-11-14 12:18:03 +03:00
datadogTicker = initDatadogClient ( ctx , config )
2017-08-23 21:46:03 +03:00
}
registry := & standardRegistry {
2019-07-18 22:36:05 +03:00
configReloadsCounter : datadogClient . NewCounter ( ddConfigReloadsName , 1.0 ) ,
configReloadsFailureCounter : datadogClient . NewCounter ( ddConfigReloadsName , 1.0 ) . With ( ddConfigReloadsFailureTagName , "true" ) ,
lastConfigReloadSuccessGauge : datadogClient . NewGauge ( ddLastConfigReloadSuccessName ) ,
lastConfigReloadFailureGauge : datadogClient . NewGauge ( ddLastConfigReloadFailureName ) ,
}
if config . AddEntryPointsLabels {
registry . epEnabled = config . AddEntryPointsLabels
registry . entryPointReqsCounter = datadogClient . NewCounter ( ddEntryPointReqsName , 1.0 )
2020-03-05 17:10:07 +03:00
registry . entryPointReqDurationHistogram , _ = NewHistogramWithScale ( datadogClient . NewHistogram ( ddEntryPointReqDurationName , 1.0 ) , time . Second )
2019-07-18 22:36:05 +03:00
registry . entryPointOpenConnsGauge = datadogClient . NewGauge ( ddEntryPointOpenConnsName )
}
if config . AddServicesLabels {
registry . svcEnabled = config . AddServicesLabels
registry . serviceReqsCounter = datadogClient . NewCounter ( ddMetricsServiceReqsName , 1.0 )
2020-03-05 17:10:07 +03:00
registry . serviceReqDurationHistogram , _ = NewHistogramWithScale ( datadogClient . NewHistogram ( ddMetricsServiceLatencyName , 1.0 ) , time . Second )
2019-07-18 22:36:05 +03:00
registry . serviceRetriesCounter = datadogClient . NewCounter ( ddRetriesTotalName , 1.0 )
registry . serviceOpenConnsGauge = datadogClient . NewGauge ( ddOpenConnsName )
registry . serviceServerUpGauge = datadogClient . NewGauge ( ddServerUpName )
2017-08-23 21:46:03 +03:00
}
return registry
}
2019-09-02 13:18:04 +03:00
func initDatadogClient ( ctx context . Context , config * types . Datadog ) * time . Ticker {
2017-08-23 21:46:03 +03:00
address := config . Address
if len ( address ) == 0 {
address = "localhost:8125"
}
2019-06-17 12:48:05 +03:00
report := time . NewTicker ( time . Duration ( config . PushInterval ) )
2017-08-23 21:46:03 +03:00
safe . Go ( func ( ) {
2019-07-18 22:36:05 +03:00
datadogClient . SendLoop ( ctx , report . C , "udp" , address )
2017-08-23 21:46:03 +03:00
} )
return report
}
// StopDatadog stops internal datadogTicker which controls the pushing of metrics to DD Agent and resets it to `nil`.
func StopDatadog ( ) {
if datadogTicker != nil {
datadogTicker . Stop ( )
}
datadogTicker = nil
}