Skip to content

Commit 6e183e0

Browse files
committed
Fix incorrect parent id of aws.lambda span
1 parent 689ed6f commit 6e183e0

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

datadog_lambda/tracing.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,17 @@ def _get_xray_trace_context():
5858
return None
5959

6060
xray_trace_entity = xray_recorder.get_trace_entity() # xray (sub)segment
61-
return {
61+
trace_context = {
6262
"trace-id": _convert_xray_trace_id(xray_trace_entity.trace_id),
6363
"parent-id": _convert_xray_entity_id(xray_trace_entity.id),
6464
"sampling-priority": _convert_xray_sampling(xray_trace_entity.sampled),
6565
}
66+
logger.debug(
67+
"Converted trace context %s from X-Ray segment %s",
68+
trace_context,
69+
(xray_trace_entity.trace_id, xray_trace_entity.id, xray_trace_entity.sampled),
70+
)
71+
return trace_context
6672

6773

6874
def _get_dd_trace_py_context():
@@ -73,6 +79,9 @@ def _get_dd_trace_py_context():
7379
parent_id = span.context.span_id
7480
trace_id = span.context.trace_id
7581
sampling_priority = span.context.sampling_priority
82+
logger.debug(
83+
"found dd trace context: %s", (span.context.trace_id, span.context.span_id)
84+
)
7685
return {
7786
"parent-id": str(parent_id),
7887
"trace-id": str(trace_id),
@@ -170,7 +179,11 @@ def extract_context_custom_extractor(extractor, event, lambda_context):
170179
Extract Datadog trace context using a custom trace extractor function
171180
"""
172181
try:
173-
(trace_id, parent_id, sampling_priority,) = extractor(event, lambda_context)
182+
(
183+
trace_id,
184+
parent_id,
185+
sampling_priority,
186+
) = extractor(event, lambda_context)
174187
return trace_id, parent_id, sampling_priority
175188
except Exception as e:
176189
logger.debug("The trace extractor returned with error %s", e)
@@ -189,9 +202,11 @@ def extract_dd_trace_context(event, lambda_context, extractor=None):
189202
trace_context_source = None
190203

191204
if extractor is not None:
192-
(trace_id, parent_id, sampling_priority,) = extract_context_custom_extractor(
193-
extractor, event, lambda_context
194-
)
205+
(
206+
trace_id,
207+
parent_id,
208+
sampling_priority,
209+
) = extract_context_custom_extractor(extractor, event, lambda_context)
195210
elif "headers" in event:
196211
(
197212
trace_id,
@@ -258,11 +273,11 @@ def get_dd_trace_context():
258273
elif xray_context and dd_trace_context:
259274
context = dd_trace_context.copy()
260275
context["parent-id"] = xray_context["parent-id"]
276+
logger.debug("Set parent id from xray trace context: %s", context["parent-id"])
261277

262278
if dd_tracing_enabled:
263279
dd_trace_py_context = _get_dd_trace_py_context()
264280
if dd_trace_py_context is not None:
265-
logger.debug("get_dd_trace_context using dd-trace context")
266281
context = dd_trace_py_context
267282

268283
return _context_obj_to_headers(context) if context is not None else {}
@@ -328,9 +343,13 @@ def is_lambda_context():
328343

329344
def set_dd_trace_py_root(trace_context_source, merge_xray_traces):
330345
if trace_context_source == TraceContextSource.EVENT or merge_xray_traces:
331-
headers = get_dd_trace_context()
346+
headers = _context_obj_to_headers(dd_trace_context)
332347
span_context = propagator.extract(headers)
333348
tracer.context_provider.activate(span_context)
349+
logger.debug(
350+
"Set dd trace root context to: %s",
351+
(span_context.trace_id, span_context.span_id),
352+
)
334353

335354

336355
def create_function_execution_span(

0 commit comments

Comments
 (0)