Opentracing instrumentation for Tornado that allows correct parent scope propagation for fire & forget coroutines.
Project description
Supporting fire & forget Tornado coroutines in python opentracing
Opentracing python library provides nice mechanism for tracing of Tornado code based on coroutines.
Coroutine must be invoked with tracer_stack_context that allows to isolate context and using parent scope in child corutines. It works only when you yielding child coroutines inside, but doesn’t work with fire & forget coroutine. In this case such coroutine can lose parent scope while yielding or take scope of unrelated coroutine (e.g. concurrent coroutines).
To avoid it you have to wrap coroutine by tracer_stack_context manually, and activate parent scope that you need right inside coroutine:
from tornado import gen
from opentracing import global_tracer
from opentracing.scope_managers.tornado import tracer_stack_context
@gen.corotine
def do_someting_in_background(parent):
with global_tracer().scope_manager.activate(parent, False):
with global_tracer.start_active_span('do something', True):
yield gen.sleep(0.5)
# do something
...
with global_tracer().start_active_span('work in background') as root:
with tracer_stack_context():
do_someting_in_background(root.span)
ff_coroutine
This library provides ff_coroutine decorator that does it for you:
from opentracing import global_tracer
from tornado_coroutines_opentracing import ff_coroutine
@ff_coroutine
def do_someting_in_background():
with global_tracer.start_active_span('do something', True):
yield gen.sleep(0.5)
# do something
...
with global_tracer().start_active_span('work in background'):
do_someting_in_background()
It also works with nested coroutines:
from opentracing import global_tracer
from tornado_coroutines_opentracing import ff_coroutine
@ff_coroutine
def bar():
with global_tracer.start_active_span('bar', True):
# do something
@ff_coroutine
def foo():
with global_tracer.start_active_span('foo', True):
yield gen.sleep(0.5)
bar()
...
with global_tracer().start_active_span('work in background'):
foo()
ff_coroutine yielded as well as Tornado coroutine (via gen.coroutine):
from opentracing import global_tracer
from tornado_coroutines_opentracing import ff_coroutine
@ff_coroutine
def bar():
with global_tracer.start_active_span('bar', True):
# do something
@ff_coroutine
def foo():
with global_tracer.start_active_span('foo', True):
yield bar()
yield gen.sleep(0.5)
...
with global_tracer().start_active_span('work in background'):
yield foo()
Sometimes you want to disable tracing in your application. You can disable ff_coroutine too:
from tornado_coroutines_opentracing import State
...
State.enabled = False
0.1.0
First release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file tornado-coroutines-opentracing-0.1.0.tar.gz
.
File metadata
- Download URL: tornado-coroutines-opentracing-0.1.0.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/2.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f210f2e281cd0c9e6699e0b9e0f34a1b3c4e3ef205a65a05f5d0e1fdce77f8e |
|
MD5 | 94ee829f27f2392c0c6ff9a258dacdb0 |
|
BLAKE2b-256 | f2f6612474829cade3f1a47e9c4381187b9b8b34392a984021f770f5729f89e8 |