Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dweitzman-codaio committed Dec 17, 2024
1 parent 35dbd46 commit ea0e07f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
6 changes: 4 additions & 2 deletions propagators/propagator-aws-xray/src/AWSXRayPropagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions propagators/propagator-aws-xray/test/AWSXRayPropagator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
SpanContext,
TraceFlags,
trace,
TextMapGetter,
} from '@opentelemetry/api';
import { TraceState } from '@opentelemetry/core';

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ea0e07f

Please sign in to comment.