From 6ea95548d69e41744467ed69c634ba7ba8f30ad5 Mon Sep 17 00:00:00 2001 From: Shubham Pampattiwar Date: Mon, 20 Jul 2026 06:10:53 -0700 Subject: [PATCH] Remove BackupLastSuccessfulTimestampCount, use Metrics() in test Remove the exported method that was only used in tests. Use the existing Metrics() getter to access the gauge directly in the backup controller test instead. Signed-off-by: Shubham Pampattiwar --- pkg/controller/backup_controller_test.go | 18 ++++++++++++++++-- pkg/metrics/metrics.go | 17 ----------------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/pkg/controller/backup_controller_test.go b/pkg/controller/backup_controller_test.go index 3a1903e0f..3aafa2c71 100644 --- a/pkg/controller/backup_controller_test.go +++ b/pkg/controller/backup_controller_test.go @@ -31,6 +31,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" snapshotv1api "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumesnapshot/v1" + "github.com/prometheus/client_golang/prometheus" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -2049,10 +2050,11 @@ func Test_updateTotalBackupMetric_prunesStaleTimestamps(t *testing.T) { require.NoError(t, err) m := metrics.NewServerMetrics() + gauge := m.Metrics()["backup_last_successful_timestamp"].(*prometheus.GaugeVec) // Simulate a previous resync that set the metric for "deleted-schedule" m.SetBackupLastSuccessfulTimestamp("deleted-schedule", baseTime) - require.Equal(t, 1, m.BackupLastSuccessfulTimestampCount()) + require.Equal(t, 1, collectGaugeCount(t, gauge)) // Current backups only contain entries for "active-schedule" backups := []velerov1api.Backup{ @@ -2070,7 +2072,19 @@ func Test_updateTotalBackupMetric_prunesStaleTimestamps(t *testing.T) { } // Only "active-schedule" should remain; "deleted-schedule" should be pruned - assert.Equal(t, 1, m.BackupLastSuccessfulTimestampCount()) + assert.Equal(t, 1, collectGaugeCount(t, gauge)) +} + +func collectGaugeCount(t *testing.T, g *prometheus.GaugeVec) int { + t.Helper() + ch := make(chan prometheus.Metric, 10) + g.Collect(ch) + close(ch) + count := 0 + for range ch { + count++ + } + return count } // Unit tests to make sure that the backup's status is updated correctly during reconcile. diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index d95867fd1..d54eb02b5 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -765,23 +765,6 @@ func (m *ServerMetrics) ResetBackupLastSuccessfulTimestamp() { } } -// BackupLastSuccessfulTimestampCount returns the number of active time series -// in the backupLastSuccessfulTimestamp gauge. -func (m *ServerMetrics) BackupLastSuccessfulTimestampCount() int { - g, ok := m.metrics[backupLastSuccessfulTimestamp].(*prometheus.GaugeVec) - if !ok { - return 0 - } - ch := make(chan prometheus.Metric, 100) - g.Collect(ch) - close(ch) - count := 0 - for range ch { - count++ - } - return count -} - // SetBackupTarballSizeBytesGauge records the size, in bytes, of a backup tarball. func (m *ServerMetrics) SetBackupTarballSizeBytesGauge(backupSchedule string, size int64) { if g, ok := m.metrics[backupTarballSizeBytesGauge].(*prometheus.GaugeVec); ok {