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

Commit

Permalink
Merge pull request #4317 from de-vri-es/test-metric-prometheus-0.5
Browse files Browse the repository at this point in the history
Fix test_metrics.py compatibility prometheus_client 0.5
  • Loading branch information
richvdh committed Dec 22, 2018
2 parents 597dafb + 48b7ff7 commit 3355c1a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/4317.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix test_metric.py compatibility with prometheus_client 0.5. Contributed by Maarten de Vries <maarten@de-vri.es>.
24 changes: 23 additions & 1 deletion tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@
from tests import unittest


def get_sample_labels_value(sample):
""" Extract the labels and values of a sample.
prometheus_client 0.5 changed the sample type to a named tuple with more
members than the plain tuple had in 0.4 and earlier. This function can
extract the labels and value from the sample for both sample types.
Args:
sample: The sample to get the labels and value from.
Returns:
A tuple of (labels, value) from the sample.
"""

# If the sample has a labels and value attribute, use those.
if hasattr(sample, "labels") and hasattr(sample, "value"):
return sample.labels, sample.value
# Otherwise fall back to treating it as a plain 3 tuple.
else:
_, labels, value = sample
return labels, value


class TestMauLimit(unittest.TestCase):
def test_basic(self):
gauge = InFlightGauge(
Expand Down Expand Up @@ -75,7 +97,7 @@ def get_metrics_from_gauge(self, gauge):
for r in gauge.collect():
results[r.name] = {
tuple(labels[x] for x in gauge.labels): value
for _, labels, value in r.samples
for labels, value in map(get_sample_labels_value, r.samples)
}

return results

0 comments on commit 3355c1a

Please sign in to comment.