diff options
author | Richard van der Hoff <1389908+richvdh@users.noreply.github.com> | 2018-12-23 00:14:47 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-23 00:14:47 +0100 |
commit | 3355c1a9ec5fcccc2dd6d84405603f062000bed1 (patch) | |
tree | d59d8868e768e191290d652a364c1872fb2d73c8 | |
parent | Merge pull request #4316 from matrix-org/rav/fix_docker_upload (diff) | |
parent | Fix test_metrics.py compatibility prometheus_client 0.5 (diff) | |
download | synapse-3355c1a9ec5fcccc2dd6d84405603f062000bed1.tar.xz |
Merge pull request #4317 from de-vri-es/test-metric-prometheus-0.5
Fix test_metrics.py compatibility prometheus_client 0.5
-rw-r--r-- | changelog.d/4317.bugfix | 1 | ||||
-rw-r--r-- | tests/test_metrics.py | 24 |
2 files changed, 24 insertions, 1 deletions
diff --git a/changelog.d/4317.bugfix b/changelog.d/4317.bugfix new file mode 100644 index 0000000000..61bad5f2da --- /dev/null +++ b/changelog.d/4317.bugfix @@ -0,0 +1 @@ +Fix test_metric.py compatibility with prometheus_client 0.5. Contributed by Maarten de Vries <maarten@de-vri.es>. 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 |