Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c158b76

Browse files
committed
Require sum with count for histograms.
This is so the average can be calculated. Signed-off-by: Brian Brazil <[email protected]>
1 parent 5c6af2d commit c158b76

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 include 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
@@ -399,6 +399,12 @@ def do_checks():
399399
raise ValueError("+Inf bucket missing: " + name)
400400
if count is not None and value != count:
401401
raise ValueError("Count does not match +Inf value: " + name)
402+
if has_sum and count is None:
403+
raise ValueError("_count must be present if _sum is present: " + name)
404+
if has_gsum and count is None:
405+
raise ValueError("_gcount must be present if _gsum is present: " + name)
406+
if not (has_sum or has_gsum) and count is not None:
407+
raise ValueError("_sum/_gsum must be present if _count is present: " + name)
402408
if has_negative_buckets and has_sum:
403409
raise ValueError("Cannot have _sum with negative buckets: " + name)
404410
if not has_negative_buckets and has_negative_gsum:
@@ -414,6 +420,7 @@ def do_checks():
414420
bucket = None
415421
has_negative_buckets = False
416422
has_sum = False
423+
has_gsum = False
417424
has_negative_gsum = False
418425
value = 0
419426
group = g
@@ -433,8 +440,10 @@ def do_checks():
433440
count = s.value
434441
elif suffix in ['_sum']:
435442
has_sum = True
436-
elif suffix in ['_gsum'] and s.value < 0:
437-
has_negative_gsum = True
443+
elif suffix in ['_gsum']:
444+
has_gsum = True
445+
if s.value < 0:
446+
has_negative_gsum = True
438447

439448
if group is not None:
440449
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)])],
@@ -745,7 +744,11 @@ def test_invalid_input(self):
745744
('# TYPE a summary\na{quantile="0.5"} -1\n# EOF\n'),
746745
# Bad histograms.
747746
('# TYPE a histogram\na_sum 1\n# EOF\n'),
747+
('# TYPE a histogram\na_bucket{le="+Inf"} 0\n#a_sum 0\n# EOF\n'),
748+
('# TYPE a histogram\na_bucket{le="+Inf"} 0\n#a_count 0\n# EOF\n'),
748749
('# TYPE a gaugehistogram\na_gsum 1\n# EOF\n'),
750+
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 0\na_gsum 0\n# EOF\n'),
751+
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 0\na_gcount 0\n# EOF\n'),
749752
('# TYPE a histogram\na_count 1\na_bucket{le="+Inf"} 0\n# EOF\n'),
750753
('# TYPE a histogram\na_bucket{le="+Inf"} 0\na_count 1\n# EOF\n'),
751754
('# TYPE a histogram\na_bucket{le="0"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'),

0 commit comments

Comments
 (0)