diff --git a/packages/tracer/src/Tracer.ts b/packages/tracer/src/Tracer.ts index 374c1359ce..b52e963658 100644 --- a/packages/tracer/src/Tracer.ts +++ b/packages/tracer/src/Tracer.ts @@ -1,3 +1,15 @@ +/** + * If the AWS_XRAY_CONTEXT_MISSING environment variable is not set, we set it to IGNORE_ERROR. + * + * This is to prevent the AWS X-Ray SDK from logging errors when using top-level await features that make HTTP requests. + * For example, when using the Parameters utility to fetch parameters during the initialization of the Lambda handler - See #2046 + */ +if ( + process.env.AWS_XRAY_CONTEXT_MISSING === '' || + process.env.AWS_XRAY_CONTEXT_MISSING === undefined +) { + process.env.AWS_XRAY_CONTEXT_MISSING = 'IGNORE_ERROR'; +} import { Utility } from '@aws-lambda-powertools/commons'; import type { AsyncHandler, diff --git a/packages/tracer/tests/unit/Tracer.test.ts b/packages/tracer/tests/unit/Tracer.test.ts index af1924bc3b..33b400a23a 100644 --- a/packages/tracer/tests/unit/Tracer.test.ts +++ b/packages/tracer/tests/unit/Tracer.test.ts @@ -59,6 +59,13 @@ describe('Class: Tracer', () => { }); describe('Method: constructor', () => { + it('sets the AWS_XRAY_CONTEXT_MISSING environment variable to IGNORE_ERROR when it is not set', () => { + // We are setting the environment variable as a side effect of importing the module, setting it within the Tracer would + // require introducing async code to the constructor, which is not a good practice, in order to lazy load the AWS X-Ray SDK for Node.js + // on demand. Between that option, and setting it as a side effect of importing the module, the latter is the better option. + expect(process.env.AWS_XRAY_CONTEXT_MISSING).toBe('IGNORE_ERROR'); + }); + it('instantiates with default settings when no option is passed', () => { // Prepare & Act const tracer = new Tracer(undefined);