8000 Check for negative counter-like, guage histogram, and quantile values… · lweith/client_python@752c7bf · GitHub
[go: up one dir, main page]

Skip to content

Commit 752c7bf

Browse files
authored
Check for negative counter-like, guage histogram, and quantile values. (prometheus#338)
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
1 parent 624bb61 commit 752c7bf

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

prometheus_client/openmetrics/parser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,12 @@ def build_metric(name, documentation, typ, unit, samples):
433433
raise ValueError("Stateset samples can only have values zero and one: " + line)
434434
if typ == 'info' and sample.value != 1:
435435
raise ValueError("Info samples can only have value one: " + line)
436-
if sample.name[len(name):] in ['_total', '_sum', '_count', '_bucket'] and math.isnan(sample.value):
436+
if typ == 'summary' and name == sample.name and sample.value < 0:
437+
raise ValueError("Quantile values cannot be negative: " + line)
438+
if sample.name[len(name):] in ['_total', '_sum', '_count', '_bucket', '_gcount', '_gsum'] and math.isnan(sample.value):
437439
raise ValueError("Counter-like samples cannot be NaN: " + line)
440+
if sample.name[len(name):] in ['_total', '_sum', '_count', '_bucket', '_gcount', '_gsum'] and sample.value < 0:
441+
raise ValueError("Counter-like samples cannot be negative: " + line)
438442
if sample.exemplar and not (
439443
typ in ['histogram', 'gaugehistogram']
440444
and sample.name.endswith('_bucket')):

tests/openmetrics/test_parser.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,13 @@ def test_empty_help(self):
248248
self.assertEqual([CounterMetricFamily("a", "", value=1)], list(families))
249249

250250
def test_labels_and_infinite(self):
251-
families = text_string_to_metric_families("""# TYPE a counter
251+
families = text_string_to_metric_families("""# TYPE a gauge
252252
# HELP a help
253-
a_total{foo="bar"} +Inf
254-
a_total{foo="baz"} -Inf
253+
a{foo="bar"} +Inf
254+
a{foo="baz"} -Inf
255255
# EOF
256256
""")
257-
metric_family = CounterMetricFamily("a", "help", labels=["foo"])
257+
metric_family = GaugeMetricFamily("a", "help", labels=["foo"])
258258
metric_family.add_metric(["bar"], float('inf'))
259259
metric_family.add_metric(["baz"], float('-inf'))
260260
self.assertEqual([metric_family], list(families))
@@ -527,12 +527,23 @@ def test_invalid_input(self):
527527
('# TYPE a stateset\na 0\n# EOF\n'),
528528
# Bad counter values.
529529
('# TYPE a counter\na_total NaN\n# EOF\n'),
530+
('# TYPE a counter\na_total -1\n# EOF\n'),
530531
('# TYPE a histogram\na_sum NaN\n# EOF\n'),
531532
('# TYPE a histogram\na_count NaN\n# EOF\n'),
532533
('# TYPE a histogram\na_bucket{le="+Inf"} NaN\n# EOF\n'),
534+
('# TYPE a histogram\na_sum -1\n# EOF\n'),
535+
('# TYPE a histogram\na_count -1\n# EOF\n'),
536+
('# TYPE a histogram\na_bucket{le="+Inf"} -1\n# EOF\n'),
533537
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} NaN\n# EOF\n'),
538+
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} -1\na_gcount -1\n# EOF\n'),
539+
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} -1\n# EOF\n'),
540+
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 1\na_gsum -1\n# EOF\n'),
541+
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 1\na_gsum NaN\n# EOF\n'),
534542
('# TYPE a summary\na_sum NaN\n# EOF\n'),
535543
('# TYPE a summary\na_count NaN\n# EOF\n'),
544+
('# TYPE a summary\na_sum -1\n# EOF\n'),
545+
('# TYPE a summary\na_count -1\n# EOF\n'),
546+
('# TYPE a summary\na{quantile="0.5"} -1\n# EOF\n'),
536547
# Bad histograms.
537548
('# TYPE a histogram\na_sum 1\n# EOF\n'),
538549
('# TYPE a gaugehistogram\na_gsum 1\n# EOF\n'),

0 commit comments

Comments
 (0)
0