test_event_metrics.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright 2019 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the 'License');
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an 'AS IS' BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from prometheus_client import generate_latest
  15. from synapse.metrics import REGISTRY
  16. from synapse.types import UserID, create_requester
  17. from tests.unittest import HomeserverTestCase
  18. class ExtremStatisticsTestCase(HomeserverTestCase):
  19. def test_exposed_to_prometheus(self) -> None:
  20. """
  21. Forward extremity counts are exposed via Prometheus.
  22. """
  23. room_creator = self.hs.get_room_creation_handler()
  24. user = UserID("alice", "test")
  25. requester = create_requester(user)
  26. # Real events, forward extremities
  27. events = [(3, 2), (6, 2), (4, 6)]
  28. for event_count, extrems in events:
  29. room_id, _, _ = self.get_success(room_creator.create_room(requester, {}))
  30. last_event = None
  31. # Make a real event chain
  32. for _ in range(event_count):
  33. ev = self.create_and_send_event(room_id, user, False, last_event)
  34. last_event = [ev]
  35. # Sprinkle in some extremities
  36. for _ in range(extrems):
  37. ev = self.create_and_send_event(room_id, user, False, last_event)
  38. # Let it run for a while, then pull out the statistics from the
  39. # Prometheus client registry
  40. self.reactor.advance(60 * 60 * 1000)
  41. self.pump(1)
  42. items = list(
  43. filter(
  44. lambda x: b"synapse_forward_extremities_" in x and b"# HELP" not in x,
  45. generate_latest(REGISTRY).split(b"\n"),
  46. )
  47. )
  48. expected = [
  49. b'synapse_forward_extremities_bucket{le="1.0"} 0.0',
  50. b'synapse_forward_extremities_bucket{le="2.0"} 2.0',
  51. b'synapse_forward_extremities_bucket{le="3.0"} 2.0',
  52. b'synapse_forward_extremities_bucket{le="5.0"} 2.0',
  53. b'synapse_forward_extremities_bucket{le="7.0"} 3.0',
  54. b'synapse_forward_extremities_bucket{le="10.0"} 3.0',
  55. b'synapse_forward_extremities_bucket{le="15.0"} 3.0',
  56. b'synapse_forward_extremities_bucket{le="20.0"} 3.0',
  57. b'synapse_forward_extremities_bucket{le="50.0"} 3.0',
  58. b'synapse_forward_extremities_bucket{le="100.0"} 3.0',
  59. b'synapse_forward_extremities_bucket{le="200.0"} 3.0',
  60. b'synapse_forward_extremities_bucket{le="500.0"} 3.0',
  61. # per https://docs.google.com/document/d/1KwV0mAXwwbvvifBvDKH_LU1YjyXE_wxCkHNoCGq1GX0/edit#heading=h.wghdjzzh72j9,
  62. # "inf" is valid: "this includes variants such as inf"
  63. b'synapse_forward_extremities_bucket{le="inf"} 3.0',
  64. b"# TYPE synapse_forward_extremities_gcount gauge",
  65. b"synapse_forward_extremities_gcount 3.0",
  66. b"# TYPE synapse_forward_extremities_gsum gauge",
  67. b"synapse_forward_extremities_gsum 10.0",
  68. ]
  69. self.assertEqual(items, expected)