11import ddtrace
22import tornado
3- import concurrent
43
54from wrapt import wrap_function_wrapper as _w
65
7- from . import handlers , application , decorators , template , futures , TracerStackContext
6+ from . import handlers , application , decorators , template , futures , compat , TracerStackContext
87from ...util import unwrap as _u
98
109
@@ -26,7 +25,7 @@ def patch():
2625 _w ('tornado.web' , 'RequestHandler.on_finish' , handlers .on_finish )
2726 _w ('tornado.web' , 'RequestHandler.log_exception' , handlers .log_exception )
2827
29- # patch Tornado decorators
28+ # patch Tornado concurrent modules
3029 _w ('tornado.concurrent' , 'run_on_executor' , decorators ._run_on_executor )
3130
3231 # patch Template system
@@ -36,7 +35,8 @@ def patch():
3635 # TODO: this may be a generic module and should be moved
3736 # in a separate contributions when we want to support multi-threading
3837 # context propagation
39- _w ('concurrent.futures' , 'ThreadPoolExecutor.submit' , futures ._wrap_submit )
38+ if compat .futures_available :
39+ _w ('concurrent.futures' , 'ThreadPoolExecutor.submit' , futures ._wrap_submit )
4040
4141 # configure the global tracer
4242 ddtrace .tracer .configure (
@@ -60,4 +60,6 @@ def unpatch():
6060 _u (tornado .web .Application , '__init__' )
6161 _u (tornado .concurrent , 'run_on_executor' )
6262 _u (tornado .template .Template , 'generate' )
63- _u (concurrent .futures .ThreadPoolExecutor , 'submit' )
63+
64+ if compat .futures_available :
65+ _u ('concurrent.futures.ThreadPoolExecutor' , 'submit' )
0 commit comments