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

Commit 14376e7

Browse files
Return a contextmanager
1 parent e111dcd commit 14376e7

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

synapse/util/tracerutils.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@
1717
import opentracing
1818
except ImportError:
1919
opentracing = None
20+
try:
21+
from jaeger_client import Config as JaegerConfig
22+
from synapse.util.scopecontextmanager import LogContextScopeManager
23+
except ImportError:
24+
JaegerConfig = None
25+
LogContextScopeManager = None
2026

27+
import contextlib
2128
import logging
2229
import re
2330
from functools import wraps
@@ -91,8 +98,7 @@ def init_tracer(config):
9198

9299
if not opentracing:
93100
logger.error(
94-
"The server has been configure to use opentracing but "
95-
"the %s module has not been installed.", e.name
101+
"The server has been configure to use opentracing but opentracing is not installed."
96102
)
97103
raise ModuleNotFoundError("opentracing")
98104

@@ -101,15 +107,10 @@ def init_tracer(config):
101107

102108

103109
def setup_tracing(config):
104-
try:
105-
from jaeger_client import Config as JaegerConfig
106-
from synapse.util.scopecontextmanager import LogContextScopeManager
107-
except ImportError as e:
110+
if not JaegerConfig:
108111
logger.error(
109-
"The server has been configure to use opentracing but "
110-
"the %s module has not been installed.", e.name
112+
"The server has been configure to use opentracing but opentracing is not installed."
111113
)
112-
raise
113114

114115
# Include the worker name
115116
name = config.worker_name if config.worker_name else "master"
@@ -129,8 +130,12 @@ def setup_tags():
129130
tags = opentracing.tags
130131

131132

133+
@contextlib.contextmanager
134+
def _noop_context_manager(*args, **kwargs):
135+
yield
136+
137+
132138
# Could use kwargs but I want these to be explicit
133-
@only_if_tracing
134139
def start_active_span(
135140
operation_name,
136141
child_of=None,
@@ -140,16 +145,19 @@ def start_active_span(
140145
ignore_active_span=False,
141146
finish_on_close=True,
142147
):
143-
# We need to enter the scope here for the logcontext to become active
144-
opentracing.tracer.start_active_span(
145-
operation_name,
146-
child_of=child_of,
147-
references=references,
148-
tags=tags,
149-
start_time=start_time,
150-
ignore_active_span=ignore_active_span,
151-
finish_on_close=finish_on_close,
152-
).__enter__()
148+
if opentracing is None:
149+
return _noop_context_manager
150+
else:
151+
# We need to enter the scope here for the logcontext to become active
152+
return opentracing.tracer.start_active_span(
153+
operation_name,
154+
child_of=child_of,
155+
references=references,
156+
tags=tags,
157+
start_time=start_time,
158+
ignore_active_span=ignore_active_span,
159+
finish_on_close=finish_on_close,
160+
)
153161

154162

155163
@only_if_tracing
@@ -203,7 +211,6 @@ def whitelisted_homeserver(destination):
203211
return False
204212

205213

206-
@only_if_tracing
207214
def start_active_span_from_context(
208215
headers,
209216
operation_name,
@@ -226,7 +233,7 @@ def start_active_span_from_context(
226233
header_dict = {k.decode(): v[0].decode() for k, v in headers.getAllRawHeaders()}
227234
context = opentracing.tracer.extract(opentracing.Format.HTTP_HEADERS, header_dict)
228235

229-
opentracing.tracer.start_active_span(
236+
return opentracing.tracer.start_active_span(
230237
operation_name,
231238
child_of=context,
232239
references=references,

0 commit comments

Comments
 (0)