2121from synapse .api .ratelimiting import Ratelimiter
2222from synapse .logging .context import run_in_background
2323from synapse .logging .opentracing import (
24+ SynapseTags ,
2425 get_active_span_text_map ,
2526 log_kv ,
2627 set_tag ,
27- start_active_span ,
2828)
2929from synapse .replication .http .devices import ReplicationUserDevicesResyncRestServlet
3030from synapse .types import JsonDict , Requester , UserID , get_domain_from_id
@@ -183,7 +183,10 @@ async def send_device_message(
183183 ) -> None :
184184 sender_user_id = requester .user .to_string ()
185185
186- set_tag ("number_of_messages" , len (messages ))
186+ message_id = random_string (16 )
187+ set_tag (SynapseTags .TO_DEVICE_MESSAGE_ID , message_id )
188+
189+ log_kv ({"number_of_to_device_messages" : len (messages )})
187190 set_tag ("sender" , sender_user_id )
188191 local_messages = {}
189192 remote_messages = {} # type: Dict[str, Dict[str, Dict[str, JsonDict]]]
@@ -205,32 +208,35 @@ async def send_device_message(
205208 "content" : message_content ,
206209 "type" : message_type ,
207210 "sender" : sender_user_id ,
211+ "message_id" : message_id ,
208212 }
209213 for device_id , message_content in by_device .items ()
210214 }
211215 if messages_by_device :
212216 local_messages [user_id ] = messages_by_device
217+ log_kv (
218+ {
219+ "user_id" : user_id ,
220+ "device_id" : list (messages_by_device ),
221+ }
222+ )
213223 else :
214224 destination = get_domain_from_id (user_id )
215225 remote_messages .setdefault (destination , {})[user_id ] = by_device
216226
217- message_id = random_string (16 )
218-
219227 context = get_active_span_text_map ()
220228
221229 remote_edu_contents = {}
222230 for destination , messages in remote_messages .items ():
223- with start_active_span ("to_device_for_user" ):
224- set_tag ("destination" , destination )
225- remote_edu_contents [destination ] = {
226- "messages" : messages ,
227- "sender" : sender_user_id ,
228- "type" : message_type ,
229- "message_id" : message_id ,
230- "org.matrix.opentracing_context" : json_encoder .encode (context ),
231- }
231+ log_kv ({"destination" : destination })
232+ remote_edu_contents [destination ] = {
233+ "messages" : messages ,
234+ "sender" : sender_user_id ,
235+ "type" : message_type ,
236+ "message_id" : message_id ,
237+ "org.matrix.opentracing_context" : json_encoder .encode (context ),
238+ }
232239
233- log_kv ({"local_messages" : local_messages })
234240 stream_id = await self .store .add_messages_to_device_inbox (
235241 local_messages , remote_edu_contents
236242 )
@@ -239,7 +245,6 @@ async def send_device_message(
239245 "to_device_key" , stream_id , users = local_messages .keys ()
240246 )
241247
242- log_kv ({"remote_messages" : remote_messages })
243248 if self .federation_sender :
244249 for destination in remote_messages .keys ():
245250 # Enqueue a new federation transaction to send the new
0 commit comments