Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ dist
.coverage
.tox
.*cache
htmlcov
13 changes: 9 additions & 4 deletions prometheus_client/openmetrics/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import unicode_literals

import math
import sys

from .. import core

Expand Down Expand Up @@ -112,11 +113,16 @@ def _parse_labels(it, text):
elif char == '"':
if not core._METRIC_LABEL_NAME_RE.match(''.join(labelname)):
raise ValueError("Invalid line: " + text)
labels[''.join(labelname)] = ''.join(labelvalue)
utf8_str = ''.join(labelvalue)
try:
utf8_str.encode('utf-8')
except UnicodeError:
raise ValueError("Invalid line: " + text)
labels[''.join(labelname)] = utf8_str
labelname = []
labelvalue = []
state = 'endoflabelvalue'
else:
else:
labelvalue.append(char)
elif state == 'endoflabelvalue':
if char == ',':
Expand All @@ -133,7 +139,7 @@ def _parse_labels(it, text):
labelvalue.append('\n')
elif char == '"':
labelvalue.append('"')
else:
else:
labelvalue.append('\\' + char)
elif state == 'endoflabels':
if char == ' ':
Expand All @@ -142,7 +148,6 @@ def _parse_labels(it, text):
raise ValueError("Invalid line: " + text)
return labels


def _parse_sample(text):
name = []
value = []
Expand Down
17 changes: 17 additions & 0 deletions tests/openmetrics/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,23 @@ def test_labels_with_curly_braces(self):
metric_family.add_metric(["bar", "b{a}z"], 1)
self.assertEqual([metric_family], list(families))

def test_labels_with_invalid_utf8_values(self):
try:
if sys.version_info >= (3,):
inj = u'\uD802'
else:
inj = b'\xef'
str = '''# TYPE a counter
# HELP a help
a_total{foo="'''+inj+'''",bar="baz"} 1
# EOF
'''
families = text_string_to_metric_families(str)
for f in families: pass
assert False
except ValueError:
assert True

def test_empty_help(self):
families = text_string_to_metric_families("""# TYPE a counter
# HELP a
Expand Down