| 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 threading import Lock |
| 22 |
import time |
| 23 |
|
| 24 |
import opentracing |
| 25 |
from opentracing import Format, Tracer |
| 26 |
from opentracing import UnsupportedFormatException |
| 27 |
from opentracing.scope_managers import ThreadLocalScopeManager |
| 28 |
|
| 29 |
from .context import SpanContext |
| 30 |
from .span import MockSpan |
| 31 |
|
| 32 |
|
| 33 |
class MockTracer(Tracer): |
| 34 |
"""MockTracer makes it easy to test the semantics of OpenTracing |
| 35 |
instrumentation. |
| 36 |
|
| 37 |
By using a MockTracer as a :class:`~opentracing.Tracer` implementation |
| 38 |
for tests, a developer can assert that :class:`~opentracing.Span` |
| 39 |
properties and relationships with other |
| 40 |
**Spans** are defined as expected by instrumentation code. |
| 41 |
|
| 42 |
By default, MockTracer registers propagators for :attr:`Format.TEXT_MAP`, |
| 43 |
:attr:`Format.HTTP_HEADERS` and :attr:`Format.BINARY`. The user should |
| 44 |
call :func:`register_propagator()` for each additional inject/extract |
| 45 |
format. |
| 46 |
""" |
| 47 |
|
| 48 |
def __init__(self, scope_manager=None): |
| 49 |
"""Initialize a MockTracer instance.""" |
| 50 |
|
| 51 |
scope_manager = ThreadLocalScopeManager() \ |
| 52 |
if scope_manager is None else scope_manager |
| 53 |
super(MockTracer, self).__init__(scope_manager) |
| 54 |
|
| 55 |
self._propagators = {} |
| 56 |
self._finished_spans = [] |
| 57 |
self._spans_lock = Lock() |
| 58 |
|
| 59 |
# Simple-as-possible (consecutive for repeatability) id generation. |
| 60 |
self._next_id = 0 |
| 61 |
self._next_id_lock = Lock() |
| 62 |
|
| 63 |
self._register_required_propagators() |
| 64 |
|
| 65 |
def register_propagator(self, format, propagator): |
| 66 |
"""Register a propagator with this MockTracer. |
| 67 |
|
| 68 |
:param string format: a :class:`~opentracing.Format` |
| 69 |
identifier like :attr:`~opentracing.Format.TEXT_MAP` |
| 70 |
:param **Propagator** propagator: a **Propagator** instance to handle |
| 71 |
inject/extract calls involving `format` |
| 72 |
""" |
| 73 |
self._propagators[format] = propagator |
| 74 |
|
| 75 |
def _register_required_propagators(self): |
| 76 |
from .text_propagator import TextPropagator |
| 77 |
from .binary_propagator import BinaryPropagator |
| 78 |
self.register_propagator(Format.TEXT_MAP, TextPropagator()) |
| 79 |
self.register_propagator(Format.HTTP_HEADERS, TextPropagator()) |
| 80 |
self.register_propagator(Format.BINARY, BinaryPropagator()) |
| 81 |
|
| 82 |
def finished_spans(self): |
| 83 |
"""Return a copy of all finished **Spans** started by this MockTracer |
| 84 |
(since construction or the last call to :meth:`~MockTracer.reset()`) |
| 85 |
|
| 86 |
:rtype: list |
| 87 |
:return: a copy of the finished **Spans**. |
| 88 |
""" |
| 89 |
with self._spans_lock: |
| 90 |
return list(self._finished_spans) |
| 91 |
|
| 92 |
def reset(self): |
| 93 |
"""Clear the finished **Spans** queue. |
| 94 |
|
| 95 |
Note that this does **not** have any effect on **Spans** created by |
| 96 |
MockTracer that have not finished yet; those |
| 97 |
will still be enqueued in :meth:`~MockTracer.finished_spans()` |
| 98 |
when they :func:`finish()`. |
| 99 |
""" |
| 100 |
with self._spans_lock: |
| 101 |
self._finished_spans = [] |
| 102 |
|
| 103 |
def _append_finished_span(self, span): |
| 104 |
with self._spans_lock: |
| 105 |
self._finished_spans.append(span) |
| 106 |
|
| 107 |
def _generate_id(self): |
| 108 |
with self._next_id_lock: |
| 109 |
self._next_id += 1 |
| 110 |
return self._next_id |
| 111 |
|
| 112 |
def start_active_span(self, |
| 113 |
operation_name, |
| 114 |
child_of=None, |
| 115 |
references=None, |
| 116 |
tags=None, |
| 117 |
start_time=None, |
| 118 |
ignore_active_span=False, |
| 119 |
finish_on_close=True): |
| 120 |
|
| 121 |
# create a new Span |
| 122 |
span = self.start_span( |
| 123 |
operation_name=operation_name, |
| 124 |
child_of=child_of, |
| 125 |
references=references, |
| 126 |
tags=tags, |
| 127 |
start_time=start_time, |
| 128 |
ignore_active_span=ignore_active_span, |
| 129 |
) |
| 130 |
|
| 131 |
return self.scope_manager.activate(span, finish_on_close) |
| 132 |
|
| 133 |
def start_span(self, |
| 134 |
operation_name=None, |
| 135 |
child_of=None, |
| 136 |
references=None, |
| 137 |
tags=None, |
| 138 |
start_time=None, |
| 139 |
ignore_active_span=False): |
| 140 |
|
| 141 |
start_time = time.time() if start_time is None else start_time |
| 142 |
|
| 143 |
# See if we have a parent_ctx in `references` |
| 144 |
parent_ctx = None |
| 145 |
if child_of is not None: |
| 146 |
parent_ctx = ( |
| 147 |
child_of if isinstance(child_of, opentracing.SpanContext) |
| 148 |
else child_of.context) |
| 149 |
elif references is not None and len(references) > 0: |
| 150 |
# TODO only the first reference is currently used |
| 151 |
parent_ctx = references[0].referenced_context |
| 152 |
|
| 153 |
# retrieve the active SpanContext |
| 154 |
if not ignore_active_span and parent_ctx is None: |
| 155 |
scope = self.scope_manager.active |
| 156 |
if scope is not None: |
| 157 |
parent_ctx = scope.span.context |
| 158 |
|
| 159 |
# Assemble the child ctx |
| 160 |
ctx = SpanContext(span_id=self._generate_id()) |
| 161 |
if parent_ctx is not None: |
| 162 |
if parent_ctx._baggage is not None: |
| 163 |
ctx._baggage = parent_ctx._baggage.copy() |
| 164 |
ctx.trace_id = parent_ctx.trace_id |
| 165 |
else: |
| 166 |
ctx.trace_id = self._generate_id() |
| 167 |
|
| 168 |
# Tie it all together |
| 169 |
return MockSpan( |
| 170 |
self, |
| 171 |
operation_name=operation_name, |
| 172 |
context=ctx, |
| 173 |
parent_id=(None if parent_ctx is None else parent_ctx.span_id), |
| 174 |
tags=tags, |
| 175 |
start_time=start_time) |
| 176 |
|
| 177 |
def inject(self, span_context, format, carrier): |
| 178 |
if format in self._propagators: |
| 179 |
self._propagators[format].inject(span_context, carrier) |
| 180 |
else: |
| 181 |
raise UnsupportedFormatException() |
| 182 |
|
| 183 |
def extract(self, format, carrier): |
| 184 |
if format in self._propagators: |
| 185 |
return self._propagators[format].extract(carrier) |
| 186 |
else: |
| 187 |
raise UnsupportedFormatException() |