Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Add ability to specify parent span in tracer_span_context in TornadoScopeManager #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

condorcet
Copy link
Contributor

Some time ago I faced with problem of tracing fire & forget coroutines in Tornado.
As documentation says, we must run such coroutines in tracer_stack_context to prevent breaking of original scope:

@gen.coroutine
def coro():
    with global_tracer().start_active_span('background_task'):
        # do something in backgound

with tracer_stack_context():
    coro()

Based on tornado's StackContext, that actually proto-contextvars, tracer_stack_context make new isolated scope for coroutines and this mechanism works well.

But tracer_stack_context doesn't provide a way to setup active span in this new scope. If you want to make span propagation for fire & forget coroutines, you have to pass and activate your (current) span explicitly in the couroutine. It can be difficult and complicates code instrumentation.
For example, my first (and ugly) iteration of wrapper for fire & forget coroutines that we successfully use in one of our microservice https://github.com/condorcet/tornado-coroutines-opentracing/blob/master/tornado_coroutines_opentracing/__init__.py#L15

The same problem with scheduling callbacks/futures in event loop -- there is no easy way to make auto-propagation of current span to them.

In the PR tracer_stack_context has new optional argument parent_span that specify span that will be used in new scope prepared by stack context.
I think it can make auto-propagation easier for Tornado applications.
For example my second iteration of wrapper based on it: https://github.com/condorcet/tornado-coroutines-opentracing/blob/072a9d65f0e49eaf63fb3aff93b1090f06a4a7b3/tornado_coroutines_opentracing/__init__.py#L33

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant