Skip to content

Commit 51b4e26

Browse files
committed
docs: formatting fixes and improvements
1 parent 80d9265 commit 51b4e26

10 files changed

+180
-140
lines changed

docs/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
FROM squidfunk/mkdocs-material
2-
RUN pip install mkdocs-git-revision-date-plugin
2+
RUN pip install mkdocs-git-revision-date-plugin mkdocs-glightbox

docs/core/logger.md

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ Logger provides an opinionated logger with output structured as JSON.
1313
* Appending additional keys to structured logs at any point in time.
1414
* Providing a custom log formatter (Bring Your Own Formatter) to output logs in a structure compatible with your organization’s Logging RFC.
1515

16+
<br />
17+
18+
<figure>
19+
<img src="../../media/logger_utility_showcase.png" loading="lazy" />
20+
<figcaption>Logger showcase - Log attributes</figcaption>
21+
</figure>
22+
1623
## Getting started
1724

1825
### Installation
@@ -25,7 +32,7 @@ npm install @aws-lambda-powertools/logger
2532

2633
### Usage
2734

28-
The `Logger` utility must always be instantiated outside of the Lambda handler. In doing this, subsequent invocations processed by the same instance of your function can reuse these resources. This saves cost by reducing function run time. In addition, `Logger` can keep track of a cold start and inject the appropriate fields into logs.
35+
The `Logger` utility must always be instantiated outside the Lambda handler. By doing this, subsequent invocations processed by the same instance of your function can reuse these resources. This saves cost by reducing function run time. In addition, `Logger` can keep track of a cold start and inject the appropriate fields into logs.
2936

3037
=== "handler.ts"
3138

@@ -45,10 +52,10 @@ The library requires two settings. You can set them as environment variables, or
4552

4653
These settings will be used across all logs emitted:
4754

48-
Setting | Description | Environment variable | Constructor parameter
49-
------------------------------------------------- |------------------------------------------------------------------------------------------------------------------| ------------------------------------------------- | -------------------------------------------------
50-
**Logging level** | Sets how verbose Logger should be (INFO, by default). Supported values are: `DEBUG`, `INFO`, `WARN`, `ERROR` | `LOG_LEVEL` | `logLevel`
51-
**Service name** | Sets the name of service of which the Lambda function is part of, that will be present across all log statements | `POWERTOOLS_SERVICE_NAME` | `serviceName`
55+
| Setting | Description | Environment variable | Constructor parameter |
56+
|-------------------|------------------------------------------------------------------------------------------------------------------|---------------------------|-----------------------|
57+
| **Logging level** | Sets how verbose Logger should be (INFO, by default). Supported values are: `DEBUG`, `INFO`, `WARN`, `ERROR` | `LOG_LEVEL` | `logLevel` |
58+
| **Service name** | Sets the name of service of which the Lambda function is part of, that will be present across all log statements | `POWERTOOLS_SERVICE_NAME` | `serviceName` |
5259

