test_metrics.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from synapse.metrics import InFlightGauge
  16. from tests import unittest
  17. def get_sample_labels_value(sample):
  18. """ Extract the labels and values of a sample.
  19. prometheus_client 0.5 changed the sample type to a named tuple with more
  20. members than the plain tuple had in 0.4 and earlier. This function can
  21. extract the labels and value from the sample for both sample types.
  22. Args:
  23. sample: The sample to get the labels and value from.
  24. Returns:
  25. A tuple of (labels, value) from the sample.
  26. """
  27. # If the sample has a labels and value attribute, use those.
  28. if hasattr(sample, "labels") and hasattr(sample, "value"):
  29. return sample.labels, sample.value
  30. # Otherwise fall back to treating it as a plain 3 tuple.
  31. else:
  32. _, labels, value = sample
  33. return labels, value
  34. class TestMauLimit(unittest.TestCase):
  35. def test_basic(self):
  36. gauge = InFlightGauge(
  37. "test1", "",
  38. labels=["test_label"],
  39. sub_metrics=["foo", "bar"],
  40. )
  41. def handle1(metrics):
  42. metrics.foo += 2
  43. metrics.bar = max(metrics.bar, 5)
  44. def handle2(metrics):
  45. metrics.foo += 3
  46. metrics.bar = max(metrics.bar, 7)
  47. gauge.register(("key1",), handle1)
  48. self.assert_dict({
  49. "test1_total": {("key1",): 1},
  50. "test1_foo": {("key1",): 2},
  51. "test1_bar": {("key1",): 5},
  52. }, self.get_metrics_from_gauge(gauge))
  53. gauge.unregister(("key1",), handle1)
  54. self.assert_dict({
  55. "test1_total": {("key1",): 0},
  56. "test1_foo": {("key1",): 0},
  57. "test1_bar": {("key1",): 0},
  58. }, self.get_metrics_from_gauge(gauge))
  59. gauge.register(("key1",), handle1)
  60. gauge.register(("key2",), handle2)
  61. self.assert_dict({
  62. "test1_total": {("key1",): 1, ("key2",): 1},
  63. "test1_foo": {("key1",): 2, ("key2",): 3},
  64. "test1_bar": {("key1",): 5, ("key2",): 7},
  65. }, self.get_metrics_from_gauge(gauge))
  66. gauge.unregister(("key2",), handle2)
  67. gauge.register(("key1",), handle2)
  68. self.assert_dict({
  69. "test1_total": {("key1",): 2, ("key2",): 0},
  70. "test1_foo": {("key1",): 5, ("key2",): 0},
  71. "test1_bar": {("key1",): 7, ("key2",): 0},
  72. }, self.get_metrics_from_gauge(gauge))
  73. def get_metrics_from_gauge(self, gauge):
  74. results = {}
  75. for r in gauge.collect():
  76. results[r.name] = {
  77. tuple(labels[x] for x in gauge.labels): value
  78. for labels, value in map(get_sample_labels_value, r.samples)
  79. }
  80. return results