1
0

test_metrics.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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", "", labels=["test_label"], sub_metrics=["foo", "bar"]
  38. )
  39. def handle1(metrics):
  40. metrics.foo += 2
  41. metrics.bar = max(metrics.bar, 5)
  42. def handle2(metrics):
  43. metrics.foo += 3
  44. metrics.bar = max(metrics.bar, 7)
  45. gauge.register(("key1",), handle1)
  46. self.assert_dict(
  47. {
  48. "test1_total": {("key1",): 1},
  49. "test1_foo": {("key1",): 2},
  50. "test1_bar": {("key1",): 5},
  51. },
  52. self.get_metrics_from_gauge(gauge),
  53. )
  54. gauge.unregister(("key1",), handle1)
  55. self.assert_dict(
  56. {
  57. "test1_total": {("key1",): 0},
  58. "test1_foo": {("key1",): 0},
  59. "test1_bar": {("key1",): 0},
  60. },
  61. self.get_metrics_from_gauge(gauge),
  62. )
  63. gauge.register(("key1",), handle1)
  64. gauge.register(("key2",), handle2)
  65. self.assert_dict(
  66. {
  67. "test1_total": {("key1",): 1, ("key2",): 1},
  68. "test1_foo": {("key1",): 2, ("key2",): 3},
  69. "test1_bar": {("key1",): 5, ("key2",): 7},
  70. },
  71. self.get_metrics_from_gauge(gauge),
  72. )
  73. gauge.unregister(("key2",), handle2)
  74. gauge.register(("key1",), handle2)
  75. self.assert_dict(
  76. {
  77. "test1_total": {("key1",): 2, ("key2",): 0},
  78. "test1_foo": {("key1",): 5, ("key2",): 0},
  79. "test1_bar": {("key1",): 7, ("key2",): 0},
  80. },
  81. self.get_metrics_from_gauge(gauge),
  82. )
  83. def get_metrics_from_gauge(self, gauge):
  84. results = {}
  85. for r in gauge.collect():
  86. results[r.name] = {
  87. tuple(labels[x] for x in gauge.labels): value
  88. for labels, value in map(get_sample_labels_value, r.samples)
  89. }
  90. return results