Skip to content

Commit cd196cc

Browse files
committed
aws-xray-propagator: ensure trace state is not overwritten
1 parent ba0644f commit cd196cc

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

propagator/opentelemetry-propagator-aws-xray/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
- Update `opentelemetry-api` version to 1.16
1111
([#2961](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2961))
12+
- aws-xray-propagator: ensure trace state is not overwritten
13+
([#3774](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3774))
1214

1315
## Version 1.0.2 (2024-08-05)
1416

propagator/opentelemetry-propagator-aws-xray/src/opentelemetry/propagators/aws/aws_xray_propagator.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,18 @@ def extract(
144144
if sampled:
145145
options |= trace.TraceFlags.SAMPLED
146146

147+
current_span = trace.get_current_span(context=context)
148+
if current_span and current_span.get_span_context():
149+
tracestate = current_span.get_span_context().trace_state
150+
else:
151+
tracestate = trace.TraceState()
152+
147153
span_context = trace.SpanContext(
148154
trace_id=trace_id,
149155
span_id=span_id,
150156
is_remote=True,
151157
trace_flags=trace.TraceFlags(options),
152-
trace_state=trace.TraceState(),
158+
trace_state=tracestate,
153159
)
154160

155161
if not span_context.is_valid:

propagator/opentelemetry-propagator-aws-xray/tests/test_aws_xray_propagator.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,48 @@ def test_fields(self, mock_trace):
335335
self.assertEqual(
336336
AwsXRayPropagatorTest.XRAY_PROPAGATOR.fields, inject_fields
337337
)
338+
339+
def test_extract_trace_state_from_context(self):
340+
"""Test that extract properly propagates the trace state extracted by other propagators."""
341+
context_with_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
342+
CaseInsensitiveDict(
343+
{
344+
TRACE_HEADER_KEY: "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=0",
345+
}
346+
),
347+
context=set_span_in_context(
348+
trace_api.NonRecordingSpan(
349+
SpanContext(
350+
int(TRACE_ID_BASE16, 16),
351+
int(SPAN_ID_BASE16, 16),
352+
True,
353+
DEFAULT_TRACE_OPTIONS,
354+
TraceState([("foo", "bar"), ("baz", "qux")]),
355+
)
356+
)
357+
),
358+
)
359+
360+
extracted_span_context = get_nested_span_context(
361+
context_with_extracted
362+
)
363+
expected_trace_state = TraceState([("foo", "bar"), ("baz", "qux")])
364+
365+
self.assertEqual(
366+
extracted_span_context.trace_state, expected_trace_state
367+
)
368+
369+
def test_extract_no_trace_state_from_context(self):
370+
"""Test that extract defaults to an empty trace state correctly."""
371+
context_with_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
372+
CaseInsensitiveDict(
373+
{
374+
TRACE_HEADER_KEY: "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=0",
375+
}
376+
)
377+
)
378+
379+
extracted_span_context = get_nested_span_context(
380+
context_with_extracted
381+
)
382+
self.assertEqual(extracted_span_context.trace_state, TraceState([]))

0 commit comments

Comments
 (0)