Debian Codegraph @

opentracing/scope_managers/__init__.py

raw
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 import threading
24
25 from opentracing import Scope, ScopeManager
26
27
28 class ThreadLocalScopeManager(ScopeManager):
29 """
30 :class:`~opentracing.ScopeManager` implementation that stores the
31 current active :class:`~opentracing.Scope` using thread-local storage.
32 """
33 def __init__(self):
34 self._tls_scope = threading.local()
35
36 def activate(self, span, finish_on_close):
37 """
38 Make a :class:`~opentracing.Span` instance active.
39
40 :param span: the :class:`~opentracing.Span` that should become active.
41 :param finish_on_close: whether *span* should automatically be
42 finished when :meth:`Scope.close()` is called.
43
44 :return: a :class:`~opentracing.Scope` instance to control the end
45 of the active period for the :class:`~opentracing.Span`.
46 It is a programming error to neglect to call :meth:`Scope.close()`
47 on the returned instance.
48 """
49 scope = _ThreadLocalScope(self, span, finish_on_close)
50 setattr(self._tls_scope, 'active', scope)
51 return scope
52
53 @property
54 def active(self):
55 """
56 Return the currently active :class:`~opentracing.Scope` which
57 can be used to access the currently active
58 :attr:`Scope.span`.
59
60 :return: the :class:`~opentracing.Scope` that is active,
61 or ``None`` if not available.
62 """
63 return getattr(self._tls_scope, 'active', None)
64
65
66 class _ThreadLocalScope(Scope):
67 def __init__(self, manager, span, finish_on_close):
68 super(_ThreadLocalScope, self).__init__(manager, span)
69 self._finish_on_close = finish_on_close
70 self._to_restore = manager.active
71
72 def close(self):
73 if self.manager.active is not self:
74 return
75
76 if self._finish_on_close:
77 self.span.finish()
78
79 setattr(self._manager._tls_scope, 'active', self._to_restore)