From 4227d8a3df7b0782d76844e89d452e0432a704f4 Mon Sep 17 00:00:00 2001 From: Norman Pellet Date: Tue, 7 Feb 2023 21:56:58 +0100 Subject: [PATCH] feat(propagator/aws-xray): Extract X-Ray header in a case-insensitive fashion (#1328) * Extract X-Ray header in a case-insensitive fashion * Back to apostrophes * Switch to 1 tab = 2 spaces * Add semis * Apply prettier to tests as well * chore: fix lint --------- Co-authored-by: Amir Blum Co-authored-by: Daniel Dyla --- .../src/AWSXRayPropagator.ts | 13 +++++++++++-- .../test/AWSXRayPropagator.test.ts | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/propagators/opentelemetry-propagator-aws-xray/src/AWSXRayPropagator.ts b/propagators/opentelemetry-propagator-aws-xray/src/AWSXRayPropagator.ts index 89215ef6f6..e06568d9ee 100644 --- a/propagators/opentelemetry-propagator-aws-xray/src/AWSXRayPropagator.ts +++ b/propagators/opentelemetry-propagator-aws-xray/src/AWSXRayPropagator.ts @@ -92,9 +92,18 @@ export class AWSXRayPropagator implements TextMapPropagator { carrier: unknown, getter: TextMapGetter ): SpanContext { - const traceHeader = getter.get(carrier, AWSXRAY_TRACE_ID_HEADER); - if (!traceHeader || typeof traceHeader !== 'string') + const headerKeys = getter.keys(carrier); + const relevantHeaderKey = headerKeys.find(e => { + return e.toLowerCase() === AWSXRAY_TRACE_ID_HEADER; + }); + if (!relevantHeaderKey) { return INVALID_SPAN_CONTEXT; + } + const traceHeader = getter.get(carrier, relevantHeaderKey); + + if (!traceHeader || typeof traceHeader !== 'string') { + return INVALID_SPAN_CONTEXT; + } let pos = 0; let trimmedPart: string; diff --git a/propagators/opentelemetry-propagator-aws-xray/test/AWSXRayPropagator.test.ts b/propagators/opentelemetry-propagator-aws-xray/test/AWSXRayPropagator.test.ts index c110745c83..5cf47916d5 100644 --- a/propagators/opentelemetry-propagator-aws-xray/test/AWSXRayPropagator.test.ts +++ b/propagators/opentelemetry-propagator-aws-xray/test/AWSXRayPropagator.test.ts @@ -299,6 +299,24 @@ describe('AWSXRayPropagator', () => { assert.deepStrictEqual(extractedSpanContext, undefined); }); + + it('extracts context in a case-insensitive fashion', () => { + carrier[AWSXRAY_TRACE_ID_HEADER.toUpperCase()] = + 'Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=1;Foo=Bar'; + const extractedSpanContext = trace + .getSpan( + xrayPropagator.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) + ) + ?.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();