Skip to content
This repository was archived by the owner on Sep 23, 2025. It is now read-only.

Commit 4fa6c10

Browse files
committed
Add support for Tornado 6
This PR adds support for Tornado 6 by conditionally using different scope manager, context manager and tracing implementation depending on the version of Tornado and Python being used. It does not require existing users to change anything other than upgrade to the latest version of this package. This package used to use the TornadoScopeManager shipped by opentracing-python. The scope manager used `tornado.stack_context` which was deprecated in Tornado 5 and removed in Tornado 6. Tornado now recommends using contextvars package introduced in Python3.7. opentracing-python already provides a ContextVarsScopeManager that builds on top of the contextvars package. It also implements AsyncioScopeManager which builds on top of asyncio and falls back on thread local storage to implement context propagation. We fallback on this for Python 3.6 and older when using Tornado 6 and newer. The package also had seen some decay and some tests were not passing. This PR updates the test suite and unit tests to get them working again. Changes this PR introduces: - Default to ContextVarsScopeManager instead of TornadoScopeManager. Fallback on TornadoScopeManager or AsyncioScopeManager based on the Tornado and Python version. - Added tox support to enable easier testing across Python and Tornado versions. - Updated travis config to work with tox environments. Now each travis build will run tests on every supported python version in parallel. Each parallel test will run all tests for all versions of tornado serially. - The PR add some code that uses the new async/await syntax. Such code is invalid for older versions of python. To make it works for all versions, we conditionally import modules depending on the Python interpreter version. - To preserve backward compatibility and to keep using common code for all tornado versions, we've added some noop implementations that are not to be used with newer versions of tornado. - `tornado.gen.coroutine` was deprecated in favour of async/await but we still support it where we can. There is a bug in Tornado 6 that prevents us from support the deprecated feature on Python3.7 with ContextVarsScopeManager. (tornadoweb/tornado#2716) - Python3.4 also does not pass the tests for `tornado.gen.coroutine` but it is not a regression caused by this PR. Testing on master results in the same behavior. For now, I've added skip markers to these tests on Python3.4. If needed, we can look into supporting these in future in a separate PR.
1 parent 2c87f42 commit 4fa6c10

26 files changed

+780
-257
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
.coverage
2+
.python-version
3+
.tox
14
*.pyc
5+
.vscode
26
dist
37
bin
48
eggs
59
lib
610
*.egg-info
711
build
812
env/
13+
venv/
14+
.pytest_cache/*

.travis.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
language: python
22
python:
33
- "2.7"
4+
- "3.4"
5+
- "3.5"
6+
- "3.6"
7+
- "3.7"
8+
- "3.8"
49

510
sudo: required
611

712
install:
13+
- pip install tox tox-travis
814
- make bootstrap
915

1016
script:
11-
- make test lint
17+
- make test

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ clean-test:
3838
lint:
3939
flake8 $(project) tests
4040

41-
test:
41+
test-local:
4242
py.test -s --cov-report term-missing:skip-covered --cov=$(project)
4343

44+
test:
45+
tox
46+
4447
build:
4548
python setup.py build
4649

README.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ In order to implement tracing in your system (for all the requests), add the fol
2525

2626
.. code-block:: python
2727
28-
from opentracing.scope_managers.tornado import TornadoScopeManager
2928
import tornado_opentracing
29+
from tornado_opentracing.scope_managers import TornadoScopeManager
3030
3131
# Create your opentracing tracer using TornadoScopeManager for active Span handling.
3232
tracer = SomeOpenTracingTracer(scope_manager=TornadoScopeManager())
@@ -48,7 +48,7 @@ In order to implement tracing in your system (for all the requests), add the fol
4848
''' Other parameters here '''
4949
opentracing_tracer_callable='opentracing.mocktracer.MockTracer',
5050
opentracing_tracer_parameters={
51-
'scope_manager': opentracing.scope_managers.TornadoScopeManager(),
51+
'scope_manager': tornado_opentracing.scope_managers.TornadoScopeManager(),
5252
},
5353
)
5454
@@ -115,11 +115,11 @@ For applications tracing individual requests, or using only the http client (no
115115
Active Span handling
116116
====================
117117

118-
For active ``Span`` handling and propagation, your ``Tracer`` should use ``opentracing.scope_managers.tornado.TornadoScopeManager``. Tracing both all requests and individual requests will set up a proper stack context automatically, and the active ``Span`` will be propagated from parent coroutines to their children. In any other case, code needs to be run under ``tracer_stack_context()`` explicitly:
118+
For active ``Span`` handling and propagation, your ``Tracer`` should use ``tornado_opentracing.scope_managers.TornadoScopeManager``. Tracing both all requests and individual requests will set up a proper stack context automatically, and the active ``Span`` will be propagated from parent coroutines to their children. In any other case, code needs to be run under ``tracer_stack_context()`` explicitly:
119119

120120
.. code-block:: python
121121
122-
from opentracing.scope_managers.tornado import tracer_stack_context
122+
from tornado_opentracing.scope_managers import tracer_stack_context
123123
124124
with tracer_stack_context():
125125
ioloop.IOLoop.current().run_sync(main_func)

VERSION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
1.0.1
1+
1.0.1
2+

examples/client-server/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from tornado import gen
55

66
import opentracing
7-
from opentracing.scope_managers.tornado import TornadoScopeManager
87
import tornado_opentracing
8+
from tornado_opentracing.scope_managers import TornadoScopeManager
99

1010

1111
def client_start_span_cb(span, request):

examples/simple/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from tornado import gen
44

55
import opentracing
6-
from opentracing.scope_managers.tornado import TornadoScopeManager
76
import tornado_opentracing
7+
from tornado_opentracing.scope_managers import TornadoScopeManager
88

99

1010
tornado_opentracing.init_tracing()

setup.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
platforms='any',
1616
install_requires=[
1717
'tornado',
18-
'opentracing>=2.0,<2.1',
18+
'opentracing>=2.1,<2.4',
1919
'wrapt',
2020
],
2121
extras_require={
2222
'tests': [
23-
'flake8<3', # see https://github.com/zheller/flake8-quotes/issues/29
23+
'flake8<4',
2424
'flake8-quotes',
25-
'pytest>=2.7,<3',
25+
'pytest>=4.6.9',
2626
'pytest-cov',
27+
'mock',
28+
'tox',
2729
],
2830
},
2931
classifiers=[

tests/helpers/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import sys
2+
3+
import tornado_opentracing
4+
from opentracing.mocktracer import MockTracer
5+
from tornado_opentracing.scope_managers import TornadoScopeManager
6+
7+
8+
if sys.version_info >= (3, 3):
9+
from ._test_case_gen import AsyncHTTPTestCase # noqa
10+
else:
11+
from ._test_case import AsyncHTTPTestCase # noqa
12+
13+
14+
tracing = tornado_opentracing.TornadoTracing(MockTracer(TornadoScopeManager()))

tests/helpers/_test_case.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import tornado.testing
2+
3+
4+
class AsyncHTTPTestCase(tornado.testing.AsyncHTTPTestCase):
5+
6+
def http_fetch(self, url, *args, **kwargs):
7+
self.http_client.fetch(url, self.stop, *args, **kwargs)
8+
return self.wait()

0 commit comments

Comments
 (0)