@@ -170,14 +170,18 @@ def set_fates(clotho, lachesis, atropos, father="Zues", mother="Themis"):
170
170
from functools import wraps
171
171
from typing import (
172
172
TYPE_CHECKING ,
173
+ Any ,
173
174
Callable ,
174
175
Collection ,
175
176
Dict ,
177
+ Generator ,
178
+ Iterable ,
176
179
List ,
177
180
Optional ,
178
181
Pattern ,
179
182
Type ,
180
183
TypeVar ,
184
+ Union ,
181
185
)
182
186
183
187
import attr
@@ -374,7 +378,7 @@ def ensure_active_span_inner_2(*args, **kwargs):
374
378
# Setup
375
379
376
380
377
- def init_tracer (hs : "HomeServer" ):
381
+ def init_tracer (hs : "HomeServer" ) -> None :
378
382
"""Set the whitelists and initialise the JaegerClient tracer"""
379
383
global opentracing
380
384
if not hs .config .tracing .opentracer_enabled :
@@ -416,11 +420,11 @@ def init_tracer(hs: "HomeServer"):
416
420
417
421
418
422
@only_if_tracing
419
- def set_homeserver_whitelist (homeserver_whitelist ) :
423
+ def set_homeserver_whitelist (homeserver_whitelist : Iterable [ str ]) -> None :
420
424
"""Sets the homeserver whitelist
421
425
422
426
Args:
423
- homeserver_whitelist (Iterable[str]): regex of whitelisted homeservers
427
+ homeserver_whitelist: regexes specifying whitelisted homeservers
424
428
"""
425
429
global _homeserver_whitelist
426
430
if homeserver_whitelist :
@@ -431,15 +435,15 @@ def set_homeserver_whitelist(homeserver_whitelist):
431
435
432
436
433
437
@only_if_tracing
434
- def whitelisted_homeserver (destination ) :
438
+ def whitelisted_homeserver (destination : str ) -> bool :
435
439
"""Checks if a destination matches the whitelist
436
440
437
441
Args:
438
- destination (str)
442
+ destination
439
443
"""
440
444
441
445
if _homeserver_whitelist :
442
- return _homeserver_whitelist .match (destination )
446
+ return _homeserver_whitelist .match (destination ) is not None
443
447
return False
444
448
445
449
@@ -586,27 +590,27 @@ def start_active_span_from_edu(
586
590
587
591
# Opentracing setters for tags, logs, etc
588
592
@only_if_tracing
589
- def active_span ():
593
+ def active_span () -> Optional [ "opentracing.Span" ] :
590
594
"""Get the currently active span, if any"""
591
595
return opentracing .tracer .active_span
592
596
593
597
594
598
@ensure_active_span ("set a tag" )
595
- def set_tag (key , value ) :
599
+ def set_tag (key : str , value : Union [ str , bool , int , float ]) -> None :
596
600
"""Sets a tag on the active span"""
597
601
assert opentracing .tracer .active_span is not None
598
602
opentracing .tracer .active_span .set_tag (key , value )
599
603
600
604
601
605
@ensure_active_span ("log" )
602
- def log_kv (key_values , timestamp = None ):
606
+ def log_kv (key_values : Dict [ str , Any ], timestamp = None ) -> None :
603
607
"""Log to the active span"""
604
608
assert opentracing .tracer .active_span is not None
605
609
opentracing .tracer .active_span .log_kv (key_values , timestamp )
606
610
607
611
608
612
@ensure_active_span ("set the traces operation name" )
609
- def set_operation_name (operation_name ) :
613
+ def set_operation_name (operation_name : str ) -> None :
610
614
"""Sets the operation name of the active span"""
611
615
assert opentracing .tracer .active_span is not None
612
616
opentracing .tracer .active_span .set_operation_name (operation_name )
@@ -758,21 +762,21 @@ def span_context_from_request(request: Request) -> "Optional[opentracing.SpanCon
758
762
759
763
760
764
@only_if_tracing
761
- def span_context_from_string (carrier ) :
765
+ def span_context_from_string (carrier : str ) -> Optional [ "opentracing.SpanContext" ] :
762
766
"""
763
767
Returns:
764
768
The active span context decoded from a string.
765
769
"""
766
- carrier = json_decoder .decode (carrier )
767
- return opentracing .tracer .extract (opentracing .Format .TEXT_MAP , carrier )
770
+ payload : Dict [ str , str ] = json_decoder .decode (carrier )
771
+ return opentracing .tracer .extract (opentracing .Format .TEXT_MAP , payload )
768
772
769
773
770
774
@only_if_tracing
771
- def extract_text_map (carrier ) :
775
+ def extract_text_map (carrier : Dict [ str , str ]) -> Optional [ "opentracing.SpanContext" ] :
772
776
"""
773
777
Wrapper method for opentracing's tracer.extract for TEXT_MAP.
774
778
Args:
775
- carrier (dict) : a dict possibly containing a span context.
779
+ carrier: a dict possibly containing a span context.
776
780
777
781
Returns:
778
782
The active span context extracted from carrier.
@@ -851,7 +855,7 @@ def err_back(result):
851
855
return decorator
852
856
853
857
854
- def tag_args (func ) :
858
+ def tag_args (func : Callable [ P , R ]) -> Callable [ P , R ] :
855
859
"""
856
860
Tags all of the args to the active span.
857
861
"""
@@ -860,19 +864,21 @@ def tag_args(func):
860
864
return func
861
865
862
866
@wraps (func )
863
- def _tag_args_inner (* args , ** kwargs ) :
867
+ def _tag_args_inner (* args : P . args , ** kwargs : P . kwargs ) -> R :
864
868
argspec = inspect .getfullargspec (func )
865
869
for i , arg in enumerate (argspec .args [1 :]):
866
- set_tag ("ARG_" + arg , args [i ])
867
- set_tag ("args" , args [len (argspec .args ) :])
870
+ set_tag ("ARG_" + arg , args [i ]) # type: ignore[index]
871
+ set_tag ("args" , args [len (argspec .args ) :]) # type: ignore[index]
868
872
set_tag ("kwargs" , kwargs )
869
873
return func (* args , ** kwargs )
870
874
871
875
return _tag_args_inner
872
876
873
877
874
878
@contextlib .contextmanager
875
- def trace_servlet (request : "SynapseRequest" , extract_context : bool = False ):
879
+ def trace_servlet (
880
+ request : "SynapseRequest" , extract_context : bool = False
881
+ ) -> Generator [None , None , None ]:
876
882
"""Returns a context manager which traces a request. It starts a span
877
883
with some servlet specific tags such as the request metrics name and
878
884
request information.
0 commit comments