test_event_metrics.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 The 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, generate_latest
  16. from synapse.types import Requester, UserID
  17. from tests.unittest import HomeserverTestCase
  18. class ExtremStatisticsTestCase(HomeserverTestCase):
  19. def test_exposed_to_prometheus(self):
  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 = Requester(user, None, False, None, None)
  26. # Real events, forward extremities
  27. events = [(3, 2), (6, 2), (4, 6)]
  28. for event_count, extrems in events:
  29. info, _ = self.get_success(room_creator.create_room(requester, {}))
  30. room_id = info["room_id"]
  31. last_event = None
  32. # Make a real event chain
  33. for i in range(event_count):
  34. ev = self.create_and_send_event(room_id, user, False, last_event)
  35. last_event = [ev]
  36. # Sprinkle in some extremities
  37. for i in range(extrems):
  38. ev = self.create_and_send_event(room_id, user, False, last_event)
  39. # Let it run for a while, then pull out the statistics from the
  40. # Prometheus client registry
  41. self.reactor.advance(60 * 60 * 1000)
  42. self.pump(1)
  43. items = set(
  44. filter(
  45. lambda x: b"synapse_forward_extremities_" in x,
  46. generate_latest(REGISTRY).split(b"\n"),
  47. )
  48. )
  49. expected = {
  50. b'synapse_forward_extremities_bucket{le="1.0"} 0.0',
  51. b'synapse_forward_extremities_bucket{le="2.0"} 2.0',
  52. b'synapse_forward_extremities_bucket{le="3.0"} 2.0',
  53. b'synapse_forward_extremities_bucket{le="5.0"} 2.0',
  54. b'synapse_forward_extremities_bucket{le="7.0"} 3.0',
  55. b'synapse_forward_extremities_bucket{le="10.0"} 3.0',
  56. b'synapse_forward_extremities_bucket{le="15.0"} 3.0',
  57. b'synapse_forward_extremities_bucket{le="20.0"} 3.0',
  58. b'synapse_forward_extremities_bucket{le="50.0"} 3.0',
  59. b'synapse_forward_extremities_bucket{le="100.0"} 3.0',
  60. b'synapse_forward_extremities_bucket{le="200.0"} 3.0',
  61. b'synapse_forward_extremities_bucket{le="500.0"} 3.0',
  62. b'synapse_forward_extremities_bucket{le="+Inf"} 3.0',
  63. b"synapse_forward_extremities_count 3.0",
  64. b"synapse_forward_extremities_sum 10.0",
  65. }
  66. self.assertEqual(items, expected)