| 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 |
from threading import Lock |
| 24 |
import time |
| 25 |
|
| 26 |
from opentracing import Span |
| 27 |
|
| 28 |
|
| 29 |
class MockSpan(Span): |
| 30 |
"""MockSpan is a thread-safe implementation of opentracing.Span. |
| 31 |
""" |
| 32 |
|
| 33 |
def __init__( |
| 34 |
self, |
| 35 |
tracer, |
| 36 |
operation_name=None, |
| 37 |
context=None, |
| 38 |
parent_id=None, |
| 39 |
tags=None, |
| 40 |
start_time=None): |
| 41 |
super(MockSpan, self).__init__(tracer, context) |
| 42 |
self._tracer = tracer |
| 43 |
self._lock = Lock() |
| 44 |
|
| 45 |
self.operation_name = operation_name |
| 46 |
self.start_time = start_time |
| 47 |
self.parent_id = parent_id |
| 48 |
self.tags = tags if tags is not None else {} |
| 49 |
self.finish_time = -1 |
| 50 |
self.finished = False |
| 51 |
self.logs = [] |
| 52 |
|
| 53 |
def set_operation_name(self, operation_name): |
| 54 |
with self._lock: |
| 55 |
self.operation_name = operation_name |
| 56 |
return super(MockSpan, self).set_operation_name(operation_name) |
| 57 |
|
| 58 |
def set_tag(self, key, value): |
| 59 |
with self._lock: |
| 60 |
if self.tags is None: |
| 61 |
self.tags = {} |
| 62 |
self.tags[key] = value |
| 63 |
return super(MockSpan, self).set_tag(key, value) |
| 64 |
|
| 65 |
def log_kv(self, key_values, timestamp=None): |
| 66 |
with self._lock: |
| 67 |
self.logs.append(LogData(key_values, timestamp)) |
| 68 |
return super(MockSpan, self).log_kv(key_values, timestamp) |
| 69 |
|
| 70 |
def finish(self, finish_time=None): |
| 71 |
with self._lock: |
| 72 |
finish_time = time.time() if finish_time is None else finish_time |
| 73 |
self.finish_time = finish_time |
| 74 |
self.finished = True |
| 75 |
self._tracer._append_finished_span(self) |
| 76 |
|
| 77 |
def set_baggage_item(self, key, value): |
| 78 |
new_context = self._context.with_baggage_item(key, value) |
| 79 |
with self._lock: |
| 80 |
self._context = new_context |
| 81 |
return self |
| 82 |
|
| 83 |
def get_baggage_item(self, key): |
| 84 |
with self._lock: |
| 85 |
return self.context.baggage.get(key) |
| 86 |
|
| 87 |
|
| 88 |
class LogData(object): |
| 89 |
def __init__( |
| 90 |
self, |
| 91 |
key_values, |
| 92 |
timestamp=None): |
| 93 |
self.key_values = key_values |
| 94 |
self.timestamp = time.time() if timestamp is None else timestamp |