Skip to content

Commit

Permalink
Check for negative counter-like and guage histogram values.
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Brazil <[email protected]>
  • Loading branch information
brian-brazil committed Nov 5, 2018
1 parent 1fbb1f3 commit 244ba2e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
4 changes: 3 additions & 1 deletion prometheus_client/openmetrics/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,10 @@ def build_metric(name, documentation, typ, unit, samples):
raise ValueError("Stateset samples can only have values zero and one: " + line)
if typ == 'info' and sample.value != 1:
raise ValueError("Info samples can only have value one: " + line)
if sample.name[len(name):] in ['_total', '_sum', '_count', '_bucket'] and math.isnan(sample.value):
if sample.name[len(name):] in ['_total', '_sum', '_count', '_bucket', '_gcount', '_gsum'] and math.isnan(sample.value):
raise ValueError("Counter-like samples cannot be NaN: " + line)
if sample.name[len(name):] in ['_total', '_sum', '_count', '_bucket', '_gcount', '_gsum'] and sample.value < 0:
raise ValueError("Counter-like samples cannot be negative: " + line)
if sample.exemplar and not (
typ in ['histogram', 'gaugehistogram']
and sample.name.endswith('_bucket')):
Expand Down
18 changes: 14 additions & 4 deletions tests/openmetrics/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,13 @@ def test_empty_help(self):
self.assertEqual([CounterMetricFamily("a", "", value=1)], list(families))

def test_labels_and_infinite(self):
families = text_string_to_metric_families("""# TYPE a counter
families = text_string_to_metric_families("""# TYPE a gauge
# HELP a help
a_total{foo="bar"} +Inf
a_total{foo="baz"} -Inf
a{foo="bar"} +Inf
a{foo="baz"} -Inf
# EOF
""")
metric_family = CounterMetricFamily("a", "help", labels=["foo"])
metric_family = GaugeMetricFamily("a", "help", labels=["foo"])
metric_family.add_metric(["bar"], float('inf'))
metric_family.add_metric(["baz"], float('-inf'))
self.assertEqual([metric_family], list(families))
Expand Down Expand Up @@ -535,12 +535,22 @@ def test_invalid_input(self):
('# TYPE a stateset\na 0\n# EOF\n'),
# Bad counter values.
('# TYPE a counter\na_total NaN\n# EOF\n'),
('# TYPE a counter\na_total -1\n# EOF\n'),
('# TYPE a histogram\na_sum NaN\n# EOF\n'),
('# TYPE a histogram\na_count NaN\n# EOF\n'),
('# TYPE a histogram\na_bucket{le="+Inf"} NaN\n# EOF\n'),
('# TYPE a histogram\na_sum -1\n# EOF\n'),
('# TYPE a histogram\na_count -1\n# EOF\n'),
('# TYPE a histogram\na_bucket{le="+Inf"} -1\n# EOF\n'),
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} NaN\n# EOF\n'),
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} -1\na_gcount -1\n# EOF\n'),
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} -1\n# EOF\n'),
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 1\na_gsum -1\n# EOF\n'),
('# TYPE a gaugehistogram\na_bucket{le="+Inf"} 1\na_gsum NaN\n# EOF\n'),
('# TYPE a summary\na_sum NaN\n# EOF\n'),
('# TYPE a summary\na_count NaN\n# EOF\n'),
('# TYPE a summary\na_sum -1\n# EOF\n'),
('# TYPE a summary\na_count -1\n# EOF\n'),
# Bad histograms.
('# TYPE a histogram\na_sum 1\n# EOF\n'),
('# TYPE a gaugehistogram\na_gsum 1\n# EOF\n'),
Expand Down

0 comments on commit 244ba2e

Please sign in to comment.