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.
Make new context manager based on python 3.7 contextvars.
- Loading branch information
Showing
27 changed files
with
1,006 additions
and
99 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
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,115 @@ | ||
# 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 | ||
from opentracing.scope_managers import ThreadLocalScopeManager | ||
|
||
|
||
_SCOPE = ContextVar('scope') | ||
|
||
|
||
class ContextVarsScopeManager(ThreadLocalScopeManager): | ||
""" | ||
: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 to their children. | ||
.. code-block:: python | ||
async def child_coroutine(span): | ||
# 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(span) | ||
... | ||
""" | ||
# TODO: update description | ||
|
||
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. | ||
""" | ||
# TODO: update description | ||
|
||
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(): | ||
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)) |
Oops, something went wrong.