Skip to content

BugFix: Skip validating and parsing comment lines early (#1108) #1109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions prometheus_client/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ def build_metric(name: str, documentation: str, typ: str, samples: List[Sample])
continue
candidate_name, quoted = '', False
if len(parts) > 2:
# Ignore comment tokens
if parts[1] != 'TYPE' and parts[1] != 'HELP':
continue
candidate_name, quoted = _unquote_unescape(parts[2])
if not quoted and not _is_valid_legacy_metric_name(candidate_name):
raise ValueError
Expand Down Expand Up @@ -342,9 +345,6 @@ def build_metric(name: str, documentation: str, typ: str, samples: List[Sample])
'histogram': ['_count', '_sum', '_bucket'],
}.get(typ, [''])
allowed_names = [name + n for n in allowed_names]
else:
# Ignore other comment tokens
pass
elif line == '':
# Ignore blank lines
pass
Expand Down
11 changes: 11 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ def test_blank_lines_and_comments(self):
""")
self.assertEqualMetrics([CounterMetricFamily("a", "help", value=1)], list(families))


def test_comments_parts_are_not_validated_against_legacy_metric_name(self):
# https://github.com/prometheus/client_python/issues/1108
families = text_string_to_metric_families("""
# A simple. comment line where third token cannot be matched against METRIC_NAME_RE under validation.py
# 3565 12345/4436467 another random comment line where third token cannot be matched against METRIC_NAME_RE under validation.py
""")
self.assertEqualMetrics([], list(families))



def test_tabs(self):
families = text_string_to_metric_families("""#\tTYPE\ta\tcounter
#\tHELP\ta\thelp
Expand Down