Skip to main content
OpenTracing Badge

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.

Release files for tornado-coroutines-opentracing 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tornado-coroutines-opentracing 0.1.0
File Size Uploaded
tornado-coroutines-opentracing-0.1.0.tar.gz 4.0 kB Details

Release files / tornado-coroutines-opentracing-0.1.0.tar.gz

Download URL tornado-coroutines-opentracing-0.1.0.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0f210f2e281cd0c9e6699e0b9e0f34a1b3c4e3ef205a65a05f5d0e1fdce77f8e
BLAKE2b-256 checksum
How to use checksums
f2f6612474829cade3f1a47e9c4381187b9b8b34392a984021f770f5729f89e8
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page