test_event_metrics.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. 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 _ 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 _ 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 = list(
  44. filter(
  45. lambda x: b"synapse_forward_extremities_" in x and b"# HELP" not 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. # per https://docs.google.com/document/d/1KwV0mAXwwbvvifBvDKH_LU1YjyXE_wxCkHNoCGq1GX0/edit#heading=h.wghdjzzh72j9,
  63. # "inf" is valid: "this includes variants such as inf"
  64. b'synapse_forward_extremities_bucket{le="inf"} 3.0',
  65. b"# TYPE synapse_forward_extremities_gcount gauge",
  66. b"synapse_forward_extremities_gcount 3.0",
  67. b"# TYPE synapse_forward_extremities_gsum gauge",
  68. b"synapse_forward_extremities_gsum 10.0",
  69. ]
  70. self.assertEqual(items, expected)