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.

Coalesce duplicate metrics write batches

rcourtman1019235b8830
diff --git a/pkg/metrics/store.go b/pkg/metrics/store.go
index 7a7e6c745a..efc9966980 100644
--- a/pkg/metrics/store.go
+++ b/pkg/metrics/store.go
@@ -135,6 +135,14 @@ type bufferedMetric struct {
tier Tier
}
+type metricBatchKey struct {
+ resourceType string
+ resourceID string
+ metricType string
+ timestampUnix int64
+ tier Tier
+}
+
// WriteMetric represents a metric sample to be written synchronously.
type WriteMetric struct {
ResourceType string
@@ -661,6 +669,18 @@ func (s *Store) writeBatch(metrics []bufferedMetric) {
return
}
+ inputCount := len(metrics)
+ metrics = coalesceMetricBatch(metrics)
+ if len(metrics) == 0 {
+ return
+ }
+ if len(metrics) < inputCount {
+ log.Debug().
+ Int("input_count", inputCount).
+ Int("count", len(metrics)).
+ Msg("Coalesced duplicate metrics before write")
+ }
+
var tx *pdb.InstrumentedTx
var err error
@@ -725,7 +745,34 @@ func (s *Store) writeBatch(metrics []bufferedMetric) {
return
}
- log.Debug().Int("count", len(metrics)).Msg("Wrote metrics batch")
+ log.Debug().Int("input_count", inputCount).Int("count", len(metrics)).Msg("Wrote metrics batch")
+}
+
+func coalesceMetricBatch(metrics []bufferedMetric) []bufferedMetric {
+ if len(metrics) < 2 {
+ return metrics
+ }
+
+ indexes := make(map[metricBatchKey]int, len(metrics))
+ coalesced := make([]bufferedMetric, 0, len(metrics))
+ for _, metric := range metrics {
+ key := metricBatchKey{
+ resourceType: metric.resourceType,
+ resourceID: metric.resourceID,
+ metricType: metric.metricType,
+ timestampUnix: metric.timestamp.Unix(),
+ tier: metric.tier,
+ }
+ if index, ok := indexes[key]; ok {
+ coalesced[index] = metric
+ continue
+ }
+
+ indexes[key] = len(coalesced)
+ coalesced = append(coalesced, metric)
+ }
+
+ return coalesced
}
// coalesceQueuedBatches drains any already-queued write batches so the worker
diff --git a/pkg/metrics/store_additional_test.go b/pkg/metrics/store_additional_test.go
index 5805b3d416..3d7df634ab 100644
--- a/pkg/metrics/store_additional_test.go
+++ b/pkg/metrics/store_additional_test.go
@@ -157,6 +157,39 @@ func TestStoreWriteBatchUpsertsDuplicateRawMetric(t *testing.T) {
}
}
+func TestCoalesceMetricBatchReducesPreSQLCardinalityAndPreservesLatestValue(t *testing.T) {
+ ts := time.Unix(1700000000, 123)
+ batch := []bufferedMetric{
+ {resourceType: "agent", resourceID: "host-1", metricType: "cpu", value: 10, timestamp: ts, tier: TierRaw},
+ {resourceType: "agent", resourceID: "host-1", metricType: "cpu", value: 20, timestamp: ts.Add(500 * time.Millisecond), tier: TierRaw},
+ {resourceType: "agent", resourceID: "host-1", metricType: "memory", value: 30, timestamp: ts, tier: TierRaw},
+ {resourceType: "agent", resourceID: "host-1", metricType: "cpu", value: 40, timestamp: ts, tier: TierMinute},
+ {resourceType: "agent", resourceID: "host-1", metricType: "cpu", value: 50, timestamp: ts.Add(time.Second), tier: TierRaw},
+ }
+
+ coalesced := coalesceMetricBatch(batch)
+ if len(coalesced) != 4 {
+ t.Fatalf("expected pre-SQL batch cardinality to drop from %d to 4, got %d: %+v", len(batch), len(coalesced), coalesced)
+ }
+
+ foundRawCPU := false
+ for _, metric := range coalesced {
+ if metric.resourceType == "agent" &&
+ metric.resourceID == "host-1" &&
+ metric.metricType == "cpu" &&
+ metric.timestamp.Unix() == ts.Unix() &&
+ metric.tier == TierRaw {
+ foundRawCPU = true
+ if metric.value != 20 {
+ t.Fatalf("expected latest duplicate raw CPU value to win before SQL execution, got %+v", metric)
+ }
+ }
+ }
+ if !foundRawCPU {
+ t.Fatalf("expected coalesced batch to retain raw CPU sample: %+v", coalesced)
+ }
+}
+
func TestResolveStoreDBPathCanonicalizesOwnedPath(t *testing.T) {
root := t.TempDir()
rawPath := filepath.Join(root, "metrics", "..", "metrics", "metrics.db")