2019-06-13 15:40:52 +03:00
|
|
|
#
|
2023-11-21 23:29:58 +03:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 14:26:48 +03:00
|
|
|
# Copyright 2019 The Matrix.org Foundation C.I.C.
|
2023-11-21 23:29:58 +03:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2019-06-13 15:40:52 +03:00
|
|
|
#
|
|
|
|
#
|
2022-11-24 12:09:17 +03:00
|
|
|
from prometheus_client import generate_latest
|
2019-06-13 15:40:52 +03:00
|
|
|
|
2022-11-24 12:09:17 +03:00
|
|
|
from synapse.metrics import REGISTRY
|
2020-10-22 12:11:06 +03:00
|
|
|
from synapse.types import UserID, create_requester
|
2019-06-13 15:40:52 +03:00
|
|
|
|
|
|
|
from tests.unittest import HomeserverTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class ExtremStatisticsTestCase(HomeserverTestCase):
|
2022-12-09 20:36:32 +03:00
|
|
|
def test_exposed_to_prometheus(self) -> None:
|
2019-06-13 15:40:52 +03:00
|
|
|
"""
|
|
|
|
Forward extremity counts are exposed via Prometheus.
|
|
|
|
"""
|
|
|
|
room_creator = self.hs.get_room_creation_handler()
|
|
|
|
|
|
|
|
user = UserID("alice", "test")
|
2020-10-22 12:11:06 +03:00
|
|
|
requester = create_requester(user)
|
2019-06-13 15:40:52 +03:00
|
|
|
|
|
|
|
# Real events, forward extremities
|
|
|
|
events = [(3, 2), (6, 2), (4, 6)]
|
|
|
|
|
|
|
|
for event_count, extrems in events:
|
2023-02-22 14:07:28 +03:00
|
|
|
room_id, _, _ = self.get_success(room_creator.create_room(requester, {}))
|
2019-06-13 15:40:52 +03:00
|
|
|
|
|
|
|
last_event = None
|
|
|
|
|
|
|
|
# Make a real event chain
|
2021-04-20 13:50:49 +03:00
|
|
|
for _ in range(event_count):
|
2019-06-13 15:40:52 +03:00
|
|
|
ev = self.create_and_send_event(room_id, user, False, last_event)
|
|
|
|
last_event = [ev]
|
|
|
|
|
|
|
|
# Sprinkle in some extremities
|
2021-04-20 13:50:49 +03:00
|
|
|
for _ in range(extrems):
|
2019-06-13 15:40:52 +03:00
|
|
|
ev = self.create_and_send_event(room_id, user, False, last_event)
|
|
|
|
|
|
|
|
# Let it run for a while, then pull out the statistics from the
|
|
|
|
# Prometheus client registry
|
|
|
|
self.reactor.advance(60 * 60 * 1000)
|
|
|
|
self.pump(1)
|
|
|
|
|
2020-09-30 00:26:28 +03:00
|
|
|
items = list(
|
2019-06-13 15:40:52 +03:00
|
|
|
filter(
|
2022-11-24 12:09:17 +03:00
|
|
|
lambda x: b"synapse_forward_extremities_" in x and b"# HELP" not in x,
|
|
|
|
generate_latest(REGISTRY).split(b"\n"),
|
2019-06-13 15:40:52 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-09-30 00:26:28 +03:00
|
|
|
expected = [
|
2020-02-21 15:15:07 +03:00
|
|
|
b'synapse_forward_extremities_bucket{le="1.0"} 0.0',
|
|
|
|
b'synapse_forward_extremities_bucket{le="2.0"} 2.0',
|
|
|
|
b'synapse_forward_extremities_bucket{le="3.0"} 2.0',
|
|
|
|
b'synapse_forward_extremities_bucket{le="5.0"} 2.0',
|
|
|
|
b'synapse_forward_extremities_bucket{le="7.0"} 3.0',
|
|
|
|
b'synapse_forward_extremities_bucket{le="10.0"} 3.0',
|
|
|
|
b'synapse_forward_extremities_bucket{le="15.0"} 3.0',
|
|
|
|
b'synapse_forward_extremities_bucket{le="20.0"} 3.0',
|
|
|
|
b'synapse_forward_extremities_bucket{le="50.0"} 3.0',
|
|
|
|
b'synapse_forward_extremities_bucket{le="100.0"} 3.0',
|
|
|
|
b'synapse_forward_extremities_bucket{le="200.0"} 3.0',
|
|
|
|
b'synapse_forward_extremities_bucket{le="500.0"} 3.0',
|
2020-09-30 00:26:28 +03:00
|
|
|
# per https://docs.google.com/document/d/1KwV0mAXwwbvvifBvDKH_LU1YjyXE_wxCkHNoCGq1GX0/edit#heading=h.wghdjzzh72j9,
|
|
|
|
# "inf" is valid: "this includes variants such as inf"
|
|
|
|
b'synapse_forward_extremities_bucket{le="inf"} 3.0',
|
|
|
|
b"# TYPE synapse_forward_extremities_gcount gauge",
|
|
|
|
b"synapse_forward_extremities_gcount 3.0",
|
|
|
|
b"# TYPE synapse_forward_extremities_gsum gauge",
|
|
|
|
b"synapse_forward_extremities_gsum 10.0",
|
|
|
|
]
|
2019-06-14 14:09:33 +03:00
|
|
|
self.assertEqual(items, expected)
|