Skip to content

✨Expose RegisterClientMetrics to register other clien… #2936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions pkg/metrics/client_go_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pkg/metrics/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
// <temp-dir>/k8s-metrics-server/serving-certs.
//
Expand Down Expand Up @@ -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,
Expand Down