Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 2e06f75

Browse files
committed
Add metrics to track /messages response time by room size
Follow-up to #13533
1 parent 5f2421d commit 2e06f75

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

synapse/rest/client/room.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
""" This module contains REST servlets to do with rooms: /rooms/<paths> """
1717
import logging
18+
from enum import Enum
1819
import re
1920
from typing import TYPE_CHECKING, Awaitable, Dict, List, Optional, Tuple
2021
from urllib import parse as urlparse
@@ -62,6 +63,32 @@
6263

6364
logger = logging.getLogger(__name__)
6465

66+
67+
class _RoomSize(Enum):
68+
"""
69+
Enum to differentiate sizes of rooms. This is a pretty good aproximation
70+
about how hard it will be to get events in the room. We could also look at
71+
room "complexity".
72+
"""
73+
74+
# This doesn't necessarily mean the room is a DM, just that there is a DM
75+
# amount of people there.
76+
DM_SIZE = "direct_message_size"
77+
SMALL = "small"
78+
SUBSTANTIAL = "substantial"
79+
LARGE = "large"
80+
81+
def get_room_size_label_for_member_count(member_count: int):
82+
if member_count <= 2:
83+
return _RoomSize.DM_SIZE
84+
elif member_count < 100:
85+
return _RoomSize.SMALL
86+
elif member_count < 1000:
87+
return _RoomSize.SUBSTANTIAL
88+
else:
89+
return _RoomSize.LARGE
90+
91+
6592
# This is an extra metric on top of `synapse_http_server_response_time_seconds`
6693
# which times the same sort of thing but this one allows us to see values
6794
# greater than 10s. We use a separate dedicated histogram with its own buckets
@@ -70,7 +97,11 @@
7097
messsages_response_timer = Histogram(
7198
"synapse_room_message_list_rest_servlet_response_time_seconds",
7299
"sec",
73-
[],
100+
# We have a label for room size so we can try to see a more realistic
101+
# picture of /messages response time for bigger rooms. We don't want the
102+
# tiny rooms that can always respond fast skewing our results when we're trying
103+
# to optimize the bigger cases.
104+
["room_size"],
74105
buckets=(
75106
0.005,
76107
0.01,
@@ -591,10 +622,13 @@ def __init__(self, hs: "HomeServer"):
591622
self.auth = hs.get_auth()
592623
self.store = hs.get_datastores().main
593624

594-
@messsages_response_timer.time()
595625
async def on_GET(
596626
self, request: SynapseRequest, room_id: str
597627
) -> Tuple[int, JsonDict]:
628+
processing_start_time = self.clock.time_msec()
629+
# Fire and forget and hope that we get a result by the end.
630+
room_member_count_co = self.store.get_number_joined_users_in_room(room_id)
631+
598632
requester = await self.auth.get_user_by_req(request, allow_guest=True)
599633
pagination_config = await PaginationConfig.from_request(
600634
self.store, request, default_limit=10
@@ -625,6 +659,12 @@ async def on_GET(
625659
event_filter=event_filter,
626660
)
627661

662+
processing_end_time = self.clock.time_msec()
663+
room_member_count = await room_member_count_co
664+
messsages_response_timer.labels(
665+
room_size=_RoomSize.get_room_size_label_for_member_count(room_member_count)
666+
).observe((processing_start_time - processing_end_time) / 1000)
667+
628668
return 200, msgs
629669

630670

0 commit comments

Comments
 (0)