diff --git a/propagators/propagator-aws-xray/src/AWSXRayPropagator.ts b/propagators/propagator-aws-xray/src/AWSXRayPropagator.ts index 7e3e62bf30..06f535ad93 100644 --- a/propagators/propagator-aws-xray/src/AWSXRayPropagator.ts +++ b/propagators/propagator-aws-xray/src/AWSXRayPropagator.ts @@ -99,8 +99,10 @@ export class AWSXRayPropagator implements TextMapPropagator { if (!relevantHeaderKey) { return INVALID_SPAN_CONTEXT; } - let traceHeader = getter.get(carrier, relevantHeaderKey); - traceHeader = Array.isArray(traceHeader) ? traceHeader[0] : traceHeader; + const rawTraceHeader = getter.get(carrier, relevantHeaderKey); + const traceHeader = Array.isArray(rawTraceHeader) + ? rawTraceHeader[0] + : rawTraceHeader; if (!traceHeader || typeof traceHeader !== 'string') { return INVALID_SPAN_CONTEXT; diff --git a/propagators/propagator-aws-xray/test/AWSXRayPropagator.test.ts b/propagators/propagator-aws-xray/test/AWSXRayPropagator.test.ts index 5cf47916d5..4a6ab8db1c 100644 --- a/propagators/propagator-aws-xray/test/AWSXRayPropagator.test.ts +++ b/propagators/propagator-aws-xray/test/AWSXRayPropagator.test.ts @@ -23,6 +23,7 @@ import { SpanContext, TraceFlags, trace, + TextMapGetter, } from '@opentelemetry/api'; import { TraceState } from '@opentelemetry/core'; @@ -317,6 +318,33 @@ describe('AWSXRayPropagator', () => { }); }); + it('should extract multi header using array getter', () => { + carrier[AWSXRAY_TRACE_ID_HEADER] = + 'Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=1'; + const multiGetter: TextMapGetter = { + get(carrier, key) { + if (carrier == null || carrier[key] === undefined) { + return undefined; + } + return [carrier[key]]; + }, + keys: function (carrier: any): string[] { + return defaultTextMapGetter.keys(carrier); + }, + }; + + const extractedSpanContext = trace + .getSpan(xrayPropagator.extract(ROOT_CONTEXT, carrier, multiGetter)) + ?.spanContext(); + + assert.deepStrictEqual(extractedSpanContext, { + traceId: TRACE_ID, + spanId: SPAN_ID, + isRemote: true, + traceFlags: TraceFlags.SAMPLED, + }); + }); + describe('.fields()', () => { it('should return a field with AWS X-Ray Trace ID header', () => { const expectedField = xrayPropagator.fields();