8000 Allow Prometheus parser to handle missing space · CloudBee7/client_python@ff23948 · GitHub
[go: up one dir, main page]

Skip to content

Commit ff23948

Browse files
committed
Allow Prometheus parser to handle missing space
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>
1 parent d6e08e3 commit ff23948

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

prometheus_client/parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ def _parse_sample(text):
116116
name = text[:label_start].strip()
117117
# We ignore the starting curly brace
118118
label = text[label_start + 1:label_end]
119-
# The value is after the label end (ignoring curly brace and space)
120-
value, timestamp = _parse_value_and_timestamp(text[label_end + 2:])
119+
# The value is after the label end (ignoring curly brace)
120+
value, timestamp = _parse_value_and_timestamp(text[label_end + 1:])
121121
return Sample(name, _parse_labels(label), value, timestamp)
122122

123123
# We don't have labels

tests/openmetrics/test_parser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,8 @@ def test_invalid_input(self):
642642
('a{a""} 1\n# EOF\n'),
643643
('a{a=} 1\n# EOF\n'),
644644
('a{a="} 1\n# EOF\n'),
645+
# Missing delimiters.
646+
('a{a="1"}1\n# EOF\n'),
645647
# Missing or extra commas.
646648
('a{a="1"b="2"} 1\n# EOF\n'),
647649
('a{a="1",,b="2"} 1\n# EOF\n'),

tests/test_parser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,15 @@ def test_spaces(self):
146146
a { foo = "buz" } 3
147147
a\t { \t foo\t = "biz"\t } \t 4
148148
a \t{\t foo = "boz"\t}\t 5
149+
a{foo="bez"}6
149150
""")
150151
metric_family = CounterMetricFamily("a", "help", labels=["foo"])
151152
metric_family.add_metric(["bar"], 1)
152153
metric_family.add_metric(["baz"], 2)
153154
metric_family.add_metric(["buz"], 3)
154155
metric_family.add_metric(["biz"], 4)
155156
metric_family.add_metric(["boz"], 5)
157+
metric_family.add_metric(["bez"], 6)
156158
self.assertEqualMetrics([metric_family], list(families))
157159

158160
def test_commas(self):

0 commit comments

Comments
 (0)
0