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

Commit 75f446d

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

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
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: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from synapse.spam_checker_api import RegistrationBehaviour
3333
from synapse.types import RoomAlias, UserProfile
3434
from synapse.util.async_helpers import maybe_awaitable
35+
from synapse.util.metrics import Measure
3536

3637
if TYPE_CHECKING:
3738
import synapse.events
@@ -162,7 +163,10 @@ def run(*args: Any, **kwargs: Any) -> Awaitable:
162163

163164

164165
class SpamChecker:
165-
def __init__(self) -> None:
166+
def __init__(self, hs: "synapse.server.HomeServer") -> None:
167+
self.hs = hs
168+
self.clock = hs.get_clock()
169+
166170
self._check_event_for_spam_callbacks: List[CHECK_EVENT_FOR_SPAM_CALLBACK] = []
167171
self._user_may_join_room_callbacks: List[USER_MAY_JOIN_ROOM_CALLBACK] = []
168172
self._user_may_invite_callbacks: List[USER_MAY_INVITE_CALLBACK] = []
@@ -255,7 +259,10 @@ async def check_event_for_spam(
255259
will be used as the error message returned to the user.
256260
"""
257261
for callback in self._check_event_for_spam_callbacks:
258-
res: Union[bool, str] = await callback(event)
262+
with Measure(
263+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
264+
):
265+
res: Union[bool, str] = await callback(event)
259266
if res:
260267
return res
261268

@@ -276,7 +283,11 @@ async def user_may_join_room(
276283
Whether the user may join the room
277284
"""
278285
for callback in self._user_may_join_room_callbacks:
279-
if await callback(user_id, room_id, is_invited) is False:
286+
with Measure(
287+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
288+
):
289+
res = await callback(user_id, room_id, is_invited)
290+
if res is False:
280291
return False
281292

282293
return True
@@ -297,7 +308,11 @@ async def user_may_invite(
297308
True if the user may send an invite, otherwise False
298309
"""
299310
for callback in self._user_may_invite_callbacks:
300-
if await callback(inviter_userid, invitee_userid, room_id) is False:
311+
with Measure(
312+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
313+
):
314+
res = await callback(inviter_userid, invitee_userid, room_id)
315+
if res is False:
301316
return False
302317

303318
return True
@@ -322,7 +337,11 @@ async def user_may_send_3pid_invite(
322337
True if the user may send the invite, otherwise False
323338
"""
324339
for callback in self._user_may_send_3pid_invite_callbacks:
325-
if await callback(inviter_userid, medium, address, room_id) is False:
340+
with Measure(
341+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
342+
):
343+
res = await callback(inviter_userid, medium, address, room_id)
344+
if res is False:
326345
return False
327346

328347
return True
@@ -339,7 +358,11 @@ async def user_may_create_room(self, userid: str) -> bool:
339358
True if the user may create a room, otherwise False
340359
"""
341360
for callback in self._user_may_create_room_callbacks:
342-
if await callback(userid) is False:
361+
with Measure(
362+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
363+
):
364+
res = await callback(userid)
365+
if res is False:
343366
return False
344367

345368
return True
@@ -359,7 +382,11 @@ async def user_may_create_room_alias(
359382
True if the user may create a room alias, otherwise False
360383
"""
361384
for callback in self._user_may_create_room_alias_callbacks:
362-
if await callback(userid, room_alias) is False:
385+
with Measure(
386+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
387+
):
388+
res = await callback(userid, room_alias)
389+
if res is False:
363390
return False
364391

365392
return True
@@ -377,7 +404,11 @@ async def user_may_publish_room(self, userid: str, room_id: str) -> bool:
377404
True if the user may publish the room, otherwise False
378405
"""
379406
for callback in self._user_may_publish_room_callbacks:
380-
if await callback(userid, room_id) is False:
407+
with Measure(
408+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
409+
):
410+
res = await callback(userid, room_id)
411+
if res is False:
381412
return False
382413

383414
return True
@@ -398,9 +429,13 @@ async def check_username_for_spam(self, user_profile: UserProfile) -> bool:
398429
True if the user is spammy.
399430
"""
400431
for callback in self._check_username_for_spam_callbacks:
401-
# Make a copy of the user profile object to ensure the spam checker cannot
402-
# modify it.
403-
if await callback(user_profile.copy()):
432+
with Measure(
433+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
434+
):
435+
# Make a copy of the user profile object to ensure the spam checker cannot
436+
# modify it.
437+
res = await callback(user_profile.copy())
438+
if res:
404439
return True
405440

406441
return False
@@ -428,9 +463,12 @@ async def check_registration_for_spam(
428463
"""
429464

430465
for callback in self._check_registration_for_spam_callbacks:
431-
behaviour = await (
432-
callback(email_threepid, username, request_info, auth_provider_id)
433-
)
466+
with Measure(
467+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
468+
):
469+
behaviour = await (
470+
callback(email_threepid, username, request_info, auth_provider_id)
471+
)
434472
assert isinstance(behaviour, RegistrationBehaviour)
435473
if behaviour != RegistrationBehaviour.ALLOW:
436474
return behaviour
@@ -472,7 +510,10 @@ async def check_media_file_for_spam(
472510
"""
473511

474512
for callback in self._check_media_file_for_spam_callbacks:
475-
spam = await callback(file_wrapper, file_info)
513+
with Measure(
514+
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
515+
):
516+
spam = await callback(file_wrapper, file_info)
476517
if spam:
477518
return True
478519

synapse/server.py

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

681681
@cache_in_self
682682
def get_spam_checker(self) -> SpamChecker:
683-
return SpamChecker()
683+
return SpamChecker(self)
684684

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

0 commit comments

Comments
 (0)