summary refs log tree commit diff
path: root/tests/test_metrics.py
diff options
context:
space:
mode:
authorMaarten de Vries <maarten@de-vri.es>2018-12-21 01:24:48 +0100
committerMaarten de Vries <maarten@de-vri.es>2018-12-21 01:53:57 +0100
commit48b7ff7a351f585f2f9d61efbeac5bd261bf5255 (patch)
tree8bbb01acb2bddcceb431fc51f24b7a94ca785866 /tests/test_metrics.py
parentMerge branch 'master' into develop (diff)
downloadsynapse-48b7ff7a351f585f2f9d61efbeac5bd261bf5255.tar.xz
Fix test_metrics.py compatibility prometheus_client 0.5
prometheus_client 0.5 has a named-tuple Sample type with more member
than the old plain tuple had. This commit makes sure the unit test
detects this and changes the way it reads the sample.

Signed-off-by: Maarten de Vries <maarten@de-vri.es>
Diffstat (limited to 'tests/test_metrics.py')
-rw-r--r--tests/test_metrics.py24
1 files changed, 23 insertions, 1 deletions
diff --git a/tests/test_metrics.py b/tests/test_metrics.py
index 17897711a1..0ff6d0e283 100644
--- a/tests/test_metrics.py
+++ b/tests/test_metrics.py
@@ -19,6 +19,28 @@ from synapse.metrics import InFlightGauge
 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(
@@ -75,7 +97,7 @@ class TestMauLimit(unittest.TestCase):
         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