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): stop mutating resolved snapshots

pulse-triage[bot]947a26b72f40
diff --git a/internal/alerts/alert_store.go b/internal/alerts/alert_store.go
index fa172d10ca..e5fe15f9c9 100644
--- a/internal/alerts/alert_store.go
+++ b/internal/alerts/alert_store.go
@@ -149,13 +149,11 @@ func (m *Manager) registerResolvedAliasUnlocked(storageKey string, resolved *Res
if m.resolvedAlias == nil {
m.resolvedAlias = make(map[string]string)
}
- backfillCanonicalIdentity(resolved.Alert)
+ // Resolved alerts may already be visible to asynchronous lifecycle
+ // callbacks. Derive aliases without mutating the shared alert snapshot.
if alias := effectiveAlertID(resolved.Alert, ""); alias != "" && alias != storageKey {
m.resolvedAlias[alias] = storageKey
}
- if resolved.Alert.CanonicalState != "" && resolved.Alert.CanonicalState != storageKey {
- m.resolvedAlias[resolved.Alert.CanonicalState] = storageKey
- }
}
func (m *Manager) unregisterResolvedAliasUnlocked(storageKey string, resolved *ResolvedAlert) {
@@ -163,13 +161,9 @@ func (m *Manager) unregisterResolvedAliasUnlocked(storageKey string, resolved *R
return
}
if resolved != nil && resolved.Alert != nil {
- backfillCanonicalIdentity(resolved.Alert)
if alias := effectiveAlertID(resolved.Alert, ""); alias != "" && alias != storageKey {
delete(m.resolvedAlias, alias)
}
- if resolved.Alert.CanonicalState != "" && resolved.Alert.CanonicalState != storageKey {
- delete(m.resolvedAlias, resolved.Alert.CanonicalState)
- }
}
}
@@ -202,8 +196,7 @@ func (m *Manager) getResolvedAlertNoLock(id string) (*ResolvedAlert, bool) {
if resolved == nil || resolved.Alert == nil {
continue
}
- backfillCanonicalIdentity(resolved.Alert)
- if resolved.Alert.CanonicalState != id {
+ if deriveCanonicalIdentity(resolved.Alert).State != id {
continue
}
if m.resolvedAlias == nil {
diff --git a/internal/alerts/alerts_test.go b/internal/alerts/alerts_test.go
index a5fa74f5c3..b50a2aac81 100644
--- a/internal/alerts/alerts_test.go
+++ b/internal/alerts/alerts_test.go
@@ -8923,6 +8923,33 @@ func TestGetResolvedAlert(t *testing.T) {
t.Fatal("mutating returned alert changed stored resolved alert")
}
})
+
+ t.Run("canonicalizes legacy output without mutating stored alert", func(t *testing.T) {
+ m := newTestManager(t)
+ stored := &Alert{
+ ID: "res1-cpu",
+ Type: "cpu",
+ ResourceID: "res1",
+ }
+ m.resolvedMutex.Lock()
+ m.recentlyResolved["legacy-storage-key"] = &ResolvedAlert{
+ Alert: stored,
+ ResolvedTime: time.Now(),
+ }
+ m.resolvedMutex.Unlock()
+
+ canonicalState := buildCanonicalStateID("res1", "metric-threshold:cpu")
+ result := m.GetResolvedAlert(canonicalState)
+ if result == nil || result.Alert == nil {
+ t.Fatal("expected canonical lookup of legacy resolved alert")
+ }
+ if result.Alert.ID != canonicalState || result.Alert.CanonicalState != canonicalState {
+ t.Fatalf("canonical output identity = (%q, %q), want %q", result.Alert.ID, result.Alert.CanonicalState, canonicalState)
+ }
+ if stored.CanonicalSpecID != "" || stored.CanonicalKind != "" || stored.CanonicalState != "" {
+ t.Fatalf("lookup mutated stored legacy alert identity: %+v", stored)
+ }
+ })
}
func TestGetAlertHistory(t *testing.T) {