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

Commit 1a5d4a4

Browse files
committed
add Measure blocks all over SpamChecker
Signed-off-by: jesopo <[email protected]>
1 parent 2aad0ae commit 1a5d4a4

File tree

3 files changed

+61
-24
lines changed

3 files changed

+61
-24
lines changed

changelog.d/12513.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Insert Measure blocks throughout SpamChecker to collect metrics.

synapse/events/spamcheck.py

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
from synapse.spam_checker_api import RegistrationBehaviour
3333
from synapse.types import RoomAlias, UserProfile
3434
from synapse.util.async_helpers import delay_cancellation, maybe_awaitable
35+
from synapse.util.async_helpers import maybe_awaitable
36+
from synapse.util.metrics import Measure
3537

3638
if TYPE_CHECKING:
3739
import synapse.events
@@ -162,7 +164,10 @@ def run(*args: Any, **kwargs: Any) -> Awaitable:
162164

163165

164166
class SpamChecker:
165-
def __init__(self) -> None:
167+
def __init__(self, hs: "synapse.server.HomeServer") -> None:
168+
self.hs = hs
169+
self.clock = hs.get_clock()
170+
166171
self._check_event_for_spam_callbacks: List[CHECK_EVENT_FOR_SPAM_CALLBACK] = []
167172
self._user_may_join_room_callbacks: List[USER_MAY_JOIN_ROOM_CALLBACK] = []
168173
self._user_may_invite_callbacks: List[USER_MAY_INVITE_CALLBACK] = []
@@ -255,7 +260,10 @@ async def check_event_for_spam(
255260
will be used as the error message returned to the user.
256261
"""
257262
for callback in self._check_event_for_spam_callbacks:
258-
res: Union[bool, str] = await delay_cancellation(callback(event))
263+
with Measure(
264+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
265+
):
266+
res: Union[bool, str] = await delay_cancellation(callback(event))
259267
if res:
260268
return res
261269

@@ -276,9 +284,12 @@ async def user_may_join_room(
276284
Whether the user may join the room
277285
"""
278286
for callback in self._user_may_join_room_callbacks:
279-
may_join_room = await delay_cancellation(
280-
callback(user_id, room_id, is_invited)
281-
)
287+
with Measure(
288+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
289+
):
290+
may_join_room = await delay_cancellation(
291+
callback(user_id, room_id, is_invited)
292+
)
282293
if may_join_room is False:
283294
return False
284295

@@ -300,9 +311,12 @@ async def user_may_invite(
300311
True if the user may send an invite, otherwise False
301312
"""
302313
for callback in self._user_may_invite_callbacks:
303-
may_invite = await delay_cancellation(
304-
callback(inviter_userid, invitee_userid, room_id)
305-
)
314+
with Measure(
315+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
316+
):
317+
may_invite = await delay_cancellation(
318+
callback(inviter_userid, invitee_userid, room_id)
319+
)
306320
if may_invite is False:
307321
return False
308322

@@ -328,9 +342,12 @@ async def user_may_send_3pid_invite(
328342
True if the user may send the invite, otherwise False
329343
"""
330344
for callback in self._user_may_send_3pid_invite_callbacks:
331-
may_send_3pid_invite = await delay_cancellation(
332-
callback(inviter_userid, medium, address, room_id)
333-
)
345+
with Measure(
346+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
347+
):
348+
may_send_3pid_invite = await delay_cancellation(
349+
callback(inviter_userid, medium, address, room_id)
350+
)
334351
if may_send_3pid_invite is False:
335352
return False
336353

@@ -348,7 +365,10 @@ async def user_may_create_room(self, userid: str) -> bool:
348365
True if the user may create a room, otherwise False
349366
"""
350367
for callback in self._user_may_create_room_callbacks:
351-
may_create_room = await delay_cancellation(callback(userid))
368+
with Measure(
369+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
370+
):
371+
may_create_room = await delay_cancellation(callback(userid))
352372
if may_create_room is False:
353373
return False
354374

@@ -369,9 +389,12 @@ async def user_may_create_room_alias(
369389
True if the user may create a room alias, otherwise False
370390
"""
371391
for callback in self._user_may_create_room_alias_callbacks:
372-
may_create_room_alias = await delay_cancellation(
373-
callback(userid, room_alias)
374-
)
392+
with Measure(
393+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
394+
):
395+
may_create_room_alias = await delay_cancellation(
396+
callback(userid, room_alias)
397+
)
375398
if may_create_room_alias is False:
376399
return False
377400

@@ -390,7 +413,10 @@ async def user_may_publish_room(self, userid: str, room_id: str) -> bool:
390413
True if the user may publish the room, otherwise False
391414
"""
392415
for callback in self._user_may_publish_room_callbacks:
393-
may_publish_room = await delay_cancellation(callback(userid, room_id))
416+
with Measure(
417+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
418+
):
419+
may_publish_room = await delay_cancellation(callback(userid, room_id))
394420
if may_publish_room is False:
395421
return False
396422

@@ -412,9 +438,13 @@ async def check_username_for_spam(self, user_profile: UserProfile) -> bool:
412438
True if the user is spammy.
413439
"""
414440
for callback in self._check_username_for_spam_callbacks:
415-
# Make a copy of the user profile object to ensure the spam checker cannot
416-
# modify it.
417-
if await delay_cancellation(callback(user_profile.copy())):
441+
with Measure(
442+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
443+
):
444+
# Make a copy of the user profile object to ensure the spam checker cannot
445+
# modify it.
446+
res = await delay_cancellation(callback(user_profile.copy()))
447+
if res:
418448
return True
419449

420450
return False
@@ -442,9 +472,12 @@ async def check_registration_for_spam(
442472
"""
443473

444474
for callback in self._check_registration_for_spam_callbacks:
445-
behaviour = await delay_cancellation(
446-
callback(email_threepid, username, request_info, auth_provider_id)
447-
)
475+
with Measure(
476+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
477+
):
478+
behaviour = await delay_cancellation(
479+
callback(email_threepid, username, request_info, auth_provider_id)
480+
)
448481
assert isinstance(behaviour, RegistrationBehaviour)
449482
if behaviour != RegistrationBehaviour.ALLOW:
450483
return behaviour
@@ -486,7 +519,10 @@ async def check_media_file_for_spam(
486519
"""
487520

488521
for callback in self._check_media_file_for_spam_callbacks:
489-
spam = await delay_cancellation(callback(file_wrapper, file_info))
522+
with Measure(
523+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
524+
):
525+
spam = await delay_cancellation(callback(file_wrapper, file_info))
490526
if spam:
491527
return True
492528

synapse/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ def get_stats_handler(self) -> StatsHandler:
681681

682682
@cache_in_self
683683
def get_spam_checker(self) -> SpamChecker:
684-
return SpamChecker()
684+
return SpamChecker(self)
685685

686686
@cache_in_self
687687
def get_third_party_event_rules(self) -> ThirdPartyEventRules:

0 commit comments

Comments
 (0)