Skip to content

Commit 122e616

Browse files
Use xray context span id when it exists (#401)
* Extract xray context span id when it exists * Add test for no Parent in _X_AMZN_TRACE_ID. --------- Co-authored-by: Daniel Ebrahimian <git@danielebra.com>
1 parent 9161c28 commit 122e616

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

datadog_lambda/tracing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ def set_dd_trace_py_root(trace_context_source, merge_xray_traces):
604604
)
605605
if merge_xray_traces:
606606
xray_context = _get_xray_trace_context()
607-
if xray_context.span_id:
607+
if xray_context and xray_context.span_id:
608608
context.span_id = xray_context.span_id
609609

610610
tracer.context_provider.activate(context)

tests/test_tracing.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from datadog_lambda.constants import (
1515
SamplingPriority,
1616
TraceHeader,
17+
TraceContextSource,
1718
XraySubsegment,
1819
)
1920
from datadog_lambda.tracing import (
@@ -854,6 +855,30 @@ def test_mixed_parent_context_when_merging(self):
854855
self.mock_activate.assert_called()
855856
self.mock_activate.assert_has_calls([call(expected_context)])
856857

858+
def test_set_dd_trace_py_root_no_span_id(self):
859+
os.environ["_X_AMZN_TRACE_ID"] = "Root=1-5e272390-8c398be037738dc042009320"
860+
861+
lambda_ctx = get_mock_context()
862+
ctx, source, event_type = extract_dd_trace_context(
863+
{
864+
"headers": {
865+
TraceHeader.TRACE_ID: "123",
866+
TraceHeader.PARENT_ID: "321",
867+
TraceHeader.SAMPLING_PRIORITY: "1",
868+
}
869+
},
870+
lambda_ctx,
871+
)
872+
set_dd_trace_py_root(TraceContextSource.EVENT, True)
873+
874+
expected_context = Context(
875+
trace_id=123, # Trace Id from incomming context
876+
span_id=321, # Span Id from incoming context
877+
sampling_priority=1, # Sampling priority from incomming context
878+
)
879+
self.mock_activate.assert_called()
880+
self.mock_activate.assert_has_calls([call(expected_context)])
881+
857882

858883
class TestAuthorizerInferredSpans(unittest.TestCase):
859884
def setUp(self):

0 commit comments

Comments
 (0)