|
| 1 | +# Copyright (c) The OpenTracing Authors. |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +# THE SOFTWARE. |
| 20 | + |
| 21 | +from __future__ import absolute_import |
| 22 | + |
| 23 | +from contextlib import contextmanager |
| 24 | +from contextvars import ContextVar |
| 25 | + |
| 26 | +from opentracing import Scope, ScopeManager |
| 27 | + |
| 28 | + |
| 29 | +_SCOPE = ContextVar('scope') |
| 30 | + |
| 31 | + |
| 32 | +class ContextVarsScopeManager(ScopeManager): |
| 33 | + """ |
| 34 | + :class:`~opentracing.ScopeManager` implementation for **asyncio** |
| 35 | + that stores the :class:`~opentracing.Scope` using ContextVar. |
| 36 | +
|
| 37 | + The scope manager provides automatic :class:`~opentracing.Span` propagation |
| 38 | + from parent coroutines, tasks and scheduled in event loop callbacks to |
| 39 | + their children. |
| 40 | +
|
| 41 | + .. code-block:: python |
| 42 | +
|
| 43 | + async def child_coroutine(): |
| 44 | + # No need manual activation of parent span in child coroutine. |
| 45 | + with tracer.start_active_span('child') as scope: |
| 46 | + ... |
| 47 | +
|
| 48 | + async def parent_coroutine(): |
| 49 | + with tracer.start_active_span('parent') as scope: |
| 50 | + ... |
| 51 | + await child_coroutine() |
| 52 | + ... |
| 53 | +
|
| 54 | + """ |
| 55 | + |
| 56 | + def activate(self, span, finish_on_close): |
| 57 | + """ |
| 58 | + Make a :class:`~opentracing.Span` instance active. |
| 59 | +
|
| 60 | + :param span: the :class:`~opentracing.Span` that should become active. |
| 61 | + :param finish_on_close: whether *span* should automatically be |
| 62 | + finished when :meth:`Scope.close()` is called. |
| 63 | +
|
| 64 | + :return: a :class:`~opentracing.Scope` instance to control the end |
| 65 | + of the active period for the :class:`~opentracing.Span`. |
| 66 | + It is a programming error to neglect to call :meth:`Scope.close()` |
| 67 | + on the returned instance. |
| 68 | + """ |
| 69 | + |
| 70 | + return self._set_scope(span, finish_on_close) |
| 71 | + |
| 72 | + @property |
| 73 | + def active(self): |
| 74 | + """ |
| 75 | + Return the currently active :class:`~opentracing.Scope` which |
| 76 | + can be used to access the currently active :attr:`Scope.span`. |
| 77 | +
|
| 78 | + :return: the :class:`~opentracing.Scope` that is active, |
| 79 | + or ``None`` if not available. |
| 80 | + """ |
| 81 | + |
| 82 | + return self._get_scope() |
| 83 | + |
| 84 | + def _set_scope(self, span, finish_on_close): |
| 85 | + return _ContextVarsScope(self, span, finish_on_close) |
| 86 | + |
| 87 | + def _get_scope(self): |
| 88 | + return _SCOPE.get(None) |
| 89 | + |
| 90 | + |
| 91 | +class _ContextVarsScope(Scope): |
| 92 | + def __init__(self, manager, span, finish_on_close): |
| 93 | + super(_ContextVarsScope, self).__init__(manager, span) |
| 94 | + self._finish_on_close = finish_on_close |
| 95 | + self._token = _SCOPE.set(self) |
| 96 | + |
| 97 | + def close(self): |
| 98 | + if self.manager.active is not self: |
| 99 | + return |
| 100 | + |
| 101 | + _SCOPE.reset(self._token) |
| 102 | + |
| 103 | + if self._finish_on_close: |
| 104 | + self.span.finish() |
| 105 | + |
| 106 | + |
| 107 | +@contextmanager |
| 108 | +def no_parent_scope(): |
| 109 | + """ |
| 110 | + Context manager that resets current Scope. Intended to break span |
| 111 | + propagation to children coroutines, tasks or scheduled callbacks. |
| 112 | +
|
| 113 | + .. code-block:: python |
| 114 | +
|
| 115 | + from opentracing.scope_managers.contextvars import no_parent_scope |
| 116 | +
|
| 117 | + def periodic() |
| 118 | + # `periodic` span will be children of root only at the first time. |
| 119 | + with self.tracer.start_active_span('periodic'): |
| 120 | + # Now we break span propagation. |
| 121 | + with no_parent_scope(): |
| 122 | + self.loop.call_soon(periodic) |
| 123 | +
|
| 124 | + with self.tracer.start_active_span('root'): |
| 125 | + self.loop.call_soon(periodic) |
| 126 | + """ |
| 127 | + token = _SCOPE.set(None) |
| 128 | + try: |
| 129 | + yield |
| 130 | + finally: |
| 131 | + _SCOPE.reset(token) |
0 commit comments