Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/propagator-aws-xray/src/AWSXRayPropagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ export class AWSXRayPropagator implements TextMapPropagator {
const spanContext = this.getSpanContextFromHeader(carrier, getter);
if (!isSpanContextValid(spanContext)) return context;

// If a previous propagator already set the trace state, ensure it's propagated
const existingSpan = trace.getSpan(context);
const existingTraceState = existingSpan?.spanContext()?.traceState;
if (existingTraceState) {
spanContext.traceState = existingTraceState
}
return trace.setSpan(context, trace.wrapSpanContext(spanContext));
}

Expand Down
38 changes: 34 additions & 4 deletions packages/propagator-aws-xray/test/AWSXRayPropagator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ describe('AWSXRayPropagator', () => {
);
});

it('should inject with TraceState', () => {
const traceState = new TraceState();
traceState.set('foo', 'bar');
it('should not inject with TraceState', () => {
const traceState = new TraceState().set('foo', 'bar').set('key', 'val');
const spanContext: SpanContext = {
traceId: TRACE_ID,
spanId: SPAN_ID,
Expand All @@ -94,7 +93,8 @@ describe('AWSXRayPropagator', () => {
defaultTextMapSetter
);

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

it('should extract with TraceState previously propagated', () => {
carrier[AWSXRAY_TRACE_ID_HEADER] =
'Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=1';

const spanContextWithTraceState: SpanContext = {
traceId: 'existing-trace-id',
spanId: 'existing-span-id',
traceFlags: TraceFlags.SAMPLED,
traceState: new TraceState().set('foo', 'bar').set('from', 'context'),
};
const incomingContext = trace.setSpan(
ROOT_CONTEXT,
trace.wrapSpanContext(spanContextWithTraceState)
);

const extractedSpanContext = trace
.getSpan(
xrayPropagator.extract(incomingContext, carrier, defaultTextMapGetter)
)
?.spanContext();

assert.deepStrictEqual(extractedSpanContext, {
traceId: TRACE_ID,
spanId: SPAN_ID,
isRemote: true,
traceFlags: TraceFlags.SAMPLED,
traceState: new TraceState().set('foo', 'bar').set('from', 'context'),
});
});

describe('.fields()', () => {
it('should return a field with AWS X-Ray Trace ID header', () => {
const expectedField = xrayPropagator.fields();
Expand Down