Ensure that the Datadog client is cleanly stopped
This commit is contained in:
parent
65a317010b
commit
9d61cb64a2
1 changed files with 16 additions and 14 deletions
|
@ -12,8 +12,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
datadogClient *dogstatsd.Dogstatsd
|
datadogClient *dogstatsd.Dogstatsd
|
||||||
datadogTicker *time.Ticker
|
datadogLoopCancelFunc context.CancelFunc
|
||||||
)
|
)
|
||||||
|
|
||||||
// Metric names consistent with https://github.com/DataDog/integrations-extras/pull/64
|
// Metric names consistent with https://github.com/DataDog/integrations-extras/pull/64
|
||||||
|
@ -44,6 +44,9 @@ const (
|
||||||
|
|
||||||
// RegisterDatadog registers the metrics pusher if this didn't happen yet and creates a datadog Registry instance.
|
// RegisterDatadog registers the metrics pusher if this didn't happen yet and creates a datadog Registry instance.
|
||||||
func RegisterDatadog(ctx context.Context, config *types.Datadog) Registry {
|
func RegisterDatadog(ctx context.Context, config *types.Datadog) Registry {
|
||||||
|
// Ensures there is only one DataDog client sending metrics at any given time.
|
||||||
|
StopDatadog()
|
||||||
|
|
||||||
// just to be sure there is a prefix defined
|
// just to be sure there is a prefix defined
|
||||||
if config.Prefix == "" {
|
if config.Prefix == "" {
|
||||||
config.Prefix = defaultMetricsPrefix
|
config.Prefix = defaultMetricsPrefix
|
||||||
|
@ -54,9 +57,7 @@ func RegisterDatadog(ctx context.Context, config *types.Datadog) Registry {
|
||||||
return nil
|
return nil
|
||||||
}))
|
}))
|
||||||
|
|
||||||
if datadogTicker == nil {
|
initDatadogClient(ctx, config)
|
||||||
datadogTicker = initDatadogClient(ctx, config)
|
|
||||||
}
|
|
||||||
|
|
||||||
registry := &standardRegistry{
|
registry := &standardRegistry{
|
||||||
configReloadsCounter: datadogClient.NewCounter(ddConfigReloadsName, 1.0),
|
configReloadsCounter: datadogClient.NewCounter(ddConfigReloadsName, 1.0),
|
||||||
|
@ -95,25 +96,26 @@ func RegisterDatadog(ctx context.Context, config *types.Datadog) Registry {
|
||||||
return registry
|
return registry
|
||||||
}
|
}
|
||||||
|
|
||||||
func initDatadogClient(ctx context.Context, config *types.Datadog) *time.Ticker {
|
func initDatadogClient(ctx context.Context, config *types.Datadog) {
|
||||||
address := config.Address
|
address := config.Address
|
||||||
if len(address) == 0 {
|
if len(address) == 0 {
|
||||||
address = "localhost:8125"
|
address = "localhost:8125"
|
||||||
}
|
}
|
||||||
|
|
||||||
report := time.NewTicker(time.Duration(config.PushInterval))
|
ctx, datadogLoopCancelFunc = context.WithCancel(ctx)
|
||||||
|
|
||||||
safe.Go(func() {
|
safe.Go(func() {
|
||||||
datadogClient.SendLoop(ctx, report.C, "udp", address)
|
ticker := time.NewTicker(time.Duration(config.PushInterval))
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
datadogClient.SendLoop(ctx, ticker.C, "udp", address)
|
||||||
})
|
})
|
||||||
|
|
||||||
return report
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// StopDatadog stops internal datadogTicker which controls the pushing of metrics to DD Agent and resets it to `nil`.
|
// StopDatadog stops the Datadog metrics pusher.
|
||||||
func StopDatadog() {
|
func StopDatadog() {
|
||||||
if datadogTicker != nil {
|
if datadogLoopCancelFunc != nil {
|
||||||
datadogTicker.Stop()
|
datadogLoopCancelFunc()
|
||||||
|
datadogLoopCancelFunc = nil
|
||||||
}
|
}
|
||||||
datadogTicker = nil
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue