Skip to content

Commit dfb5e15

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

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,17 @@ def extract(
144144
if sampled:
145145
options |= trace.TraceFlags.SAMPLED
146146

147+
try:
148+
tracestate = trace.get_current_span(context=context).get_span_context().trace_state
149+
except AttributeError:
150+
tracestate = trace.TraceState()
151+
147152
span_context = trace.SpanContext(
148153
trace_id=trace_id,
149154
span_id=span_id,
150155
is_remote=True,
151156
trace_flags=trace.TraceFlags(options),
152-
trace_state=trace.TraceState(),
157+
trace_state=tracestate,
153158
)
154159

155160
if not span_context.is_valid:

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,42 @@ 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(context_with_extracted)
361+
expected_trace_state = TraceState([("foo", "bar"), ("baz", "qux")])
362+
363+
self.assertEqual(extracted_span_context.trace_state, expected_trace_state)
364+
365+
def test_extract_no_trace_state_from_context(self):
366+
"""Test that extract defaults to an empty trace state correctly."""
367+
context_with_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
368+
CaseInsensitiveDict(
369+
{
370+
TRACE_HEADER_KEY: "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=0",
371+
}
372+
)
373+
)
374+
375+
extracted_span_context = get_nested_span_context(context_with_extracted)
376+
self.assertEqual(extracted_span_context.trace_state, TraceState([]))

0 commit comments

Comments
 (0)