From f1cca19377e72acf00d809d4e242903093c3b108 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Tue, 28 Jun 2022 13:19:53 +0200 Subject: [PATCH 1/2] feat: disable tracer when running in amplify mock --- packages/tracer/src/Tracer.ts | 10 +++++++++- packages/tracer/tests/unit/helpers.test.ts | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/tracer/src/Tracer.ts b/packages/tracer/src/Tracer.ts index ba554f72ae..6f04e90f03 100644 --- a/packages/tracer/src/Tracer.ts +++ b/packages/tracer/src/Tracer.ts @@ -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; } } diff --git a/packages/tracer/tests/unit/helpers.test.ts b/packages/tracer/tests/unit/helpers.test.ts index d74b478ae8..c127d79279 100644 --- a/packages/tracer/tests/unit/helpers.test.ts +++ b/packages/tracer/tests/unit/helpers.test.ts @@ -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'; From 975629738ed6e0f453c73ee898571792b51d9279 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Tue, 28 Jun 2022 13:20:17 +0200 Subject: [PATCH 2/2] chore: housekeeping typo --- docs/core/tracer.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/tracer.md b/docs/core/tracer.md index cf5ff06995..3e6d1029e4 100644 --- a/docs/core/tracer.md +++ b/docs/core/tracer.md @@ -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"