17
17
import opentracing
18
18
except ImportError :
19
19
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
20
26
27
+ import contextlib
21
28
import logging
22
29
import re
23
30
from functools import wraps
@@ -91,8 +98,7 @@ def init_tracer(config):
91
98
92
99
if not opentracing :
93
100
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."
96
102
)
97
103
raise ModuleNotFoundError ("opentracing" )
98
104
@@ -101,15 +107,10 @@ def init_tracer(config):
101
107
102
108
103
109
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 :
108
111
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."
111
113
)
112
- raise
113
114
114
115
# Include the worker name
115
116
name = config .worker_name if config .worker_name else "master"
@@ -129,8 +130,12 @@ def setup_tags():
129
130
tags = opentracing .tags
130
131
131
132
133
+ @contextlib .contextmanager
134
+ def _noop_context_manager (* args , ** kwargs ):
135
+ yield
136
+
137
+
132
138
# Could use kwargs but I want these to be explicit
133
- @only_if_tracing
134
139
def start_active_span (
135
140
operation_name ,
136
141
child_of = None ,
@@ -140,16 +145,19 @@ def start_active_span(
140
145
ignore_active_span = False ,
141
146
finish_on_close = True ,
142
147
):
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
+ )
153
161
154
162
155
163
@only_if_tracing
@@ -203,7 +211,6 @@ def whitelisted_homeserver(destination):
203
211
return False
204
212
205
213
206
- @only_if_tracing
207
214
def start_active_span_from_context (
208
215
headers ,
209
216
operation_name ,
@@ -226,7 +233,7 @@ def start_active_span_from_context(
226
233
header_dict = {k .decode (): v [0 ].decode () for k , v in headers .getAllRawHeaders ()}
227
234
context = opentracing .tracer .extract (opentracing .Format .HTTP_HEADERS , header_dict )
228
235
229
- opentracing .tracer .start_active_span (
236
+ return opentracing .tracer .start_active_span (
230
237
operation_name ,
231
238
child_of = context ,
232
239
references = references ,
0 commit comments