8000 Merge pull request #133 from prometheus/parse-comma · sonlinux/client_python@fc6aa20 · GitHub
[go: up one dir, main page]

Skip to content

Commit fc6aa20

Browse files
authored
Merge pull request prometheus#133 from prometheus/parse-comma
Support trailing commas in parser
2 parents 67fe14a + 434302b commit fc6aa20

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

prometheus_client/parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def _parse_sample(text):
7171
value.append(char)
7272
state = 'value'
7373
elif state == 'startoflabelname':
74-
if char == ' ' or char == '\t':
74+
if char == ' ' or char == '\t' or char == ',':
7575
pass
7676
elif char == '}':
7777
state = 'endoflabels'
@@ -121,7 +121,7 @@ def _parse_sample(text):
121121
labelvalue.append('\\' + char)
122122
elif state == 'nextlabel':
123123
if char == ',':
124-
state = 'labelname'
124+
state = 'startoflabelname'
125125
elif char == '}':
126126
state = 'endoflabels'
127127
elif char == ' ' or char == '\t':

tests/test_parser.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,26 @@ def test_spaces(self):
135135
metric_family.add_metric(["baz"], 2)
136136
self.assertEqual([metric_family], list(families))
137137

138+
def test_commas(self):
139+
families = text_string_to_metric_families("""# TYPE a counter
140+
# HELP a help
141+
a{foo="bar",} 1
142+
# TYPE b counter
143+
# HELP b help
144+
b{,} 2
145+
""")
146+
a = CounterMetricFamily("a", "help", labels=["foo"])
147+
a.add_metric(["bar"], 1)
148+
b = CounterMetricFamily("b", "help", value=2)
149+
self.assertEqual([a, b], list(families))
150+
151+
def test_empty_brackets(self):
152+
families = text_string_to_metric_families("""# TYPE a counter
153+
# HELP a help
154+
a{} 1
155+
""")
156+
self.assertEqual([CounterMetricFamily("a", "help", value=1)], list(families))
157+
138158
def test_nan(self):
139159
families = text_string_to_metric_families("""a NaN
140160
""")

0 commit comments

Comments
 (0)
0