@@ -55,10 +55,10 @@ Setting | Description | Environment variable | Constructor parameter
55
55
56
56
[ // ] :# ( START EDITING FROM HERE DOWN )
57
57
58
+ === "index.ts"
58
59
59
-
60
- ```typescript hl_lines="4 6"
61
- import {Metrics, MetricUnits} from '@aws-lambda-powertools/metrics';
60
+ ```typescript hl_lines="5 7"
61
+ import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
62
62
63
63
64
64
// Sets metric namespace and service via env var
@@ -76,8 +76,8 @@ You can create metrics using `addMetric`, and you can create dimensions for all
76
76
=== "Metrics"
77
77
78
78
```typescript hl_lines="8"
79
- import {Metrics, MetricUnits} from '@aws-lambda-powertools/metrics';
80
- import { Context } from 'aws-lambda'; // npm install @types/aws-lambda
79
+ import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
80
+ import { Context } from 'aws-lambda';
81
81
82
82
83
83
const metrics = new Metrics({namespace:"ServerlessAirline", service:"orders"});
@@ -89,8 +89,8 @@ You can create metrics using `addMetric`, and you can create dimensions for all
89
89
=== "Metrics with custom dimensions"
90
90
91
91
```typescript hl_lines="8-9"
92
- import {Metrics, MetricUnits} from '@aws-lambda-powertools/metrics';
93
- import { Context } from 'aws-lambda'; // npm install @types/aws-lambda
92
+ import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
93
+ import { Context } from 'aws-lambda';
94
94
95
95
96
96
const metrics = new Metrics({namespace:"ServerlessAirline", service:"orders"});
@@ -119,8 +119,8 @@ If you'd like to remove them at some point, you can use `clearDefaultDimensions`
119
119
=== "setDefaultDimensions method"
120
120
121
121
```typescript hl_lines="5"
122
- import {Metrics, MetricUnits} from '@aws-lambda-powertools/metrics';
123
- import { Context } from 'aws-lambda'; // npm install @types/aws-lambda
122
+ import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
123
+ import { Context } from 'aws-lambda';
124
124
125
125
const metrics = new Metrics({namespace:"ServerlessAirline", service:"orders"});
126
126
metrics.setDefaultDimensions({ 'environment': 'prod', 'anotherDimension': 'whatever' });
@@ -133,8 +133,8 @@ If you'd like to remove them at some point, you can use `clearDefaultDimensions`
133
133
=== "with logMetrics decorator"
134
134
135
135
```typescript hl_lines="6 11"
136
- import {Metrics, MetricUnits} from '@aws-lambda-powertools/metrics';
137
- import { Context, Callback } from 'aws-lambda'; // npm install @types/aws-lambda
136
+ import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
137
+ import { Context, Callback } from 'aws-lambda';
138
138
139
139
140
140
const metrics = new Metrics({namespace:"ServerlessAirline", service:"orders"});
@@ -158,21 +158,22 @@ As you finish adding all your metrics, you need to serialize and flush them to s
158
158
You can do that automatically with the ` logMetrics ` decorator.
159
159
160
160
!!! warning
161
- decorators can only be attached to a class declaration, method, accessor, property, or parameter. Therefore, if you are more into functional programming check the next section instead. See the [ official doc] ( https://www.typescriptlang.org/docs/handbook/decorators.html ) for more details.
161
+ Decorators can only be attached to a class declaration, method, accessor, property, or parameter. Therefore, if you are more into functional programming check the next section instead. See the [ official doc] ( https://www.typescriptlang.org/docs/handbook/decorators.html ) for more details.
162
162
163
163
This decorator also ** validates** , ** serializes** , and ** flushes** all your metrics. During metrics validation, if no metrics are provided then a warning will be logged, but no exception will be raised.
164
164
165
165
``` typescript hl_lines="8"
166
- import {Metrics , MetricUnits } from ' @aws-lambda-powertools/metrics' ;
167
- import { Context , Callback } from ' aws-lambda' ; // npm install @types/aws-lambda
166
+ import { Metrics , MetricUnits } from ' @aws-lambda-powertools/metrics' ;
167
+ import { Context , Callback } from ' aws-lambda' ;
168
168
169
169
const metrics = new Metrics ({namespace:" ExampleApplication" , service:" ExampleService" });
170
170
171
171
export class MyFunction {
172
172
173
- @metrics .logMetrics ()
174
- public handler<TEvent , TResult >(_event : TEvent , _context : Context , _callback : Callback <TResult >): void | Promise <TResult > {
175
- metrics .addMetric (' BookingConfirmation' , MetricUnits .Count , 1 );
173
+ @metrics .logMetrics ()
174
+ public handler<TEvent , TResult >(_event : TEvent , _context : Context , _callback : Callback <TResult >): void | Promise <TResult > {
175
+ metrics .addMetric (' BookingConfirmation' , MetricUnits .Count , 1 );
176
+ }
176
177
}
177
178
```
178
179
@@ -218,8 +219,8 @@ If you prefer not to use `logMetrics` decorator because you might want to encaps
218
219
!!! warning
219
220
Metrics, dimensions and namespace validation still applies.
220
221
221
- ` ` ` typescript hl_lines= " 9 "
222
- import {Metrics , MetricUnits } from ' @aws-lambda-powertools/metrics' ;
222
+ ``` typescript hl_lines="8 "
223
+ import { Metrics , MetricUnits } from ' @aws-lambda-powertools/metrics' ;
223
224
224
225
const metrics = new Metrics ();
225
226
@@ -236,7 +237,7 @@ const lambdaHandler: Handler = async () => {
236
237
If you want to ensure that at least one metric is emitted, you can pass ` raiseOnEmptyMetrics ` to the ** logMetrics** decorator:
237
238
238
239
``` typescript hl_lines="7"
239
- import {Metrics , MetricUnits } from ' @aws-lambda-powertools/metrics' ;
240
+ import { Metrics , MetricUnits } from ' @aws-lambda-powertools/metrics' ;
240
241
241
242
const metrics = new Metrics ();
242
243
@@ -254,7 +255,7 @@ class Lambda implements LambdaInterface {
254
255
You can optionally capture cold start metrics with ` logMetrics ` decorator via ` captureColdStartMetric ` param.
255
256
256
257
``` typescript hl_lines="7"
257
- import {Metrics , MetricUnits } from ' @aws-lambda-powertools/metrics' ;
258
+ import { Metrics , MetricUnits } from ' @aws-lambda-powertools/metrics' ;
258
259
259
260
const metrics = new Metrics ();
260
261
@@ -284,7 +285,7 @@ You can add high-cardinality data as part of your Metrics log with `addMetadata`
284
285
**This will not be available during metrics visualization** - Use **dimensions** for this purpose
285
286
286
287
` ` ` typescript hl_lines = " 9"
287
- import {Metrics , MetricUnits } from ' @aws-lambda-powertools/metrics' ;
288
+ import { Metrics , MetricUnits } from ' @aws-lambda-powertools/metrics' ;
288
289
289
290
const metrics = new Metrics ();
290
291
0 commit comments