test_metrics.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. # Copyright 2019 Matrix.org Foundation C.I.C.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from synapse.metrics import REGISTRY, InFlightGauge, generate_latest
  17. from synapse.util.caches.descriptors import Cache
  18. from tests import unittest
  19. def get_sample_labels_value(sample):
  20. """ Extract the labels and values of a sample.
  21. prometheus_client 0.5 changed the sample type to a named tuple with more
  22. members than the plain tuple had in 0.4 and earlier. This function can
  23. extract the labels and value from the sample for both sample types.
  24. Args:
  25. sample: The sample to get the labels and value from.
  26. Returns:
  27. A tuple of (labels, value) from the sample.
  28. """
  29. # If the sample has a labels and value attribute, use those.
  30. if hasattr(sample, "labels") and hasattr(sample, "value"):
  31. return sample.labels, sample.value
  32. # Otherwise fall back to treating it as a plain 3 tuple.
  33. else:
  34. _, labels, value = sample
  35. return labels, value
  36. class TestMauLimit(unittest.TestCase):
  37. def test_basic(self):
  38. gauge = InFlightGauge(
  39. "test1", "", labels=["test_label"], 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. {
  50. "test1_total": {("key1",): 1},
  51. "test1_foo": {("key1",): 2},
  52. "test1_bar": {("key1",): 5},
  53. },
  54. self.get_metrics_from_gauge(gauge),
  55. )
  56. gauge.unregister(("key1",), handle1)
  57. self.assert_dict(
  58. {
  59. "test1_total": {("key1",): 0},
  60. "test1_foo": {("key1",): 0},
  61. "test1_bar": {("key1",): 0},
  62. },
  63. self.get_metrics_from_gauge(gauge),
  64. )
  65. gauge.register(("key1",), handle1)
  66. gauge.register(("key2",), handle2)
  67. self.assert_dict(
  68. {
  69. "test1_total": {("key1",): 1, ("key2",): 1},
  70. "test1_foo": {("key1",): 2, ("key2",): 3},
  71. "test1_bar": {("key1",): 5, ("key2",): 7},
  72. },
  73. self.get_metrics_from_gauge(gauge),
  74. )
  75. gauge.unregister(("key2",), handle2)
  76. gauge.register(("key1",), handle2)
  77. self.assert_dict(
  78. {
  79. "test1_total": {("key1",): 2, ("key2",): 0},
  80. "test1_foo": {("key1",): 5, ("key2",): 0},
  81. "test1_bar": {("key1",): 7, ("key2",): 0},
  82. },
  83. self.get_metrics_from_gauge(gauge),
  84. )
  85. def get_metrics_from_gauge(self, gauge):
  86. results = {}
  87. for r in gauge.collect():
  88. results[r.name] = {
  89. tuple(labels[x] for x in gauge.labels): value
  90. for labels, value in map(get_sample_labels_value, r.samples)
  91. }
  92. return results
  93. class BuildInfoTests(unittest.TestCase):
  94. def test_get_build(self):
  95. """
  96. The synapse_build_info metric reports the OS version, Python version,
  97. and Synapse version.
  98. """
  99. items = list(
  100. filter(
  101. lambda x: b"synapse_build_info{" in x,
  102. generate_latest(REGISTRY).split(b"\n"),
  103. )
  104. )
  105. self.assertEqual(len(items), 1)
  106. self.assertTrue(b"osversion=" in items[0])
  107. self.assertTrue(b"pythonversion=" in items[0])
  108. self.assertTrue(b"version=" in items[0])
  109. class CacheMetricsTests(unittest.HomeserverTestCase):
  110. def test_cache_metric(self):
  111. """
  112. Caches produce metrics reflecting their state when scraped.
  113. """
  114. CACHE_NAME = "cache_metrics_test_fgjkbdfg"
  115. cache = Cache(CACHE_NAME, max_entries=777)
  116. items = {
  117. x.split(b"{")[0].decode("ascii"): x.split(b" ")[1].decode("ascii")
  118. for x in filter(
  119. lambda x: b"cache_metrics_test_fgjkbdfg" in x,
  120. generate_latest(REGISTRY).split(b"\n"),
  121. )
  122. }
  123. self.assertEqual(items["synapse_util_caches_cache_size"], "0.0")
  124. self.assertEqual(items["synapse_util_caches_cache_max_size"], "777.0")
  125. cache.prefill("1", "hi")
  126. items = {
  127. x.split(b"{")[0].decode("ascii"): x.split(b" ")[1].decode("ascii")
  128. for x in filter(
  129. lambda x: b"cache_metrics_test_fgjkbdfg" in x,
  130. generate_latest(REGISTRY).split(b"\n"),
  131. )
  132. }
  133. self.assertEqual(items["synapse_util_caches_cache_size"], "1.0")
  134. self.assertEqual(items["synapse_util_caches_cache_max_size"], "777.0")