diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 86d78028c..d54eb02b5 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -758,6 +758,13 @@ func (m *ServerMetrics) RegisterPodVolumeOpLatencyGauge(node, pvbName, opName, b } } +// ResetBackupLastSuccessfulTimestamp removes all schedule-level backupLastSuccessfulTimestamp values. +func (m *ServerMetrics) ResetBackupLastSuccessfulTimestamp() { + if g, ok := m.metrics[backupLastSuccessfulTimestamp].(*prometheus.GaugeVec); ok { + g.Reset() + } +} + // 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 { diff --git a/pkg/metrics/metrics_test.go b/pkg/metrics/metrics_test.go index a24f2bf33..07004f172 100644 --- a/pkg/metrics/metrics_test.go +++ b/pkg/metrics/metrics_test.go @@ -457,6 +457,38 @@ func getHistogramCount(t *testing.T, vec *prometheus.HistogramVec, scheduleLabel return 0 } +// TestResetBackupLastSuccessfulTimestamp verifies that ResetBackupLastSuccessfulTimestamp +// removes all schedule-level values from the backupLastSuccessfulTimestamp gauge. +func TestResetBackupLastSuccessfulTimestamp(t *testing.T) { + m := NewServerMetrics() + + now := time.Now() + m.SetBackupLastSuccessfulTimestamp("schedule-1", now) + m.SetBackupLastSuccessfulTimestamp("schedule-2", now.Add(-time.Hour)) + m.SetBackupLastSuccessfulTimestamp("", now.Add(-2*time.Hour)) + + // Verify all three entries exist + g := m.metrics[backupLastSuccessfulTimestamp].(*prometheus.GaugeVec) + assert.Equal(t, 3, collectGaugeCount(t, g)) + + // Reset should remove all entries + m.ResetBackupLastSuccessfulTimestamp() + assert.Equal(t, 0, collectGaugeCount(t, g)) +} + +// collectGaugeCount returns the number of time series in a GaugeVec. +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 +} + // TestRepoMaintenanceMetrics verifies that repo maintenance metrics are properly recorded. func TestRepoMaintenanceMetrics(t *testing.T) { tests := []struct {