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

Commit

Permalink
Return noop context manager if not tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
JorikSchellekens committed Jul 4, 2019
1 parent 24b3834 commit 386285b
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions synapse/util/tracerutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ def init_tracer(config):
The config used by the homserver. Here it's used to set the service
name to the homeserver's.
"""
global opentracing
if not config.tracer_config.get("tracer_enabled", False):
# We don't have a tracer
opentracing = None
return

if not opentracing:
Expand Down Expand Up @@ -148,7 +150,7 @@ def start_active_span(
finish_on_close=True,
):
if opentracing is None:
return _noop_context_manager
return _noop_context_manager()
else:
# We need to enter the scope here for the logcontext to become active
return opentracing.tracer.start_active_span(
Expand Down Expand Up @@ -232,6 +234,9 @@ def start_active_span_from_context(
# Twisted encodes the values as lists whereas opentracing doesn't.
# So, we take the first item in the list.
# Also, twisted uses byte arrays while opentracing expects strings.
if opentracing is None:
return _noop_context_manager()

header_dict = {k.decode(): v[0].decode() for k, v in headers.getAllRawHeaders()}
context = opentracing.tracer.extract(opentracing.Format.HTTP_HEADERS, header_dict)

Expand Down Expand Up @@ -313,7 +318,7 @@ def trace_servlet(func):
@wraps(func)
@defer.inlineCallbacks
def f(request, *args, **kwargs):
with start_active_span_from_context(
scope = start_active_span_from_context(
request.requestHeaders,
"incoming-client-request",
tags={
Expand All @@ -323,8 +328,16 @@ def f(request, *args, **kwargs):
tags.HTTP_URL: request.get_redacted_uri(),
tags.PEER_HOST_IPV6: request.getClientIP(),
},
):
)
# A context manager would be the most logical here but defer.returnValue
# raises an exception in order to provide the return value. This causes
# opentracing to mark each request as erroring, in order to avoid this we
# need to give the finally clause explicitly.
scope.__enter__()
try:
result = yield defer.maybeDeferred(func, request, *args, **kwargs)
defer.returnValue(result)
finally:
scope.__exit__(None, None, None)

return f

0 comments on commit 386285b

Please sign in to comment.