test_metrics.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 importlib import metadata
  16. from typing import Dict, Tuple
  17. from unittest.mock import patch
  18. from pkg_resources import parse_version
  19. from prometheus_client.core import Sample
  20. from typing_extensions import Protocol
  21. from synapse.app._base import _set_prometheus_client_use_created_metrics
  22. from synapse.metrics import REGISTRY, InFlightGauge, generate_latest
  23. from synapse.util.caches.deferred_cache import DeferredCache
  24. from tests import unittest
  25. def get_sample_labels_value(sample: Sample) -> Tuple[Dict[str, str], float]:
  26. """Extract the labels and values of a sample.
  27. prometheus_client 0.5 changed the sample type to a named tuple with more
  28. members than the plain tuple had in 0.4 and earlier. This function can
  29. extract the labels and value from the sample for both sample types.
  30. Args:
  31. sample: The sample to get the labels and value from.
  32. Returns:
  33. A tuple of (labels, value) from the sample.
  34. """
  35. # If the sample has a labels and value attribute, use those.
  36. if hasattr(sample, "labels") and hasattr(sample, "value"):
  37. return sample.labels, sample.value
  38. # Otherwise fall back to treating it as a plain 3 tuple.
  39. else:
  40. # In older versions of prometheus_client Sample was a 3-tuple.
  41. labels: Dict[str, str]
  42. value: float
  43. _, labels, value = sample # type: ignore[misc]
  44. return labels, value
  45. class TestMauLimit(unittest.TestCase):
  46. def test_basic(self) -> None:
  47. class MetricEntry(Protocol):
  48. foo: int
  49. bar: int
  50. gauge: InFlightGauge[MetricEntry] = InFlightGauge(
  51. "test1", "", labels=["test_label"], sub_metrics=["foo", "bar"]
  52. )
  53. def handle1(metrics: MetricEntry) -> None:
  54. metrics.foo += 2
  55. metrics.bar = max(metrics.bar, 5)
  56. def handle2(metrics: MetricEntry) -> None:
  57. metrics.foo += 3
  58. metrics.bar = max(metrics.bar, 7)
  59. gauge.register(("key1",), handle1)
  60. self.assert_dict(
  61. {
  62. "test1_total": {("key1",): 1},
  63. "test1_foo": {("key1",): 2},
  64. "test1_bar": {("key1",): 5},
  65. },
  66. self.get_metrics_from_gauge(gauge),
  67. )
  68. gauge.unregister(("key1",), handle1)
  69. self.assert_dict(
  70. {
  71. "test1_total": {("key1",): 0},
  72. "test1_foo": {("key1",): 0},
  73. "test1_bar": {("key1",): 0},
  74. },
  75. self.get_metrics_from_gauge(gauge),
  76. )
  77. gauge.register(("key1",), handle1)
  78. gauge.register(("key2",), handle2)
  79. self.assert_dict(
  80. {
  81. "test1_total": {("key1",): 1, ("key2",): 1},
  82. "test1_foo": {("key1",): 2, ("key2",): 3},
  83. "test1_bar": {("key1",): 5, ("key2",): 7},
  84. },
  85. self.get_metrics_from_gauge(gauge),
  86. )
  87. gauge.unregister(("key2",), handle2)
  88. gauge.register(("key1",), handle2)
  89. self.assert_dict(
  90. {
  91. "test1_total": {("key1",): 2, ("key2",): 0},
  92. "test1_foo": {("key1",): 5, ("key2",): 0},
  93. "test1_bar": {("key1",): 7, ("key2",): 0},
  94. },
  95. self.get_metrics_from_gauge(gauge),
  96. )
  97. def get_metrics_from_gauge(
  98. self, gauge: InFlightGauge
  99. ) -> Dict[str, Dict[Tuple[str, ...], float]]:
  100. results = {}
  101. for r in gauge.collect():
  102. results[r.name] = {
  103. tuple(labels[x] for x in gauge.labels): value
  104. for labels, value in map(get_sample_labels_value, r.samples)
  105. }
  106. return results
  107. class BuildInfoTests(unittest.TestCase):
  108. def test_get_build(self) -> None:
  109. """
  110. The synapse_build_info metric reports the OS version, Python version,
  111. and Synapse version.
  112. """
  113. items = list(
  114. filter(
  115. lambda x: b"synapse_build_info{" in x,
  116. generate_latest(REGISTRY).split(b"\n"),
  117. )
  118. )
  119. self.assertEqual(len(items), 1)
  120. self.assertTrue(b"osversion=" in items[0])
  121. self.assertTrue(b"pythonversion=" in items[0])
  122. self.assertTrue(b"version=" in items[0])
  123. class CacheMetricsTests(unittest.HomeserverTestCase):
  124. def test_cache_metric(self) -> None:
  125. """
  126. Caches produce metrics reflecting their state when scraped.
  127. """
  128. CACHE_NAME = "cache_metrics_test_fgjkbdfg"
  129. cache: DeferredCache[str, str] = DeferredCache(CACHE_NAME, max_entries=777)
  130. items = {
  131. x.split(b"{")[0].decode("ascii"): x.split(b" ")[1].decode("ascii")
  132. for x in filter(
  133. lambda x: b"cache_metrics_test_fgjkbdfg" in x,
  134. generate_latest(REGISTRY).split(b"\n"),
  135. )
  136. }
  137. self.assertEqual(items["synapse_util_caches_cache_size"], "0.0")
  138. self.assertEqual(items["synapse_util_caches_cache_max_size"], "777.0")
  139. cache.prefill("1", "hi")
  140. items = {
  141. x.split(b"{")[0].decode("ascii"): x.split(b" ")[1].decode("ascii")
  142. for x in filter(
  143. lambda x: b"cache_metrics_test_fgjkbdfg" in x,
  144. generate_latest(REGISTRY).split(b"\n"),
  145. )
  146. }
  147. self.assertEqual(items["synapse_util_caches_cache_size"], "1.0")
  148. self.assertEqual(items["synapse_util_caches_cache_max_size"], "777.0")
  149. class PrometheusMetricsHackTestCase(unittest.HomeserverTestCase):
  150. if parse_version(metadata.version("prometheus_client")) < parse_version("0.14.0"):
  151. skip = "prometheus-client too old"
  152. def test_created_metrics_disabled(self) -> None:
  153. """
  154. Tests that a brittle hack, to disable `_created` metrics, works.
  155. This involves poking at the internals of prometheus-client.
  156. It's not the end of the world if this doesn't work.
  157. This test gives us a way to notice if prometheus-client changes
  158. their internals.
  159. """
  160. import prometheus_client.metrics
  161. PRIVATE_FLAG_NAME = "_use_created"
  162. # By default, the pesky `_created` metrics are enabled.
  163. # Check this assumption is still valid.
  164. self.assertTrue(getattr(prometheus_client.metrics, PRIVATE_FLAG_NAME))
  165. with patch("prometheus_client.metrics") as mock:
  166. setattr(mock, PRIVATE_FLAG_NAME, True)
  167. _set_prometheus_client_use_created_metrics(False)
  168. self.assertFalse(getattr(mock, PRIVATE_FLAG_NAME, False))