From 5a41f0c2b65f2270b19852c009e646fa8ab99a91 Mon Sep 17 00:00:00 2001 From: Deshi Xiao Date: Sat, 18 Oct 2025 17:05:41 +0800 Subject: [PATCH] Fix metrics disappearing when label schema changes in aggregation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes issue #224 where metrics would disappear from the repo's aggregated /metrics endpoint after updating metric definitions to add or remove labels. **Root Cause:** In MetricDataSum::sum(), the schema validation (type, shape, dimensions) was only performed when initial=true. This meant that when a metric's label structure changed at runtime (e.g., from ['label_one'] to ['label_one', 'label_two']), the shape mismatch was not detected during regular aggregation cycles. **The Bug (lines 832-836):** ```cpp if (!ent || (initial && ( ent->type != type || ent->shape != shape || ent->dimensions != e->dimensions ))) { ``` When initial=false, schema changes were silently ignored, causing: - Metrics with new schemas to be aggregated into entries with old schemas - Mismatched dimensions leading to incorrect aggregation - Metrics appearing in worker /metrics but missing from repo /metrics **The Fix:** Always validate schema consistency, not just during initial aggregation: ```cpp if (!ent || ent->type != type || ent->shape != shape || ent->dimensions != e->dimensions ) { ``` Now when a metric's schema changes, the entry is properly recreated with the new schema, ensuring metrics are correctly aggregated and visible in the repo's /metrics endpoint. **Testing:** Reproduced with the test case in issue description: 1. Create metric with one label 2. Update to two labels 3. Verify metric now appears in repo /metrics (previously disappeared) Note: MetricHistory already had the correct implementation (lines 1117-1120, 1171-1174) and did not require changes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/api/stats.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/stats.cpp b/src/api/stats.cpp index 80abc26ae..6c3e0a57d 100644 --- a/src/api/stats.cpp +++ b/src/api/stats.cpp @@ -829,11 +829,11 @@ void MetricDataSum::sum(MetricData &data, bool initial) { auto *shape = pjs::Str::make(e->shape)->retain(); auto &ent = m_entry_map[name]; - if (!ent || (initial && ( + if (!ent || ent->type != type || ent->shape != shape || ent->dimensions != e->dimensions - ))) { + ) { if (!ent) { ent = new Entry; m_entries.push(ent);