discovery(k8s): Only register client-go metrics adapters when needed

Previously the metrics adapters for client-go were registered in an init function.
This resulted in clobbering default metrics providers when these packages are imported
into an application that leverages the default client-go metrics registry.

Instead, let's only register these adapters when requested.

Signed-off-by: Stephen Heckler <sheckler@cloudflare.com>
This commit is contained in:
Stephen Heckler 2024-04-25 12:33:29 -05:00
parent 0305490e4e
commit 31a4217784
2 changed files with 8 additions and 10 deletions

View file

@ -19,16 +19,6 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
var (
clientGoRequestMetrics = &clientGoRequestMetricAdapter{}
clientGoWorkloadMetrics = &clientGoWorkqueueMetricsProvider{}
)
func init() {
clientGoRequestMetrics.RegisterWithK8sGoClient()
clientGoWorkloadMetrics.RegisterWithK8sGoClient()
}
// Metrics to be used with a discovery manager. // Metrics to be used with a discovery manager.
type Metrics struct { type Metrics struct {
FailedConfigs prometheus.Gauge FailedConfigs prometheus.Gauge

View file

@ -35,6 +35,11 @@ const (
workqueueMetricsNamespace = KubernetesMetricsNamespace + "_workqueue" workqueueMetricsNamespace = KubernetesMetricsNamespace + "_workqueue"
) )
var (
clientGoRequestMetrics = &clientGoRequestMetricAdapter{}
clientGoWorkloadMetrics = &clientGoWorkqueueMetricsProvider{}
)
var ( var (
// Metrics for client-go's HTTP requests. // Metrics for client-go's HTTP requests.
clientGoRequestResultMetricVec = prometheus.NewCounterVec( clientGoRequestResultMetricVec = prometheus.NewCounterVec(
@ -135,6 +140,9 @@ func clientGoMetrics() []prometheus.Collector {
} }
func RegisterK8sClientMetricsWithPrometheus(registerer prometheus.Registerer) error { func RegisterK8sClientMetricsWithPrometheus(registerer prometheus.Registerer) error {
clientGoRequestMetrics.RegisterWithK8sGoClient()
clientGoWorkloadMetrics.RegisterWithK8sGoClient()
for _, collector := range clientGoMetrics() { for _, collector := range clientGoMetrics() {
err := registerer.Register(collector) err := registerer.Register(collector)
if err != nil { if err != nil {