5360
For a **complete list** of supported environment variables, refer to [this section](./../index.md#environment-variables).
5461

@@ -87,15 +94,15 @@ For a **complete list** of supported environment variables, refer to [this secti
8794

8895
Your Logger will include the following keys to your structured logging (default log formatter):
8996

90-
Key | Example | Note
91-
------------------------------------------------- | ------------------------------------------------- | ---------------------------------------------------------------------------------
92-
**level**: `string` | `INFO` | Logging level set for the Lambda function"s invocation
93-
**message**: `string` | `Query performed to DynamoDB` | A descriptive, human-readable representation of this log item
94-
**sampling_rate**: `float` | `0.1` | When enabled, it prints all the logs of a percentage of invocations, e.g. 10%
95-
**service**: `string` | `serverlessAirline` | A unique name identifier of the service this Lambda function belongs to, by default `service_undefined`
96-
**timestamp**: `string` | `2011-10-05T14:48:00.000Z` | Timestamp string in simplified extended ISO format (ISO 8601)
97-
**xray_trace_id**: `string` | `1-5759e988-bd862e3fe1be46a994272793` | X-Ray Trace ID. This value is always presented in Lambda environment, whether [tracing is enabled](https://docs.aws.amazon.com/lambda/latest/dg/services-xray.html){target="_blank"} or not. Logger will always log this value.
98-
**error**: `Object` | `{ name: "Error", location: "/my-project/handler.ts:18", message: "Unexpected error #1", stack: "[stacktrace]"}` | Optional - An object containing information about the Error passed to the logger
97+
| Key | Example | Note |
98+
|-----------------------------|------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
99+
| **level**: `string` | `INFO` | Logging level set for the Lambda function"s invocation |
100+
| **message**: `string` | `Query performed to DynamoDB` | A descriptive, human-readable representation of this log item |
101+
| **sampling_rate**: `float` | `0.1` | When enabled, it prints all the logs of a percentage of invocations, e.g. 10% |
102+
| **service**: `string` | `serverlessAirline` | A unique name identifier of the service this Lambda function belongs to, by default `service_undefined` |
103+
| **timestamp**: `string` | `2011-10-05T14:48:00.000Z` | Timestamp string in simplified extended ISO format (ISO 8601) |
104+
| **xray_trace_id**: `string` | `1-5759e988-bd862e3fe1be46a994272793` | X-Ray Trace ID. This value is always presented in Lambda environment, whether [tracing is enabled](https://docs.aws.amazon.com/lambda/latest/dg/services-xray.html){target="_blank"} or not. Logger will always log this value. |
105+
| **error**: `Object` | `{ name: "Error", location: "/my-project/handler.ts:18", message: "Unexpected error #1", stack: "[stacktrace]"}` | Optional - An object containing information about the Error passed to the logger |
99106

100107
### Capturing Lambda context info
101108

@@ -111,27 +118,11 @@ Key | Example
111118
**function_arn**: `string` | `arn:aws:lambda:eu-west-1:123456789012:function:shopping-cart-api-lambda-prod-eu-west-1`
112119
**function_request_id**: `string` | `c6af9ac6-7b61-11e6-9a41-93e812345678`
113120

114-
=== "Manual"
115-
116-
```typescript hl_lines="7"
117-
import { Logger } from '@aws-lambda-powertools/logger';
118-
119-
const logger = new Logger();
120-
121-
export const handler = async (_event, context): Promise<void> => {
122-
123-
logger.addContext(context);
124-
125-
logger.info('This is an INFO log with some context');
126-
127-
};
128-
```
129-
130121
=== "Middy Middleware"
131122

132123
!!! tip "Using Middy for the first time?"
133124
You can install Middy by running `npm i @middy/core`.
134-
Learn more about [its usage and lifecycle in the official Middy documentation](https://github.com/middyjs/middy#usage){target="_blank"}.
125+
Learn more about [its usage and lifecycle in the official Middy documentation](https://middy.js.org/docs/intro/getting-started){target="_blank"}.
135126

136127
```typescript hl_lines="1-2 10-11"
137128
import { Logger, injectLambdaContext } from '@aws-lambda-powertools/logger';
@@ -167,6 +158,21 @@ Key | Example
167158
export const myFunction = new Lambda();
168159
export const handler = myFunction.handler;
169160
```
161+
=== "Manual"
162+
163+
```typescript hl_lines="7"
164+
import { Logger } from '@aws-lambda-powertools/logger';
165+
166+
const logger = new Logger();
167+
168+
export const handler = async (_event, context): Promise<void> => {
169+
170+
logger.addContext(context);
171+
172+
logger.info('This is an INFO log with some context');
173+
174+
};
175+
```
170176

171177
In each case, the printed log will look like this:
172178

@@ -189,7 +195,7 @@ In each case, the printed log will look like this:
189195

190196
#### Log incoming event
191197

192-
When debugging in non-production environments, you can instruct Logger to log the incoming event with the middleware/decorator parameter `logEvent` or via POWERTOOLS_LOGGER_LOG_EVENT env var set to `true`.
198+
When debugging in non-production environments, you can instruct Logger to log the incoming event with the middleware/decorator parameter `logEvent` or via `POWERTOOLS_LOGGER_LOG_EVENT` env var set to `true`.
193199

194200
???+ warning
195201
This is disabled by default to prevent sensitive info being logged

docs/core/metrics.md

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ These metrics can be visualized through [Amazon CloudWatch Console](https://cons
1414
* Metrics are created asynchronously by the CloudWatch service. You do not need any custom stacks, and there is no impact to Lambda function latency.
1515
* Creating a one-off metric with different dimensions.
1616

17+
<br />
18+
19+
<figure>
20+
<img src="../../media/metrics_utility_showcase.png" loading="lazy" />
21+
<figcaption>Metrics showcase - Handler Annotations</figcaption>
22+
</figure>
23+
1724
## Terminologies
1825

1926
If you're new to Amazon CloudWatch, there are two terminologies you must be aware of before using this utility:
@@ -26,6 +33,7 @@ If you're new to Amazon CloudWatch, there are two terminologies you must be awar
2633
<figcaption>Metric terminology, visually explained</figcaption>
2734
</figure>
2835

36+
2937
## Getting started
3038

3139
### Installation
@@ -58,10 +66,10 @@ The library requires two settings. You can set them as environment variables, or
5866

5967
These settings will be used across all metrics emitted:
6068

61-
Setting | Description | Environment variable | Constructor parameter
62-
------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------- | -------------------------------------------------
63-
**Metric namespace** | Logical container where all metrics will be placed e.g. `serverlessAirline` | `POWERTOOLS_METRICS_NAMESPACE` | `namespace`
64-
**Service** | Optionally, sets **service** metric dimension across all metrics e.g. `payment` | `POWERTOOLS_SERVICE_NAME` | `serviceName`
69+
| Setting | Description | Environment variable | Constructor parameter |
70+
|----------------------|---------------------------------------------------------------------------------|--------------------------------|-----------------------|
71+
| **Metric namespace** | Logical container where all metrics will be placed e.g. `serverlessAirline` | `POWERTOOLS_METRICS_NAMESPACE` | `namespace` |
72+
| **Service** | Optionally, sets **service** metric dimension across all metrics e.g. `payment` | `POWERTOOLS_SERVICE_NAME` | `serviceName` |
6573

6674
For a **complete list** of supported environment variables, refer to [this section](./../index.md#environment-variables).
6775

@@ -221,7 +229,7 @@ You can add default dimensions to your metrics by passing them as parameters in
221229

222230
!!! tip "Using Middy for the first time?"
223231
You can install Middy by running `npm i @middy/core`.
224-
Learn more about [its usage and lifecycle in the official Middy documentation](https://github.com/middyjs/middy#usage){target="_blank"}.
232+
Learn more about [its usage and lifecycle in the official Middy documentation](https://middy.js.org/docs/intro/getting-started){target="_blank"}.
225233

226234
```typescript hl_lines="1-2 11 13"
227235
import { Metrics, MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics';
@@ -292,24 +300,24 @@ If you do not use the middleware or decorator, you have to flush your metrics ma
292300
* Namespace is set only once (or none)
293301
* Metric units must be [supported by CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_MetricDatum.html)
294302

295-
#### Manually
296-
297-
You can manually flush the metrics with `publishStoredMetrics` as follows:
303+
#### Middy middleware
298304

299-
!!! warning
300-
Metrics, dimensions and namespace validation still applies.
305+
See below an example of how to automatically flush metrics with the Middy-compatible `logMetrics` middleware.
301306

302307
=== "handler.ts"
303308

304-
```typescript hl_lines="7"
305-
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
309+
```typescript hl_lines="1-2 7 10-11"
310+
import { Metrics, MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics';
311+
import middy from '@middy/core';
306312

307313
const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
308314

309-
export const handler = async (_event: any, _context: any): Promise<void> => {
310-
metrics.addMetric('successfulBooking', MetricUnits.Count, 10);
311-
metrics.publishStoredMetrics();
315+
const lambdaHandler = async (_event: any, _context: any): Promise<void> => {
316+
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
312317
};
318+
319+
export const handler = middy(lambdaHandler)
320+
.use(logMetrics(metrics));
313321
```
314322

315323
=== "Example CloudWatch Logs excerpt"
@@ -321,7 +329,7 @@ You can manually flush the metrics with `publishStoredMetrics` as follows:
321329
"Timestamp": 1592234975665,
322330
"CloudWatchMetrics": [
323331
{
324-
"Namespace": "successfulBooking",
332+
"Namespace": "serverlessAirline",
325333
"Dimensions": [
326334
[
327335
"service"
@@ -333,31 +341,34 @@ You can manually flush the metrics with `publishStoredMetrics` as follows:
333341
"Unit": "Count"
334342
}
335343
]
336-
}
337-
]
338344
},
339345
"service": "orders"
340346
}
341347
```
342348

343-
#### Middy middleware
349+
#### Using the class decorator
344350

345-
See below an example of how to automatically flush metrics with the Middy-compatible `logMetrics` middleware.
351+
!!! info
352+
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.
353+
See the [official TypeScript documentation](https://www.typescriptlang.org/docs/handbook/decorators.html) for more details.
354+
355+
The `logMetrics` decorator of the metrics utility can be used when your Lambda handler function is implemented as method of a Class.
346356

347357
=== "handler.ts"
348358

349-
```typescript hl_lines="1-2 7 10-11"
350-
import { Metrics, MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics';
351-
import middy from '@middy/core';
359+
```typescript hl_lines="8"
360+
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
361+
import { LambdaInterface } from '@aws-lambda-powertools/commons';
352362

353363
const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
354364

355-
const lambdaHandler = async (_event: any, _context: any): Promise<void> => {
356-
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
357-
};
365+
export class MyFunction implements LambdaInterface {
358366

359-
export const handler = middy(lambdaHandler)
360-
.use(logMetrics(metrics));
367+
@metrics.logMetrics()
368+
public async handler(_event: any, _context: any): Promise<void> {
369+
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
370+
}
371+
}
361372
```
362373

363374
=== "Example CloudWatch Logs excerpt"
@@ -369,7 +380,7 @@ See below an example of how to automatically flush metrics with the Middy-compat
369380
"Timestamp": 1592234975665,
370381
"CloudWatchMetrics": [
371382
{
372-
"Namespace": "serverlessAirline",
383+
"Namespace": "successfulBooking",
373384
"Dimensions": [
374385
[
375386
"service"
@@ -386,29 +397,24 @@ See below an example of how to automatically flush metrics with the Middy-compat
386397
}
387398
```
388399

389-
#### Using the class decorator
400+
#### Manually
390401

391-
!!! info
392-
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.
393-
See the [official TypeScript documentation](https://www.typescriptlang.org/docs/handbook/decorators.html) for more details.
402+
You can manually flush the metrics with `publishStoredMetrics` as follows:
394403

395-
The `logMetrics` decorator of the metrics utility can be used when your Lambda handler function is implemented as method of a Class.
404+
!!! warning
405+
Metrics, dimensions and namespace validation still applies.
396406

397407
=== "handler.ts"
398408

399-
```typescript hl_lines="8"
409+
```typescript hl_lines="7"
400410
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
401-
import { LambdaInterface } from '@aws-lambda-powertools/commons';
402411

403412
const metrics = new Metrics({ namespace: 'serverlessAirline', serviceName: 'orders' });
404413

405-
export class MyFunction implements LambdaInterface {
406-
407-
@metrics.logMetrics()
408-
public async handler(_event: any, _context: any): Promise<void> {
409-
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
410-
}
411-
}
414+
export const handler = async (_event: any, _context: any): Promise<void> => {
415+
metrics.addMetric('successfulBooking', MetricUnits.Count, 10);
416+
metrics.publishStoredMetrics();
417+
};
412418
```
413419

414420
=== "Example CloudWatch Logs excerpt"
@@ -432,6 +438,8 @@ The `logMetrics` decorator of the metrics utility can be used when your Lambda h
432438
"Unit": "Count"
433439
}
434440
]
441+
}
442+
]
435443
},
436444
"service": "orders"
437445
}

0 commit comments

Comments
 (0)