Skip to content

Commit 938b73e

Browse files
authored
BugFix: Skip validating and parsing comment lines early (#1108) (#1109)
Signed-off-by: Wissam Abu Ahmad <[email protected]>
1 parent 8dfa10e commit 938b73e

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

prometheus_client/parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ def build_metric(name: str, documentation: str, typ: str, samples: List[Sample])
308308
continue
309309
candidate_name, quoted = '', False
310310
if len(parts) > 2:
311+
# Ignore comment tokens
312+
if parts[1] != 'TYPE' and parts[1] != 'HELP':
313+
continue
311314
candidate_name, quoted = _unquote_unescape(parts[2])
312315
if not quoted and not _is_valid_legacy_metric_name(candidate_name):
313316
raise ValueError
@@ -342,9 +345,6 @@ def build_metric(name: str, documentation: str, typ: str, samples: List[Sample])
342345
'histogram': ['_count', '_sum', '_bucket'],
343346
}.get(typ, [''])
344347
allowed_names = [name + n for n in allowed_names]
345-
else:
346-
# Ignore other comment tokens
347-
pass
348348
elif line == '':
349349
# Ignore blank lines
350350
pass

tests/test_parser.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ def test_blank_lines_and_comments(self):
120120
""")
121121
self.assertEqualMetrics([CounterMetricFamily("a", "help", value=1)], list(families))
122122

123+
124+
def test_comments_parts_are_not_validated_against_legacy_metric_name(self):
125+
# https://github.com/prometheus/client_python/issues/1108
126+
families = text_string_to_metric_families("""
127+
# A simple. comment line where third token cannot be matched against METRIC_NAME_RE under validation.py
128+
# 3565 12345/4436467 another random comment line where third token cannot be matched against METRIC_NAME_RE under validation.py
129+
""")
130+
self.assertEqualMetrics([], list(families))
131+
132+
133+
123134
def test_tabs(self):
124135
families = text_string_to_metric_families("""#\tTYPE\ta\tcounter
125136
#\tHELP\ta\thelp

0 commit comments

Comments
 (0)