From b48dc6ff7377c55e837143182d097013beff1d31 Mon Sep 17 00:00:00 2001 From: Baptiste Girard-Carrabin Date: Wed, 18 Dec 2024 14:33:02 +0100 Subject: [PATCH] [metrics] Fix panic during metrics manager startup This fixes a regression introduced in #1876 where the driver would start panicking on startup if `--http-endpoint` was specified. This was caused by the metrics not being initialized anymore during startup. The proposed fix involves using the `Reset` methods of the metrics object instead of trying to redefine them each time they need to be reset. --- pkg/metrics/metrics.go | 7 +------ pkg/metrics/metrics_test_util.go | 3 ++- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index b72c162ee..47d8c24bd 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -38,11 +38,6 @@ const ( ) var ( - gkeComponentVersion *metrics.GaugeVec - pdcsiOperationErrorsMetric *metrics.CounterVec -) - -func initMetrics() { // This metric is exposed only from the controller driver component when GKE_PDCSI_VERSION env variable is set. gkeComponentVersion = metrics.NewGaugeVec(&metrics.GaugeOpts{ Name: "component_version", @@ -57,7 +52,7 @@ func initMetrics() { StabilityLevel: metrics.ALPHA, }, []string{"driver_name", "method_name", "grpc_status_code", "disk_type", "enable_confidential_storage", "enable_storage_pools"}) -} +) type MetricsManager struct { registry metrics.KubeRegistry diff --git a/pkg/metrics/metrics_test_util.go b/pkg/metrics/metrics_test_util.go index c77142413..ce0d13d95 100644 --- a/pkg/metrics/metrics_test_util.go +++ b/pkg/metrics/metrics_test_util.go @@ -19,5 +19,6 @@ package metrics // Test-only method used for resetting metric counts. func (mm *MetricsManager) ResetMetrics() { // Re-initialize metrics - initMetrics() + gkeComponentVersion.Reset() + pdcsiOperationErrorsMetric.Reset() }