@@ -106,6 +106,7 @@ You can create metrics using `addMetric`, and you can create dimensions for all
106
106
107
107
export const handler = async (event: any, context: Context) => {
108
108
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
109
+ metrics.purgeStoredMetrics();
109
110
}
110
111
```
111
112
=== "Metrics with custom dimensions"
@@ -120,6 +121,7 @@ You can create metrics using `addMetric`, and you can create dimensions for all
120
121
export const handler = async (event: any, context: Context) => {
121
122
metrics.addDimension('environment', 'prod');
122
123
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
124
+ metrics.purgeStoredMetrics();
123
125
}
124
126
```
125
127
@@ -242,21 +244,22 @@ If you do not the middleware or decorator, you have to flush your metrics manual
242
244
243
245
See below an example of how to automatically flush metrics with the Middy-compatible ` logMetrics ` middleware.
244
246
247
+ === "handler.ts"
245
248
246
- ``` typescript hl_lines="3 8 11-12"
247
- import { Metrics , MetricUnits , logMetrics } from ' @aws-lambda-powertools/metrics' ;
248
- import { Context } from ' aws-lambda' ;
249
- import middy from ' @middy/core' ;
249
+ ```typescript hl_lines="3 8 11-12"
250
+ import { Metrics, MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics';
251
+ import { Context } from 'aws-lambda';
252
+ import middy from '@middy/core';
250
253
251
- const metrics = new Metrics ({ namespace: ' exampleApplication' , service: ' exampleService' });
254
+ const metrics = new Metrics({ namespace: 'exampleApplication' , service: 'exampleService' });
252
255
253
- const lambdaHandler = async (event : any , context : Context ) => {
254
- metrics .addMetric (' bookingConfirmation' , MetricUnits .Count , 1 );
255
- }
256
+ const lambdaHandler = async (event: any, context: Context) => {
257
+ metrics.addMetric('bookingConfirmation', MetricUnits.Count, 1);
258
+ }
256
259
257
- export const handler = middy (lambdaHandler )
258
- .use (logMetrics (metrics ));
259
- ```
260
+ export const handler = middy(lambdaHandler)
261
+ .use(logMetrics(metrics));
262
+ ```
260
263
261
264
=== "Example CloudWatch Logs excerpt"
262
265
@@ -295,20 +298,22 @@ See below an example of how to automatically flush metrics with the Middy-compat
295
298
The ` logMetrics ` decorator of the metrics utility can be used when your Lambda handler function is implemented as method of a Class.
296
299
297
300
298
- ``` typescript hl_lines="8"
299
- import { Metrics , MetricUnits } from ' @aws-lambda-powertools/metrics' ;
300
- import { Context , Callback } from ' aws-lambda' ;
301
+ === "handler.ts"
301
302
302
- const metrics = new Metrics ({namespace:" exampleApplication" , service:" exampleService" });
303
+ ```typescript hl_lines="8"
304
+ import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
305
+ import { Context, Callback } from 'aws-lambda';
303
306
304
- export class MyFunction {
307
+ const metrics = new Metrics({namespace:"exampleApplication", service:"exampleService"});
305
308
306
- @metrics .logMetrics ()
307
- public handler<TEvent , TResult >(_event : TEvent , _context : Context , _callback : Callback <TResult >): void | Promise <TResult > {
308
- metrics .addMetric (' bookingConfirmation' , MetricUnits .Count , 1 );
309
+ export class MyFunction {
310
+
311
+ @metrics.logMetrics()
312
+ public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {
313
+ metrics.addMetric('bookingConfirmation', MetricUnits.Count, 1);
314
+ }
309
315
}
310
- }
311
- ```
316
+ ```
312
317
313
318
=== "Example CloudWatch Logs excerpt"
314
319
@@ -353,9 +358,7 @@ const metrics = new Metrics();
353
358
354
359
const lambdaHandler: Handler = async () => {
355
360
metrics .addMetric (' test-metric' , MetricUnits .Count , 10 );
356
- const metricsObject = metrics .serializeMetrics ();
357
361
metrics .purgeStoredMetrics ();
358
- console .log (JSON .stringify (metricsObject ));
359
362
};
360
363
```
361
364
@@ -435,20 +438,22 @@ You can add high-cardinality data as part of your Metrics log with `addMetadata`
435
438
!!! warning
436
439
** This will not be available during metrics visualization** - Use ** dimensions** for this purpose
437
440
438
- ``` typescript hl_lines="8"
439
- import { Metrics , MetricUnits , logMetrics } from ' @aws-lambda-powertools/metrics' ;
440
- import { Context } from ' aws-lambda' ;
441
- import middy from ' @middy/core' ;
441
+ === "handler.ts"
442
442
443
- const metrics = new Metrics ({namespace:" serverlessAirline" , service:" orders" });
443
+ ```typescript hl_lines="8"
444
+ import { Metrics, MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics';
445
+ import { Context } from 'aws-lambda';
446
+ import middy from '@middy/core';
444
447
445
- const lambdaHandler = async (event : any , context : Context ) => {
446
- metrics .addMetadata (' bookingId' , ' 7051cd10-6283-11ec-90d6-0242ac120003' );
447
- }
448
+ const metrics = new Metrics({namespace:"serverlessAirline", service:"orders"});
448
449
449
- export const handler = middy (lambdaHandler )
450
- .use (logMetrics (metrics ));
451
- ```
450
+ const lambdaHandler = async (event: any, context: Context) => {
451
+ metrics.addMetadata('bookingId', '7051cd10-6283-11ec-90d6-0242ac120003');
452
+ }
453
+
454
+ export const handler = middy(lambdaHandler)
455
+ .use(logMetrics(metrics));
456
+ ```
452
457
453
458
=== "Example CloudWatch Logs excerpt"
454
459
0 commit comments