1
0

test_metrics.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # Copyright 2018 New Vector Ltd
  2. # Copyright 2019 Matrix.org Foundation C.I.C.
  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 REGISTRY, InFlightGauge, generate_latest
  16. from synapse.util.caches.deferred_cache import DeferredCache
  17. from tests import unittest
  18. def get_sample_labels_value(sample):
  19. """Extract the labels and values of a sample.
  20. prometheus_client 0.5 changed the sample type to a named tuple with more
  21. members than the plain tuple had in 0.4 and earlier. This function can
  22. extract the labels and value from the sample for both sample types.
  23. Args:
  24. sample: The sample to get the labels and value from.
  25. Returns:
  26. A tuple of (labels, value) from the sample.
  27. """
  28. # If the sample has a labels and value attribute, use those.
  29. if hasattr(sample, "labels") and hasattr(sample, "value"):
  30. return sample.labels, sample.value
  31. # Otherwise fall back to treating it as a plain 3 tuple.
  32. else:
  33. _, labels, value = sample
  34. return labels, value
  35. class TestMauLimit(unittest.TestCase):
  36. def test_basic(self):
  37. gauge = InFlightGauge(
  38. "test1", "", labels=["test_label"], sub_metrics=["foo", "bar"]
  39. )
  40. def handle1(metrics):
  41. metrics.foo += 2
  42. metrics.bar = max(metrics.bar, 5)
  43. def handle2(metrics):
  44. metrics.foo += 3
  45. metrics.bar = max(metrics.bar, 7)
  46. gauge.register(("key1",), handle1)
  47. self.assert_dict(
  48. {
  49. "test1_total": {("key1",): 1},
  50. "test1_foo": {("key1",): 2},
  51. "test1_bar": {("key1",): 5},
  52. },
  53. self.get_metrics_from_gauge(gauge),
  54. )
  55. gauge.unregister(("key1",), handle1)
  56. self.assert_dict(
  57. {
  58. "test1_total": {("key1",): 0},
  59. "test1_foo": {("key1",): 0},
  60. "test1_bar": {("key1",): 0},
  61. },
  62. self.get_metrics_from_gauge(gauge),
  63. )
  64. gauge.register(("key1",), handle1)
  65. gauge.register(("key2",), handle2)
  66. self.assert_dict(
  67. {
  68. "test1_total": {("key1",): 1, ("key2",): 1},
  69. "test1_foo": {("key1",): 2, ("key2",): 3},
  70. "test1_bar": {("key1",): 5, ("key2",): 7},
  71. },
  72. self.get_metrics_from_gauge(gauge),
  73. )
  74. gauge.unregister(("key2",), handle2)
  75. gauge.register(("key1",), handle2)
  76. self.assert_dict(
  77. {
  78. "test1_total": {("key1",): 2, ("key2",): 0},
  79. "test1_foo": {("key1",): 5, ("key2",): 0},
  80. "test1_bar": {("key1",): 7, ("key2",): 0},
  81. },
  82. self.get_metrics_from_gauge(gauge),
  83. )
  84. def get_metrics_from_gauge(self, gauge):
  85. results = {}
  86. for r in gauge.collect():
  87. results[r.name] = {
  88. tuple(labels[x] for x in gauge.labels): value
  89. for labels, value in map(get_sample_labels_value, r.samples)
  90. }
  91. return results
  92. class BuildInfoTests(unittest.TestCase):
  93. def test_get_build(self):
  94. """
  95. The synapse_build_info metric reports the OS version, Python version,
  96. and Synapse version.
  97. """
  98. items = list(
  99. filter(
  100. lambda x: b"synapse_build_info{" in x,
  101. generate_latest(REGISTRY).split(b"\n"),
  102. )
  103. )
  104. self.assertEqual(len(items), 1)
  105. self.assertTrue(b"osversion=" in items[0])
  106. self.assertTrue(b"pythonversion=" in items[0])
  107. self.assertTrue(b"version=" in items[0])
  108. class CacheMetricsTests(unittest.HomeserverTestCase):
  109. def test_cache_metric(self):
  110. """
  111. Caches produce metrics reflecting their state when scraped.
  112. """
  113. CACHE_NAME = "cache_metrics_test_fgjkbdfg"
  114. cache = DeferredCache(CACHE_NAME, max_entries=777)
  115. items = {
  116. x.split(b"{")[0].decode("ascii"): x.split(b" ")[1].decode("ascii")
  117. for x in filter(
  118. lambda x: b"cache_metrics_test_fgjkbdfg" in x,
  119. generate_latest(REGISTRY).split(b"\n"),
  120. )
  121. }
  122. self.assertEqual(items["synapse_util_caches_cache_size"], "0.0")
  123. self.assertEqual(items["synapse_util_caches_cache_max_size"], "777.0")
  124. cache.prefill("1", "hi")
  125. items = {
  126. x.split(b"{")[0].decode("ascii"): x.split(b" ")[1].decode("ascii")
  127. for x in filter(
  128. lambda x: b"cache_metrics_test_fgjkbdfg" in x,
  129. generate_latest(REGISTRY).split(b"\n"),
  130. )
  131. }
  132. self.assertEqual(items["synapse_util_caches_cache_size"], "1.0")
  133. self.assertEqual(items["synapse_util_caches_cache_max_size"], "777.0")