This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AsyncioScopeManager based on contextvars and supporting Tornado 6 (…
…#118) * Asyncio context manager with contextvars. Add different versions of tornado to travis.yml. * Make new context manager based on python 3.7 contextvars. * Inherit ContextVarsScopeManagerFix directly from ScopeManager, fix docstrings and README * Update testbed/test_multiple_callbacks/README.md Co-Authored-By: Yuri Shkuro <yurishkuro@users.noreply.github.com> * Update testbed/test_nested_callbacks/README.md Co-Authored-By: Yuri Shkuro <yurishkuro@users.noreply.github.com> * Update testbed/test_subtask_span_propagation/README.md Co-Authored-By: Yuri Shkuro <yurishkuro@users.noreply.github.com> * Fix typo in testbed docs * Remove obsolete description from testbed docs * Update testbed/test_common_request_handler/README.md Co-Authored-By: Yuri Shkuro <yurishkuro@users.noreply.github.com>
- Loading branch information
1 parent
7d2e62b
commit 170f927
Showing
26 changed files
with
1,033 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# Copyright (c) The OpenTracing Authors. | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
# THE SOFTWARE. | ||
|
||
from __future__ import absolute_import | ||
|
||
from contextlib import contextmanager | ||
from contextvars import ContextVar | ||
|
||
from opentracing import Scope, ScopeManager | ||
|
||
|
||
_SCOPE = ContextVar('scope') | ||
|
||
|
||
class ContextVarsScopeManager(ScopeManager): | ||
""" | ||
:class:`~opentracing.ScopeManager` implementation for **asyncio** | ||
that stores the :class:`~opentracing.Scope` using ContextVar. | ||
The scope manager provides automatic :class:`~opentracing.Span` propagation | ||
from parent coroutines, tasks and scheduled in event loop callbacks to | ||
their children. | ||
.. code-block:: python | ||
async def child_coroutine(): | ||
# No need manual activation of parent span in child coroutine. | ||
with tracer.start_active_span('child') as scope: | ||
... | ||
async def parent_coroutine(): | ||
with tracer.start_active_span('parent') as scope: | ||
... | ||
await child_coroutine() | ||
... | ||
""" | ||
|
||
def activate(self, span, finish_on_close): | ||
""" | ||
Make a :class:`~opentracing.Span` instance active. | ||
:param span: the :class:`~opentracing.Span` that should become active. | ||
:param finish_on_close: whether *span* should automatically be | ||
finished when :meth:`Scope.close()` is called. | ||
:return: a :class:`~opentracing.Scope` instance to control the end | ||
of the active period for the :class:`~opentracing.Span`. | ||
It is a programming error to neglect to call :meth:`Scope.close()` | ||
on the returned instance. | ||
""" | ||
|
||
return self._set_scope(span, finish_on_close) | ||
|
||
@property | ||
def active(self): | ||
""" | ||
Return the currently active :class:`~opentracing.Scope` which | ||
can be used to access the currently active :attr:`Scope.span`. | ||
:return: the :class:`~opentracing.Scope` that is active, | ||
or ``None`` if not available. | ||
""" | ||
|
||
return self._get_scope() | ||
|
||
def _set_scope(self, span, finish_on_close): | ||
return _ContextVarsScope(self, span, finish_on_close) | ||
|
||
def _get_scope(self): | ||
return _SCOPE.get(None) | ||
|
||
|
||
class _ContextVarsScope(Scope): | ||
def __init__(self, manager, span, finish_on_close): | ||
super(_ContextVarsScope, self).__init__(manager, span) | ||
self._finish_on_close = finish_on_close | ||
self._token = _SCOPE.set(self) | ||
|
||
def close(self): | ||
if self.manager.active is not self: | ||
return | ||
|
||
_SCOPE.reset(self._token) | ||
|
||
if self._finish_on_close: | ||
self.span.finish() | ||
|
||
|
||
@contextmanager | ||
def no_parent_scope(): | ||
""" | ||
Context manager that resets current Scope. Intended to break span | ||
propagation to children coroutines, tasks or scheduled callbacks. | ||
.. code-block:: python | ||
from opentracing.scope_managers.contextvars import no_parent_scope | ||
def periodic() | ||
# `periodic` span will be children of root only at the first time. | ||
with self.tracer.start_active_span('periodic'): | ||
# Now we break span propagation. | ||
with no_parent_scope(): | ||
self.loop.call_soon(periodic) | ||
with self.tracer.start_active_span('root'): | ||
self.loop.call_soon(periodic) | ||
""" | ||
token = _SCOPE.set(None) | ||
try: | ||
yield | ||
finally: | ||
_SCOPE.reset(token) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from __future__ import print_function | ||
|
||
import asyncio | ||
|
||
from opentracing.mocktracer import MockTracer | ||
from ..testcase import OpenTracingTestCase | ||
from opentracing.scope_managers.contextvars import ContextVarsScopeManager | ||
from ..utils import stop_loop_when | ||
|
||
|
||
class TestAsyncioContextVars(OpenTracingTestCase): | ||
def setUp(self): | ||
self.tracer = MockTracer(ContextVarsScopeManager()) | ||
self.loop = asyncio.get_event_loop() | ||
|
||
def test_main(self): | ||
# Start an isolated task and query for its result -and finish it- | ||
# in another task/thread | ||
span = self.tracer.start_span('initial') | ||
self.submit_another_task(span) | ||
|
||
stop_loop_when(self.loop, | ||
lambda: len(self.tracer.finished_spans()) >= 3) | ||
self.loop.run_forever() | ||
|
||
initial, subtask, task = self.tracer.finished_spans() | ||
|
||
self.assertEmptySpan(initial, 'initial') | ||
self.assertEmptySpan(subtask, 'subtask') | ||
self.assertEmptySpan(task, 'task') | ||
|
||
# task/subtask are part of the same trace, | ||
# and subtask is a child of task | ||
self.assertSameTrace(subtask, task) | ||
self.assertIsChildOf(subtask, task) | ||
|
||
# initial task is not related in any way to those two tasks | ||
self.assertNotSameTrace(initial, subtask) | ||
self.assertHasNoParent(initial) | ||
|
||
async def task(self, span): | ||
# Create a new Span for this task | ||
with self.tracer.start_active_span('task'): | ||
|
||
with self.tracer.scope_manager.activate(span, True): | ||
# Simulate work strictly related to the initial Span | ||
pass | ||
|
||
# Use the task span as parent of a new subtask | ||
with self.tracer.start_active_span('subtask'): | ||
pass | ||
|
||
def submit_another_task(self, span): | ||
self.loop.create_task(self.task(span)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from __future__ import print_function | ||
|
||
|
||
import asyncio | ||
|
||
import opentracing | ||
from opentracing.ext import tags | ||
from opentracing.mocktracer import MockTracer | ||
from opentracing.scope_managers.contextvars import ContextVarsScopeManager | ||
from ..testcase import OpenTracingTestCase | ||
from ..utils import get_logger, get_one_by_tag, stop_loop_when | ||
|
||
|
||
logger = get_logger(__name__) | ||
|
||
|
||
class Server(object): | ||
def __init__(self, *args, **kwargs): | ||
tracer = kwargs.pop('tracer') | ||
queue = kwargs.pop('queue') | ||
super(Server, self).__init__(*args, **kwargs) | ||
|
||
self.tracer = tracer | ||
self.queue = queue | ||
|
||
async def run(self): | ||
value = await self.queue.get() | ||
self.process(value) | ||
|
||
def process(self, message): | ||
logger.info('Processing message in server') | ||
|
||
ctx = self.tracer.extract(opentracing.Format.TEXT_MAP, message) | ||
with self.tracer.start_active_span('receive', | ||
child_of=ctx) as scope: | ||
scope.span.set_tag(tags.SPAN_KIND, tags.SPAN_KIND_RPC_SERVER) | ||
|
||
|
||
class Client(object): | ||
def __init__(self, tracer, queue): | ||
self.tracer = tracer | ||
self.queue = queue | ||
|
||
async def send(self): | ||
with self.tracer.start_active_span('send') as scope: | ||
scope.span.set_tag(tags.SPAN_KIND, tags.SPAN_KIND_RPC_CLIENT) | ||
|
||
message = {} | ||
self.tracer.inject(scope.span.context, | ||
opentracing.Format.TEXT_MAP, | ||
message) | ||
await self.queue.put(message) | ||
|
||
logger.info('Sent message from client') | ||
|
||
|
||
class TestAsyncioContextVars(OpenTracingTestCase): | ||
def setUp(self): | ||
self.tracer = MockTracer(ContextVarsScopeManager()) | ||
self.queue = asyncio.Queue() | ||
self.loop = asyncio.get_event_loop() | ||
self.server = Server(tracer=self.tracer, queue=self.queue) | ||
|
||
def test(self): | ||
client = Client(self.tracer, self.queue) | ||
self.loop.create_task(self.server.run()) | ||
self.loop.create_task(client.send()) | ||
|
||
stop_loop_when(self.loop, | ||
lambda: len(self.tracer.finished_spans()) >= 2) | ||
self.loop.run_forever() | ||
|
||
spans = self.tracer.finished_spans() | ||
self.assertIsNotNone(get_one_by_tag(spans, | ||
tags.SPAN_KIND, | ||
tags.SPAN_KIND_RPC_SERVER)) | ||
self.assertIsNotNone(get_one_by_tag(spans, | ||
tags.SPAN_KIND, | ||
tags.SPAN_KIND_RPC_CLIENT)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
from __future__ import print_function | ||
|
||
import functools | ||
|
||
import asyncio | ||
|
||
from opentracing.ext import tags | ||
|
Oops, something went wrong.