8000 Check for exemplars being too long. · ethervoid/client_python@61480b2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 61480b2

Browse files
committed
Check for exemplars being too long.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
1 parent e8c3daa commit 61480b2

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

prometheus_client/openmetrics/parser.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ def _parse_sample(text):
217217
ts = _parse_timestamp(timestamp)
218218
exemplar = None
219219
if exemplar_labels is not None:
220+
exemplar_length = sum([len(k) + len(v) + 3 for k, v in exemplar_labels.items()]) + 2
221+
if exemplar_length > 64:
222+
raise ValueError("Exmplar labels are too long: " + text)
220223
exemplar = core.Exemplar(exemplar_labels,
221224
_parse_value(exemplar_value),
222225
_parse_timestamp(exemplar_timestamp))

tests/openmetrics/test_parser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ def test_histogram_exemplars(self):
122122
# HELP a help
123123
a_bucket{le="1"} 0 # {a="b"} 0.5
124124
a_bucket{le="2"} 2 123 # {a="c"} 0.5
125-
a_bucket{le="+Inf"} 3 # {a="d"} 4 123
125+
a_bucket{le="+Inf"} 3 # {a="1234567890123456789012345678901234567890123456789012345678"} 4 123
126126
# EOF
127127
""")
128128
hfm = HistogramMetricFamily("a", "help")
129129
hfm.add_sample("a_bucket", {"le": "1"}, 0.0, None, Exemplar({"a": "b"}, 0.5))
130130
hfm.add_sample("a_bucket", {"le": "2"}, 2.0, Timestamp(123, 0), Exemplar({"a": "c"}, 0.5)),
131-
hfm.add_sample("a_bucket", {"le": "+Inf"}, 3.0, None, Exemplar({"a": "d"}, 4, Timestamp(123, 0)))
131+
hfm.add_sample("a_bucket", {"le": "+Inf"}, 3.0, None, Exemplar({"a": "1234567890123456789012345678901234567890123456789012345678"}, 4, Timestamp(123, 0)))
132132
self.assertEqual([hfm], list(families))
133133

134134
def test_simple_gaugehistogram(self):
@@ -484,6 +484,7 @@ def test_invalid_input(self):
484484
('# TYPE a histogram\na_bucket{le="+Inf"} 1 # {}1\n# EOF\n'),
485485
('# TYPE a histogram\na_bucket{le="+Inf"} 1 # {} 1 \n# EOF\n'),
486486
('# TYPE a histogram\na_bucket{le="+Inf"} 1 # {} 1 1 \n# EOF\n'),
487+
('# TYPE a histogram\na_bucket{le="+Inf"} 1 # {a="12345678901234567890123456789012345678901234567890123456789"} 1 1\n# EOF\n'),
487488
# Exemplars on unallowed s 43C5 amples.
488489
('# TYPE a histogram\na_sum 1 # {a="b"} 0.5\n# EOF\n'),
489490
('# TYPE a gaugehistogram\na_sum 1 # {a="b"} 0.5\n# EOF\n'),

0 commit comments

Comments
 (0)
0