forked from DataDog/dd-trace-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.py
58 lines (47 loc) · 1.84 KB
/
provider.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from .context import ThreadLocalContext
class BaseContextProvider(object):
"""
A ``ContextProvider`` is an interface that provides the blueprint
for a callable class, capable to retrieve the current active
``Context`` instance. Context providers must inherit this class
and implement:
* the ``active`` method, that returns the current active ``Context``
* the ``activate`` method, that sets the current active ``Context``
"""
def _has_active_context(self):
raise NotImplementedError
def activate(self, context):
raise NotImplementedError
def active(self):
raise NotImplementedError
def __call__(self, *args, **kwargs):
"""Method available for backward-compatibility. It proxies the call to
``self.active()`` and must not do anything more.
"""
return self.active()
class DefaultContextProvider(BaseContextProvider):
"""
Default context provider that retrieves all contexts from the current
thread-local storage. It is suitable for synchronous programming and
Python WSGI frameworks.
"""
def __init__(self):
self._local = ThreadLocalContext()
def _has_active_context(self):
"""
Check whether we have a currently active context.
:returns: Whether we have an active context
:rtype: bool
"""
return self._local._has_active_context()
def activate(self, context):
"""Makes the given ``context`` active, so that the provider calls
the thread-local storage implementation.
"""
return self._local.set(context)
def active(self):
"""Returns the current active ``Context`` for this tracer. Returned
``Context`` must be thread-safe or thread-local for this specific
implementation.
"""
return self._local.get()