Skip to content

Commit 2937ef1

Browse files
committed
refactor: Use ContextVarsScopeManager instead of AsyncioScopeManager.
As we support now only the Python runtime >= 3.7, the usage of the ContextVarsScopeManager as Tracer's scope manager is indicated to provide automatic Span propagation from parent coroutines, tasks and scheduled in event loop callbacks to their children. Signed-off-by: Paulo Vital <[email protected]>
1 parent f393063 commit 2937ef1

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

instana/instrumentation/asyncio.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,38 @@
77
from ..log import logger
88
from ..singletons import async_tracer
99
from ..configurator import config
10+
from opentracing.scope_managers.contextvars import no_parent_scope
11+
from opentracing.scope_managers.constants import ACTIVE_ATTR
1012

1113
try:
1214
import asyncio
1315

1416
@wrapt.patch_function_wrapper('asyncio','ensure_future')
1517
def ensure_future_with_instana(wrapped, instance, argv, kwargs):
1618
if config['asyncio_task_context_propagation']['enabled'] is False:
17-
return wrapped(*argv, **kwargs)
19+
with no_parent_scope():
20+
return wrapped(*argv, **kwargs)
1821

1922
scope = async_tracer.scope_manager.active
2023
task = wrapped(*argv, **kwargs)
2124

2225
if scope is not None:
23-
async_tracer.scope_manager._set_task_scope(scope, task=task)
26+
setattr(task, ACTIVE_ATTR, scope)
2427

2528
return task
2629

2730
if hasattr(asyncio, "create_task"):
2831
@wrapt.patch_function_wrapper('asyncio','create_task')
2932
def create_task_with_instana(wrapped, instance, argv, kwargs):
3033
if config['asyncio_task_context_propagation']['enabled'] is False:
31-
return wrapped(*argv, **kwargs)
34+
with no_parent_scope():
35+
return wrapped(*argv, **kwargs)
3236

3337
scope = async_tracer.scope_manager.active
3438
task = wrapped(*argv, **kwargs)
3539

3640
if scope is not None:
37-
async_tracer.scope_manager._set_task_scope(scope, task=task)
41+
setattr(task, ACTIVE_ATTR, scope)
3842

3943
return task
4044

instana/singletons.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,11 @@ def set_agent(new_agent):
9494
# this package.
9595
tracer = InstanaTracer(recorder=span_recorder)
9696

97-
if sys.version_info >= (3, 4):
98-
try:
99-
from opentracing.scope_managers.asyncio import AsyncioScopeManager
100-
101-
async_tracer = InstanaTracer(scope_manager=AsyncioScopeManager(), recorder=span_recorder)
102-
except Exception:
103-
logger.debug("Error setting up async_tracer:", exc_info=True)
97+
try:
98+
from opentracing.scope_managers.contextvars import ContextVarsScopeManager
99+
async_tracer = InstanaTracer(scope_manager=ContextVarsScopeManager(), recorder=span_recorder)
100+
except Exception:
101+
logger.debug("Error setting up async_tracer:", exc_info=True)
104102

105103
# Mock the tornado tracer until tornado is detected and instrumented first
106104
tornado_tracer = tracer

0 commit comments

Comments
 (0)