Skip to content

Commit 70eeac9

Browse files
committed
address propagation issue with aws-sdk-js-v3 recursion detection middleware
1 parent 60591ec commit 70eeac9

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ function patchAwsLambdaInstrumentation(instrumentation: Instrumentation): void {
294294
// Override the upstream private _getV3SmithyClientSendPatch method to add middleware to inject X-Ray Trace Context into HTTP Headers
295295
// https://github.com/open-telemetry/opentelemetry-js-contrib/blob/instrumentation-aws-sdk-v0.48.0/plugins/node/opentelemetry-instrumentation-aws-sdk/src/aws-sdk.ts#L373-L384
296296
const awsXrayPropagator = new AWSXRayPropagator();
297+
const AWSXRAY_TRACE_ID_HEADER_CAPITALIZED = 'X-Amzn-Trace-Id';
297298
const V3_CLIENT_CONFIG_KEY = Symbol('opentelemetry.instrumentation.aws-sdk.client.config');
298299
type V3PluginCommand = AwsV3Command<any, any, any, any, any> & {
299300
[V3_CLIENT_CONFIG_KEY]?: any;
@@ -307,6 +308,12 @@ function patchAwsSdkInstrumentation(instrumentation: Instrumentation): void {
307308
this.middlewareStack?.add(
308309
(next: any, context: any) => async (middlewareArgs: any) => {
309310
awsXrayPropagator.inject(otelContext.active(), middlewareArgs.request.headers, defaultTextMapSetter);
311+
// Need to set capitalized version of the trace id to ensure that the Recursion Detection Middleware
312+
// of aws-sdk-js-v3 will detect the propagated X-Ray Context
313+
// See: https://github.com/aws/aws-sdk-js-v3/blob/v3.768.0/packages/middleware-recursion-detection/src/index.ts#L13
314+
middlewareArgs.request.headers[AWSXRAY_TRACE_ID_HEADER_CAPITALIZED] =
315+
middlewareArgs.request.headers[AWSXRAY_TRACE_ID_HEADER];
316+
delete middlewareArgs.request.headers[AWSXRAY_TRACE_ID_HEADER];
310317
const result = await next(middlewareArgs);
311318
return result;
312319
},

0 commit comments

Comments
 (0)