Skip to content

Commit 62d6c9e

Browse files
committed
docs(metrics): remove serializeMetrics and fix cloudwatch excerpt
1 parent 1e17d07 commit 62d6c9e

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

Diff for: docs/core/metrics.md

+39-34
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ You can create metrics using `addMetric`, and you can create dimensions for all
106106

107107
export const handler = async (event: any, context: Context) => {
108108
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
109+
metrics.purgeStoredMetrics();
109110
}
110111
```
111112
=== "Metrics with custom dimensions"
@@ -120,6 +121,7 @@ You can create metrics using `addMetric`, and you can create dimensions for all
120121
export const handler = async (event: any, context: Context) => {
121122
metrics.addDimension('environment', 'prod');
122123
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
124+
metrics.purgeStoredMetrics();
123125
}
124126
```
125127

@@ -242,21 +244,22 @@ If you do not the middleware or decorator, you have to flush your metrics manual
242244

243245
See below an example of how to automatically flush metrics with the Middy-compatible `logMetrics` middleware.
244246

247+
=== "handler.ts"
245248

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';
250253

251-
const metrics = new Metrics({ namespace: 'exampleApplication' , service: 'exampleService' });
254+
const metrics = new Metrics({ namespace: 'exampleApplication' , service: 'exampleService' });
252255

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+
}
256259

257-
export const handler = middy(lambdaHandler)
258-
.use(logMetrics(metrics));
259-
```
260+
export const handler = middy(lambdaHandler)
261+
.use(logMetrics(metrics));
262+
```
260263

261264
=== "Example CloudWatch Logs excerpt"
262265

@@ -295,20 +298,22 @@ See below an example of how to automatically flush metrics with the Middy-compat
295298
The `logMetrics` decorator of the metrics utility can be used when your Lambda handler function is implemented as method of a Class.
296299

297300

298-
```typescript hl_lines="8"
299-
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
300-
import { Context, Callback } from 'aws-lambda';
301+
=== "handler.ts"
301302

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';
303306

304-
export class MyFunction {
307+
const metrics = new Metrics({namespace:"exampleApplication", service:"exampleService"});
305308

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+
}
309315
}
310-
}
311-
```
316+
```
312317

313318
=== "Example CloudWatch Logs excerpt"
314319

@@ -353,9 +358,7 @@ const metrics = new Metrics();
353358

354359
const lambdaHandler: Handler = async () => {
355360
metrics.addMetric('test-metric', MetricUnits.Count, 10);
356-
const metricsObject = metrics.serializeMetrics();
357361
metrics.purgeStoredMetrics();
358-
console.log(JSON.stringify(metricsObject));
359362
};
360363
```
361364

@@ -435,20 +438,22 @@ You can add high-cardinality data as part of your Metrics log with `addMetadata`
435438
!!! warning
436439
**This will not be available during metrics visualization** - Use **dimensions** for this purpose
437440

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"
442442

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';
444447

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"});
448449

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+
```
452457

453458
=== "Example CloudWatch Logs excerpt"
454459

Diff for: test

Whitespace-only changes.

0 commit comments

Comments
 (0)