Skip to content

Commit 024d628

Browse files
authored
feat(tracer): auto disable when running inside amplify mock (#1010)
* feat: disable tracer when running in amplify mock * chore: housekeeping typo
1 parent 12d4c93 commit 024d628

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

Diff for: docs/core/tracer.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ For a **complete list** of supported environment variables, refer to [this secti
6565

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

68-
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.
68+
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.
6969

7070
=== "handler.ts"
7171

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,14 @@ class Tracer extends Utility implements TracerInterface {
597597
private getEnvVarsService(): EnvironmentVariablesService {
598598
return this.envVarsService;
599599
}
600+
601+
/**
602+
* Determine if we are running inside an Amplify CLI process.
603+
* Used internally during initialization.
604+
*/
605+
private isAmplifyCli(): boolean {
606+
return this.getEnvVarsService().getAwsExecutionEnv() === 'AWS_Lambda_amplify-mock';
607+
}
600608

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

798-
if (this.isLambdaSamCli() || !this.isLambdaExecutionEnv()) {
806+
if (this.isAmplifyCli() || this.isLambdaSamCli() || !this.isLambdaExecutionEnv()) {
799807
this.tracingEnabled = false;
800808
}
801809
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,20 @@ describe('Helper: createTracer function', () => {
204204

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

207+
test('when AWS_EXECUTION_ENV environment variable is equal to AWS_Lambda_amplify-mock, tracing is disabled', () => {
208+
// Prepare
209+
process.env.AWS_EXECUTION_ENV = 'AWS_Lambda_amplify-mock';
210+
211+
// Act
212+
const tracer = createTracer();
213+
214+
// Assess
215+
expect(tracer).toEqual(expect.objectContaining({
216+
tracingEnabled: false,
217+
}));
218+
219+
});
220+
207221
test('when AWS_SAM_LOCAL environment variable is set, tracing is disabled', () => {
208222
// Prepare
209223
process.env.AWS_SAM_LOCAL = 'true';

0 commit comments

Comments
 (0)