diff --git a/docs/core/tracer.md b/docs/core/tracer.md index b5a3baf386..2283fc962b 100644 --- a/docs/core/tracer.md +++ b/docs/core/tracer.md @@ -48,6 +48,8 @@ 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. + === "handler.ts" ```typescript hl_lines="1 4" diff --git a/packages/tracing/package-lock.json b/packages/tracing/package-lock.json index d0fe37524c..f3888e06e5 100644 --- a/packages/tracing/package-lock.json +++ b/packages/tracing/package-lock.json @@ -6,10 +6,10 @@ "packages": { "": { "name": "@aws-lambda-powertools/tracer", - "version": "0.5.0", + "version": "0.6.0", "license": "MIT-0", "dependencies": { - "@aws-lambda-powertools/commons": "^0.2.0", + "@aws-lambda-powertools/commons": "^0.6.0", "aws-xray-sdk-core": "^3.3.3" }, "devDependencies": { @@ -1025,9 +1025,9 @@ } }, "node_modules/@aws-lambda-powertools/commons": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/@aws-lambda-powertools/commons/-/commons-0.2.0.tgz", - "integrity": "sha512-h0YItiAkJkTTbKBJxR2Xe5iQMjkyZq7eLPJe4Dpm4RaiwzXG2Ejwt1jt/QoDcaJoxqWPrUnWteNtQzpEB8u2qQ==", + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/@aws-lambda-powertools/commons/-/commons-0.6.0.tgz", + "integrity": "sha512-8flZn/gH1l5u1UzcSM+MI5euEVmcpZRn2GXypgh/2OEKaTKqIZL7eH6NbbcP8hjbsvyGg5PKBdyLeVBK0IqcwQ==", "dependencies": { "@types/aws-lambda": "^8.10.72" } @@ -3833,23 +3833,6 @@ "url": "https://opencollective.com/typescript-eslint" } }, - "node_modules/@typescript-eslint/scope-manager": { - "version": "5.9.1", - "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-5.9.1.tgz", - "integrity": "sha512-8BwvWkho3B/UOtzRyW07ffJXPaLSUKFBjpq8aqsRvu6HdEuzCY57+ffT7QoV4QXJXWSU1+7g3wE4AlgImmQ9pQ==", - "dev": true, - "dependencies": { - "@typescript-eslint/types": "5.9.1", - "@typescript-eslint/visitor-keys": "5.9.1" - }, - "engines": { - "node": "^12.22.0 || ^14.17.0 || >=16.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/typescript-eslint" - } - }, "node_modules/@typescript-eslint/type-utils": { "version": "5.11.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-5.11.0.tgz", @@ -3876,46 +3859,6 @@ } } }, - "node_modules/@typescript-eslint/types": { - "version": "5.9.1", - "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-5.9.1.tgz", - "integrity": "sha512-SsWegWudWpkZCwwYcKoDwuAjoZXnM1y2EbEerTHho19Hmm+bQ56QG4L4jrtCu0bI5STaRTvRTZmjprWlTw/5NQ==", - "dev": true, - "engines": { - "node": "^12.22.0 || ^14.17.0 || >=16.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/typescript-eslint" - } - }, - "node_modules/@typescript-eslint/typescript-estree": { - "version": "5.9.1", - "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-5.9.1.tgz", - "integrity": "sha512-gL1sP6A/KG0HwrahVXI9fZyeVTxEYV//6PmcOn1tD0rw8VhUWYeZeuWHwwhnewnvEMcHjhnJLOBhA9rK4vmb8A==", - "dev": true, - "dependencies": { - "@typescript-eslint/types": "5.9.1", - "@typescript-eslint/visitor-keys": "5.9.1", - "debug": "^4.3.2", - "globby": "^11.0.4", - "is-glob": "^4.0.3", - "semver": "^7.3.5", - "tsutils": "^3.21.0" - }, - "engines": { - "node": "^12.22.0 || ^14.17.0 || >=16.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/typescript-eslint" - }, - "peerDependenciesMeta": { - "typescript": { - "optional": true - } - } - }, "node_modules/@typescript-eslint/utils": { "version": "5.11.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-5.11.0.tgz", @@ -4014,23 +3957,6 @@ "url": "https://opencollective.com/typescript-eslint" } }, - "node_modules/@typescript-eslint/visitor-keys": { - "version": "5.9.1", - "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-5.9.1.tgz", - "integrity": "sha512-Xh37pNz9e9ryW4TVdwiFzmr4hloty8cFj8GTWMXh3Z8swGwyQWeCcNgF0hm6t09iZd6eiZmIf4zHedQVP6TVtg==", - "dev": true, - "dependencies": { - "@typescript-eslint/types": "5.9.1", - "eslint-visitor-keys": "^3.0.0" - }, - "engines": { - "node": "^12.22.0 || ^14.17.0 || >=16.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/typescript-eslint" - } - }, "node_modules/abab": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/abab/-/abab-2.0.5.tgz", @@ -11216,9 +11142,9 @@ } }, "@aws-lambda-powertools/commons": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/@aws-lambda-powertools/commons/-/commons-0.2.0.tgz", - "integrity": "sha512-h0YItiAkJkTTbKBJxR2Xe5iQMjkyZq7eLPJe4Dpm4RaiwzXG2Ejwt1jt/QoDcaJoxqWPrUnWteNtQzpEB8u2qQ==", + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/@aws-lambda-powertools/commons/-/commons-0.6.0.tgz", + "integrity": "sha512-8flZn/gH1l5u1UzcSM+MI5euEVmcpZRn2GXypgh/2OEKaTKqIZL7eH6NbbcP8hjbsvyGg5PKBdyLeVBK0IqcwQ==", "requires": { "@types/aws-lambda": "^8.10.72" } @@ -13565,16 +13491,6 @@ } } }, - "@typescript-eslint/scope-manager": { - "version": "5.9.1", - "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-5.9.1.tgz", - "integrity": "sha512-8BwvWkho3B/UOtzRyW07ffJXPaLSUKFBjpq8aqsRvu6HdEuzCY57+ffT7QoV4QXJXWSU1+7g3wE4AlgImmQ9pQ==", - "dev": true, - "requires": { - "@typescript-eslint/types": "5.9.1", - "@typescript-eslint/visitor-keys": "5.9.1" - } - }, "@typescript-eslint/type-utils": { "version": "5.11.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-5.11.0.tgz", @@ -13586,27 +13502,6 @@ "tsutils": "^3.21.0" } }, - "@typescript-eslint/types": { - "version": "5.9.1", - "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-5.9.1.tgz", - "integrity": "sha512-SsWegWudWpkZCwwYcKoDwuAjoZXnM1y2EbEerTHho19Hmm+bQ56QG4L4jrtCu0bI5STaRTvRTZmjprWlTw/5NQ==", - "dev": true - }, - "@typescript-eslint/typescript-estree": { - "version": "5.9.1", - "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-5.9.1.tgz", - "integrity": "sha512-gL1sP6A/KG0HwrahVXI9fZyeVTxEYV//6PmcOn1tD0rw8VhUWYeZeuWHwwhnewnvEMcHjhnJLOBhA9rK4vmb8A==", - "dev": true, - "requires": { - "@typescript-eslint/types": "5.9.1", - "@typescript-eslint/visitor-keys": "5.9.1", - "debug": "^4.3.2", - "globby": "^11.0.4", - "is-glob": "^4.0.3", - "semver": "^7.3.5", - "tsutils": "^3.21.0" - } - }, "@typescript-eslint/utils": { "version": "5.11.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-5.11.0.tgz", @@ -13664,16 +13559,6 @@ } } }, - "@typescript-eslint/visitor-keys": { - "version": "5.9.1", - "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-5.9.1.tgz", - "integrity": "sha512-Xh37pNz9e9ryW4TVdwiFzmr4hloty8cFj8GTWMXh3Z8swGwyQWeCcNgF0hm6t09iZd6eiZmIf4zHedQVP6TVtg==", - "dev": true, - "requires": { - "@typescript-eslint/types": "5.9.1", - "eslint-visitor-keys": "^3.0.0" - } - }, "abab": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/abab/-/abab-2.0.5.tgz", diff --git a/packages/tracing/package.json b/packages/tracing/package.json index b13b281197..245c6a3fb4 100644 --- a/packages/tracing/package.json +++ b/packages/tracing/package.json @@ -64,7 +64,7 @@ "url": "https://github.com/awslabs/aws-lambda-powertools-typescript/issues" }, "dependencies": { - "@aws-lambda-powertools/commons": "^0.2.0", + "@aws-lambda-powertools/commons": "^0.6.0", "aws-xray-sdk-core": "^3.3.3" } } diff --git a/packages/tracing/src/Tracer.ts b/packages/tracing/src/Tracer.ts index 0c3627f4ab..b2ee7cd938 100644 --- a/packages/tracing/src/Tracer.ts +++ b/packages/tracing/src/Tracer.ts @@ -1,4 +1,5 @@ import { Handler } from 'aws-lambda'; +import { Utility } from '@aws-lambda-powertools/commons'; import { TracerInterface } from '.'; import { ConfigServiceInterface, EnvironmentVariablesService } from './config'; import { HandlerMethodDecorator, TracerOptions, MethodDecorator } from './types'; @@ -109,9 +110,7 @@ import { Segment, Subsegment } from 'aws-xray-sdk-core'; * } * ``` */ -class Tracer implements TracerInterface { - - public static coldStart: boolean = true; +class Tracer extends Utility implements TracerInterface { public provider: ProviderServiceInterface; @@ -128,6 +127,8 @@ class Tracer implements TracerInterface { private tracingEnabled: boolean = true; public constructor(options: TracerOptions = {}) { + super(); + this.setOptions(options); this.provider = new ProviderService(); if (!this.isTracingEnabled()) { @@ -196,10 +197,7 @@ class Tracer implements TracerInterface { */ public annotateColdStart(): void { if (this.isTracingEnabled()) { - this.putAnnotation('ColdStart', Tracer.coldStart); - } - if (Tracer.coldStart) { - Tracer.coldStart = false; + this.putAnnotation('ColdStart', this.getColdStart()); } } @@ -430,27 +428,6 @@ class Tracer implements TracerInterface { return descriptor; }; } - - /** - * Retrieve the current value of `ColdStart`. - * - * If Tracer has been initialized outside the Lambda handler then the same instance - * of Tracer will be reused throughout the lifecycle of that same Lambda execution environment - * and this method will return `false` after the first invocation. - * - * @see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html - * - * @returns boolean - `true` if is cold start, otherwise `false` - */ - public static getColdStart(): boolean { - if (Tracer.coldStart) { - Tracer.coldStart = false; - - return true; - } - - return false; - } /** * Get the active segment or subsegment in the current scope. diff --git a/packages/tracing/tests/unit/Tracer.test.ts b/packages/tracing/tests/unit/Tracer.test.ts index b97e1e4d5a..2a4eaa57f2 100644 --- a/packages/tracing/tests/unit/Tracer.test.ts +++ b/packages/tracing/tests/unit/Tracer.test.ts @@ -40,7 +40,6 @@ describe('Class: Tracer', () => { }; beforeEach(() => { - Tracer.coldStart = true; jest.clearAllMocks(); jest.resetModules(); process.env = { ...ENVIRONMENT_VARIABLES }; @@ -259,20 +258,6 @@ describe('Class: Tracer', () => { }); - describe('Method: getColdStart', () => { - - test('when called, it returns false the first time and always true after that', () => { - - // Assess - expect(Tracer.getColdStart()).toBe(true); - expect(Tracer.getColdStart()).toBe(false); - expect(Tracer.getColdStart()).toBe(false); - expect(Tracer.getColdStart()).toBe(false); - - }); - - }); - describe('Method: getSegment', () => { test('when called outside of a namespace or without parent segment, and tracing is enabled, it throws an error', () => { diff --git a/packages/tracing/tests/unit/middy.test.ts b/packages/tracing/tests/unit/middy.test.ts index fc1605c97c..55b72dde7a 100644 --- a/packages/tracing/tests/unit/middy.test.ts +++ b/packages/tracing/tests/unit/middy.test.ts @@ -32,7 +32,6 @@ describe('Middy middleware', () => { }; beforeEach(() => { - Tracer.coldStart = true; jest.clearAllMocks(); jest.resetModules(); process.env = { ...ENVIRONMENT_VARIABLES };