8000 Check exemplars are only where they're meant to be. · ethervoid/client_python@95c7285 · GitHub
[go: up one dir, main page]

Skip to content

Commit 95c7285

Browse files
committed
Check exemplars are only where they're meant to be.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
1 parent 8a4a65c commit 95c7285

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

prometheus_client/openmetrics/parser.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ def build_metric(name, documentation, typ, unit, samples):
250250
raise ValueError("Units not allowed for this metric type: " + name)
251251
metric = core.Metric(name, documentation, typ, unit)
252252
# TODO: check labelvalues are valid utf8
253-
# TODO: check only histogram buckets have exemplars.
254253
# TODO: check samples are appropriately grouped and ordered
255254
# TODO: check info/stateset values are 1/0
256255
# TODO: check for metadata in middle of samples
@@ -320,18 +319,22 @@ def build_metric(name, documentation, typ, unit, samples):
320319
raise ValueError("Invalid line: " + line)
321320
else:
322321
sample = _parse_sample(line)
323-
if sample[0] not in allowed_names:
322+
if sample.name not in allowed_names:
324323
if name != '':
325324
yield build_metric(name, documentation, typ, unit, samples)
326325
# Start an untyped metric.
327-
name = sample[0]
326+
name = sample.name
328327
documentation = ''
329328
unit = ''
330329
typ = 'untyped'
331330
samples = [sample]
332-
allowed_names = [sample[0]]
331+
allowed_names = [sample.name]
333332
else:
334333
samples.append(sample)
334+
if sample.exemplar and not (
335+
typ in ['histogram', 'gaugehistogram']
336+
and sample.name.endswith('_bucket')):
337+
raise ValueError("Invalid line only histogram/gaugehistogram buckets can have exemplars: " + line)
335338

336339
if name != '':
337340
yield build_metric(name, documentation, typ, unit, samples)

tests/openmetrics/test_parser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def test_simple_gaugehistogram(self):
134134
""")
135135
self.assertEqual([GaugeHistogramMetricFamily("a", "help", gsum_value=2, buckets=[("1", 0.0), ("+Inf", 3.0)])], list(families))
136136

137-
def test_histogram_exemplars(self):
137+
def test_gaugehistogram_exemplars(self):
138138
families = text_string_to_metric_families("""# TYPE a gaugehistogram
139139
# HELP a help
140140
a_bucket{le="1"} 0 # {a="b"} 0.5
@@ -451,6 +451,10 @@ def test_invalid_input(self):
451451
('# TYPE a histogram\na_bucket{le="+Inf"} 1 # {}1\n# EOF\n'),
452452
('# TYPE a histogram\na_bucket{le="+Inf"} 1 # {} 1 \n# EOF\n'),
453453
('# TYPE a histogram\na_bucket{le="+Inf"} 1 # {} 1 1 \n# EOF\n'),
454+
# Exemplars on unallowed samples.
455+
('# TYPE a histogram\na_sum 1 # {a="b"} 0.5\n# EOF\n'),
456+
('# TYPE a gaugehistogram\na_sum 1 # {a="b"} 0.5\n# EOF\n'),
457+
('# TYPE a_bucket gauge\na_bucket 1 # {a="b"} 0.5\n# EOF\n'),
454458
]:
455459
with self.assertRaises(ValueError):
456460
list(text_string_to_metric_families(case))

0 commit comments

Comments
 (0)
0