mirror of
https://github.com/element-hq/synapse.git
synced 2024-12-14 11:57:44 +00:00
Add opentracing types (#11603)
This commit is contained in:
parent
8428ef66c7
commit
8ad39438fa
5 changed files with 18 additions and 13 deletions
1
changelog.d/11603.misc
Normal file
1
changelog.d/11603.misc
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Add opentracing type stubs and fix associated mypy errors.
|
3
mypy.ini
3
mypy.ini
|
@ -308,9 +308,6 @@ ignore_missing_imports = True
|
||||||
[mypy-netaddr]
|
[mypy-netaddr]
|
||||||
ignore_missing_imports = True
|
ignore_missing_imports = True
|
||||||
|
|
||||||
[mypy-opentracing]
|
|
||||||
ignore_missing_imports = True
|
|
||||||
|
|
||||||
[mypy-parameterized.*]
|
[mypy-parameterized.*]
|
||||||
ignore_missing_imports = True
|
ignore_missing_imports = True
|
||||||
|
|
||||||
|
|
1
setup.py
1
setup.py
|
@ -107,6 +107,7 @@ CONDITIONAL_REQUIREMENTS["mypy"] = [
|
||||||
"mypy-zope==0.3.2",
|
"mypy-zope==0.3.2",
|
||||||
"types-bleach>=4.1.0",
|
"types-bleach>=4.1.0",
|
||||||
"types-jsonschema>=3.2.0",
|
"types-jsonschema>=3.2.0",
|
||||||
|
"types-opentracing>=2.4.2",
|
||||||
"types-Pillow>=8.3.4",
|
"types-Pillow>=8.3.4",
|
||||||
"types-pyOpenSSL>=20.0.7",
|
"types-pyOpenSSL>=20.0.7",
|
||||||
"types-PyYAML>=5.4.10",
|
"types-PyYAML>=5.4.10",
|
||||||
|
|
|
@ -222,8 +222,8 @@ try:
|
||||||
|
|
||||||
tags = opentracing.tags
|
tags = opentracing.tags
|
||||||
except ImportError:
|
except ImportError:
|
||||||
opentracing = None
|
opentracing = None # type: ignore[assignment]
|
||||||
tags = _DummyTagNames
|
tags = _DummyTagNames # type: ignore[assignment]
|
||||||
try:
|
try:
|
||||||
from jaeger_client import Config as JaegerConfig
|
from jaeger_client import Config as JaegerConfig
|
||||||
|
|
||||||
|
@ -366,7 +366,7 @@ def init_tracer(hs: "HomeServer"):
|
||||||
global opentracing
|
global opentracing
|
||||||
if not hs.config.tracing.opentracer_enabled:
|
if not hs.config.tracing.opentracer_enabled:
|
||||||
# We don't have a tracer
|
# We don't have a tracer
|
||||||
opentracing = None
|
opentracing = None # type: ignore[assignment]
|
||||||
return
|
return
|
||||||
|
|
||||||
if not opentracing or not JaegerConfig:
|
if not opentracing or not JaegerConfig:
|
||||||
|
@ -452,7 +452,7 @@ def start_active_span(
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if opentracing is None:
|
if opentracing is None:
|
||||||
return noop_context_manager()
|
return noop_context_manager() # type: ignore[unreachable]
|
||||||
|
|
||||||
return opentracing.tracer.start_active_span(
|
return opentracing.tracer.start_active_span(
|
||||||
operation_name,
|
operation_name,
|
||||||
|
@ -477,7 +477,7 @@ def start_active_span_follows_from(
|
||||||
forced, the new span will also have tracing forced.
|
forced, the new span will also have tracing forced.
|
||||||
"""
|
"""
|
||||||
if opentracing is None:
|
if opentracing is None:
|
||||||
return noop_context_manager()
|
return noop_context_manager() # type: ignore[unreachable]
|
||||||
|
|
||||||
references = [opentracing.follows_from(context) for context in contexts]
|
references = [opentracing.follows_from(context) for context in contexts]
|
||||||
scope = start_active_span(operation_name, references=references)
|
scope = start_active_span(operation_name, references=references)
|
||||||
|
@ -514,7 +514,7 @@ def start_active_span_from_request(
|
||||||
# Also, twisted uses byte arrays while opentracing expects strings.
|
# Also, twisted uses byte arrays while opentracing expects strings.
|
||||||
|
|
||||||
if opentracing is None:
|
if opentracing is None:
|
||||||
return noop_context_manager()
|
return noop_context_manager() # type: ignore[unreachable]
|
||||||
|
|
||||||
header_dict = {
|
header_dict = {
|
||||||
k.decode(): v[0].decode() for k, v in request.requestHeaders.getAllRawHeaders()
|
k.decode(): v[0].decode() for k, v in request.requestHeaders.getAllRawHeaders()
|
||||||
|
@ -553,7 +553,7 @@ def start_active_span_from_edu(
|
||||||
references = references or []
|
references = references or []
|
||||||
|
|
||||||
if opentracing is None:
|
if opentracing is None:
|
||||||
return noop_context_manager()
|
return noop_context_manager() # type: ignore[unreachable]
|
||||||
|
|
||||||
carrier = json_decoder.decode(edu_content.get("context", "{}")).get(
|
carrier = json_decoder.decode(edu_content.get("context", "{}")).get(
|
||||||
"opentracing", {}
|
"opentracing", {}
|
||||||
|
@ -594,18 +594,21 @@ def active_span():
|
||||||
@ensure_active_span("set a tag")
|
@ensure_active_span("set a tag")
|
||||||
def set_tag(key, value):
|
def set_tag(key, value):
|
||||||
"""Sets a tag on the active span"""
|
"""Sets a tag on the active span"""
|
||||||
|
assert opentracing.tracer.active_span is not None
|
||||||
opentracing.tracer.active_span.set_tag(key, value)
|
opentracing.tracer.active_span.set_tag(key, value)
|
||||||
|
|
||||||
|
|
||||||
@ensure_active_span("log")
|
@ensure_active_span("log")
|
||||||
def log_kv(key_values, timestamp=None):
|
def log_kv(key_values, timestamp=None):
|
||||||
"""Log to the active span"""
|
"""Log to the active span"""
|
||||||
|
assert opentracing.tracer.active_span is not None
|
||||||
opentracing.tracer.active_span.log_kv(key_values, timestamp)
|
opentracing.tracer.active_span.log_kv(key_values, timestamp)
|
||||||
|
|
||||||
|
|
||||||
@ensure_active_span("set the traces operation name")
|
@ensure_active_span("set the traces operation name")
|
||||||
def set_operation_name(operation_name):
|
def set_operation_name(operation_name):
|
||||||
"""Sets the operation name of the active span"""
|
"""Sets the operation name of the active span"""
|
||||||
|
assert opentracing.tracer.active_span is not None
|
||||||
opentracing.tracer.active_span.set_operation_name(operation_name)
|
opentracing.tracer.active_span.set_operation_name(operation_name)
|
||||||
|
|
||||||
|
|
||||||
|
@ -674,6 +677,7 @@ def inject_header_dict(
|
||||||
span = opentracing.tracer.active_span
|
span = opentracing.tracer.active_span
|
||||||
|
|
||||||
carrier: Dict[str, str] = {}
|
carrier: Dict[str, str] = {}
|
||||||
|
assert span is not None
|
||||||
opentracing.tracer.inject(span.context, opentracing.Format.HTTP_HEADERS, carrier)
|
opentracing.tracer.inject(span.context, opentracing.Format.HTTP_HEADERS, carrier)
|
||||||
|
|
||||||
for key, value in carrier.items():
|
for key, value in carrier.items():
|
||||||
|
@ -716,6 +720,7 @@ def get_active_span_text_map(destination=None):
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
carrier: Dict[str, str] = {}
|
carrier: Dict[str, str] = {}
|
||||||
|
assert opentracing.tracer.active_span is not None
|
||||||
opentracing.tracer.inject(
|
opentracing.tracer.inject(
|
||||||
opentracing.tracer.active_span.context, opentracing.Format.TEXT_MAP, carrier
|
opentracing.tracer.active_span.context, opentracing.Format.TEXT_MAP, carrier
|
||||||
)
|
)
|
||||||
|
@ -731,6 +736,7 @@ def active_span_context_as_string():
|
||||||
"""
|
"""
|
||||||
carrier: Dict[str, str] = {}
|
carrier: Dict[str, str] = {}
|
||||||
if opentracing:
|
if opentracing:
|
||||||
|
assert opentracing.tracer.active_span is not None
|
||||||
opentracing.tracer.inject(
|
opentracing.tracer.inject(
|
||||||
opentracing.tracer.active_span.context, opentracing.Format.TEXT_MAP, carrier
|
opentracing.tracer.active_span.context, opentracing.Format.TEXT_MAP, carrier
|
||||||
)
|
)
|
||||||
|
@ -773,7 +779,7 @@ def trace(func=None, opname=None):
|
||||||
|
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
if opentracing is None:
|
if opentracing is None:
|
||||||
return func
|
return func # type: ignore[unreachable]
|
||||||
|
|
||||||
_opname = opname if opname else func.__name__
|
_opname = opname if opname else func.__name__
|
||||||
|
|
||||||
|
@ -864,7 +870,7 @@ def trace_servlet(request: "SynapseRequest", extract_context: bool = False):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if opentracing is None:
|
if opentracing is None:
|
||||||
yield
|
yield # type: ignore[unreachable]
|
||||||
return
|
return
|
||||||
|
|
||||||
request_tags = {
|
request_tags = {
|
||||||
|
|
|
@ -71,7 +71,7 @@ class LogContextScopeManager(ScopeManager):
|
||||||
if not ctx:
|
if not ctx:
|
||||||
# We don't want this scope to affect.
|
# We don't want this scope to affect.
|
||||||
logger.error("Tried to activate scope outside of loggingcontext")
|
logger.error("Tried to activate scope outside of loggingcontext")
|
||||||
return Scope(None, span)
|
return Scope(None, span) # type: ignore[arg-type]
|
||||||
elif ctx.scope is not None:
|
elif ctx.scope is not None:
|
||||||
# We want the logging scope to look exactly the same so we give it
|
# We want the logging scope to look exactly the same so we give it
|
||||||
# a blank suffix
|
# a blank suffix
|
||||||
|
|
Loading…
Reference in a new issue