Repository: traefik/traefik
File: pkg/metrics/datadog.go (Go)

Code before:
		registry.routerReqsCounter = datadogClient.NewCounter(ddMetricsRouterReqsName, 1.0)
		registry.routerReqsTLSCounter = datadogClient.NewCounter(ddMetricsRouterReqsTLSName, 1.0)
		registry.routerReqDurationHistogram, _ = NewHistogramWithScale(datadogClient.NewHistogram(ddMetricsRouterReqsDurationName, 1.0), time.Second)
		registry.routerOpenConnsGauge = datadogClient.NewGauge(ddRouterOpenConnsName)
	}

	if config.AddServicesLabels {
		registry.svcEnabled = config.AddServicesLabels
		registry.serviceReqsCounter = datadogClient.NewCounter(ddMetricsServiceReqsName, 1.0)
		registry.serviceReqsTLSCounter = datadogClient.NewCounter(ddMetricsServiceReqsTLSName, 1.0)
		registry.serviceReqDurationHistogram, _ = NewHistogramWithScale(datadogClient.NewHistogram(ddMetricsServiceReqsDurationName, 1.0), time.Second)
		registry.serviceRetriesCounter = datadogClient.NewCounter(ddRetriesTotalName, 1.0)
		registry.serviceOpenConnsGauge = datadogClient.NewGauge(ddOpenConnsName)
		registry.serviceServerUpGauge = datadogClient.NewGauge(ddServerUpName)
	}

	return registry
}

func initDatadogClient(ctx context.Context, config *types.Datadog) {
	address := config.Address
	if len(address) == 0 {
		address = "localhost:8125"
	}

	report := time.NewTicker(time.Duration(config.PushInterval))

	safe.Go(func() {
		defer report.Stop()
		datadogClient.SendLoop(ctx, report.C, "udp", address)
	})
}

// StopDatadog stops internal datadogTicker which controls the pushing of metrics to DD Agent and resets it to `nil`.
func StopDatadog() {
	if datadogTickerCancelFunc != nil {
		datadogTickerCancelFunc()
		datadogTickerCancelFunc = nil
	}
}


Review comment (suggestion):
```suggestion

	ctx, datadogTickerCancelFunc = context.WithCancel(ctx)
	report := time.NewTicker(time.Duration(config.PushInterval))
```

Code after:
		registry.routerReqsCounter = datadogClient.NewCounter(ddMetricsRouterReqsName, 1.0)
		registry.routerReqsTLSCounter = datadogClient.NewCounter(ddMetricsRouterReqsTLSName, 1.0)
		registry.routerReqDurationHistogram, _ = NewHistogramWithScale(datadogClient.NewHistogram(ddMetricsRouterReqsDurationName, 1.0), time.Second)
		registry.routerOpenConnsGauge = datadogClient.NewGauge(ddRouterOpenConnsName)
	}

	if config.AddServicesLabels {
		registry.svcEnabled = config.AddServicesLabels
		registry.serviceReqsCounter = datadogClient.NewCounter(ddMetricsServiceReqsName, 1.0)
		registry.serviceReqsTLSCounter = datadogClient.NewCounter(ddMetricsServiceReqsTLSName, 1.0)
		registry.serviceReqDurationHistogram, _ = NewHistogramWithScale(datadogClient.NewHistogram(ddMetricsServiceReqsDurationName, 1.0), time.Second)
		registry.serviceRetriesCounter = datadogClient.NewCounter(ddRetriesTotalName, 1.0)
		registry.serviceOpenConnsGauge = datadogClient.NewGauge(ddOpenConnsName)
		registry.serviceServerUpGauge = datadogClient.NewGauge(ddServerUpName)
	}

	return registry
}

func initDatadogClient(ctx context.Context, config *types.Datadog) {
	address := config.Address
	if len(address) == 0 {
		address = "localhost:8125"
	}

	ctx, datadogLoopCancelFunc = context.WithCancel(ctx)

	safe.Go(func() {
		ticker := time.NewTicker(time.Duration(config.PushInterval))
		defer ticker.Stop()

		datadogClient.SendLoop(ctx, ticker.C, "udp", address)
	})
}

// StopDatadog stops the Datadog metrics pusher.
func StopDatadog() {
	if datadogLoopCancelFunc != nil {
		datadogLoopCancelFunc()
		datadogLoopCancelFunc = nil
	}
}