-
Notifications
You must be signed in to change notification settings - Fork 156
fix(metrics): Support multiple addMetric() call with the same metric name #390
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
Changes from 3 commits
532d4ed
2752ed7
d06020f
55b20ea
7580f26
05323b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,6 +132,54 @@ You can create metrics using `addMetric`, and you can create dimensions for all | |
!!! warning "Do not create metrics or dimensions outside the handler" | ||
Metrics or dimensions added in the global scope will only be added during cold start. Disregard if that's the intended behaviour. | ||
|
||
### Adding the same metric multiple times | ||
You can call `addMetric()` with the same name multiple times. The values will be grouped together in an array. | ||
|
||
=== "addMetric() with the same name" | ||
|
||
```typescript hl_lines="8 10" | ||
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics'; | ||
import { Context } from 'aws-lambda'; | ||
|
||
|
||
const metrics = new Metrics({namespace:"serverlessAirline", service:"orders"}); | ||
|
||
export const handler = async (event: any, context: Context) => { | ||
metrics.addMetric('performedActionA', MetricUnits.Count, 2); | ||
// do something else... | ||
metrics.addMetric('performedActionA', MetricUnits.Count, 1); | ||
} | ||
``` | ||
=== "Example CloudWatch Logs excerpt" | ||
|
||
```json hl_lines="2-5 18-19" | ||
{ | ||
"performedActionA": [ | ||
2, | ||
1 | ||
], | ||
"_aws": { | ||
"Timestamp": 1592234975665, | ||
"CloudWatchMetrics": [ | ||
{ | ||
"Namespace": "serverlessAirline", | ||
"Dimensions": [ | ||
[ | ||
"service" | ||
] | ||
], | ||
"Metrics": [ | ||
{ | ||
"Name": "performedActionA", | ||
"Unit": "Count" | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
"service": "orders" | ||
} | ||
``` | ||
### Adding default dimensions | ||
|
||
You can use add default dimensions to your metrics by passing them as parameters in 4 ways: | ||
|
@@ -264,23 +312,23 @@ See below an example of how to automatically flush metrics with the Middy-compat | |
{ | ||
"bookingConfirmation": 1.0, | ||
"_aws": { | ||
"Timestamp": 1592234975665, | ||
"CloudWatchMetrics": [ | ||
{ | ||
"Namespace": "exampleApplication", | ||
"Dimensions": [ | ||
[ | ||
"service" | ||
] | ||
], | ||
"Metrics": [ | ||
"Timestamp": 1592234975665, | ||
"CloudWatchMetrics": [ | ||
{ | ||
"Name": "bookingConfirmation", | ||
"Unit": "Count" | ||
"Namespace": "exampleApplication", | ||
"Dimensions": [ | ||
[ | ||
"service" | ||
] | ||
], | ||
"Metrics": [ | ||
{ | ||
"Name": "bookingConfirmation", | ||
"Unit": "Count" | ||
} | ||
] | ||
Comment on lines
+315
to
+329
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just shifting indentation. The current version misses one more indentation under "_aws" |
||
} | ||
] | ||
} | ||
] | ||
}, | ||
"service": "exampleService" | ||
} | ||
|
@@ -316,23 +364,23 @@ export class MyFunction { | |
{ | ||
"bookingConfirmation": 1.0, | ||
"_aws": { | ||
"Timestamp": 1592234975665, | ||
"CloudWatchMetrics": [ | ||
{ | ||
"Namespace": "exampleApplication", | ||
"Dimensions": [ | ||
[ | ||
"service" | ||
] | ||
], | ||
"Metrics": [ | ||
"Timestamp": 1592234975665, | ||
"CloudWatchMetrics": [ | ||
{ | ||
"Name": "bookingConfirmation", | ||
"Unit": "Count" | ||
"Namespace": "exampleApplication", | ||
"Dimensions": [ | ||
[ | ||
"service" | ||
] | ||
], | ||
"Metrics": [ | ||
{ | ||
"Name": "bookingConfirmation", | ||
"Unit": "Count" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
"service": "exampleService" | ||
} | ||
|
@@ -456,23 +504,23 @@ You can add high-cardinality data as part of your Metrics log with `addMetadata` | |
{ | ||
"successfulBooking": 1.0, | ||
"_aws": { | ||
"Timestamp": 1592234975665, | ||
"CloudWatchMetrics": [ | ||
{ | ||
"Namespace": "exampleApplication", | ||
"Dimensions": [ | ||
[ | ||
"service" | ||
] | ||
], | ||
"Metrics": [ | ||
"Timestamp": 1592234975665, | ||
"CloudWatchMetrics": [ | ||
{ | ||
"Name": "successfulBooking", | ||
"Unit": "Count" | ||
"Namespace": "exampleApplication", | ||
"Dimensions": [ | ||
[ | ||
"service" | ||
] | ||
], | ||
"Metrics": [ | ||
{ | ||
"Name": "successfulBooking", | ||
"Unit": "Count" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
"service": "booking", | ||
"bookingId": "7051cd10-6283-11ec-90d6-0242ac120003" | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
"commit": "commit", | ||
"test": "jest --group=unit --detectOpenHandles --coverage --verbose", | ||
"test:e2e": "jest --group=e2e", | ||
"watch": "jest --watch", | ||
"watch": "jest --group=unit --watch ", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not practical to run e2e test in the watch mode so I filtered this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point ! |
||
"build": "tsc", | ||
"lint": "eslint --ext .ts --fix --no-error-on-unmatched-pattern src tests", | ||
"format": "eslint --fix --ext .ts --fix --no-error-on-unmatched-pattern src tests", | ||
|
Uh oh!
There was an error while loading. Please reload this page.