Skip to content

Commit 672e6a8

Browse files
authored
feat(metrics): adopted Utility class (#548)
* feat: adopted Utility class * build: update to [email protected]
1 parent 2a56ecd commit 672e6a8

File tree

4 files changed

+16
-128
lines changed

4 files changed

+16
-128
lines changed

Diff for: docs/core/metrics.md

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ For a **complete list** of supported environment variables, refer to [this secti
6161

6262
#### Example using AWS Serverless Application Model (SAM)
6363

64+
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.
65+
6466
=== "handler.ts"
6567

6668
```typescript hl_lines="1 4"

Diff for: packages/metrics/package-lock.json

+8-123
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/metrics/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
"url": "https://github.com/awslabs/aws-lambda-powertools-typescript/issues"
8585
},
8686
"dependencies": {
87-
"@aws-lambda-powertools/commons": "^0.2.0",
87+
"@aws-lambda-powertools/commons": "^0.6.0",
8888
"@types/aws-lambda": "^8.10.72"
8989
}
9090
}

Diff for: packages/metrics/src/Metrics.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Callback, Context } from 'aws-lambda';
2+
import { Utility } from '@aws-lambda-powertools/commons';
23
import { MetricsInterface } from '.';
34
import { ConfigServiceInterface, EnvironmentVariablesService } from './config';
45
import {
@@ -79,20 +80,21 @@ const DEFAULT_NAMESPACE = 'default_namespace';
7980
* };
8081
* ```
8182
*/
82-
class Metrics implements MetricsInterface {
83+
class Metrics extends Utility implements MetricsInterface {
8384
private customConfigService?: ConfigServiceInterface;
8485
private defaultDimensions: Dimensions = {};
8586
private dimensions: Dimensions = {};
8687
private envVarsService?: EnvironmentVariablesService;
8788
private functionName?: string;
88-
private isColdStart: boolean = true;
8989
private isSingleMetric: boolean = false;
9090
private metadata: { [key: string]: string } = {};
9191
private namespace?: string;
9292
private shouldThrowOnEmptyMetrics: boolean = false;
9393
private storedMetrics: StoredMetrics = {};
9494

9595
public constructor(options: MetricsOptions = {}) {
96+
super();
97+
9698
this.dimensions = {};
9799
this.setOptions(options);
98100
}
@@ -172,8 +174,7 @@ class Metrics implements MetricsInterface {
172174
* ```
173175
*/
174176
public captureColdStartMetric(): void {
175-
if (!this.isColdStart) return;
176-
this.isColdStart = false;
177+
if (!this.isColdStart()) return;
177178
const singleMetric = this.singleMetric();
178179

179180
if (this.dimensions.service) {

0 commit comments

Comments
 (0)