Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
fix(alerts): make the flapping cooldown actually suppress notifications
rcourtmanc74b9f60fa78
diff --git a/internal/alerts/alerts_test.go b/internal/alerts/alerts_test.goindex c74fc2ae25..5cfc8820d5 100644--- a/internal/alerts/alerts_test.go+++ b/internal/alerts/alerts_test.go@@ -20220,3 +20220,119 @@ func TestCheckStorageClearsZFSAlertsWhenPoolDetaches(t *testing.T) {t.Error("expected unrelated storage to keep its ZFS alerts")}}++// The flapping cooldown is a promise Pulse makes to the operator: the flapping+// postmortem finding tells them notifications were suppressed for+// FlappingCooldownMinutes and suggests raising it. These tests pin that the+// cooldown actually gates dispatch, rather than being recorded and ignored.++func TestFlappingCooldownSuppressesAfterWindowDrains(t *testing.T) {+ m := newTestManager(t)++ const trackingKey = "cooldown-holds"++ m.mu.Lock()+ m.config.FlappingEnabled = true+ m.config.FlappingThreshold = 5+ m.config.FlappingWindowSeconds = 300+ m.config.FlappingCooldownMinutes = 15++ // Four prior state changes inside the window; this call is the fifth and+ // trips the threshold.+ now := time.Now()+ for i := 1; i <= 4; i++ {+ m.flappingHistory[trackingKey] = append(+ m.flappingHistory[trackingKey],+ now.Add(-time.Duration(i)*time.Second),+ )+ }+ suppress, justTransitioned := m.checkFlappingLocked(trackingKey)+ m.mu.Unlock()++ if !suppress || !justTransitioned {+ t.Fatalf("expected the threshold breach to suppress and transition, got suppress=%v justTransitioned=%v", suppress, justTransitioned)+ }++ // Drain the sliding window so the count alone would no longer suppress.+ // The cooldown is still running, so dispatch must stay suppressed.+ m.mu.Lock()+ m.flappingHistory[trackingKey] = nil+ suppressDuringCooldown, transitionedAgain := m.checkFlappingLocked(trackingKey)+ cooldownEnd, stillRecorded := m.suppressedUntil[trackingKey]+ m.mu.Unlock()++ if !suppressDuringCooldown {+ t.Error("expected the cooldown to keep suppressing once the sliding window drained")+ }+ if transitionedAgain {+ t.Error("expected no second transition while the same cooldown is running")+ }+ if !stillRecorded || !cooldownEnd.After(time.Now()) {+ t.Error("expected the cooldown deadline to remain in the future")+ }+}++func TestFlappingCooldownReleasesAndCanReArm(t *testing.T) {+ m := newTestManager(t)++ const trackingKey = "cooldown-releases"++ m.mu.Lock()+ m.config.FlappingEnabled = true+ m.config.FlappingThreshold = 3+ m.config.FlappingWindowSeconds = 300+ m.config.FlappingCooldownMinutes = 15++ // An episode whose cooldown has already elapsed.+ m.flappingActive[trackingKey] = true+ m.suppressedUntil[trackingKey] = time.Now().Add(-time.Minute)++ // A lone state change after the cooldown expires must get through.+ suppress, _ := m.checkFlappingLocked(trackingKey)+ latchCleared := !m.flappingActive[trackingKey]+ m.mu.Unlock()++ if suppress {+ t.Error("expected dispatch once the cooldown has been served and the resource is quiet")+ }+ if !latchCleared {+ t.Error("expected the flapping latch to clear so a later episode can open a fresh cooldown")+ }++ // A fresh episode must be able to arm a new cooldown, which the permanently+ // latched flappingActive flag previously prevented.+ m.mu.Lock()+ now := time.Now()+ for i := 1; i <= 3; i++ {+ m.flappingHistory[trackingKey] = append(+ m.flappingHistory[trackingKey],+ now.Add(-time.Duration(i)*time.Second),+ )+ }+ suppressAgain, transitionedAgain := m.checkFlappingLocked(trackingKey)+ _, rearmed := m.suppressedUntil[trackingKey]+ m.mu.Unlock()++ if !suppressAgain || !transitionedAgain {+ t.Errorf("expected a later episode to suppress and transition again, got suppress=%v justTransitioned=%v", suppressAgain, transitionedAgain)+ }+ if !rearmed {+ t.Error("expected a fresh cooldown deadline for the later episode")+ }+}++func TestFlappingDisabledIgnoresCooldown(t *testing.T) {+ m := newTestManager(t)++ const trackingKey = "flapping-off"++ m.mu.Lock()+ m.config.FlappingEnabled = false+ m.suppressedUntil[trackingKey] = time.Now().Add(15 * time.Minute)+ suppress, justTransitioned := m.checkFlappingLocked(trackingKey)+ m.mu.Unlock()++ if suppress || justTransitioned {+ t.Errorf("expected disabled flapping detection to suppress nothing, got suppress=%v justTransitioned=%v", suppress, justTransitioned)+ }+}diff --git a/internal/alerts/notification_policy.go b/internal/alerts/notification_policy.goindex cbfa8d19a3..5bb41fcfd0 100644--- a/internal/alerts/notification_policy.go+++ b/internal/alerts/notification_policy.go@@ -87,6 +87,26 @@ func (m *Manager) checkFlappingLocked(trackingKey string) (suppress bool, justTrnow := time.Now()windowDuration := time.Duration(m.config.FlappingWindowSeconds) * time.Second+ // Honour an active cooldown before anything else. The cooldown recorded+ // below used to be write-only on this path: suppression was decided purely+ // by the sliding-window count, so it ended the moment the count dipped+ // under the threshold instead of holding for FlappingCooldownMinutes. A+ // resource oscillating just under the threshold rate was therefore never+ // damped at all, while the flapping postmortem told the operator that+ // notifications had been suppressed for the configured cooldown and+ // suggested raising a value that changed nothing.+ if until, cooling := m.suppressedUntil[trackingKey]; cooling {+ if now.Before(until) {+ return true, false+ }+ // The cooldown has been served. Clear the latch so a later episode can+ // open a fresh one; flappingActive is otherwise only ever set to true,+ // which would leave every subsequent episode with no cooldown at all.+ delete(m.suppressedUntil, trackingKey)+ delete(m.flappingActive, trackingKey)+ delete(m.flappingHistory, trackingKey)+ }+// Record this state changem.flappingHistory[trackingKey] = append(m.flappingHistory[trackingKey], now)