Skip to content

Commit

Permalink
feat(metrics): adopted Utility class (#548)
Browse files Browse the repository at this point in the history
* feat: adopted Utility class

* build: update to [email protected]
  • Loading branch information
dreamorosi authored Feb 22, 2022
1 parent 2a56ecd commit 672e6a8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 128 deletions.
2 changes: 2 additions & 0 deletions docs/core/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
131 changes: 8 additions & 123 deletions packages/metrics/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/metrics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
9 changes: 5 additions & 4 deletions packages/metrics/src/Metrics.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -79,20 +80,21 @@ 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;
private shouldThrowOnEmptyMetrics: boolean = false;
private storedMetrics: StoredMetrics = {};

public constructor(options: MetricsOptions = {}) {
super();

this.dimensions = {};
this.setOptions(options);
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 672e6a8

Please sign in to comment.