| 1 |
# Copyright (c) 2017-2019 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 |
| 24 |
|
| 25 |
|
| 26 |
class Scope(object): |
| 27 |
"""A scope formalizes the activation and deactivation of a :class:`Span`, |
| 28 |
usually from a CPU standpoint. Many times a :class:`Span` will be extant |
| 29 |
(in that :meth:`Span.finish()` has not been called) despite being in a |
| 30 |
non-runnable state from a CPU/scheduler standpoint. For instance, a |
| 31 |
:class:`Span` representing the client side of an RPC will be unfinished but |
| 32 |
blocked on IO while the RPC is still outstanding. A scope defines when a |
| 33 |
given :class:`Span` is scheduled and on the path. |
| 34 |
|
| 35 |
:param manager: the :class:`ScopeManager` that created this :class:`Scope`. |
| 36 |
:type manager: ScopeManager |
| 37 |
|
| 38 |
:param span: the :class:`Span` used for this :class:`Scope`. |
| 39 |
:type span: Span |
| 40 |
""" |
| 41 |
def __init__(self, manager, span): |
| 42 |
"""Initializes a scope for *span*.""" |
| 43 |
self._manager = manager |
| 44 |
self._span = span |
| 45 |
|
| 46 |
@property |
| 47 |
def span(self): |
| 48 |
"""Returns the :class:`Span` wrapped by this :class:`Scope`. |
| 49 |
|
| 50 |
:rtype: Span |
| 51 |
""" |
| 52 |
return self._span |
| 53 |
|
| 54 |
@property |
| 55 |
def manager(self): |
| 56 |
"""Returns the :class:`ScopeManager` that created this :class:`Scope`. |
| 57 |
|
| 58 |
:rtype: ScopeManager |
| 59 |
""" |
| 60 |
return self._manager |
| 61 |
|
| 62 |
def close(self): |
| 63 |
"""Marks the end of the active period for this :class:`Scope`, updating |
| 64 |
:attr:`ScopeManager.active` in the process. |
| 65 |
|
| 66 |
NOTE: Calling this method more than once on a single :class:`Scope` |
| 67 |
leads to undefined behavior. |
| 68 |
""" |
| 69 |
pass |
| 70 |
|
| 71 |
def __enter__(self): |
| 72 |
"""Allows :class:`Scope` to be used inside a Python Context Manager.""" |
| 73 |
return self |
| 74 |
|
| 75 |
def __exit__(self, exc_type, exc_val, exc_tb): |
| 76 |
"""Calls :meth:`close()` when the execution is outside the Python |
| 77 |
Context Manager. |
| 78 |
|
| 79 |
If exception has occurred during execution, it is automatically logged |
| 80 |
and added as a tag to the :class:`Span`. |
| 81 |
:attr:`~operation.ext.tags.ERROR` will also be set to `True`. |
| 82 |
""" |
| 83 |
Span._on_error(self.span, exc_type, exc_val, exc_tb) |
| 84 |
self.close() |