From 672e6a82a2c66f99153c63a53e9d31481afd897a Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Tue, 22 Feb 2022 13:48:40 +0100 Subject: [PATCH] feat(metrics): adopted Utility class (#548) * feat: adopted Utility class * build: update to commons@0.6.0 --- docs/core/metrics.md | 2 + packages/metrics/package-lock.json | 131 ++--------------------------- packages/metrics/package.json | 2 +- packages/metrics/src/Metrics.ts | 9 +- 4 files changed, 16 insertions(+), 128 deletions(-) diff --git a/docs/core/metrics.md b/docs/core/metrics.md index a1b18d887d..ae0fadcee7 100644 --- a/docs/core/metrics.md +++ b/docs/core/metrics.md @@ -61,6 +61,8 @@ For a **complete list** of supported environment variables, refer to [this secti #### Example using AWS Serverless Application Model (SAM) +The `Metrics` 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/metrics/package-lock.json b/packages/metrics/package-lock.json index eaac48f10a..058cbd1576 100644 --- a/packages/metrics/package-lock.json +++ b/packages/metrics/package-lock.json @@ -6,10 +6,10 @@ "packages": { "": { "name": "@aws-lambda-powertools/metrics", - "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", "@types/aws-lambda": "^8.10.72" }, "devDependencies": { @@ -810,9 +810,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" } @@ -2372,23 +2372,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", @@ -2415,46 +2398,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", @@ -2553,23 +2496,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", @@ -10151,9 +10077,9 @@ "dev": true }, "@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" } @@ -11234,16 +11160,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", @@ -11255,27 +11171,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", @@ -11333,16 +11228,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/metrics/package.json b/packages/metrics/package.json index 3ee47e08c2..ce2346895b 100644 --- a/packages/metrics/package.json +++ b/packages/metrics/package.json @@ -84,7 +84,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", "@types/aws-lambda": "^8.10.72" } } diff --git a/packages/metrics/src/Metrics.ts b/packages/metrics/src/Metrics.ts index dba649700a..36aad90687 100644 --- a/packages/metrics/src/Metrics.ts +++ b/packages/metrics/src/Metrics.ts @@ -1,4 +1,5 @@ import { Callback, Context } from 'aws-lambda'; +import { Utility } from '@aws-lambda-powertools/commons'; import { MetricsInterface } from '.'; import { ConfigServiceInterface, EnvironmentVariablesService } from './config'; import { @@ -79,13 +80,12 @@ const DEFAULT_NAMESPACE = 'default_namespace'; * }; * ``` */ -class Metrics implements MetricsInterface { +class Metrics extends Utility implements MetricsInterface { private customConfigService?: ConfigServiceInterface; private defaultDimensions: Dimensions = {}; private dimensions: Dimensions = {}; private envVarsService?: EnvironmentVariablesService; private functionName?: string; - private isColdStart: boolean = true; private isSingleMetric: boolean = false; private metadata: { [key: string]: string } = {}; private namespace?: string; @@ -93,6 +93,8 @@ class Metrics implements MetricsInterface { private storedMetrics: StoredMetrics = {}; public constructor(options: MetricsOptions = {}) { + super(); + this.dimensions = {}; this.setOptions(options); } @@ -172,8 +174,7 @@ class Metrics implements MetricsInterface { * ``` */ public captureColdStartMetric(): void { - if (!this.isColdStart) return; - this.isColdStart = false; + if (!this.isColdStart()) return; const singleMetric = this.singleMetric(); if (this.dimensions.service) {