Skip to content

Commit

Permalink
Allow Prometheus parser to handle missing space
Browse files Browse the repository at this point in the history
If there is no delimiter separating the labels and value when parsing
the Prometheus format the parser currently will chop off the first digit
of the value. Instead, allow a missing delimiter in the Prometheus
format to align with what the Go parser does.

In addition, add a test to specifically check that a delimiter is still
required in OpenMetrics.

Signed-off-by: Chris Marchbanks <csmarchbanks@gmail.com>
  • Loading branch information
csmarchbanks committed Dec 21, 2022
1 parent d6e08e3 commit ff23948
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions prometheus_client/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ def _parse_sample(text):
name = text[:label_start].strip()
# We ignore the starting curly brace
label = text[label_start + 1:label_end]
# The value is after the label end (ignoring curly brace and space)
value, timestamp = _parse_value_and_timestamp(text[label_end + 2:])
# The value is after the label end (ignoring curly brace)
value, timestamp = _parse_value_and_timestamp(text[label_end + 1:])
return Sample(name, _parse_labels(label), value, timestamp)

# We don't have labels
Expand Down
2 changes: 2 additions & 0 deletions tests/openmetrics/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,8 @@ def test_invalid_input(self):
('a{a""} 1\n# EOF\n'),
('a{a=} 1\n# EOF\n'),
('a{a="} 1\n# EOF\n'),
# Missing delimiters.
('a{a="1"}1\n# EOF\n'),
# Missing or extra commas.
('a{a="1"b="2"} 1\n# EOF\n'),
('a{a="1",,b="2"} 1\n# EOF\n'),
Expand Down
2 changes: 2 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,15 @@ def test_spaces(self):
a { foo = "buz" } 3
a\t { \t foo\t = "biz"\t } \t 4
a \t{\t foo = "boz"\t}\t 5
a{foo="bez"}6
""")
metric_family = CounterMetricFamily("a", "help", labels=["foo"])
metric_family.add_metric(["bar"], 1)
metric_family.add_metric(["baz"], 2)
metric_family.add_metric(["buz"], 3)
metric_family.add_metric(["biz"], 4)
metric_family.add_metric(["boz"], 5)
metric_family.add_metric(["bez"], 6)
self.assertEqualMetrics([metric_family], list(families))

def test_commas(self):
Expand Down

0 comments on commit ff23948

Please sign in to comment.