Skip to content

Commit a121061

Browse files
committed
fix(metrics): skip histogram emission when count is missing
1 parent 43319be commit a121061

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

internal/metrics/exporters/prometheus.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ func (c *forgeCollector) emitHistogram(ch chan<- prometheus.Metric, fqName strin
154154
cumulative[b] = running
155155
}
156156

157-
count, _ := toUint64(v["count"])
157+
count, countOK := toUint64(v["count"])
158+
if !countOK {
159+
return
160+
}
158161
sum, _ := toFloat(v["sum"])
159162

160163
keys, vals := sortedLabels(labels)

internal/metrics/exporters/prometheus_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ func TestBridge_GatherTextHasNoTimestamps(t *testing.T) {
4949
}
5050
}
5151

52+
func TestBridge_HistogramMissingCountSkipped(t *testing.T) {
53+
snapshot := func() map[string]any {
54+
return map[string]any{
55+
"some_hist": map[string]any{
56+
// "count" key is intentionally absent
57+
"sum": float64(7.5),
58+
"buckets": map[float64]uint64{
59+
0.1: 1,
60+
0.5: 2,
61+
},
62+
},
63+
}
64+
}
65+
b := NewPrometheusBridge(snapshot, PrometheusConfig{Namespace: ""})
66+
67+
if n := testutil.CollectAndCount(b.collector); n != 0 {
68+
t.Fatalf("expected 0 metrics when count is missing, got %d", n)
69+
}
70+
}
71+
5272
func TestBridge_Histogram(t *testing.T) {
5373
snapshot := func() map[string]any {
5474
return map[string]any{

0 commit comments

Comments
 (0)