@@ -51,13 +51,7 @@ The `Metrics` utility must always be instantiated outside of the Lambda handler.
51
51
=== "handler.ts"
52
52
53
53
```typescript hl_lines="1 3"
54
- import { Metrics } from '@aws-lambda-powertools/metrics';
55
-
56
- const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
57
-
58
- export const handler = async (_event, _context): Promise<void> => {
59
- // ...
60
- };
54
+ --8<-- "docs/snippets/metrics/basicUsage.ts"
61
55
```
62
56
63
57
### Utility settings
@@ -81,16 +75,7 @@ The `Metrics` utility is instantiated outside of the Lambda handler. In doing th
81
75
=== "handler.ts"
82
76
83
77
```typescript hl_lines="1 4"
84
- import { Metrics } from '@aws-lambda-powertools/metrics';
85
-
86
- // Metrics parameters fetched from the environment variables (see template.yaml tab)
87
- const metrics = new Metrics();
88
-
89
- // You can also pass the parameters in the constructor
90
- // const metrics = new Metrics({
91
- // namespace: 'serverlessAirline',
92
- // serviceName: 'orders'
93
- // });
78
+ --8<-- "docs/snippets/metrics/sam.ts"
94
79
```
95
80
96
81
=== "template.yml"
@@ -116,28 +101,13 @@ You can create metrics using the `addMetric` method, and you can create dimensio
116
101
=== "Metrics"
117
102
118
103
```typescript hl_lines="6"
119
- import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
120
-
121
- const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
122
-
123
- export const handler = async (_event: any, _context: any): Promise<void> => {
124
- metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
125
- metrics.publishStoredMetrics();
126
- };
104
+ --8<-- "docs/snippets/metrics/createMetrics.ts"
127
105
```
128
106
129
107
=== "Metrics with custom dimensions"
130
108
131
109
```typescript hl_lines="6-7"
132
- import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
133
-
134
- const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
135
-
136
- export const handler = async (_event: any, _context: any): Promise<void> => {
137
- metrics.addDimension('environment', 'prod');
138
- metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
139
- metrics.publishStoredMetrics();
140
- };
110
+ --8<-- "docs/snippets/metrics/customDimensions.ts"
141
111
```
142
112
143
113
!!! tip "Autocomplete Metric Units"
@@ -155,17 +125,8 @@ You can call `addMetric()` with the same name multiple times. The values will be
155
125
156
126
=== "addMetric() with the same name"
157
127
158
- ```typescript hl_lines="8 10"
159
- import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
160
- import { Context } from 'aws-lambda';
161
-
162
- const metrics = new Metrics({ namespace:'serverlessAirline', serviceName:'orders' });
163
-
164
- export const handler = async (event: any, context: Context): Promise<void> => {
165
- metrics.addMetric('performedActionA', MetricUnits.Count, 2);
166
- // do something else...
167
- metrics.addMetric('performedActionA', MetricUnits.Count, 1);
168
- };
128
+ ```typescript hl_lines="7 9"
129
+ --8<-- "docs/snippets/metrics/multiValueMetrics.ts"
169
130
```
170
131
=== "Example CloudWatch Logs excerpt"
171
132
@@ -210,17 +171,7 @@ You can add default dimensions to your metrics by passing them as parameters in
210
171
=== "constructor"
211
172
212
173
```typescript hl_lines="6"
213
- import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
214
-
215
- const metrics = new Metrics({
216
- namespace: 'serverlessAirline',
217
- serviceName: 'orders',
218
- defaultDimensions: { 'environment': 'prod', 'foo': 'bar' }
219
- });
220
-
221
- export const handler = async (_event: any, _context: any): Promise<void> => {
222
- metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
223
- };
174
+ --8<-- "docs/snippets/metrics/defaultDimensions.ts"
224
175
```
225
176
226
177
=== "Middy middleware"
@@ -230,53 +181,19 @@ You can add default dimensions to your metrics by passing them as parameters in
230
181
Learn more about [its usage and lifecycle in the official Middy documentation](https://middy.js.org/docs/intro/getting-started){target="_blank"}.
231
182
232
183
```typescript hl_lines="1-2 11 13"
233
- import { Metrics, MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics';
234
- import middy from '@middy/core';
235
-
236
- const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
237
-
238
- const lambdaHandler = async (_event: any, _context: any): Promise<void> => {
239
- metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
240
- };
241
-
242
- // Wrap the handler with middy
243
- export const handler = middy(lambdaHandler)
244
- // Use the middleware by passing the Metrics instance as a parameter
245
- .use(logMetrics(metrics, { defaultDimensions:{ 'environment': 'prod', 'foo': 'bar' } }));
184
+ --8<-- "docs/snippets/metrics/defaultDimensionsMiddy.ts"
246
185
```
247
186
248
187
=== "setDefaultDimensions method"
249
188
250
189
```typescript hl_lines="4"
251
- import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
252
-
253
- const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
254
- metrics.setDefaultDimensions({ 'environment': 'prod', 'foo': 'bar' });
255
-
256
- export const handler = async (event: any, _context: any): Promise<void> => {
257
- metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
258
- };
190
+ --8<-- "docs/snippets/metrics/setDefaultDimensions.ts"
259
191
```
260
192
261
193
=== "with logMetrics decorator"
262
194
263
195
```typescript hl_lines="9"
264
- import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
265
- import { LambdaInterface } from '@aws-lambda-powertools/commons';
266
-
267
- const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
268
- const DEFAULT_DIMENSIONS = { 'environment': 'prod', 'foo': 'bar' };
269
-
270
- export class Lambda implements LambdaInterface {
271
- // Decorate your handler class method
272
- @metrics.logMetrics({ defaultDimensions: DEFAULT_DIMENSIONS })
273
- public async handler(_event: any, _context: any): Promise<void> {
274
- metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
275
- }
276
- }
277
-
278
- const handlerClass = new Lambda();
279
- export const handler = handlerClass.handler.bind(handlerClass); // (1)
196
+ --8<-- "docs/snippets/metrics/defaultDimensionsDecorator.ts"
280
197
```
281
198
282
199
1. Binding your handler method allows your handler to access `this` within the class methods.
@@ -310,17 +227,7 @@ See below an example of how to automatically flush metrics with the Middy-compat
310
227
=== "handler.ts"
311
228
312
229
```typescript hl_lines="1-2 7 10-11"
313
- import { Metrics, MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics';
314
- import middy from '@middy/core';
315
-
316
- const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
317
-
318
- const lambdaHandler = async (_event: any, _context: any): Promise<void> => {
319
- metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
320
- };
321
-
322
- export const handler = middy(lambdaHandler)
323
- .use(logMetrics(metrics));
230
+ --8<-- "docs/snippets/metrics/middy.ts"
324
231
```
325
232
326
233
=== "Example CloudWatch Logs excerpt"
@@ -360,21 +267,7 @@ The `logMetrics` decorator of the metrics utility can be used when your Lambda h
360
267
=== "handler.ts"
361
268
362
269
```typescript hl_lines="8"
363
- import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
364
- import { LambdaInterface } from '@aws-lambda-powertools/commons';
365
-
366
- const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
367
-
368
- class Lambda implements LambdaInterface {
369
-
370
- @metrics.logMetrics()
371
- public async handler(_event: any, _context: any): Promise<void> {
372
- metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
373
- }
374
- }
375
-
376
- const handlerClass = new Lambda();
377
- export const handler = handlerClass.handler.bind(handlerClass); // (1)
270
+ --8<-- "docs/snippets/metrics/decorator.ts"
378
271
```
379
272
380
273
1. Binding your handler method allows your handler to access `this` within the class methods.
@@ -415,14 +308,7 @@ Metrics, dimensions and namespace validation still applies.
415
308
=== "handler.ts"
416
309
417
310
```typescript hl_lines="7"
418
- import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
419
-
420
- const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
421
-
422
- export const handler = async (_event: any, _context: any): Promise<void> => {
423
- metrics.addMetric('successfulBooking', MetricUnits.Count, 10);
424
- metrics.publishStoredMetrics();
425
- };
311
+ --8<-- "docs/snippets/metrics/manual.ts"
426
312
```
427
313
428
314
=== "Example CloudWatch Logs excerpt"
@@ -460,17 +346,7 @@ If you want to ensure that at least one metric is emitted before you flush them,
460
346
=== "handler.ts"
461
347
462
348
```typescript hl_lines="11"
463
- import { Metrics, MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics';
464
- import middy from '@middy/core';
465
-
466
- const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
467
-
468
- const lambdaHandler = async (_event: any, _context: any): Promise<void> => {
469
- metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
470
- };
471
-
472
- export const handler = middy(lambdaHandler)
473
- .use(logMetrics(metrics, { throwOnEmptyMetrics: true }));
349
+ --8<-- "docs/snippets/metrics/throwOnEmptyMetrics.ts"
474
350
```
475
351
476
352
### Capturing a cold start invocation as metric
@@ -480,34 +356,13 @@ You can optionally capture cold start metrics with the `logMetrics` middleware o
480
356
=== "Middy Middleware"
481
357
482
358
```typescript hl_lines="11"
483
- import { Metrics, MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics';
484
- import middy from '@middy/core';
485
-
486
- const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
487
-
488
- const lambdaHandler = async (_event: any, _context: any): Promise<void> => {
489
- metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
490
- };
491
-
492
- export const handler = middy(lambdaHandler)
493
- .use(logMetrics(metrics, { captureColdStartMetric: true }));
359
+ --8<-- "docs/snippets/metrics/captureColdStartMetricMiddy.ts"
494
360
```
495
361
496
362
=== "logMetrics decorator"
497
363
498
364
```typescript hl_lines="8"
499
- import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
500
- import { LambdaInterface } from '@aws-lambda-powertools/commons';
501
-
502
- const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
503
-
504
- export class MyFunction implements LambdaInterface {
505
-
506
- @metrics.logMetrics({ captureColdStartMetric: true })
507
- public async handler(_event: any, _context: any): Promise<void> {
508
- metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
509
- }
510
- }
365
+ --8<-- "docs/snippets/metrics/captureColdStartMetricDecorator.ts"
511
366
```
512
367
513
368
If it's a cold start invocation, this feature will:
@@ -531,18 +386,7 @@ You can add high-cardinality data as part of your Metrics log with the `addMetad
531
386
=== "handler.ts"
532
387
533
388
```typescript hl_lines="8"
534
- import { Metrics, MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics';
535
- import middy from '@middy/core';
536
-
537
- const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
538
-
539
- const lambdaHandler = async (_event: any, _context: any): Promise<void> => {
540
- metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
541
- metrics.addMetadata('bookingId', '7051cd10-6283-11ec-90d6-0242ac120003');
542
- };
543
-
544
- export const handler = middy(lambdaHandler)
545
- .use(logMetrics(metrics));
389
+ --8<-- "docs/snippets/metrics/addMetadata.ts"
546
390
```
547
391
548
392
=== "Example CloudWatch Logs excerpt"
@@ -594,51 +438,13 @@ CloudWatch EMF uses the same dimensions across all your metrics. Use `singleMetr
594
438
=== "Middy Middleware"
595
439
596
440
```typescript hl_lines="11 13-14"
597
- import { Metrics, MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics';
598
- import middy from '@middy/core';
599
-
600
- const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
601
-
602
- const lambdaHandler = async (_event: any, _context: any): Promise<void> => {
603
- metrics.addDimension('metricUnit', 'milliseconds');
604
- // This metric will have the "metricUnit" dimension, and no "metricType" dimension:
605
- metrics.addMetric('latency', MetricUnits.Milliseconds, 56);
606
-
607
- const singleMetric = metrics.singleMetric();
608
- // This metric will have the "metricType" dimension, and no "metricUnit" dimension:
609
- singleMetric.addDimension('metricType', 'business');
610
- singleMetric.addMetric('orderSubmitted', MetricUnits.Count, 1);
611
- };
612
-
613
- export const handler = middy(lambdaHandler)
614
- .use(logMetrics(metrics));
441
+ --8<-- "docs/snippets/metrics/singleMetricDifferentDimsMiddy.ts"
615
442
```
616
443
617
444
=== "logMetrics decorator"
618
445
619
446
```typescript hl_lines="14 16-17"
620
- import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
621
- import { LambdaInterface } from '@aws-lambda-powertools/commons';
622
-
623
- const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
624
-
625
- class Lambda implements LambdaInterface {
626
-
627
- @metrics.logMetrics()
628
- public async handler(_event: any, _context: any): Promise<void> {
629
- metrics.addDimension('metricUnit', 'milliseconds');
630
- // This metric will have the "metricUnit" dimension, and no "metricType" dimension:
631
- metrics.addMetric('latency', MetricUnits.Milliseconds, 56);
632
-
633
- const singleMetric = metrics.singleMetric();
634
- // This metric will have the "metricType" dimension, and no "metricUnit" dimension:
635
- singleMetric.addDimension('metricType', 'business');
636
- singleMetric.addMetric('orderSubmitted', MetricUnits.Count, 1);
637
- }
638
- }
639
-
640
- const handlerClass = new Lambda();
641
- export const handler = handlerClass.handler.bind(handlerClass); // (1)
447
+ --8<-- "docs/snippets/metrics/singleMetricDifferentDimsDecorator.ts"
642
448
```
643
449
644
450
1. Binding your handler method allows your handler to access `this` within the class methods.
0 commit comments