| 1 |
import threading |
| 2 |
import logging |
| 3 |
import os |
| 4 |
|
| 5 |
from .exceptions.exceptions import SegmentNotFoundException |
| 6 |
|
| 7 |
log = logging.getLogger(__name__) |
| 8 |
|
| 9 |
MISSING_SEGMENT_MSG = 'cannot find the current segment/subsegment, please make sure you have a segment open' |
| 10 |
SUPPORTED_CONTEXT_MISSING = ('RUNTIME_ERROR', 'LOG_ERROR') |
| 11 |
CXT_MISSING_STRATEGY_KEY = 'AWS_XRAY_CONTEXT_MISSING' |
| 12 |
|
| 13 |
|
| 14 |
class Context(object): |
| 15 |
""" |
| 16 |
The context storage class to store trace entities(segments/subsegments). |
| 17 |
The default implementation uses threadlocal to store these entities. |
| 18 |
It also provides interfaces to manually inject trace entities which will |
| 19 |
replace the current stored entities and to clean up the storage. |
| 20 |
|
| 21 |
For any data access or data mutation, if there is no active segment present |
| 22 |
if will use user-defined behavior to handle such case. By default it throws |
| 23 |
an runtime error. |
| 24 |
|
| 25 |
This data structure is thread-safe. |
| 26 |
""" |
| 27 |
def __init__(self, context_missing='RUNTIME_ERROR'): |
| 28 |
|
| 29 |
self._local = threading.local() |
| 30 |
strategy = os.getenv(CXT_MISSING_STRATEGY_KEY, context_missing) |
| 31 |
self._context_missing = strategy |
| 32 |
|
| 33 |
def put_segment(self, segment): |
| 34 |
""" |
| 35 |
Store the segment created by ``xray_recorder`` to the context. |
| 36 |
It overrides the current segment if there is already one. |
| 37 |
""" |
| 38 |
setattr(self._local, 'entities', [segment]) |
| 39 |
|
| 40 |
def end_segment(self, end_time=None): |
| 41 |
""" |
| 42 |
End the current active segment. |
| 43 |
|
| 44 |
:param int end_time: epoch in seconds. If not specified the current |
| 45 |
system time will be used. |
| 46 |
""" |
| 47 |
entity = self.get_trace_entity() |
| 48 |
if not entity: |
| 49 |
log.warning("No segment to end") |
| 50 |
return |
| 51 |
if self._is_subsegment(entity): |
| 52 |
entity.parent_segment.close(end_time) |
| 53 |
else: |
| 54 |
entity.close(end_time) |
| 55 |
|
| 56 |
def put_subsegment(self, subsegment): |
| 57 |
""" |
| 58 |
Store the subsegment created by ``xray_recorder`` to the context. |
| 59 |
If you put a new subsegment while there is already an open subsegment, |
| 60 |
the new subsegment becomes the child of the existing subsegment. |
| 61 |
""" |
| 62 |
entity = self.get_trace_entity() |
| 63 |
if not entity: |
| 64 |
log.warning("Active segment or subsegment not found. Discarded %s." % subsegment.name) |
| 65 |
return |
| 66 |
|
| 67 |
entity.add_subsegment(subsegment) |
| 68 |
self._local.entities.append(subsegment) |
| 69 |
|
| 70 |
def end_subsegment(self, end_time=None): |
| 71 |
""" |
| 72 |
End the current active segment. Return False if there is no |
| 73 |
subsegment to end. |
| 74 |
|
| 75 |
:param int end_time: epoch in seconds. If not specified the current |
| 76 |
system time will be used. |
| 77 |
""" |
| 78 |
subsegment = self.get_trace_entity() |
| 79 |
if self._is_subsegment(subsegment): |
| 80 |
subsegment.close(end_time) |
| 81 |
self._local.entities.pop() |
| 82 |
return True |
| 83 |
else: |
| 84 |
log.warning("No subsegment to end.") |
| 85 |
return False |
| 86 |
|
| 87 |
def get_trace_entity(self): |
| 88 |
""" |
| 89 |
Return the current trace entity(segment/subsegment). If there is none, |
| 90 |
it behaves based on pre-defined ``context_missing`` strategy. |
| 91 |
""" |
| 92 |
if not getattr(self._local, 'entities', None): |
| 93 |
return self.handle_context_missing() |
| 94 |
|
| 95 |
return self._local.entities[-1] |
| 96 |
|
| 97 |
def set_trace_entity(self, trace_entity): |
| 98 |
""" |
| 99 |
Store the input trace_entity to local context. It will overwrite all |
| 100 |
existing ones if there is any. |
| 101 |
""" |
| 102 |
setattr(self._local, 'entities', [trace_entity]) |
| 103 |
|
| 104 |
def clear_trace_entities(self): |
| 105 |
""" |
| 106 |
clear all trace_entities stored in the local context. |
| 107 |
In case of using threadlocal to store trace entites, it will |
| 108 |
clean up all trace entities created by the current thread. |
| 109 |
""" |
| 110 |
self._local.__dict__.clear() |
| 111 |
|
| 112 |
def handle_context_missing(self): |
| 113 |
""" |
| 114 |
Called whenever there is no trace entity to access or mutate. |
| 115 |
""" |
| 116 |
if self.context_missing == 'RUNTIME_ERROR': |
| 117 |
log.error(MISSING_SEGMENT_MSG) |
| 118 |
raise SegmentNotFoundException(MISSING_SEGMENT_MSG) |
| 119 |
else: |
| 120 |
log.error(MISSING_SEGMENT_MSG) |
| 121 |
|
| 122 |
def _is_subsegment(self, entity): |
| 123 |
|
| 124 |
return hasattr(entity, 'type') and entity.type == 'subsegment' |
| 125 |
|
| 126 |
@property |
| 127 |
def context_missing(self): |
| 128 |
return self._context_missing |
| 129 |
|
| 130 |
@context_missing.setter |
| 131 |
def context_missing(self, value): |
| 132 |
if value not in SUPPORTED_CONTEXT_MISSING: |
| 133 |
log.warning('specified context_missing not supported, using default.') |
| 134 |
return |
| 135 |
|
| 136 |
self._context_missing = value |