diff --git a/pkg/metrics/client_go_adapter.go b/pkg/metrics/client_go_adapter.go index ff28998c44..c6e95f29b9 100644 --- a/pkg/metrics/client_go_adapter.go +++ b/pkg/metrics/client_go_adapter.go @@ -40,18 +40,21 @@ var ( ) func init() { - registerClientMetrics() -} - -// registerClientMetrics sets up the client latency metrics from client-go. -func registerClientMetrics() { // register the metrics with our registry Registry.MustRegister(requestResult) +} +// RegisterClientMetrics sets up metrics from client-go. +// Notice that clientmetrics.Register can only be called once, so only the RegisterOpts from the first call to this +// function will take effect. +func RegisterClientMetrics(opts clientmetrics.RegisterOpts) { + if opts.RequestResult == nil { + opts.RequestResult = &resultAdapter{ + metric: requestResult, + } + } // register the metrics with client-go - clientmetrics.Register(clientmetrics.RegisterOpts{ - RequestResult: &resultAdapter{metric: requestResult}, - }) + clientmetrics.Register(opts) } // this section contains adapters, implementations, and other sundry organic, artisanally diff --git a/pkg/metrics/server/server.go b/pkg/metrics/server/server.go index 5eb0c62a72..5914c8d275 100644 --- a/pkg/metrics/server/server.go +++ b/pkg/metrics/server/server.go @@ -30,6 +30,7 @@ import ( "github.com/go-logr/logr" "github.com/prometheus/client_golang/prometheus/promhttp" "k8s.io/client-go/rest" + clientmetrics "k8s.io/client-go/tools/metrics" certutil "k8s.io/client-go/util/cert" "sigs.k8s.io/controller-runtime/pkg/certwatcher" @@ -82,6 +83,9 @@ type Options struct { // endpoint by setting this field to filters.WithAuthenticationAndAuthorization. FilterProvider func(c *rest.Config, httpClient *http.Client) (Filter, error) + // ClientGoMetricsRegisterOpts contains the options used to register client-go metrics. + ClientGoMetricsRegisterOpts clientmetrics.RegisterOpts + // CertDir is the directory that contains the server key and certificate. Defaults to // /k8s-metrics-server/serving-certs. // @@ -138,6 +142,8 @@ func NewServer(o Options, config *rest.Config, httpClient *http.Client) (Server, } } + metrics.RegisterClientMetrics(o.ClientGoMetricsRegisterOpts) + return &defaultServer{ metricsFilter: metricsFilter, options: o,