59
59
from synapse .events .snapshot import EventContext
60
60
from synapse .federation .federation_client import InvalidResponseError
61
61
from synapse .logging .context import nested_logging_context
62
- from synapse .logging .opentracing import trace
62
+ from synapse .logging .opentracing import (
63
+ SynapseTags ,
64
+ set_tag ,
65
+ start_active_span ,
66
+ tag_args ,
67
+ trace ,
68
+ )
63
69
from synapse .metrics .background_process_metrics import run_as_background_process
64
70
from synapse .replication .http .devices import ReplicationUserDevicesResyncRestServlet
65
71
from synapse .replication .http .federation import (
@@ -410,6 +416,7 @@ async def check_join_restrictions(
410
416
prev_member_event ,
411
417
)
412
418
419
+ @trace
413
420
async def process_remote_join (
414
421
self ,
415
422
origin : str ,
@@ -715,7 +722,7 @@ async def _get_missing_events_for_pdu(
715
722
716
723
@trace
717
724
async def _process_pulled_events (
718
- self , origin : str , events : Iterable [EventBase ], backfilled : bool
725
+ self , origin : str , events : Collection [EventBase ], backfilled : bool
719
726
) -> None :
720
727
"""Process a batch of events we have pulled from a remote server
721
728
@@ -730,6 +737,15 @@ async def _process_pulled_events(
730
737
backfilled: True if this is part of a historical batch of events (inhibits
731
738
notification to clients, and validation of device keys.)
732
739
"""
740
+ set_tag (
741
+ SynapseTags .FUNC_ARG_PREFIX + "event_ids" ,
742
+ str ([event .event_id for event in events ]),
743
+ )
744
+ set_tag (
745
+ SynapseTags .FUNC_ARG_PREFIX + "event_ids.length" ,
746
+ str (len (events )),
747
+ )
748
+ set_tag (SynapseTags .FUNC_ARG_PREFIX + "backfilled" , str (backfilled ))
733
749
logger .debug (
734
750
"processing pulled backfilled=%s events=%s" ,
735
751
backfilled ,
@@ -753,6 +769,7 @@ async def _process_pulled_events(
753
769
await self ._process_pulled_event (origin , ev , backfilled = backfilled )
754
770
755
771
@trace
772
+ @tag_args
756
773
async def _process_pulled_event (
757
774
self , origin : str , event : EventBase , backfilled : bool
758
775
) -> None :
@@ -854,6 +871,7 @@ async def _process_pulled_event(
854
871
else :
855
872
raise
856
873
874
+ @trace
857
875
async def _compute_event_context_with_maybe_missing_prevs (
858
876
self , dest : str , event : EventBase
859
877
) -> EventContext :
@@ -970,6 +988,8 @@ async def _compute_event_context_with_maybe_missing_prevs(
970
988
event , state_ids_before_event = state_map , partial_state = partial_state
971
989
)
972
990
991
+ @trace
992
+ @tag_args
973
993
async def _get_state_ids_after_missing_prev_event (
974
994
self ,
975
995
destination : str ,
@@ -1009,10 +1029,10 @@ async def _get_state_ids_after_missing_prev_event(
1009
1029
logger .debug ("Fetching %i events from cache/store" , len (desired_events ))
1010
1030
have_events = await self ._store .have_seen_events (room_id , desired_events )
1011
1031
1012
- missing_desired_events = desired_events - have_events
1032
+ missing_desired_event_ids = desired_events - have_events
1013
1033
logger .debug (
1014
1034
"We are missing %i events (got %i)" ,
1015
- len (missing_desired_events ),
1035
+ len (missing_desired_event_ids ),
1016
1036
len (have_events ),
1017
1037
)
1018
1038
@@ -1024,13 +1044,30 @@ async def _get_state_ids_after_missing_prev_event(
1024
1044
# already have a bunch of the state events. It would be nice if the
1025
1045
# federation api gave us a way of finding out which we actually need.
1026
1046
1027
- missing_auth_events = set (auth_event_ids ) - have_events
1028
- missing_auth_events .difference_update (
1029
- await self ._store .have_seen_events (room_id , missing_auth_events )
1047
+ missing_auth_event_ids = set (auth_event_ids ) - have_events
1048
+ missing_auth_event_ids .difference_update (
1049
+ await self ._store .have_seen_events (room_id , missing_auth_event_ids )
1030
1050
)
1031
- logger .debug ("We are also missing %i auth events" , len (missing_auth_events ))
1051
+ logger .debug ("We are also missing %i auth events" , len (missing_auth_event_ids ))
1032
1052
1033
- missing_events = missing_desired_events | missing_auth_events
1053
+ missing_event_ids = missing_desired_event_ids | missing_auth_event_ids
1054
+
1055
+ set_tag (
1056
+ SynapseTags .RESULT_PREFIX + "missing_auth_event_ids" ,
1057
+ str (missing_auth_event_ids ),
1058
+ )
1059
+ set_tag (
1060
+ SynapseTags .RESULT_PREFIX + "missing_auth_event_ids.length" ,
1061
+ str (len (missing_auth_event_ids )),
1062
+ )
1063
+ set_tag (
1064
+ SynapseTags .RESULT_PREFIX + "missing_desired_event_ids" ,
1065
+ str (missing_desired_event_ids ),
1066
+ )
1067
+ set_tag (
1068
+ SynapseTags .RESULT_PREFIX + "missing_desired_event_ids.length" ,
1069
+ str (len (missing_desired_event_ids )),
1070
+ )
1034
1071
1035
1072
# Making an individual request for each of 1000s of events has a lot of
1036
1073
# overhead. On the other hand, we don't really want to fetch all of the events
@@ -1041,13 +1078,13 @@ async def _get_state_ids_after_missing_prev_event(
1041
1078
#
1042
1079
# TODO: might it be better to have an API which lets us do an aggregate event
1043
1080
# request
1044
- if (len (missing_events ) * 10 ) >= len (auth_event_ids ) + len (state_event_ids ):
1081
+ if (len (missing_event_ids ) * 10 ) >= len (auth_event_ids ) + len (state_event_ids ):
1045
1082
logger .debug ("Requesting complete state from remote" )
1046
1083
await self ._get_state_and_persist (destination , room_id , event_id )
1047
1084
else :
1048
- logger .debug ("Fetching %i events from remote" , len (missing_events ))
1085
+ logger .debug ("Fetching %i events from remote" , len (missing_event_ids ))
1049
1086
await self ._get_events_and_persist (
1050
- destination = destination , room_id = room_id , event_ids = missing_events
1087
+ destination = destination , room_id = room_id , event_ids = missing_event_ids
1051
1088
)
1052
1089
1053
1090
# We now need to fill out the state map, which involves fetching the
@@ -1104,6 +1141,14 @@ async def _get_state_ids_after_missing_prev_event(
1104
1141
event_id ,
1105
1142
failed_to_fetch ,
1106
1143
)
1144
+ set_tag (
1145
+ SynapseTags .RESULT_PREFIX + "failed_to_fetch" ,
1146
+ str (failed_to_fetch ),
1147
+ )
1148
+ set_tag (
1149
+ SynapseTags .RESULT_PREFIX + "failed_to_fetch.length" ,
1150
+ str (len (failed_to_fetch )),
1151
+ )
1107
1152
1108
1153
if remote_event .is_state () and remote_event .rejected_reason is None :
1109
1154
state_map [
@@ -1112,6 +1157,8 @@ async def _get_state_ids_after_missing_prev_event(
1112
1157
1113
1158
return state_map
1114
1159
1160
+ @trace
1161
+ @tag_args
1115
1162
async def _get_state_and_persist (
1116
1163
self , destination : str , room_id : str , event_id : str
1117
1164
) -> None :
@@ -1133,6 +1180,7 @@ async def _get_state_and_persist(
1133
1180
destination = destination , room_id = room_id , event_ids = (event_id ,)
1134
1181
)
1135
1182
1183
+ @trace
1136
1184
async def _process_received_pdu (
1137
1185
self ,
1138
1186
origin : str ,
@@ -1283,6 +1331,7 @@ async def _resync_device(self, sender: str) -> None:
1283
1331
except Exception :
1284
1332
logger .exception ("Failed to resync device for %s" , sender )
1285
1333
1334
+ @trace
1286
1335
async def _handle_marker_event (self , origin : str , marker_event : EventBase ) -> None :
1287
1336
"""Handles backfilling the insertion event when we receive a marker
1288
1337
event that points to one.
@@ -1414,6 +1463,8 @@ async def backfill_event_id(
1414
1463
1415
1464
return event_from_response
1416
1465
1466
+ @trace
1467
+ @tag_args
1417
1468
async def _get_events_and_persist (
1418
1469
self , destination : str , room_id : str , event_ids : Collection [str ]
1419
1470
) -> None :
@@ -1459,6 +1510,7 @@ async def get_event(event_id: str) -> None:
1459
1510
logger .info ("Fetched %i events of %i requested" , len (events ), len (event_ids ))
1460
1511
await self ._auth_and_persist_outliers (room_id , events )
1461
1512
1513
+ @trace
1462
1514
async def _auth_and_persist_outliers (
1463
1515
self , room_id : str , events : Iterable [EventBase ]
1464
1516
) -> None :
@@ -1477,6 +1529,16 @@ async def _auth_and_persist_outliers(
1477
1529
"""
1478
1530
event_map = {event .event_id : event for event in events }
1479
1531
1532
+ event_ids = event_map .keys ()
1533
+ set_tag (
1534
+ SynapseTags .FUNC_ARG_PREFIX + "event_ids" ,
1535
+ str (event_ids ),
1536
+ )
1537
+ set_tag (
1538
+ SynapseTags .FUNC_ARG_PREFIX + "event_ids.length" ,
1539
+ str (len (event_ids )),
1540
+ )
1541
+
1480
1542
# filter out any events we have already seen. This might happen because
1481
1543
# the events were eagerly pushed to us (eg, during a room join), or because
1482
1544
# another thread has raced against us since we decided to request the event.
@@ -1593,6 +1655,7 @@ async def prep(event: EventBase) -> None:
1593
1655
backfilled = True ,
1594
1656
)
1595
1657
1658
+ @trace
1596
1659
async def _check_event_auth (
1597
1660
self , origin : Optional [str ], event : EventBase , context : EventContext
1598
1661
) -> None :
@@ -1631,6 +1694,14 @@ async def _check_event_auth(
1631
1694
claimed_auth_events = await self ._load_or_fetch_auth_events_for_event (
1632
1695
origin , event
1633
1696
)
1697
+ set_tag (
1698
+ SynapseTags .RESULT_PREFIX + "claimed_auth_events" ,
1699
+ str ([ev .event_id for ev in claimed_auth_events ]),
1700
+ )
1701
+ set_tag (
1702
+ SynapseTags .RESULT_PREFIX + "claimed_auth_events.length" ,
1703
+ str (len (claimed_auth_events )),
1704
+ )
1634
1705
1635
1706
# ... and check that the event passes auth at those auth events.
1636
1707
# https://spec.matrix.org/v1.3/server-server-api/#checks-performed-on-receipt-of-a-pdu:
@@ -1728,6 +1799,7 @@ async def _check_event_auth(
1728
1799
)
1729
1800
context .rejected = RejectedReason .AUTH_ERROR
1730
1801
1802
+ @trace
1731
1803
async def _maybe_kick_guest_users (self , event : EventBase ) -> None :
1732
1804
if event .type != EventTypes .GuestAccess :
1733
1805
return
@@ -1935,6 +2007,8 @@ async def _load_or_fetch_auth_events_for_event(
1935
2007
# instead we raise an AuthError, which will make the caller ignore it.
1936
2008
raise AuthError (code = HTTPStatus .FORBIDDEN , msg = "Auth events could not be found" )
1937
2009
2010
+ @trace
2011
+ @tag_args
1938
2012
async def _get_remote_auth_chain_for_event (
1939
2013
self , destination : str , room_id : str , event_id : str
1940
2014
) -> None :
@@ -1963,6 +2037,7 @@ async def _get_remote_auth_chain_for_event(
1963
2037
1964
2038
await self ._auth_and_persist_outliers (room_id , remote_auth_events )
1965
2039
2040
+ @trace
1966
2041
async def _run_push_actions_and_persist_event (
1967
2042
self , event : EventBase , context : EventContext , backfilled : bool = False
1968
2043
) -> None :
@@ -2071,8 +2146,17 @@ async def persist_events_and_notify(
2071
2146
self ._message_handler .maybe_schedule_expiry (event )
2072
2147
2073
2148
if not backfilled : # Never notify for backfilled events
2074
- for event in events :
2075
- await self ._notify_persisted_event (event , max_stream_token )
2149
+ with start_active_span ("notify_persisted_events" ):
2150
+ set_tag (
2151
+ SynapseTags .RESULT_PREFIX + "event_ids" ,
2152
+ str ([ev .event_id for ev in events ]),
2153
+ )
2154
+ set_tag (
2155
+ SynapseTags .RESULT_PREFIX + "event_ids.length" ,
2156
+ str (len (events )),
2157
+ )
2158
+ for event in events :
2159
+ await self ._notify_persisted_event (event , max_stream_token )
2076
2160
2077
2161
return max_stream_token .stream
2078
2162
0 commit comments