Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(metrics): adopted Utility class #548

Merged
merged 2 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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