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
Stabilize rolling metric history snapshots
pulse-triage[bot]75a8e673474b
diff --git a/docs/release-control/v6/internal/subsystems/monitoring.md b/docs/release-control/v6/internal/subsystems/monitoring.mdindex cd32f8d010..d97419aa05 100644--- a/docs/release-control/v6/internal/subsystems/monitoring.md+++ b/docs/release-control/v6/internal/subsystems/monitoring.md@@ -441,7 +441,11 @@ identity and durable history as charts. `internal/monitoring/metric_window_proviresolves the unified resource metrics target, reads the fresh in-memory tail,and falls back to the SQLite metrics store when that tail lacks the requestedcoverage. Persistent fallbacks are briefly cached to bound restart-time query-load, merged by timestamp, and returned as observations only. When the durable+load, merged by timestamp, and returned as observations only. Each request must+snapshot the active `MetricsHistory` under the monitor lock and use that same+history generation for its in-memory read and persistent-cache access; mock+history seeding may replace the active generation concurrently, but a request+must never lock one generation's cache mutex and unlock another's. When the durableseries and fresh in-memory tail contain the same timestamp, the in-memory valueis authoritative so an older persisted or rolled-up value cannot replace thelatest observation. Alert policy owns averaging, readiness, hysteresis, anddiff --git a/internal/monitoring/metric_window_provider.go b/internal/monitoring/metric_window_provider.goindex 339c94b629..7129dca78a 100644--- a/internal/monitoring/metric_window_provider.go+++ b/internal/monitoring/metric_window_provider.go@@ -36,6 +36,9 @@ func (m *Monitor) metricWindowPoints(request alerts.MetricWindowRequest) ([]alerif m == nil {return nil, fmt.Errorf("monitor unavailable")}+ m.mu.RLock()+ history := m.metricsHistory+ m.mu.RUnlock()resourceType := strings.TrimSpace(request.ResourceType)resourceID := strings.TrimSpace(request.ResourceID)if target := m.MetricsTargetForResource(request.ResourceID); target != nil {@@ -55,9 +58,9 @@ func (m *Monitor) metricWindowPoints(request alerts.MetricWindowRequest) ([]alerif duration <= 0 {return nil, fmt.Errorf("invalid metric window")}- points := m.inMemoryMetricWindow(resourceType, resourceID, metric, duration)+ points := m.inMemoryMetricWindow(history, resourceType, resourceID, metric, duration)if metricWindowCoverage(points) < duration*8/10 {- stored := m.persistentMetricWindow(resourceType, resourceID, metric, request.Start, request.End)+ stored := m.persistentMetricWindow(history, resourceType, resourceID, metric, request.Start, request.End)// Append the fresh in-memory tail last so it remains authoritative when// SQLite contains an older value at the same timestamp.points = mergeMetricWindowPoints(stored, points, request.Start, request.End)@@ -70,39 +73,39 @@ func (m *Monitor) metricWindowPoints(request alerts.MetricWindowRequest) ([]alerreturn result, nil}-func (m *Monitor) inMemoryMetricWindow(resourceType, resourceID, metric string, duration time.Duration) []MetricPoint {- if m.metricsHistory == nil {+func (m *Monitor) inMemoryMetricWindow(history *MetricsHistory, resourceType, resourceID, metric string, duration time.Duration) []MetricPoint {+ if history == nil {return nil}switch strings.ToLower(resourceType) {case "node":- return m.metricsHistory.GetNodeMetrics(resourceID, metric, duration)+ return history.GetNodeMetrics(resourceID, metric, duration)case "storage":- return m.metricsHistory.GetAllStorageMetrics(resourceID, duration)[metric]+ return history.GetAllStorageMetrics(resourceID, duration)[metric]case "disk":- return m.metricsHistory.GetDiskMetrics(resourceID, metric, duration)+ return history.GetDiskMetrics(resourceID, metric, duration)default:- return m.metricsHistory.GetGuestMetrics(resourceID, metric, duration)+ return history.GetGuestMetrics(resourceID, metric, duration)}}-func (m *Monitor) persistentMetricWindow(resourceType, resourceID, metric string, start, end time.Time) []MetricPoint {+func (m *Monitor) persistentMetricWindow(history *MetricsHistory, resourceType, resourceID, metric string, start, end time.Time) []MetricPoint {if m.metricsStore == nil {return nil}cacheKey := strings.Join([]string{resourceType, resourceID, metric, fmt.Sprint(end.Sub(start).Seconds())}, "\x00")now := time.Now()- if m.metricsHistory != nil {- m.metricsHistory.metricWindowMu.Lock()- if cached, ok := m.metricsHistory.metricWindowCache[cacheKey]; ok && now.Before(cached.expiresAt) {+ if history != nil {+ history.metricWindowMu.Lock()+ if cached, ok := history.metricWindowCache[cacheKey]; ok && now.Before(cached.expiresAt) {points := make([]MetricPoint, len(cached.points))for i, point := range cached.points {points[i] = MetricPoint{Timestamp: point.Timestamp, Value: point.Value}}- m.metricsHistory.metricWindowMu.Unlock()+ history.metricWindowMu.Unlock()return points}- m.metricsHistory.metricWindowMu.Unlock()+ history.metricWindowMu.Unlock()}var best []MetricPoint@@ -120,24 +123,24 @@ func (m *Monitor) persistentMetricWindow(resourceType, resourceID, metric stringbest = converted}}- if m.metricsHistory != nil {+ if history != nil {cached := make([]alerts.MetricWindowPoint, len(best))for i, point := range best {cached[i] = alerts.MetricWindowPoint{Timestamp: point.Timestamp, Value: point.Value}}- m.metricsHistory.metricWindowMu.Lock()- if m.metricsHistory.metricWindowCache == nil {- m.metricsHistory.metricWindowCache = make(map[string]metricWindowCacheEntry)+ history.metricWindowMu.Lock()+ if history.metricWindowCache == nil {+ history.metricWindowCache = make(map[string]metricWindowCacheEntry)}- if len(m.metricsHistory.metricWindowCache) >= 1024 {- for key, entry := range m.metricsHistory.metricWindowCache {+ if len(history.metricWindowCache) >= 1024 {+ for key, entry := range history.metricWindowCache {if !now.Before(entry.expiresAt) {- delete(m.metricsHistory.metricWindowCache, key)+ delete(history.metricWindowCache, key)}}}- m.metricsHistory.metricWindowCache[cacheKey] = metricWindowCacheEntry{points: cached, expiresAt: now.Add(metricWindowPersistentCacheTTL)}- m.metricsHistory.metricWindowMu.Unlock()+ history.metricWindowCache[cacheKey] = metricWindowCacheEntry{points: cached, expiresAt: now.Add(metricWindowPersistentCacheTTL)}+ history.metricWindowMu.Unlock()}return best}diff --git a/internal/monitoring/metric_window_provider_test.go b/internal/monitoring/metric_window_provider_test.goindex d0e012cde0..9931595f4b 100644--- a/internal/monitoring/metric_window_provider_test.go+++ b/internal/monitoring/metric_window_provider_test.go@@ -1,6 +1,7 @@package monitoringimport (+ "strings""testing""time"@@ -63,3 +64,46 @@ func TestMetricsHistoryResetClearsMetricWindowCache(t *testing.T) {t.Fatalf("metric window cache survived reset: %+v", history.metricWindowCache)}}++func TestMetricWindowPointsKeepsHistorySnapshotDuringReplacement(t *testing.T) {+ now := time.Now().UTC()+ monitor := newChartFallbackTestMonitor(t)+ history := monitor.metricsHistory+ cacheKey := strings.Join([]string{"vm", "vm-1", "cpu", "300"}, "\x00")+ history.metricWindowCache = map[string]metricWindowCacheEntry{+ cacheKey: {+ points: []alerts.MetricWindowPoint{{Timestamp: now.Add(-time.Minute), Value: 42}},+ expiresAt: now.Add(time.Minute),+ },+ }++ history.metricWindowMu.Lock()+ result := make(chan []alerts.MetricWindowPoint, 1)+ go func() {+ points, _ := monitor.metricWindowPoints(alerts.MetricWindowRequest{+ ResourceID: "vm-1",+ ResourceType: "vm",+ Metric: "cpu",+ Start: now.Add(-5 * time.Minute),+ End: now,+ })+ result <- points+ }()++ // Give the request time to snapshot history and block on its cache mutex,+ // matching the mock seed replacement that exposed the startup panic.+ time.Sleep(20 * time.Millisecond)+ monitor.mu.Lock()+ monitor.metricsHistory = NewMetricsHistory(32, time.Hour)+ monitor.mu.Unlock()+ history.metricWindowMu.Unlock()++ select {+ case points := <-result:+ if len(points) != 1 || points[0].Value != 42 {+ t.Fatalf("metricWindowPoints = %+v, want cached point from the request snapshot", points)+ }+ case <-time.After(time.Second):+ t.Fatal("metricWindowPoints did not finish after history replacement")+ }+}