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

Commit 01be194

Browse files
author
David Robertson
committed
Batch of easier annotations
1 parent c51915a commit 01be194

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

synapse/config/tracer.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Any, Set
15+
from typing import Any, List, Set
1616

1717
from synapse.types import JsonDict
1818
from synapse.util.check_dependencies import DependencyException, check_requirements
@@ -49,7 +49,9 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
4949

5050
# The tracer is enabled so sanitize the config
5151

52-
self.opentracer_whitelist = opentracing_config.get("homeserver_whitelist", [])
52+
self.opentracer_whitelist: List[str] = opentracing_config.get(
53+
"homeserver_whitelist", []
54+
)
5355
if not isinstance(self.opentracer_whitelist, list):
5456
raise ConfigError("Tracer homeserver_whitelist config is malformed")
5557

synapse/logging/opentracing.py

+26-20
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,18 @@ def set_fates(clotho, lachesis, atropos, father="Zues", mother="Themis"):
170170
from functools import wraps
171171
from typing import (
172172
TYPE_CHECKING,
173+
Any,
173174
Callable,
174175
Collection,
175176
Dict,
177+
Generator,
178+
Iterable,
176179
List,
177180
Optional,
178181
Pattern,
179182
Type,
180183
TypeVar,
184+
Union,
181185
)
182186

183187
import attr
@@ -374,7 +378,7 @@ def ensure_active_span_inner_2(*args, **kwargs):
374378
# Setup
375379

376380

377-
def init_tracer(hs: "HomeServer"):
381+
def init_tracer(hs: "HomeServer") -> None:
378382
"""Set the whitelists and initialise the JaegerClient tracer"""
379383
global opentracing
380384
if not hs.config.tracing.opentracer_enabled:
@@ -416,11 +420,11 @@ def init_tracer(hs: "HomeServer"):
416420

417421

418422
@only_if_tracing
419-
def set_homeserver_whitelist(homeserver_whitelist):
423+
def set_homeserver_whitelist(homeserver_whitelist: Iterable[str]) -> None:
420424
"""Sets the homeserver whitelist
421425
422426
Args:
423-
homeserver_whitelist (Iterable[str]): regex of whitelisted homeservers
427+
homeserver_whitelist: regexes specifying whitelisted homeservers
424428
"""
425429
global _homeserver_whitelist
426430
if homeserver_whitelist:
@@ -431,15 +435,15 @@ def set_homeserver_whitelist(homeserver_whitelist):
431435

432436

433437
@only_if_tracing
434-
def whitelisted_homeserver(destination):
438+
def whitelisted_homeserver(destination: str) -> bool:
435439
"""Checks if a destination matches the whitelist
436440
437441
Args:
438-
destination (str)
442+
destination
439443
"""
440444

441445
if _homeserver_whitelist:
442-
return _homeserver_whitelist.match(destination)
446+
return _homeserver_whitelist.match(destination) is not None
443447
return False
444448

445449

@@ -586,27 +590,27 @@ def start_active_span_from_edu(
586590

587591
# Opentracing setters for tags, logs, etc
588592
@only_if_tracing
589-
def active_span():
593+
def active_span() -> Optional["opentracing.Span"]:
590594
"""Get the currently active span, if any"""
591595
return opentracing.tracer.active_span
592596

593597

594598
@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:
596600
"""Sets a tag on the active span"""
597601
assert opentracing.tracer.active_span is not None
598602
opentracing.tracer.active_span.set_tag(key, value)
599603

600604

601605
@ensure_active_span("log")
602-
def log_kv(key_values, timestamp=None):
606+
def log_kv(key_values: Dict[str, Any], timestamp=None) -> None:
603607
"""Log to the active span"""
604608
assert opentracing.tracer.active_span is not None
605609
opentracing.tracer.active_span.log_kv(key_values, timestamp)
606610

607611

608612
@ensure_active_span("set the traces operation name")
609-
def set_operation_name(operation_name):
613+
def set_operation_name(operation_name: str) -> None:
610614
"""Sets the operation name of the active span"""
611615
assert opentracing.tracer.active_span is not None
612616
opentracing.tracer.active_span.set_operation_name(operation_name)
@@ -758,21 +762,21 @@ def span_context_from_request(request: Request) -> "Optional[opentracing.SpanCon
758762

759763

760764
@only_if_tracing
761-
def span_context_from_string(carrier):
765+
def span_context_from_string(carrier: str) -> Optional["opentracing.SpanContext"]:
762766
"""
763767
Returns:
764768
The active span context decoded from a string.
765769
"""
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)
768772

769773

770774
@only_if_tracing
771-
def extract_text_map(carrier):
775+
def extract_text_map(carrier: Dict[str, str]) -> Optional["opentracing.SpanContext"]:
772776
"""
773777
Wrapper method for opentracing's tracer.extract for TEXT_MAP.
774778
Args:
775-
carrier (dict): a dict possibly containing a span context.
779+
carrier: a dict possibly containing a span context.
776780
777781
Returns:
778782
The active span context extracted from carrier.
@@ -851,7 +855,7 @@ def err_back(result):
851855
return decorator
852856

853857

854-
def tag_args(func):
858+
def tag_args(func: Callable[P, R]) -> Callable[P, R]:
855859
"""
856860
Tags all of the args to the active span.
857861
"""
@@ -860,19 +864,21 @@ def tag_args(func):
860864
return func
861865

862866
@wraps(func)
863-
def _tag_args_inner(*args, **kwargs):
867+
def _tag_args_inner(*args: P.args, **kwargs: P.kwargs) -> R:
864868
argspec = inspect.getfullargspec(func)
865869
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]
868872
set_tag("kwargs", kwargs)
869873
return func(*args, **kwargs)
870874

871875
return _tag_args_inner
872876

873877

874878
@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]:
876882
"""Returns a context manager which traces a request. It starts a span
877883
with some servlet specific tags such as the request metrics name and
878884
request information.

0 commit comments

Comments
 (0)