Skip to content

Commit 2e26e2f

Browse files
committed
Require sum with count for histograms.
This is so the average can be calculated. Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
1 parent 0e72c17 commit 2e26e2f

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

prometheus_client/metrics_core.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,11 @@ def add_metric(self, labels, buckets, sum_value, timestamp=None):
216216
timestamp,
217217
exemplar,
218218
))
219-
# +Inf is last and provides the count value.
220-
self.samples.append(
221-
Sample(self.name + '_count', dict(zip(self._labelnames, labels)), buckets[-1][1], timestamp))
222-
# Don't iunclude sum if there's negative buckets.
219+
# Don't iunclude sum and thus count if there's negative buckets.
223220
if float(buckets[0][0]) >= 0 and sum_value is not None:
221+
# +Inf is last and provides the count value.
222+
self.samples.append(
223+
Sample(self.name + '_count', dict(zip(self._labelnames, labels)), buckets[-1][1], timestamp))
224224
self.samples.append(
225225
Sample(self.name + '_sum', dict(zip(self._labelnames, labels)), sum_value, timestamp))
226226

prometheus_client/openmetrics/parser.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,12 @@ def do_checks():
398398
raise ValueError("+Inf bucket missing: " + name)
399399
if count is not None and value != count:
400400
raise ValueError("Count does not match +Inf value: " + name)
401+
if has_sum and count is None:
402+
raise ValueError("_count must be present if _sum is present: " + name)
403+
if has_gsum and count is None:
404+
raise ValueError("_gcount must be present if _gsum is present: " + name)
405+
if not (has_sum or has_gsum) and count is not None:
406+
raise ValueError("_sum/_gsum must be present if _count is present: " + name)
401407
if has_negative_buckets and has_sum:
402408
raise ValueError("Cannot have _sum with negative buckets: " + name)
403409
if not has_negative_buckets and has_negative_gsum:
@@ -413,6 +419,7 @@ def do_checks():
413419
bucket = None
414420
has_negative_buckets = False
415421
has_sum = False
422+
has_gsum = False
416423
has_negative_gsum = False
417424
value = 0
418425
group = g
@@ -432,8 +439,10 @@ def do_checks():
432439
count = s.value
433440
elif suffix in ['_sum']:
434441
has_sum = True
435-
elif suffix in ['_gsum'] and s.value < 0:
436-
has_negative_gsum = True
442+
elif suffix in ['_gsum']:
443+
has_gsum = True
444+
if s.value < 0:
445+
has_negative_gsum = True
437446

438447
if group is not None:
439448
do_checks()

tests/openmetrics/test_parser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ def test_negative_bucket_histogram(self):
142142
a_bucket{le="-1.0"} 0
143143
a_bucket{le="1.0"} 1
144144
a_bucket{le="+Inf"} 3
145-
a_count 3
146145
# EOF
147146
""")
148147
self.assertEqual([HistogramMetricFamily("a", "help", buckets=[("-1.0", 0.0), ("1.0", 1.0), ("+Inf", 3.0)])],
@@ -744,7 +743,11 @@ def test_invalid_input(self):
744743
('# TYPE a summary\na{quantile="0.5"} -1\n# EOF\n'),
745744
# Bad histograms.
746745
('# TYPE a histogram\na_sum 1\n# EOF\n'),
746+
('# TYPE a histogram\na_bucket{le="+Inf"} 0\n#a_sum 0\n# EOF\n'),
747+
('# TYPE a histogram\na_bucket{le="+Inf"} 0\n#a_count 0\n# EOF\n'),
747748
('# TYPE a gaugehistogram\na_gsum 1\n# EOF\n'),
749+
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 0\na_gsum 0\n# EOF\n'),
750+
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 0\na_gcount 0\n# EOF\n'),
748751
('# TYPE a histogram\na_count 1\na_bucket{le="+Inf"} 0\n# EOF\n'),
749752
('# TYPE a histogram\na_bucket{le="+Inf"} 0\na_count 1\n# EOF\n'),
750753
('# TYPE a histogram\na_bucket{le="0"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'),

0 commit comments

Comments
 (0)