| 1 |
import re |
| 2 |
|
| 3 |
from aws_xray_sdk.core.models.trace_header import TraceHeader |
| 4 |
from aws_xray_sdk.core.models import http |
| 5 |
|
| 6 |
|
| 7 |
first_cap_re = re.compile('(.)([A-Z][a-z]+)') |
| 8 |
all_cap_re = re.compile('([a-z0-9])([A-Z])') |
| 9 |
|
| 10 |
|
| 11 |
def inject_trace_header(headers, entity): |
| 12 |
""" |
| 13 |
Extract trace id, entity id and sampling decision |
| 14 |
from the input entity and inject these information |
| 15 |
to headers. |
| 16 |
|
| 17 |
:param dict headers: http headers to inject |
| 18 |
:param Entity entity: trace entity that the trace header |
| 19 |
value generated from. |
| 20 |
""" |
| 21 |
if not entity: |
| 22 |
return |
| 23 |
|
| 24 |
to_insert = TraceHeader( |
| 25 |
root=entity.trace_id, |
| 26 |
parent=entity.id, |
| 27 |
sampled=entity.sampled, |
| 28 |
) |
| 29 |
|
| 30 |
value = to_insert.to_header_str() |
| 31 |
|
| 32 |
headers[http.XRAY_HEADER] = value |
| 33 |
|
| 34 |
|
| 35 |
def calculate_sampling_decision(trace_header, recorder, |
| 36 |
service_name, method, path): |
| 37 |
""" |
| 38 |
Return 1 if should sample and 0 if should not. |
| 39 |
The sampling decision coming from ``trace_header`` always has |
| 40 |
the highest precedence. If the ``trace_header`` doesn't contain |
| 41 |
sampling decision then it checks if sampling is enabled or not |
| 42 |
in the recorder. If not enbaled it returns 1. Otherwise it uses |
| 43 |
sampling rule to decide. |
| 44 |
""" |
| 45 |
if trace_header.sampled is not None: |
| 46 |
return trace_header.sampled |
| 47 |
elif not recorder.sampling: |
| 48 |
return 1 |
| 49 |
elif recorder.sampler.should_trace( |
| 50 |
service_name=service_name, |
| 51 |
method=method, |
| 52 |
path=path, |
| 53 |
): |
| 54 |
return 1 |
| 55 |
else: |
| 56 |
return 0 |
| 57 |
|
| 58 |
|
| 59 |
def construct_xray_header(headers): |
| 60 |
""" |
| 61 |
Construct a ``TraceHeader`` object from dictionary headers |
| 62 |
of the incoming request. This method should always return |
| 63 |
a ``TraceHeader`` object regardless of tracing header's presence |
| 64 |
in the incoming request. |
| 65 |
""" |
| 66 |
header_str = headers.get(http.XRAY_HEADER) or headers.get(http.ALT_XRAY_HEADER) |
| 67 |
if header_str: |
| 68 |
return TraceHeader.from_header_str(header_str) |
| 69 |
else: |
| 70 |
return TraceHeader() |
| 71 |
|
| 72 |
|
| 73 |
def calculate_segment_name(host_name, recorder): |
| 74 |
""" |
| 75 |
Returns the segment name based on recorder configuration and |
| 76 |
input host name. This is a helper generally used in web framework |
| 77 |
middleware where a host name is available from incoming request's headers. |
| 78 |
""" |
| 79 |
if recorder.dynamic_naming: |
| 80 |
return recorder.dynamic_naming.get_name(host_name) |
| 81 |
else: |
| 82 |
return recorder.service |
| 83 |
|
| 84 |
|
| 85 |
def to_snake_case(name): |
| 86 |
""" |
| 87 |
Convert the input string to snake-cased string. |
| 88 |
""" |
| 89 |
s1 = first_cap_re.sub(r'\1_\2', name) |
| 90 |
# handle acronym words |
| 91 |
return all_cap_re.sub(r'\1_\2', s1).lower() |