|
| 1 | +from aws_xray_sdk.core import xray_recorder |
| 2 | + |
| 3 | +from datadog_lambda.constants import ( |
| 4 | + SamplingPriority, |
| 5 | + TraceHeader, |
| 6 | + XraySubsegment, |
| 7 | +) |
| 8 | + |
| 9 | +dd_trace_context = {} |
| 10 | + |
| 11 | + |
| 12 | +def extract_dd_trace_context(event): |
| 13 | + """ |
| 14 | + Extract Datadog trace context from the Lambda `event` object. |
| 15 | +
|
| 16 | + Write the context to a global `dd_trace_context`, so the trace |
| 17 | + can be continued on the outgoing requests with the context injected. |
| 18 | +
|
| 19 | + Save the context to an X-Ray subsegment's metadata field, so the X-Ray |
| 20 | + trace can be converted to a Datadog trace in the Datadog backend with |
| 21 | + the correct context. |
| 22 | + """ |
| 23 | + global dd_trace_context |
| 24 | + headers = event.get('headers', {}) |
| 25 | + trace_id = headers.get(TraceHeader.TRACE_ID) |
| 26 | + parent_id = headers.get(TraceHeader.PARENT_ID) |
| 27 | + sampling_priority = headers.get(TraceHeader.SAMPLING_PRIORITY) |
| 28 | + if trace_id and parent_id and sampling_priority: |
| 29 | + dd_trace_context = { |
| 30 | + 'trace-id': trace_id, |
| 31 | + 'parent-id': parent_id, |
| 32 | + 'sampling-priority': sampling_priority, |
| 33 | + } |
| 34 | + xray_recorder.begin_subsegment(XraySubsegment.NAME) |
| 35 | + subsegment = xray_recorder.current_subsegment() |
| 36 | + subsegment.put_metadata( |
| 37 | + XraySubsegment.KEY, |
| 38 | + dd_trace_context, |
| 39 | + XraySubsegment.NAMESPACE |
| 40 | + ) |
| 41 | + xray_recorder.end_subsegment() |
| 42 | + else: |
| 43 | + # AWS Lambda runtime caches global variables between invocations, |
| 44 | + # reset to avoid using the context from the last invocation. |
| 45 | + dd_trace_context = {} |
| 46 | + |
| 47 | + |
| 48 | +def get_dd_trace_context(): |
| 49 | + """ |
| 50 | + Return the Datadog trace context to be propogated on the outgoing requests. |
| 51 | +
|
| 52 | + If the Lambda function is invoked by a Datadog-traced service, a Datadog |
| 53 | + trace |
| 54 | + context may already exist, and it should be used. Otherwise, use the |
| 55 | + current X-Ray trace entity. |
| 56 | +
|
| 57 | + Most of widely-used HTTP clients are patched to inject the context |
| 58 | + automatically, but this function can be used to manually inject the trace |
| 59 | + context to an outgoing request. |
| 60 | + """ |
| 61 | + global dd_trace_context |
| 62 | + xray_trace_entity = xray_recorder.get_trace_entity() # xray (sub)segment |
| 63 | + if dd_trace_context: |
| 64 | + return { |
| 65 | + TraceHeader.TRACE_ID: |
| 66 | + dd_trace_context['trace-id'], |
| 67 | + TraceHeader.PARENT_ID: _convert_xray_entity_id( |
| 68 | + xray_trace_entity.id), |
| 69 | + TraceHeader.SAMPLING_PRIORITY: |
| 70 | + dd_trace_context['sampling-priority'], |
| 71 | + } |
| 72 | + else: |
| 73 | + return { |
| 74 | + TraceHeader.TRACE_ID: _convert_xray_trace_id( |
| 75 | + xray_trace_entity.trace_id), |
| 76 | + TraceHeader.PARENT_ID: _convert_xray_entity_id( |
| 77 | + xray_trace_entity.id), |
| 78 | + TraceHeader.SAMPLING_PRIORITY: _convert_xray_sampling( |
| 79 | + xray_trace_entity.sampled), |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +def _convert_xray_trace_id(xray_trace_id): |
| 84 | + """ |
| 85 | + Convert X-Ray trace id (hex)'s last 63 bits to a Datadog trace id (int). |
| 86 | + """ |
| 87 | + return str(0x7FFFFFFFFFFFFFFF & int(xray_trace_id[-16:], 16)) |
| 88 | + |
| 89 | + |
| 90 | +def _convert_xray_entity_id(xray_entity_id): |
| 91 | + """ |
| 92 | + Convert X-Ray (sub)segement id (hex) to a Datadog span id (int). |
| 93 | + """ |
| 94 | + return str(int(xray_entity_id, 16)) |
| 95 | + |
| 96 | + |
| 97 | +def _convert_xray_sampling(xray_sampled): |
| 98 | + """ |
| 99 | + Convert X-Ray sampled (True/False) to its Datadog counterpart. |
| 100 | + """ |
| 101 | + return str(SamplingPriority.USER_KEEP) if xray_sampled \ |
| 102 | + else str(SamplingPriority.USER_REJECT) |
0 commit comments