Skip to content

armgit / Pulse

Public Imported from GitHub
0

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.

fix(alerts): deduplicate flapping history entries on load and periodic cleanup

rcourtman5bc209d9191e
diff --git a/internal/alerts/history.go b/internal/alerts/history.go
index 69a65aa4d9..fd423efe31 100644
--- a/internal/alerts/history.go
+++ b/internal/alerts/history.go
@@ -292,6 +292,7 @@ func (hm *HistoryManager) loadHistory() error {
// Clean old entries immediately
hm.cleanOldEntries()
+ hm.deduplicateHistory()
return nil
}
@@ -495,6 +496,7 @@ func (hm *HistoryManager) cleanupRoutine() {
select {
case <-startupDelay.C:
hm.cleanOldEntries()
+ hm.deduplicateHistory()
case <-hm.stopChan:
return
}
@@ -503,6 +505,7 @@ func (hm *HistoryManager) cleanupRoutine() {
select {
case <-ticker.C:
hm.cleanOldEntries()
+ hm.deduplicateHistory()
case <-hm.stopChan:
return
}
@@ -535,6 +538,57 @@ func (hm *HistoryManager) cleanOldEntries() {
}
}
+// historyDedupWindow is the maximum gap between consecutive same-alert entries
+// for them to be considered duplicates of one continuous incident.
+const historyDedupWindow = 5 * time.Minute
+
+// deduplicateHistory collapses consecutive entries for the same alert identity
+// where the gap between entries is shorter than the dedup window. This removes
+// flapping/re-fire noise where a bug or polling pattern caused the same alert
+// to be recorded many times in quick succession. The surviving entry keeps the
+// earliest timestamp and the latest LastSeen.
+func (hm *HistoryManager) deduplicateHistory() {
+ hm.mu.Lock()
+ defer hm.mu.Unlock()
+
+ if len(hm.history) < 2 {
+ return
+ }
+
+ deduped := make([]HistoryEntry, 0, len(hm.history))
+ lastIdxByKey := make(map[string]int)
+ lastTimeByKey := make(map[string]time.Time)
+ removed := 0
+
+ for _, entry := range hm.history {
+ key := historyIdentityKey(&entry.Alert)
+
+ if lastTime, ok := lastTimeByKey[key]; ok {
+ if entry.Timestamp.Sub(lastTime) <= historyDedupWindow {
+ idx := lastIdxByKey[key]
+ if entry.Alert.LastSeen.After(deduped[idx].Alert.LastSeen) {
+ deduped[idx].Alert.LastSeen = entry.Alert.LastSeen
+ }
+ lastTimeByKey[key] = entry.Timestamp
+ removed++
+ continue
+ }
+ }
+
+ deduped = append(deduped, entry)
+ lastIdxByKey[key] = len(deduped) - 1
+ lastTimeByKey[key] = entry.Timestamp
+ }
+
+ if removed > 0 {
+ hm.history = deduped
+ log.Info().
+ Int("removed", removed).
+ Int("remaining", len(deduped)).
+ Msg("deduplicated alert history entries")
+ }
+}
+
// RemoveAlert removes a specific alert from history by ID
func (hm *HistoryManager) RemoveAlert(alertID string) {
hm.mu.Lock()
diff --git a/internal/alerts/history_dedup_test.go b/internal/alerts/history_dedup_test.go
new file mode 100644
index 0000000000..293f456634
--- /dev/null
+++ b/internal/alerts/history_dedup_test.go
@@ -0,0 +1,121 @@
+package alerts
+
+import (
+ "testing"
+ "time"
+)
+
+func TestDeduplicateHistory(t *testing.T) {
+ t.Run("collapses consecutive same-alert entries within dedup window", func(t *testing.T) {
+ hm := &HistoryManager{
+ history: make([]HistoryEntry, 0),
+ dataDir: t.TempDir(),
+ stopChan: make(chan struct{}),
+ }
+
+ base := time.Now()
+ alert := Alert{
+ ID: "zfs-1::zfs-1-health",
+ CanonicalState: "zfs-1::zfs-1-health",
+ Type: "zfs-pool-errors",
+ Level: AlertLevelWarning,
+ ResourceID: "zfs-1",
+ StartTime: base,
+ LastSeen: base.Add(1 * time.Minute),
+ }
+
+ for i := 0; i < 10; i++ {
+ dup := *alert.Clone()
+ dup.StartTime = base.Add(time.Duration(i*2) * time.Minute)
+ dup.LastSeen = base.Add(time.Duration(i*2+1) * time.Minute)
+ hm.history = append(hm.history, HistoryEntry{
+ Alert: dup,
+ Timestamp: base.Add(time.Duration(i*2) * time.Minute),
+ })
+ }
+
+ hm.deduplicateHistory()
+
+ if len(hm.history) != 1 {
+ t.Fatalf("expected 1 entry after dedup, got %d", len(hm.history))
+ }
+ if !hm.history[0].Alert.LastSeen.Equal(base.Add(19 * time.Minute)) {
+ t.Errorf("expected LastSeen to be latest (%v), got %v", base.Add(19*time.Minute), hm.history[0].Alert.LastSeen)
+ }
+ })
+
+ t.Run("preserves entries beyond dedup window", func(t *testing.T) {
+ hm := &HistoryManager{
+ history: make([]HistoryEntry, 0),
+ dataDir: t.TempDir(),
+ stopChan: make(chan struct{}),
+ }
+
+ base := time.Now()
+ key := "storage-1::storage-1-health"
+
+ for _, offset := range []time.Duration{0, 2 * time.Minute, 10 * time.Minute, 12 * time.Minute} {
+ hm.history = append(hm.history, HistoryEntry{
+ Alert: Alert{
+ ID: key,
+ CanonicalState: key,
+ StartTime: base.Add(offset),
+ LastSeen: base.Add(offset + 1*time.Minute),
+ },
+ Timestamp: base.Add(offset),
+ })
+ }
+
+ hm.deduplicateHistory()
+
+ if len(hm.history) != 2 {
+ t.Fatalf("expected 2 entries (two chains separated by 8min gap), got %d", len(hm.history))
+ }
+ })
+
+ t.Run("collapses interleaved same-alert entries within dedup window", func(t *testing.T) {
+ hm := &HistoryManager{
+ history: make([]HistoryEntry, 0),
+ dataDir: t.TempDir(),
+ stopChan: make(chan struct{}),
+ }
+
+ base := time.Now()
+ // alert-a and alert-b alternate every 1 minute
+ // Each alert appears twice within the 5-min window, so each collapses to 1
+ for i, key := range []string{"alert-a", "alert-b", "alert-a", "alert-b"} {
+ hm.history = append(hm.history, HistoryEntry{
+ Alert: Alert{
+ ID: key,
+ CanonicalState: key,
+ StartTime: base.Add(time.Duration(i) * time.Minute),
+ LastSeen: base.Add(time.Duration(i) * time.Minute),
+ },
+ Timestamp: base.Add(time.Duration(i) * time.Minute),
+ })
+ }
+
+ hm.deduplicateHistory()
+
+ if len(hm.history) != 2 {
+ t.Fatalf("expected 2 entries (interleaved same-alert pairs collapsed), got %d", len(hm.history))
+ }
+ })
+
+ t.Run("no-op on single entry", func(t *testing.T) {
+ hm := &HistoryManager{
+ history: make([]HistoryEntry, 0),
+ stopChan: make(chan struct{}),
+ }
+ hm.history = append(hm.history, HistoryEntry{
+ Alert: Alert{ID: "x"},
+ Timestamp: time.Now(),
+ })
+
+ hm.deduplicateHistory()
+
+ if len(hm.history) != 1 {
+ t.Fatalf("expected 1 entry, got %d", len(hm.history))
+ }
+ })
+}