Skip to content

Commit 01cfbd0

Browse files
committed
ensure x-ray trace id from context overwrites id from event header
1 parent 21887a0 commit 01cfbd0

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

aws-distro-opentelemetry-node-autoinstrumentation/src/patches/instrumentation-patch.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,17 @@ export const customExtractor = (event: any, _handlerContext: Context): OtelConte
102102
? (_handlerContext as any)[lambdaContextXrayTraceIdKey]
103103
: undefined;
104104
const xrayTraceIdFromLambdaEnv = process.env[traceContextEnvironmentKey];
105-
const xrayTraceId = xrayTraceIdFromLambdaContext || xrayTraceIdFromLambdaEnv;
105+
const xrayTraceIdFromLambda = xrayTraceIdFromLambdaContext || xrayTraceIdFromLambdaEnv;
106106

107107
const httpHeaders = event.headers || {};
108-
if (xrayTraceId) {
109-
httpHeaders[AWSXRAY_TRACE_ID_HEADER] = xrayTraceId;
108+
if (xrayTraceIdFromLambda) {
109+
// Delete any X-Ray Trace ID via case-insensitive checks since we will overwrite it here.
110+
Object.keys(httpHeaders).forEach(key => {
111+
if (key.toLowerCase() === AWSXRAY_TRACE_ID_HEADER.toLowerCase()) {
112+
delete httpHeaders[key];
113+
}
114+
});
115+
httpHeaders[AWSXRAY_TRACE_ID_HEADER] = xrayTraceIdFromLambda;
110116
}
111117

112118
const extractedContext = propagation.extract(otelContext.active(), httpHeaders, headerGetter);

aws-distro-opentelemetry-node-autoinstrumentation/test/patches/instrumentation-patch.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,9 @@ describe('AWS Lambda Instrumentation Propagation', () => {
12931293

12941294
const lambdaEvent = {
12951295
headers: {
1296+
'x-amzn-trace-ID': sampledAwsHeader,
12961297
'x-amzn-trace-id': sampledAwsHeader,
1298+
'X-Amzn-Trace-Id': sampledAwsHeader,
12971299
},
12981300
};
12991301
const lambdaCtx2 = {

0 commit comments

Comments
 (0)