| 1 |
# Copyright (c) 2017 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 .span import Span, SpanContext |
| 24 |
from .scope import Scope |
| 25 |
|
| 26 |
|
| 27 |
class ScopeManager(object): |
| 28 |
"""The :class:`ScopeManager` interface abstracts both the activation of |
| 29 |
a :class:`Span` and access to an active :class:`Span`/:class:`Scope`. |
| 30 |
""" |
| 31 |
def __init__(self): |
| 32 |
# TODO: `tracer` should not be None, but we don't have a reference; |
| 33 |
# should we move the NOOP SpanContext, Span, Scope to somewhere |
| 34 |
# else so that they're globally reachable? |
| 35 |
self._noop_span = Span(tracer=None, context=SpanContext()) |
| 36 |
self._noop_scope = Scope(self, self._noop_span) |
| 37 |
|
| 38 |
def activate(self, span, finish_on_close): |
| 39 |
"""Makes a :class:`Span` active. |
| 40 |
|
| 41 |
:param span: the :class:`Span` that should become active. |
| 42 |
:param finish_on_close: whether :class:`Span` should be automatically |
| 43 |
finished when :meth:`Scope.close()` is called. |
| 44 |
|
| 45 |
:rtype: Scope |
| 46 |
:return: a :class:`Scope` to control the end of the active period for |
| 47 |
*span*. It is a programming error to neglect to call |
| 48 |
:meth:`Scope.close()` on the returned instance. |
| 49 |
""" |
| 50 |
return self._noop_scope |
| 51 |
|
| 52 |
@property |
| 53 |
def active(self): |
| 54 |
"""Returns the currently active :class:`Scope` which can be used to access the |
| 55 |
currently active :attr:`Scope.span`. |
| 56 |
|
| 57 |
If there is a non-null :class:`Scope`, its wrapped :class:`Span` |
| 58 |
becomes an implicit parent of any newly-created :class:`Span` at |
| 59 |
:meth:`Tracer.start_active_span()` time. |
| 60 |
|
| 61 |
:rtype: Scope |
| 62 |
:return: the :class:`Scope` that is active, or ``None`` if not |
| 63 |
available. |
| 64 |
""" |
| 65 |
return self._noop_scope |