From edd8524fcecb96071b6df5fb193514b34bf0b253 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Fri, 8 Jul 2022 14:08:20 +0200 Subject: [PATCH] feat(tracer): auto disable when running inside amplify mock (#1010) * feat: disable tracer when running in amplify mock * chore: housekeeping typo --- docs/core/tracer.md | 2 +- packages/tracer/src/Tracer.ts | 10 +++++++++- packages/tracer/tests/unit/helpers.test.ts | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/docs/core/tracer.md b/docs/core/tracer.md index 9c01052c43..672cc43bb7 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" 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';