@@ -132,6 +132,55 @@ You can create metrics using `addMetric`, and you can create dimensions for all
132
132
!!! warning "Do not create metrics or dimensions outside the handler"
133
133
Metrics or dimensions added in the global scope will only be added during cold start. Disregard if that's the intended behaviour.
134
134
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
+ ```
135
184
### Adding default dimensions
136
185
137
186
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
264
313
{
265
314
"bookingConfirmation": 1.0,
266
315
"_aws": {
267
- "Timestamp": 1592234975665,
268
- "CloudWatchMetrics": [
269
- {
270
- "Namespace": "exampleApplication",
271
- "Dimensions": [
272
- [
273
- "service"
274
- ]
275
- ],
276
- "Metrics": [
316
+ "Timestamp": 1592234975665,
317
+ "CloudWatchMetrics": [
277
318
{
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
+ ]
280
331
}
281
332
]
282
- }
283
- ]
284
333
},
285
334
"service": "exampleService"
286
335
}
@@ -316,23 +365,23 @@ export class MyFunction {
316
365
{
317
366
"bookingConfirmation": 1.0,
318
367
"_aws": {
319
- "Timestamp": 1592234975665,
320
- "CloudWatchMetrics": [
321
- {
322
- "Namespace": "exampleApplication",
323
- "Dimensions": [
324
- [
325
- "service"
326
- ]
327
- ],
328
- "Metrics": [
368
+ "Timestamp": 1592234975665,
369
+ "CloudWatchMetrics": [
329
370
{
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
+ ]
332
383
}
333
384
]
334
- }
335
- ]
336
385
},
337
386
"service": "exampleService"
338
387
}
@@ -456,23 +505,23 @@ You can add high-cardinality data as part of your Metrics log with `addMetadata`
456
505
{
457
506
"successfulBooking": 1.0,
458
507
"_aws": {
459
- "Timestamp": 1592234975665,
460
- "CloudWatchMetrics": [
461
- {
462
- "Namespace": "exampleApplication",
463
- "Dimensions": [
464
- [
465
- "service"
466
- ]
467
- ],
468
- "Metrics": [
508
+ "Timestamp": 1592234975665,
509
+ "CloudWatchMetrics": [
469
510
{
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
+ ]
472
523
}
473
524
]
474
- }
475
- ]
476
525
},
477
526
"service": "booking",
478
527
"bookingId": "7051cd10-6283-11ec-90d6-0242ac120003"
0 commit comments