Skip to content

Commit 419c8d2

Browse files
committed
improv(metrics): improve docs, examples, naming
1 parent f430195 commit 419c8d2

File tree

5 files changed

+24
-21
lines changed

5 files changed

+24
-21
lines changed

docs/core/metrics.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -171,35 +171,35 @@ See examples below:
171171

172172
=== "with logMetrics decorator"
173173

174-
```typescript hl_lines="6 12"
174+
```typescript hl_lines="9"
175175
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
176-
import { Context, Callback } from 'aws-lambda';
177-
import middy from '@middy/core';
178-
176+
import { Context, Callback } from 'aws-lambda';
179177

180178
const metrics = new Metrics({namespace:"serverlessAirline", service:"orders"});
181179
const DEFAULT_DIMENSIONS = {"environment": "prod", "another": "one"};
182180

183-
184181
export class MyFunction {
185182

186-
@metrics.logMetrics({defaultDimensions: DEFAULT_DIMENSIONS})
187-
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {
188-
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
183+
@metrics.logMetrics({defaultDimensions: DEFAULT_DIMENSIONS})
184+
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {
185+
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
186+
}
189187
}
190188
```
191189

192190
### Flushing metrics
193191

194-
As you finish adding all your metrics, you need to serialize and flush them to standard output.
192+
As you finish adding all your metrics, you need to serialize and "flush them" (= print them to standard output).
195193

196194
You can flush metrics automatically using one of the following methods:
197195

198196
* [Middy-compatible](https://github.com/middyjs/middy){target=_blank} middleware
199197
* class decorator
200198
* manually
201199

202-
The middleware and the decorator also **validate**, **serialize**, and **flush** all your metrics. During metrics validation, if no metrics are provided then a warning will be logged, but no exception will be raised.
200+
Using the Middy middleware or decorator will **automatically validate, serialize, and flush** all your metrics. During metrics validation, if no metrics are provided then a warning will be logged, but no exception will be raised.
201+
If you do not the middleware or decorator, you have to flush your metrics manually.
202+
203203

204204
!!! warning "Metric validation"
205205
If metrics are provided, and any of the following criteria are not met, a **`RangeError`** exception will be raised:
@@ -257,10 +257,13 @@ See below an example of how to automatically flush metrics with the Middy-compat
257257
}
258258
```
259259

260-
#### Using the class decorator
260+
#### Using a class decorator
261261

262-
Decorators can only be attached to a class declaration, method, accessor, property, or parameter. Therefore, if prefer to write your handler as a standard function, check the middleware or manual methods instead.
263-
See the [official TypeScript documentation](https://www.typescriptlang.org/docs/handbook/decorators.html) for more details.
262+
!!! info
263+
Decorators can only be attached to a class declaration, method, accessor, property, or parameter. Therefore, if you prefer to write your handler as a standard function rather than a Class method, check the [middleware](#using-a-middleware) or [manual](#manually) method sections instead.
264+
See the [official TypeScript documentation](https://www.typescriptlang.org/docs/handbook/decorators.html) for more details.
265+
266+
The `logMetrics` decorator of the metrics utility can be used when your Lambda handler function is implemented as method of a Class.
264267

265268

266269
```typescript hl_lines="8"
@@ -309,7 +312,7 @@ export class MyFunction {
309312

310313
#### Manually
311314

312-
If you wish to do so, you can manually flush the metrics with `purgeStoredMetrics` and clear metrics as follows:
315+
If you wish to do so, you can manually flush metrics with `purgeStoredMetrics` and clear metrics as follows:
313316

314317
!!! warning
315318
Metrics, dimensions and namespace validation still applies.
@@ -392,7 +395,7 @@ If it's a cold start invocation, this feature will:
392395

393396
This has the advantage of keeping cold start metric separate from your application metrics, where you might have unrelated dimensions.
394397

395-
!!! info "We do not emit 0 as a value for the ColdStart metric for cost-efficiency reasons. [Let us know](https://github.com/awslabs/aws-lambda-powertools-typecsript/issues/new?assignees=&labels=feature-request%2C+triage&template=feature_request.md&title=) if you'd prefer a flag to override it."
398+
!!! info "We do not emit 0 as a value for the ColdStart metric for cost-efficiency reasons. [Let us know](https://github.com/awslabs/aws-lambda-powertools-typescript/issues/new?assignees=&labels=feature-request%2C+triage&template=feature_request.md&title=) if you'd prefer a flag to override it."
396399

397400
## Advanced
398401

@@ -459,10 +462,9 @@ CloudWatch EMF uses the same dimensions across all your metrics. Use `singleMetr
459462

460463
=== "logMetrics middleware"
461464

462-
```typescript hl_lines="8 10 12"
465+
```typescript hl_lines="7 9 11 15"
463466
import { Metrics, MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics';
464467
import { Context } from 'aws-lambda';
465-
import middy from '@middy/core';
466468

467469
const metrics = new Metrics({namespace:"serverlessAirline", service:"orders"});
468470

@@ -480,7 +482,7 @@ CloudWatch EMF uses the same dimensions across all your metrics. Use `singleMetr
480482

481483
=== "logMetrics decorator"
482484

483-
```typescript hl_lines="11 13 15"
485+
```typescript hl_lines="9 11 13 15"
484486
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
485487
import { Context, Callback } from 'aws-lambda';
486488
import middy from '@middy/core';

packages/metrics/examples/empty-metrics.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world';
1313
const metrics = new Metrics();
1414

1515
const lambdaHandler = async (): Promise<void> => {
16-
// No metric added
16+
// Notice that no metrics are added
17+
// Since the raiseOnEmptyMetrics parameter is set to true, the Powertool throw an Error
1718
};
1819

1920
const handlerWithMiddleware = middy(lambdaHandler)

packages/metrics/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"example:empty-metrics": "ts-node examples/empty-metrics.ts",
2727
"example:single-metric": "ts-node examples/single-metric.ts",
2828
"example:cold-start": "ts-node examples/cold-start.ts",
29-
"example:programatic-access": "ts-node examples/programmatic-access.ts",
29+
"example:manual-metrics-print": "ts-node examples/manual-metrics-print.ts",
3030
"example:constructor-options": "ts-node examples/constructor-options.ts",
3131
"example:default-dimensions-constructor": "ts-node examples/default-dimensions-constructor.ts",
3232
"example:hello-world-decorator": "ts-node examples/decorator/hello-world.ts",
@@ -36,7 +36,7 @@
3636
"example:empty-metrics-decorator": "ts-node examples/decorator/empty-metrics.ts",
3737
"example:single-metric-decorator": "ts-node examples/decorator/single-metric.ts",
3838
"example:cold-start-decorator": "ts-node examples/decorator/cold-start.ts",
39-
"example:programatic-access-decorator": "ts-node examples/decorator/programatic-access.ts",
39+
"example:manual-metrics-print-decorator": "ts-node examples/decorator/manual-metrics-print.ts",
4040
"example:constructor-options-decorator": "ts-node examples/decorator/constructor-options.ts",
4141
"example:default-dimensions-constructor-decorator": "ts-node examples/decorator/default-dimensions-constructor.ts"
4242
},

0 commit comments

Comments
 (0)