Skip to content

Commit

Permalink
feat(tracer): auto disable when running inside amplify mock (#1010)
Browse files Browse the repository at this point in the history
* feat: disable tracer when running in amplify mock

* chore: housekeeping typo
  • Loading branch information
dreamorosi authored Jul 8, 2022
1 parent 12d4c93 commit 024d628
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/core/tracer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

=== "handler.ts"

Expand Down
10 changes: 9 additions & 1 deletion packages/tracer/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}
Expand Down
14 changes: 14 additions & 0 deletions packages/tracer/tests/unit/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 024d628

Please sign in to comment.