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): re-fire cooldown for stateful alerts to prevent duplicate history

rcourtmanfa7e4711e75b
diff --git a/internal/alerts/canonical_lifecycle.go b/internal/alerts/canonical_lifecycle.go
index 6c834e34ba..c1b2d88acb 100644
--- a/internal/alerts/canonical_lifecycle.go
+++ b/internal/alerts/canonical_lifecycle.go
@@ -544,6 +544,27 @@ func (m *Manager) evaluateCanonicalStatefulAlert(params canonicalStatefulAlertPa
}
if existing == nil {
+ if resolved, ok := m.getResolvedAlertNoLock(storageKey); ok && resolved != nil && resolved.Alert != nil {
+ if resolved.ResolvedTime.After(time.Now().Add(-5 * time.Minute)) {
+ if !resolved.Alert.StartTime.IsZero() {
+ alert.StartTime = resolved.Alert.StartTime
+ }
+ m.removeResolvedAlertUnlocked(storageKey)
+ if params.AddToHistory {
+ m.historyManager.UpdateAlertLastSeenForAlert(alert, alert.LastSeen)
+ }
+ log.Debug().
+ Str("alertID", storageKey).
+ Time("resolvedAt", resolved.ResolvedTime).
+ Msg("Stateful alert re-fired within cooldown, reactivated without new history entry")
+ if params.RateLimit && !m.checkRateLimit(trackingKey) {
+ return result, true
+ }
+ m.dispatchAlert(alert, params.DispatchAsync)
+ return result, true
+ }
+ }
+
if params.AddToHistory {
m.historyManager.AddAlert(*alert)
}
diff --git a/internal/alerts/canonical_stateful_test.go b/internal/alerts/canonical_stateful_test.go
new file mode 100644
index 0000000000..344d63e58d
--- /dev/null
+++ b/internal/alerts/canonical_stateful_test.go
@@ -0,0 +1,120 @@
+package alerts
+
+import (
+ "testing"
+ "time"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/storagehealth"
+ "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
+)
+
+func TestStatefulAlertReFireCooldown(t *testing.T) {
+ t.Run("re-fire within cooldown does not create duplicate history entry", func(t *testing.T) {
+ m := newTestManager(t)
+
+ specResourceID := "storage-1/zfs-pool:tank"
+ alertID := buildCanonicalStateID(specResourceID, specResourceID+"-health")
+ reasons := []storagehealth.Reason{
+ {Code: "zfs_pool_state", Severity: storagehealth.RiskCritical, Summary: "ZFS pool tank is DEGRADED"},
+ }
+
+ params := canonicalHealthAssessmentAlertParams{
+ SpecID: specResourceID + "-health",
+ Signal: "zfs_pool",
+ Codes: zfsPoolAssessmentCodes,
+ Reasons: reasons,
+ AlertID: alertID,
+ AlertType: "zfs-pool-state",
+ SpecResourceID: specResourceID,
+ ResourceID: specResourceID,
+ ResourceName: "tank",
+ ResourceType: unifiedresources.ResourceTypeStorage,
+ Node: "node-1",
+ Instance: "node-1",
+ Metadata: map[string]interface{}{"resourceType": "storage"},
+ }
+
+ m.syncCanonicalHealthAssessmentAlert(params)
+
+ original := testRequireActiveAlert(t, m, alertID)
+ originalStart := original.StartTime
+
+ historyAfterFire := len(m.historyManager.GetAllHistory(1000))
+ if historyAfterFire != 1 {
+ t.Fatalf("expected 1 history entry after initial fire, got %d", historyAfterFire)
+ }
+
+ m.mu.Lock()
+ m.clearAlertNoLock(alertID)
+ m.mu.Unlock()
+
+ if testHasActiveAlert(t, m, alertID) {
+ t.Fatal("expected alert to be cleared")
+ }
+
+ m.syncCanonicalHealthAssessmentAlert(params)
+
+ reactivated := testRequireActiveAlert(t, m, alertID)
+
+ historyAfterReFire := len(m.historyManager.GetAllHistory(1000))
+ if historyAfterReFire != historyAfterFire {
+ t.Errorf("expected %d history entries after re-fire (same as after initial fire), got %d", historyAfterFire, historyAfterReFire)
+ }
+
+ if !reactivated.StartTime.Equal(originalStart) {
+ t.Errorf("expected reactivated alert to preserve original StartTime %v, got %v", originalStart, reactivated.StartTime)
+ }
+ })
+
+ t.Run("re-fire after cooldown expiry creates new history entry", func(t *testing.T) {
+ m := newTestManager(t)
+
+ specResourceID := "storage-2/zfs-pool:data"
+ alertID := buildCanonicalStateID(specResourceID, specResourceID+"-health")
+ reasons := []storagehealth.Reason{
+ {Code: "zfs_pool_state", Severity: storagehealth.RiskCritical, Summary: "ZFS pool data is FAULTED"},
+ }
+
+ params := canonicalHealthAssessmentAlertParams{
+ SpecID: specResourceID + "-health",
+ Signal: "zfs_pool",
+ Codes: zfsPoolAssessmentCodes,
+ Reasons: reasons,
+ AlertID: alertID,
+ AlertType: "zfs-pool-state",
+ SpecResourceID: specResourceID,
+ ResourceID: specResourceID,
+ ResourceName: "data",
+ ResourceType: unifiedresources.ResourceTypeStorage,
+ Node: "node-2",
+ Instance: "node-2",
+ Metadata: map[string]interface{}{"resourceType": "storage"},
+ }
+
+ m.syncCanonicalHealthAssessmentAlert(params)
+
+ historyAfterFire := len(m.historyManager.GetAllHistory(1000))
+ if historyAfterFire != 1 {
+ t.Fatalf("expected 1 history entry after initial fire, got %d", historyAfterFire)
+ }
+
+ m.mu.Lock()
+ m.clearAlertNoLock(alertID)
+ m.mu.Unlock()
+
+ m.resolvedMutex.Lock()
+ if resolved, ok := m.recentlyResolved[alertID]; ok && resolved != nil {
+ resolved.ResolvedTime = time.Now().Add(-10 * time.Minute)
+ }
+ m.resolvedMutex.Unlock()
+
+ m.syncCanonicalHealthAssessmentAlert(params)
+
+ testRequireActiveAlert(t, m, alertID)
+
+ historyAfterReFire := len(m.historyManager.GetAllHistory(1000))
+ if historyAfterReFire != 2 {
+ t.Errorf("expected 2 history entries after re-fire past cooldown, got %d", historyAfterReFire)
+ }
+ })
+}