Skip to content

Commit

Permalink
fix: register k8s metrics
Browse files Browse the repository at this point in the history
controller-runtime also calls `metrics.Register()` during init and this
function can be called only once. To ensure that the k8s client metrics
get updated, the global variables need to be set again by the operator.

https://github.com/kubernetes-sigs/controller-runtime/blob/67b27f27e514bd9ac4cf9a2d84dec089ece95bf7/pkg/metrics/client_go_adapter.go#L42-L55
https://github.com/kubernetes/client-go/blob/aa7909e7d7c0661792ba21b9e882f3cd6ad0ce53/tools/metrics/metrics.go#L129-L170

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
  • Loading branch information
simonpasquier committed Apr 18, 2024
1 parent 6d917ce commit 349d97e
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/k8sutil/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ type clientGoHTTPMetricAdapter struct {
duration *prometheus.SummaryVec
}

var _ = metrics.LatencyMetric(&clientGoHTTPMetricAdapter{})
var _ = metrics.ResultMetric(&clientGoHTTPMetricAdapter{})

type clientGoRateLimiterMetricAdapter struct {
duration *prometheus.SummaryVec
}

var _ = metrics.LatencyMetric(&clientGoRateLimiterMetricAdapter{})

// MustRegisterClientGoMetrics registers k8s.io/client-go metrics.
// It panics if it encounters an error (e.g. metrics already registered).
func MustRegisterClientGoMetrics(registerer prometheus.Registerer) {
Expand Down Expand Up @@ -64,13 +69,23 @@ func MustRegisterClientGoMetrics(registerer prometheus.Registerer) {
),
}

// controller-runtime also calls metrics.Register() during init and this
// function can be called only once. To ensure that the k8s client metrics
// get updated, the global variables need to be set again here.
//
// Details:
// https://github.com/kubernetes-sigs/controller-runtime/blob/67b27f27e514bd9ac4cf9a2d84dec089ece95bf7/pkg/metrics/client_go_adapter.go#L42-L55
// https://github.com/kubernetes/client-go/blob/aa7909e7d7c0661792ba21b9e882f3cd6ad0ce53/tools/metrics/metrics.go#L129-L170
metrics.Register(
metrics.RegisterOpts{
RequestLatency: httpMetrics,
RequestResult: httpMetrics,
RateLimiterLatency: rateLimiterMetrics,
},
)
metrics.RequestLatency = httpMetrics
metrics.RequestResult = httpMetrics
metrics.RateLimiterLatency = rateLimiterMetrics

registerer.MustRegister(httpMetrics.count, httpMetrics.duration, rateLimiterMetrics.duration)
}
Expand Down

0 comments on commit 349d97e

Please sign in to comment.