| 1 |
import os |
| 2 |
import logging |
| 3 |
import threading |
| 4 |
|
| 5 |
from .models.facade_segment import FacadeSegment |
| 6 |
from .models.trace_header import TraceHeader |
| 7 |
from .context import Context |
| 8 |
|
| 9 |
|
| 10 |
log = logging.getLogger(__name__) |
| 11 |
|
| 12 |
|
| 13 |
LAMBDA_TRACE_HEADER_KEY = '_X_AMZN_TRACE_ID' |
| 14 |
LAMBDA_TASK_ROOT_KEY = 'LAMBDA_TASK_ROOT' |
| 15 |
TOUCH_FILE_DIR = '/tmp/.aws-xray/' |
| 16 |
TOUCH_FILE_PATH = '/tmp/.aws-xray/initialized' |
| 17 |
|
| 18 |
|
| 19 |
def check_in_lambda(): |
| 20 |
""" |
| 21 |
Return None if SDK is not loaded in AWS Lambda worker. |
| 22 |
Otherwise drop a touch file and return a lambda context. |
| 23 |
""" |
| 24 |
if not os.getenv(LAMBDA_TASK_ROOT_KEY): |
| 25 |
return None |
| 26 |
|
| 27 |
try: |
| 28 |
os.mkdir(TOUCH_FILE_DIR) |
| 29 |
except OSError: |
| 30 |
log.debug('directory %s already exists', TOUCH_FILE_DIR) |
| 31 |
|
| 32 |
try: |
| 33 |
f = open(TOUCH_FILE_PATH, 'w+') |
| 34 |
f.close() |
| 35 |
# utime force second parameter in python2.7 |
| 36 |
os.utime(TOUCH_FILE_PATH, None) |
| 37 |
except (IOError, OSError): |
| 38 |
log.warning("Unable to write to %s. Failed to signal SDK initialization." % TOUCH_FILE_PATH) |
| 39 |
|
| 40 |
return LambdaContext() |
| 41 |
|
| 42 |
|
| 43 |
class LambdaContext(Context): |
| 44 |
""" |
| 45 |
Lambda service will generate a segment for each function invocation which |
| 46 |
cannot be mutated. The context doesn't keep any manually created segment |
| 47 |
but instead every time ``get_trace_entity()`` gets called it refresh the facade |
| 48 |
segment based on environment variables set by Lambda worker. |
| 49 |
""" |
| 50 |
def __init__(self): |
| 51 |
|
| 52 |
self._local = threading.local() |
| 53 |
|
| 54 |
def put_segment(self, segment): |
| 55 |
""" |
| 56 |
No-op. |
| 57 |
""" |
| 58 |
log.warning('Cannot create segments inside Lambda function. Discarded.') |
| 59 |
|
| 60 |
def end_segment(self, end_time=None): |
| 61 |
""" |
| 62 |
No-op. |
| 63 |
""" |
| 64 |
log.warning('Cannot end segment inside Lambda function. Ignored.') |
| 65 |
|
| 66 |
def put_subsegment(self, subsegment): |
| 67 |
""" |
| 68 |
Refresh the facade segment every time this function is invoked to prevent |
| 69 |
a new subsegment from being attached to a leaked segment/subsegment. |
| 70 |
""" |
| 71 |
current_entity = self.get_trace_entity() |
| 72 |
|
| 73 |
if not self._is_subsegment(current_entity) and current_entity.initializing: |
| 74 |
log.warning("Subsegment %s discarded due to Lambda worker still initializing" % subsegment.name) |
| 75 |
|
| 76 |
current_entity.add_subsegment(subsegment) |
| 77 |
self._local.entities.append(subsegment) |
| 78 |
|
| 79 |
def get_trace_entity(self): |
| 80 |
self._refresh_context() |
| 81 |
if getattr(self._local, 'entities', None): |
| 82 |
return self._local.entities[-1] |
| 83 |
else: |
| 84 |
return self._local.segment |
| 85 |
|
| 86 |
def _refresh_context(self): |
| 87 |
""" |
| 88 |
Get current facade segment. To prevent resource leaking in Lambda worker, |
| 89 |
every time there is segment present, we compare its trace id to current |
| 90 |
environment variables. If it is different we create a new facade segment |
| 91 |
and clean up subsegments stored. |
| 92 |
""" |
| 93 |
header_str = os.getenv(LAMBDA_TRACE_HEADER_KEY) |
| 94 |
trace_header = TraceHeader.from_header_str(header_str) |
| 95 |
segment = getattr(self._local, 'segment', None) |
| 96 |
|
| 97 |
if segment: |
| 98 |
# Ensure customers don't have leaked subsegments across invocations |
| 99 |
if not trace_header.root or trace_header.root == segment.trace_id: |
| 100 |
return |
| 101 |
else: |
| 102 |
self._initialize_context(trace_header) |
| 103 |
else: |
| 104 |
self._initialize_context(trace_header) |
| 105 |
|
| 106 |
@property |
| 107 |
def context_missing(self): |
| 108 |
return None |
| 109 |
|
| 110 |
@context_missing.setter |
| 111 |
def context_missing(self, value): |
| 112 |
pass |
| 113 |
|
| 114 |
def handle_context_missing(self): |
| 115 |
""" |
| 116 |
No-op. |
| 117 |
""" |
| 118 |
pass |
| 119 |
|
| 120 |
def _initialize_context(self, trace_header): |
| 121 |
""" |
| 122 |
Create a facade segment based on environment variables |
| 123 |
set by AWS Lambda and initialize storage for subsegments. |
| 124 |
""" |
| 125 |
sampled = None |
| 126 |
if trace_header.sampled == 0: |
| 127 |
sampled = False |
| 128 |
elif trace_header.sampled == 1: |
| 129 |
sampled = True |
| 130 |
|
| 131 |
segment = FacadeSegment( |
| 132 |
name='facade', |
| 133 |
traceid=trace_header.root, |
| 134 |
entityid=trace_header.parent, |
| 135 |
sampled=sampled, |
| 136 |
) |
| 137 |
setattr(self._local, 'segment', segment) |
| 138 |
setattr(self._local, 'entities', []) |