|
19 | 19 | from tests import unittest
|
20 | 20 |
|
21 | 21 |
|
| 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 | + |
22 | 44 | class TestMauLimit(unittest.TestCase):
|
23 | 45 | def test_basic(self):
|
24 | 46 | gauge = InFlightGauge(
|
@@ -75,7 +97,7 @@ def get_metrics_from_gauge(self, gauge):
|
75 | 97 | for r in gauge.collect():
|
76 | 98 | results[r.name] = {
|
77 | 99 | 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) |
79 | 101 | }
|
80 | 102 |
|
81 | 103 | return results
|
0 commit comments