Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improv(tracer): set AWS_XRAY_CONTEXT_MISSING to IGNORE_ERROR when no value is set #3058

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/tracer/src/Tracer.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
7 changes: 7 additions & 0 deletions packages/tracer/tests/unit/Tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down