1
0

test_metrics.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 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])