Repository: prometheus/client_python

Issue:
[BUG] Prometheus parser 0.22.0 does not early skip comment lines
**Issue**

Starting with version 0.22.0, comment lines do not skip validation and parsing early enough and third token goes into [_is_valid_legacy_metric_name](https://github.com/prometheus/client_python/blob/master/prometheus_client/validation.py#L52) which results with an error when it does not match the [METRIC_NAME_RE](https://github.com/prometheus/client_python/blob/master/prometheus_client/validation.py#L4)

**Example:**

"""#  A random comment which goes into validation and passes"""
"""#  Another random. comment which goes into validation and fails"""
"""#  Whatever 335/125 random comment which goes into validation and fails"""

**Expected behaviour:**
Skip all comment  lines.  list([text_string_to_metric_families](https://github.com/prometheus/client_python/blob/master/prometheus_client/parser.py#L13)) should result with empty list []

**Current behaviour:**
A [ValueError](https://github.com/prometheus/client_python/blob/master/prometheus_client/parser.py#L313) is raised for the second and third comment lines above

I'll be pushing a PR for the fix

Hints from issue comments:
@wissamir: Fix proposal PR https://github.com/prometheus/client_python/pull/1109

Solution patch:
diff --git a/prometheus_client/parser.py b/prometheus_client/parser.py
index 92d6672..0434edf 100644
--- a/prometheus_client/parser.py
+++ b/prometheus_client/parser.py
@@ -308,6 +308,9 @@ def text_fd_to_metric_families(fd: TextIO) -> Iterable[Metric]:
                 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
@@ -342,9 +345,6 @@ def text_fd_to_metric_families(fd: TextIO) -> Iterable[Metric]:
                     '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


Test patch:
diff --git a/tests/test_parser.py b/tests/test_parser.py
index 10a2fc9..e18a878 100644
--- a/tests/test_parser.py
+++ b/tests/test_parser.py
@@ -120,6 +120,17 @@ a 1
 """)
         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