Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 3355c1a

Browse files
authored
Merge pull request #4317 from de-vri-es/test-metric-prometheus-0.5
Fix test_metrics.py compatibility prometheus_client 0.5
2 parents 597dafb + 48b7ff7 commit 3355c1a

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

changelog.d/4317.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix test_metric.py compatibility with prometheus_client 0.5. Contributed by Maarten de Vries <[email protected]>.

tests/test_metrics.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,28 @@
1919
from tests import unittest
2020

2121

22+
def get_sample_labels_value(sample):
23+
""" Extract the labels and values of a sample.
24+
25+
prometheus_client 0.5 changed the sample type to a named tuple with more
26+
members than the plain tuple had in 0.4 and earlier. This function can
27+
extract the labels and value from the sample for both sample types.
28+
29+
Args:
30+
sample: The sample to get the labels and value from.
31+
Returns:
32+
A tuple of (labels, value) from the sample.
33+
"""
34+
35+
# If the sample has a labels and value attribute, use those.
36+
if hasattr(sample, "labels") and hasattr(sample, "value"):
37+
return sample.labels, sample.value
38+
# Otherwise fall back to treating it as a plain 3 tuple.
39+
else:
40+
_, labels, value = sample
41+
return labels, value
42+
43+
2244
class TestMauLimit(unittest.TestCase):
2345
def test_basic(self):
2446
gauge = InFlightGauge(
@@ -75,7 +97,7 @@ def get_metrics_from_gauge(self, gauge):
7597
for r in gauge.collect():
7698
results[r.name] = {
7799
tuple(labels[x] for x in gauge.labels): value
78-
for _, labels, value in r.samples
100+
for labels, value in map(get_sample_labels_value, r.samples)
79101
}
80102

81103
return results

0 commit comments

Comments
 (0)