| 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 gevent |
| 24 |
|
| 25 |
from opentracing import Scope, ScopeManager |
| 26 |
from .constants import ACTIVE_ATTR |
| 27 |
|
| 28 |
|
| 29 |
class GeventScopeManager(ScopeManager): |
| 30 |
""" |
| 31 |
:class:`~opentracing.ScopeManager` implementation for **gevent** |
| 32 |
that stores the :class:`~opentracing.Scope` in the current greenlet |
| 33 |
(:func:`gevent.getcurrent()`). |
| 34 |
|
| 35 |
Automatic :class:`~opentracing.Span` propagation from parent greenlets to |
| 36 |
their children is not provided, which needs to be |
| 37 |
done manually: |
| 38 |
|
| 39 |
.. code-block:: python |
| 40 |
|
| 41 |
def child_greenlet(span): |
| 42 |
# activate the parent Span, but do not finish it upon |
| 43 |
# deactivation. That will be done by the parent greenlet. |
| 44 |
with tracer.scope_manager.activate(span, finish_on_close=False): |
| 45 |
with tracer.start_active_span('child') as scope: |
| 46 |
... |
| 47 |
|
| 48 |
def parent_greenlet(): |
| 49 |
with tracer.start_active_span('parent') as scope: |
| 50 |
... |
| 51 |
gevent.spawn(child_greenlet, span).join() |
| 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 |
scope = _GeventScope(self, span, finish_on_close) |
| 71 |
self._set_greenlet_scope(scope) |
| 72 |
|
| 73 |
return scope |
| 74 |
|
| 75 |
@property |
| 76 |
def active(self): |
| 77 |
""" |
| 78 |
Return the currently active :class:`~opentracing.Scope` which |
| 79 |
can be used to access the currently active |
| 80 |
:attr:`Scope.span`. |
| 81 |
|
| 82 |
:return: the :class:`~opentracing.Scope` that is active, |
| 83 |
or ``None`` if not available. |
| 84 |
""" |
| 85 |
|
| 86 |
return self._get_greenlet_scope() |
| 87 |
|
| 88 |
def _get_greenlet_scope(self, greenlet=None): |
| 89 |
if greenlet is None: |
| 90 |
greenlet = gevent.getcurrent() |
| 91 |
|
| 92 |
return getattr(greenlet, ACTIVE_ATTR, None) |
| 93 |
|
| 94 |
def _set_greenlet_scope(self, scope, greenlet=None): |
| 95 |
if greenlet is None: |
| 96 |
greenlet = gevent.getcurrent() |
| 97 |
|
| 98 |
setattr(greenlet, ACTIVE_ATTR, scope) |
| 99 |
|
| 100 |
|
| 101 |
class _GeventScope(Scope): |
| 102 |
def __init__(self, manager, span, finish_on_close): |
| 103 |
super(_GeventScope, self).__init__(manager, span) |
| 104 |
self._finish_on_close = finish_on_close |
| 105 |
self._to_restore = manager.active |
| 106 |
|
| 107 |
def close(self): |
| 108 |
if self.manager.active is not self: |
| 109 |
return |
| 110 |
|
| 111 |
self.manager._set_greenlet_scope(self._to_restore) |
| 112 |
|
| 113 |
if self._finish_on_close: |
| 114 |
self.span.finish() |