|
7 | 7 | import os
|
8 | 8 | import json
|
9 | 9 |
|
10 |
| -from aws_xray_sdk.core import xray_recorder |
11 |
| -from aws_xray_sdk.core.lambda_launcher import LambdaContext |
12 | 10 | from datadog_lambda.constants import (
|
13 | 11 | SamplingPriority,
|
14 | 12 | TraceHeader,
|
15 | 13 | XraySubsegment,
|
16 | 14 | TraceContextSource,
|
| 15 | + XRAY_TRACE_ID_HEADER_NAME |
17 | 16 | )
|
18 | 17 | from ddtrace import tracer, patch
|
19 | 18 | from ddtrace import __version__ as ddtrace_version
|
@@ -54,19 +53,18 @@ def _convert_xray_sampling(xray_sampled):
|
54 | 53 |
|
55 | 54 |
|
56 | 55 | def _get_xray_trace_context():
|
57 |
| - if not is_lambda_context(): |
| 56 | + xray_trace_entity = _get_context_from_raw_xray_trace_id(os.environ.get(XRAY_TRACE_ID_HEADER_NAME,"")) |
| 57 | + if xray_trace_entity is None: |
58 | 58 | return None
|
59 |
| - |
60 |
| - xray_trace_entity = xray_recorder.get_trace_entity() # xray (sub)segment |
61 | 59 | trace_context = {
|
62 |
| - "trace-id": _convert_xray_trace_id(xray_trace_entity.trace_id), |
63 |
| - "parent-id": _convert_xray_entity_id(xray_trace_entity.id), |
64 |
| - "sampling-priority": _convert_xray_sampling(xray_trace_entity.sampled), |
| 60 | + "trace-id": _convert_xray_trace_id(xray_trace_entity.get("trace_id")), |
| 61 | + "parent-id": _convert_xray_entity_id(xray_trace_entity.get("id")), |
| 62 | + "sampling-priority": _convert_xray_sampling(xray_trace_entity.get("sampled")), |
65 | 63 | }
|
66 | 64 | logger.debug(
|
67 | 65 | "Converted trace context %s from X-Ray segment %s",
|
68 | 66 | trace_context,
|
69 |
| - (xray_trace_entity.trace_id, xray_trace_entity.id, xray_trace_entity.sampled), |
| 67 | + (xray_trace_entity.get("trace_id"), xray_trace_entity.get("id"), xray_trace_entity.get("sampled")), |
70 | 68 | )
|
71 | 69 | return trace_context
|
72 | 70 |
|
@@ -97,29 +95,28 @@ def _context_obj_to_headers(obj):
|
97 | 95 | TraceHeader.SAMPLING_PRIORITY: str(obj.get("sampling-priority")),
|
98 | 96 | }
|
99 | 97 |
|
100 |
| - |
101 |
| -def create_dd_dummy_metadata_subsegment( |
102 |
| - subsegment_metadata_value, subsegment_metadata_key |
103 |
| -): |
104 |
| - """ |
105 |
| - Create a Datadog subsegment to pass the Datadog trace context or Lambda function |
106 |
| - tags into its metadata field, so the X-Ray trace can be converted to a Datadog |
107 |
| - trace in the Datadog backend with the correct context. |
108 |
| - """ |
109 |
| - try: |
110 |
| - xray_recorder.begin_subsegment(XraySubsegment.NAME) |
111 |
| - subsegment = xray_recorder.current_subsegment() |
112 |
| - subsegment.put_metadata( |
113 |
| - subsegment_metadata_key, subsegment_metadata_value, XraySubsegment.NAMESPACE |
114 |
| - ) |
115 |
| - xray_recorder.end_subsegment() |
116 |
| - except Exception as e: |
117 |
| - logger.debug( |
118 |
| - "failed to create dd dummy metadata subsegment with error %s", |
119 |
| - e, |
120 |
| - exc_info=True, |
121 |
| - ) |
122 |
| - |
| 98 | +def _get_context_from_raw_xray_trace_id(raw_trace_id): |
| 99 | + #example : Root=1-5e272390-8c398be037738dc042009320;Parent=94ae789b969f1cc5;Sampled=1 |
| 100 | + if len(raw_trace_id) == 0: |
| 101 | + return None |
| 102 | + parts = raw_trace_id.split(";") |
| 103 | + if len(parts) != 3: |
| 104 | + return None |
| 105 | + root = parts[0].replace("Root=", "") |
| 106 | + parent = parts[1].replace("Parent=", "") |
| 107 | + sampled = parts[2].replace("Sampled=", "") |
| 108 | + if len(root) == len(parts[0]) or len(parent) == len(parts[1]) or len(sampled) == len(parts[2]): |
| 109 | + return None |
| 110 | + print({ |
| 111 | + "id": parent, |
| 112 | + "trace_id": root, |
| 113 | + "sampled": sampled, |
| 114 | + }) |
| 115 | + return { |
| 116 | + "id": parent, |
| 117 | + "trace_id": root, |
| 118 | + "sampled": sampled, |
| 119 | + } |
123 | 120 |
|
124 | 121 | def extract_context_from_lambda_context(lambda_context):
|
125 | 122 | """
|
@@ -301,6 +298,7 @@ def get_dd_trace_context():
|
301 | 298 | return _context_obj_to_headers(context) if context is not None else {}
|
302 | 299 |
|
303 | 300 |
|
| 301 | + |
304 | 302 | def set_correlation_ids():
|
305 | 303 | """
|
306 | 304 | Create a dummy span, and overrides its trace_id and span_id, to make
|
@@ -353,14 +351,8 @@ def inject_correlation_ids():
|
353 | 351 |
|
354 | 352 | logger.debug("logs injection configured")
|
355 | 353 |
|
356 |
| - |
357 | 354 | def is_lambda_context():
|
358 |
| - """ |
359 |
| - Return True if the X-Ray context is `LambdaContext`, rather than the |
360 |
| - regular `Context` (e.g., when testing lambda functions locally). |
361 |
| - """ |
362 |
| - return type(xray_recorder.context) == LambdaContext |
363 |
| - |
| 355 | + return os.environ.get("FUNCTION_NAME_HEADER_NAME", "") != "" |
364 | 356 |
|
365 | 357 | def set_dd_trace_py_root(trace_context_source, merge_xray_traces):
|
366 | 358 | if trace_context_source == TraceContextSource.EVENT or merge_xray_traces:
|
|
0 commit comments