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

feat(tracer): auto disable when running inside amplify mock #1010

Merged
merged 2 commits into from
Jul 8, 2022
Merged
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
2 changes: 1 addition & 1 deletion docs/core/tracer.md
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@ For a **complete list** of supported environment variables, refer to [this secti

#### Example using AWS Serverless Application Model (SAM)

The `Tracer` utility is instantiated outside of the Lambda handler. In doing this, the same instance can be used across multiple invocations inside the same execution environment. This allows `Metrics` to be aware of things like whether or not a given invocation had a cold start or not.
The `Tracer` utility is instantiated outside of the Lambda handler. In doing this, the same instance can be used across multiple invocations inside the same execution environment. This allows `Tracer` to be aware of things like whether or not a given invocation had a cold start or not.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Housekeeping


=== "handler.ts"

10 changes: 9 additions & 1 deletion packages/tracer/src/Tracer.ts
Original file line number Diff line number Diff line change
@@ -597,6 +597,14 @@ class Tracer extends Utility implements TracerInterface {
private getEnvVarsService(): EnvironmentVariablesService {
return this.envVarsService;
}

/**
* Determine if we are running inside an Amplify CLI process.
* Used internally during initialization.
*/
private isAmplifyCli(): boolean {
return this.getEnvVarsService().getAwsExecutionEnv() === 'AWS_Lambda_amplify-mock';
}

/**
* Determine if we are running in a Lambda execution environment.
@@ -795,7 +803,7 @@ class Tracer extends Utility implements TracerInterface {
return;
}

if (this.isLambdaSamCli() || !this.isLambdaExecutionEnv()) {
if (this.isAmplifyCli() || this.isLambdaSamCli() || !this.isLambdaExecutionEnv()) {
this.tracingEnabled = false;
}
}
14 changes: 14 additions & 0 deletions packages/tracer/tests/unit/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -204,6 +204,20 @@ describe('Helper: createTracer function', () => {

describe('Environment Variables configs', () => {

test('when AWS_EXECUTION_ENV environment variable is equal to AWS_Lambda_amplify-mock, tracing is disabled', () => {
// Prepare
process.env.AWS_EXECUTION_ENV = 'AWS_Lambda_amplify-mock';

// Act
const tracer = createTracer();

// Assess
expect(tracer).toEqual(expect.objectContaining({
tracingEnabled: false,
}));

});

test('when AWS_SAM_LOCAL environment variable is set, tracing is disabled', () => {
// Prepare
process.env.AWS_SAM_LOCAL = 'true';