15
15
import functools
16
16
import logging
17
17
import re
18
+ import time
18
19
from typing import TYPE_CHECKING , Any , Awaitable , Callable , Optional , Tuple , cast
19
20
20
21
from synapse .api .errors import Codes , FederationDeniedError , SynapseError
24
25
from synapse .http .site import SynapseRequest
25
26
from synapse .logging .context import run_in_background
26
27
from synapse .logging .opentracing import (
28
+ active_span ,
27
29
set_tag ,
28
30
span_context_from_request ,
31
+ start_active_span ,
29
32
start_active_span_follows_from ,
30
33
whitelisted_homeserver ,
31
34
)
@@ -265,9 +268,10 @@ async def new_func(
265
268
content = parse_json_object_from_request (request )
266
269
267
270
try :
268
- origin : Optional [str ] = await authenticator .authenticate_request (
269
- request , content
270
- )
271
+ with start_active_span ("authenticate_request" ):
272
+ origin : Optional [str ] = await authenticator .authenticate_request (
273
+ request , content
274
+ )
271
275
except NoAuthenticationError :
272
276
origin = None
273
277
if self .REQUIRE_AUTH :
@@ -282,32 +286,57 @@ async def new_func(
282
286
# update the active opentracing span with the authenticated entity
283
287
set_tag ("authenticated_entity" , origin )
284
288
285
- # if the origin is authenticated and whitelisted, link to its span context
289
+ # if the origin is authenticated and whitelisted, use its span context
290
+ # as the parent.
286
291
context = None
287
292
if origin and whitelisted_homeserver (origin ):
288
293
context = span_context_from_request (request )
289
294
290
- scope = start_active_span_follows_from (
291
- "incoming-federation-request" , contexts = (context ,) if context else ()
292
- )
295
+ if context :
296
+ servlet_span = active_span ()
297
+ # a scope which uses the origin's context as a parent
298
+ processing_start_time = time .time ()
299
+ scope = start_active_span_follows_from (
300
+ "incoming-federation-request" ,
301
+ child_of = context ,
302
+ contexts = (servlet_span ,),
303
+ start_time = processing_start_time ,
304
+ )
293
305
294
- with scope :
295
- if origin and self .RATELIMIT :
296
- with ratelimiter .ratelimit (origin ) as d :
297
- await d
298
- if request ._disconnected :
299
- logger .warning (
300
- "client disconnected before we started processing "
301
- "request"
306
+ else :
307
+ # just use our context as a parent
308
+ scope = start_active_span (
309
+ "incoming-federation-request" ,
310
+ )
311
+
312
+ try :
313
+ with scope :
314
+ if origin and self .RATELIMIT :
315
+ with ratelimiter .ratelimit (origin ) as d :
316
+ await d
317
+ if request ._disconnected :
318
+ logger .warning (
319
+ "client disconnected before we started processing "
320
+ "request"
321
+ )
322
+ return None
323
+ response = await func (
324
+ origin , content , request .args , * args , ** kwargs
302
325
)
303
- return None
326
+ else :
304
327
response = await func (
305
328
origin , content , request .args , * args , ** kwargs
306
329
)
307
- else :
308
- response = await func (
309
- origin , content , request .args , * args , ** kwargs
330
+ finally :
331
+ # if we used the origin's context as the parent, add a new span using
332
+ # the servlet span as a parent, so that we have a link
333
+ if context :
334
+ scope2 = start_active_span_follows_from (
335
+ "process-federation_request" ,
336
+ contexts = (scope .span ,),
337
+ start_time = processing_start_time ,
310
338
)
339
+ scope2 .close ()
311
340
312
341
return response
313
342
0 commit comments