Skip to content

Commit 49190c5

Browse files
committed
fix: ensure aws-xray-propagator maintains trace state
1 parent a19474f commit 49190c5

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

packages/propagator-aws-xray/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
### Bug Fixes
88

9+
* fix: ensure aws-xray-propagator propagates trace state ([#3092](https://github.com/open-telemetry/opentelemetry-js-contrib/pull/3092))
10+
911
* **deps:** update all patch versions ([#2948](https://github.com/open-telemetry/opentelemetry-js-contrib/issues/2948)) ([5836d7a](https://github.com/open-telemetry/opentelemetry-js-contrib/commit/5836d7ab3244adef62b715ef22a26b54dba6719b))
1012

1113
## [2.1.0](https://github.com/open-telemetry/opentelemetry-js-contrib/compare/propagator-aws-xray-v2.0.0...propagator-aws-xray-v2.1.0) (2025-05-02)

packages/propagator-aws-xray/src/AWSXRayPropagator.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,17 @@ export class AWSXRayPropagator implements TextMapPropagator {
8181
const spanContext = this.getSpanContextFromHeader(carrier, getter);
8282
if (!isSpanContextValid(spanContext)) return context;
8383

84-
return trace.setSpan(context, trace.wrapSpanContext(spanContext));
84+
// If a previous propagator already set the trace state, ensure it's propagated
85+
const existingSpan = trace.getSpan(context);
86+
const existingTraceState = existingSpan?.spanContext()?.traceState;
87+
const finalSpanContext: SpanContext = existingTraceState
88+
? {
89+
...spanContext,
90+
traceState: existingTraceState,
91+
}
92+
: spanContext;
93+
94+
return trace.setSpan(context, trace.wrapSpanContext(finalSpanContext));
8595
}
8696

8797
fields(): string[] {

packages/propagator-aws-xray/test/AWSXRayPropagator.test.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ describe('AWSXRayPropagator', () => {
7979
);
8080
});
8181

82-
it('should inject with TraceState', () => {
83-
const traceState = new TraceState();
84-
traceState.set('foo', 'bar');
82+
it('should not inject with TraceState', () => {
83+
const traceState = new TraceState().set('foo', 'bar').set('key', 'val');
8584
const spanContext: SpanContext = {
8685
traceId: TRACE_ID,
8786
spanId: SPAN_ID,
@@ -94,7 +93,8 @@ describe('AWSXRayPropagator', () => {
9493
defaultTextMapSetter
9594
);
9695

97-
// TODO: assert trace state when the propagator supports it
96+
// Rely on W3CTraceContextPropagator to propagate trace state
97+
// As such, it's expected that trace state is not populated here
9898
assert.deepStrictEqual(
9999
carrier[AWSXRAY_TRACE_ID_HEADER],
100100
'Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=1'
@@ -345,6 +345,36 @@ describe('AWSXRayPropagator', () => {
345345
});
346346
});
347347

348+
it('should extract with TraceState previously propagated', () => {
349+
carrier[AWSXRAY_TRACE_ID_HEADER] =
350+
'Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=1';
351+
352+
const spanContextWithTraceState: SpanContext = {
353+
traceId: 'existing-trace-id',
354+
spanId: 'existing-span-id',
355+
traceFlags: TraceFlags.SAMPLED,
356+
traceState: new TraceState().set('foo', 'bar').set('from', 'context'),
357+
};
358+
const incomingContext = trace.setSpan(
359+
ROOT_CONTEXT,
360+
trace.wrapSpanContext(spanContextWithTraceState)
361+
);
362+
363+
const extractedSpanContext = trace
364+
.getSpan(
365+
xrayPropagator.extract(incomingContext, carrier, defaultTextMapGetter)
366+
)
367+
?.spanContext();
368+
369+
assert.deepStrictEqual(extractedSpanContext, {
370+
traceId: TRACE_ID,
371+
spanId: SPAN_ID,
372+
isRemote: true,
373+
traceFlags: TraceFlags.SAMPLED,
374+
traceState: new TraceState().set('foo', 'bar').set('from', 'context'),
375+
});
376+
});
377+
348378
describe('.fields()', () => {
349379
it('should return a field with AWS X-Ray Trace ID header', () => {
350380
const expectedField = xrayPropagator.fields();

0 commit comments

Comments
 (0)