From 4ef30f3b6858fcd219751eb991faef913b1043d8 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Thu, 12 Sep 2024 23:34:06 +0200 Subject: [PATCH 1/3] improv(tracer): set AWS_XRAY_CONTEXT_MISSING to IGNORE_ERROR when no value is set --- packages/tracer/src/index.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/tracer/src/index.ts b/packages/tracer/src/index.ts index 56c5e72163..d52804d554 100644 --- a/packages/tracer/src/index.ts +++ b/packages/tracer/src/index.ts @@ -1 +1,13 @@ +/** + * 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'; +} export { Tracer } from './Tracer.js'; From 3502f8a514d878ec56d5950812797e8ca6d13977 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Thu, 12 Sep 2024 23:35:00 +0200 Subject: [PATCH 2/3] improv(tracer): move env variable out of barrel file --- packages/tracer/src/Tracer.ts | 12 ++++++++++++ packages/tracer/src/index.ts | 12 ------------ 2 files changed, 12 insertions(+), 12 deletions(-) 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/src/index.ts b/packages/tracer/src/index.ts index d52804d554..56c5e72163 100644 --- a/packages/tracer/src/index.ts +++ b/packages/tracer/src/index.ts @@ -1,13 +1 @@ -/** - * 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'; -} export { Tracer } from './Tracer.js'; From 9308129c52d705156d4ff339bacbe54017800f38 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Fri, 13 Sep 2024 00:01:22 +0200 Subject: [PATCH 3/3] test(tracer): add test to ensure env var stays set --- packages/tracer/tests/unit/Tracer.test.ts | 7 +++++++ 1 file changed, 7 insertions(+) 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);