Skip to content

Commit 2752ed7

Browse files
committed
docs(metrics): Add section about adding the same metric name multiple time
1 parent 532d4ed commit 2752ed7

File tree

1 file changed

+91
-42
lines changed

1 file changed

+91
-42
lines changed

docs/core/metrics.md

Lines changed: 91 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,55 @@ You can create metrics using `addMetric`, and you can create dimensions for all
132132
!!! warning "Do not create metrics or dimensions outside the handler"
133133
Metrics or dimensions added in the global scope will only be added during cold start. Disregard if that's the intended behaviour.
134134

135+
### Adding the same metric multiple times
136+
You can call `addMetric()` with the same name multiple times. The values will be grouped together in an array.
137+
138+
=== "addMetric() with the same name"
139+
140+
```typescript hl_lines="8 10"
141+
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
142+
import { Context } from 'aws-lambda';
143+
144+
145+
const metrics = new Metrics({namespace:"serverlessAirline", service:"orders"});
146+
147+
export const handler = async (event: any, context: Context) => {
148+
metrics.addMetric('performedActionA', MetricUnits.Count, 2);
149+
// do something else...
150+
metrics.addMetric('performedActionA', MetricUnits.Count, 1);
151+
}
152+
```
153+
=== "Example CloudWatch Logs excerpt"
154+
155+
```json hl_lines="15-16 23-26"
156+
{
157+
"successfulBooking": 1.0,
158+
"_aws": {
159+
"Timestamp": 1592234975665,
160+
"CloudWatchMetrics": [
161+
{
162+
"Namespace": "serverlessAirline",
163+
"Dimensions": [
164+
[
165+
"service"
166+
]
167+
],
168+
"Metrics": [
169+
{
170+
"Name": "performedActionA",
171+
"Unit": "Count"
172+
}
173+
]
174+
}
175+
]
176+
},
177+
"service": "orders",
178+
"performedActionA": [
179+
2,
180+
1
181+
]
182+
}
183+
```
135184
### Adding default dimensions
136185

137186
You can use add default dimensions to your metrics by passing them as parameters in 4 ways:
@@ -264,23 +313,23 @@ See below an example of how to automatically flush metrics with the Middy-compat
264313
{
265314
"bookingConfirmation": 1.0,
266315
"_aws": {
267-
"Timestamp": 1592234975665,
268-
"CloudWatchMetrics": [
269-
{
270-
"Namespace": "exampleApplication",
271-
"Dimensions": [
272-
[
273-
"service"
274-
]
275-
],
276-
"Metrics": [
316+
"Timestamp": 1592234975665,
317+
"CloudWatchMetrics": [
277318
{
278-
"Name": "bookingConfirmation",
279-
"Unit": "Count"
319+
"Namespace": "exampleApplication",
320+
"Dimensions": [
321+
[
322+
"service"
323+
]
324+
],
325+
"Metrics": [
326+
{
327+
"Name": "bookingConfirmation",
328+
"Unit": "Count"
329+
}
330+
]
280331
}
281332
]
282-
}
283-
]
284333
},
285334
"service": "exampleService"
286335
}
@@ -316,23 +365,23 @@ export class MyFunction {
316365
{
317366
"bookingConfirmation": 1.0,
318367
"_aws": {
319-
"Timestamp": 1592234975665,
320-
"CloudWatchMetrics": [
321-
{
322-
"Namespace": "exampleApplication",
323-
"Dimensions": [
324-
[
325-
"service"
326-
]
327-
],
328-
"Metrics": [
368+
"Timestamp": 1592234975665,
369+
"CloudWatchMetrics": [
329370
{
330-
"Name": "bookingConfirmation",
331-
"Unit": "Count"
371+
"Namespace": "exampleApplication",
372+
"Dimensions": [
373+
[
374+
"service"
375+
]
376+
],
377+
"Metrics": [
378+
{
379+
"Name": "bookingConfirmation",
380+
"Unit": "Count"
381+
}
382+
]
332383
}
333384
]
334-
}
335-
]
336385
},
337386
"service": "exampleService"
338387
}
@@ -456,23 +505,23 @@ You can add high-cardinality data as part of your Metrics log with `addMetadata`
456505
{
457506
"successfulBooking": 1.0,
458507
"_aws": {
459-
"Timestamp": 1592234975665,
460-
"CloudWatchMetrics": [
461-
{
462-
"Namespace": "exampleApplication",
463-
"Dimensions": [
464-
[
465-
"service"
466-
]
467-
],
468-
"Metrics": [
508+
"Timestamp": 1592234975665,
509+
"CloudWatchMetrics": [
469510
{
470-
"Name": "successfulBooking",
471-
"Unit": "Count"
511+
"Namespace": "exampleApplication",
512+
"Dimensions": [
513+
[
514+
"service"
515+
]
516+
],
517+
"Metrics": [
518+
{
519+
"Name": "successfulBooking",
520+
"Unit": "Count"
521+
}
522+
]
472523
}
473524
]
474-
}
475-
]
476525
},
477526
"service": "booking",
478527
"bookingId": "7051cd10-6283-11ec-90d6-0242ac120003"

0 commit comments

Comments
 (0)