Skip to content

Commit c0d2158

Browse files
authored
improv(tracer): set AWS_XRAY_CONTEXT_MISSING to IGNORE_ERROR when no value is set (#3058)
1 parent 8a463ad commit c0d2158

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

Diff for: packages/tracer/src/Tracer.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
/**
2+
* If the AWS_XRAY_CONTEXT_MISSING environment variable is not set, we set it to IGNORE_ERROR.
3+
*
4+
* This is to prevent the AWS X-Ray SDK from logging errors when using top-level await features that make HTTP requests.
5+
* For example, when using the Parameters utility to fetch parameters during the initialization of the Lambda handler - See #2046
6+
*/
7+
if (
8+
process.env.AWS_XRAY_CONTEXT_MISSING === '' ||
9+
process.env.AWS_XRAY_CONTEXT_MISSING === undefined
10+
) {
11+
process.env.AWS_XRAY_CONTEXT_MISSING = 'IGNORE_ERROR';
12+
}
113
import { Utility } from '@aws-lambda-powertools/commons';
214
import type {
315
AsyncHandler,

Diff for: packages/tracer/tests/unit/Tracer.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ describe('Class: Tracer', () => {
5959
});
6060

6161
describe('Method: constructor', () => {
62+
it('sets the AWS_XRAY_CONTEXT_MISSING environment variable to IGNORE_ERROR when it is not set', () => {
63+
// We are setting the environment variable as a side effect of importing the module, setting it within the Tracer would
64+
// 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
65+
// on demand. Between that option, and setting it as a side effect of importing the module, the latter is the better option.
66+
expect(process.env.AWS_XRAY_CONTEXT_MISSING).toBe('IGNORE_ERROR');
67+
});
68+
6269
it('instantiates with default settings when no option is passed', () => {
6370
// Prepare & Act
6471
const tracer = new Tracer(undefined);

0 commit comments

Comments
 (0)