Skip to content

Commit 2e03b46

Browse files
committed
fix: ensure aws-xray-propagator maintains trace state
1 parent fb514f7 commit 2e03b46

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

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

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

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+
if (existingTraceState) {
88+
spanContext.traceState = existingTraceState
89+
}
8490
return trace.setSpan(context, trace.wrapSpanContext(spanContext));
8591
}
8692

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)