Skip to content

Commit d0aaad5

Browse files
authored
Merge pull request #130 from heitorlessa/docs/readability-improvements
docs: readability improvements
2 parents 3f74b03 + efd38d8 commit d0aaad5

File tree

4 files changed

+87
-48
lines changed

4 files changed

+87
-48
lines changed

Diff for: docs/content/core/logger.mdx

+19-18
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Key | Type | Example | Description
5858
**sampling_rate** | int | 0.1 | Debug logging sampling rate in percentage e.g. 1% in this case
5959
**message** | any | "Collecting payment" | Log statement value. Unserializable JSON values will be casted to string
6060

61-
## Capturing context Lambda info
61+
## Capturing Lambda context info
6262

6363
You can enrich your structured logs with key Lambda context information via `inject_lambda_context`.
6464

@@ -79,22 +79,6 @@ def handler(event, context):
7979
...
8080
```
8181

82-
You can also explicitly log any incoming event using `log_event` param or via `POWERTOOLS_LOGGER_LOG_EVENT` env var.
83-
84-
<Note type="warning">
85-
This is disabled by default to prevent sensitive info being logged.
86-
</Note><br/>
87-
88-
```python:title=log_handler_event.py
89-
from aws_lambda_powertools import Logger
90-
91-
logger = Logger()
92-
93-
@logger.inject_lambda_context(log_event=True) # highlight-start
94-
def handler(event, context):
95-
...
96-
```
97-
9882
When used, this will include the following keys:
9983

10084
Key | Type | Example
@@ -145,6 +129,23 @@ Key | Type | Example
145129
}
146130
```
147131
</details>
132+
<br/>
133+
134+
You can also explicitly log any incoming event using `log_event` param or via `POWERTOOLS_LOGGER_LOG_EVENT` env var.
135+
136+
<Note type="warning">
137+
This is disabled by default to prevent sensitive info being logged.
138+
</Note><br/>
139+
140+
```python:title=log_handler_event.py
141+
from aws_lambda_powertools import Logger
142+
143+
logger = Logger()
144+
145+
@logger.inject_lambda_context(log_event=True) # highlight-line
146+
def handler(event, context):
147+
...
148+
```
148149

149150
## Appending additional keys
150151

@@ -222,7 +223,7 @@ If you ever forget to use `child` param, we will return an existing `Logger` wit
222223

223224
You can dynamically set a percentage of your logs to **DEBUG** level using `sample_rate` param or via env var `POWERTOOLS_LOGGER_SAMPLE_RATE`.
224225

225-
This happens on an entire request basis, and <strong>DEBUG</strong> level is set at the constructor. That means, concurrent requests or infrequent invocations are more likely to occur as [new Lambda execution contexts are created](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html), not reused.
226+
Sampling calculation happens at the Logger class initialization. This means, when configured it, sampling it's more likely to happen during concurrent requests, or infrequent invocations as [new Lambda execution contexts are created](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html), not reused.
226227

227228
<Note type="info">
228229
If you want this logic to happen on every invocation regardless whether Lambda reuses the execution environment or not, then create your Logger inside your Lambda handler.

Diff for: docs/content/core/metrics.mdx

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ description: Core utility
55

66
import Note from "../../src/components/Note"
77

8-
Metrics creates custom metrics asynchronously via logging metrics to standard output following Amazon CloudWatch Embedded Metric Format (EMF).
8+
Metrics creates custom metrics asynchronously by logging metrics to standard output following Amazon CloudWatch Embedded Metric Format (EMF).
9+
10+
These metrics can be visualized through [Amazon CloudWatch Console](https://console.aws.amazon.com/cloudwatch/).
911

1012
**Key features**
1113

@@ -32,7 +34,9 @@ Resources:
3234
```
3335

3436
We recommend you use your application or main service as a metric namespace.
35-
You can explicitly set a namespace name via `namespace` param or via `POWERTOOLS_METRICS_NAMESPACE` env var. This sets **namespace** key that will be used for all metrics.
37+
You can explicitly set a namespace name via `namespace` param or via `POWERTOOLS_METRICS_NAMESPACE` env var.
38+
39+
This sets **namespace** key that will be used for all metrics.
3640
You can also pass a service name via `service` param or `POWERTOOLS_SERVICE_NAME` env var. This will create a dimension with the service name.
3741

3842
```python:title=app.py
@@ -67,7 +71,7 @@ metrics.add_metric(name="SuccessfulBooking", unit=MetricUnit.Count, value=1)
6771

6872
`MetricUnit` enum facilitate finding a supported metric unit by CloudWatch. Alternatively, you can pass the value as a string if you already know them e.g. "Count".
6973

70-
CloudWatch EMF supports a max of 100 metrics. Metrics will automatically flush all metrics when adding the 100th metric, where subsequent metrics will be aggregated into a new EMF object.
74+
CloudWatch EMF supports a max of 100 metrics. Metrics utility will flush all metrics when adding the 100th metric while subsequent metrics will be aggregated into a new EMF object, for your convenience.
7175

7276
## Creating a metric with a different dimension
7377

Diff for: docs/content/core/tracer.mdx

+29-9
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ Tracer is an opinionated thin wrapper for [AWS X-Ray Python SDK](https://github.
1717
* Support tracing async methods, generators, and context managers
1818
* Auto patch supported modules, or a tuple of explicit modules supported by AWS X-Ray
1919

20-
<Note type="warning">
21-
<strong>Returning sensitive information from your Lambda handler or functions, where Tracer is used?</strong>
22-
<br/><br/>
23-
You can disable Tracer from capturing their responses as tracing metadata with <strong><code>capture_response=False</code></strong> parameter in both capture_lambda_handler and capture_method decorators.
24-
</Note><br/>
25-
2620
## Initialization
2721

2822
Your AWS Lambda function must have permission to send traces to AWS X-Ray - Here is an example using AWS Serverless Application Model (SAM)
@@ -58,7 +52,7 @@ You can trace your Lambda function handler via `capture_lambda_handler`.
5852
When using this decorator, Tracer performs these additional tasks to ease operations:
5953

6054
* Creates a `ColdStart` annotation to easily filter traces that have had an initialization overhead
61-
* Adds any response, or full exceptions generated by the handler as metadata
55+
* Captures any response, or full exceptions generated by the handler, and include as tracing metadata
6256

6357
```python:title=lambda_handler.py
6458
from aws_lambda_powertools import Tracer
@@ -69,7 +63,17 @@ def handler(event, context):
6963
charge_id = event.get('charge_id')
7064
payment = collect_payment(charge_id)
7165
...
66+
```
67+
68+
<Note type="warning">
69+
<strong>Returning sensitive information from your Lambda handler or functions, where Tracer is used?</strong>
70+
<br/><br/>
71+
You can disable Tracer from capturing their responses as tracing metadata with <strong><code>capture_response=False</code></strong> parameter in both capture_lambda_handler and capture_method decorators.
72+
</Note><br/>
7273

74+
```python:title=do_not_capture_response_as_metadata.py
75+
# Disables Tracer from capturing response and adding as metadata
76+
# Useful when dealing with sensitive data
7377
@tracer.capture_lambda_handler(capture_response=False) # highlight-line
7478
def handler(event, context):
7579
return "sensitive_information"
@@ -170,7 +174,23 @@ def handler(evt, ctx): # highlight-line
170174
another_result = list(collect_payment_gen())
171175
```
172176

173-
## Tracing aiohttp requests
177+
## Patching modules
178+
179+
Tracer automatically patches all [supported libraries by X-Ray](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-python-patching.html) during initialization, by default. Underneath, AWS X-Ray SDK checks whether a supported library has been imported before patching.
180+
181+
If you're looking to shave a few microseconds, or milliseconds depending on your function memory configuration, you can patch specific modules using `patch_modules` param:
182+
183+
```python:title=app.py
184+
import boto3
185+
import requests
186+
187+
from aws_lambda_powertools import Tracer
188+
189+
modules_to_be_patched = ["boto3", "requests"]
190+
tracer = Tracer(patch_modules=modules_to_be_patched) # highlight-line
191+
```
192+
193+
### Tracing aiohttp requests
174194

175195
<Note type="info">
176196
This snippet assumes you have <strong>aiohttp</strong> as a dependency
@@ -199,7 +219,7 @@ You can use `tracer.provider` attribute to access all methods provided by AWS X-
199219

200220
This is useful when you need a feature available in X-Ray that is not available in the Tracer utility, for example [thread-safe](https://github.com/aws/aws-xray-sdk-python/#user-content-trace-threadpoolexecutor), or [context managers](https://github.com/aws/aws-xray-sdk-python/#user-content-start-a-custom-segmentsubsegment).
201221

202-
## Concurrent asynchronous functions
222+
### Concurrent asynchronous functions
203223

204224
<Note type="info">
205225
<a href="https://github.com/aws/aws-xray-sdk-python/issues/164">As of now, X-Ray SDK will raise an exception when async functions are run and traced concurrently</a>.

Diff for: docs/content/index.mdx

+32-18
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,47 @@ title: Homepage
33
description: AWS Lambda Powertools Python
44
---
55

6+
import Note from "../src/components/Note"
7+
68
Powertools is a suite of utilities for AWS Lambda Functions that makes tracing with AWS X-Ray, structured logging and creating custom metrics asynchronously easier.
79

10+
<Note type="info">
11+
<strong>Looking for a quick run through of the core utilities?</strong><br/><br/>
12+
Check out <a href="https://aws.amazon.com/blogs/opensource/simplifying-serverless-best-practices-with-lambda-powertools/">this detailed blog post</a> with a practical example.
13+
</Note>
14+
815
## Install
916

1017
Powertools is available in PyPi. You can use your favourite dependency management tool to install it
1118

1219
* [poetry](https://python-poetry.org/): `poetry add aws-lambda-powertools`
1320
* [pip](https://pip.pypa.io/en/latest/index.html): `pip install aws-lambda-powertools`
1421

15-
## Features
16-
17-
* [Tracing](./core/tracer) - Decorators and utilities to trace Lambda function handlers, and both synchronous and asynchronous functions
18-
* [Logging](./core/logger) - Structured logging made easier, and decorator to enrich structured logging with key Lambda context details
19-
* [Metrics](./core/metrics) - Custom Metrics created asynchronously via CloudWatch Embedded Metric Format (EMF)
20-
* [Bring your own middleware](./utilities/middleware_factory) - Decorator factory to create your own middleware to run logic before, and after each Lambda invocation
21-
* [Parameters utility](./utilities/parameters) - Retrieve parameter values from AWS Systems Manager Parameter Store, AWS Secrets Manager, or Amazon DynamoDB, and cache them for a specific amount of time
22-
23-
## Tenets
22+
**Quick hello world example using SAM CLI**
23+
```bash:title=hello_world.sh
24+
sam init --location https://github.com/aws-samples/cookiecutter-aws-sam-python
25+
```
2426

25-
* **AWS Lambda only** – We optimise for AWS Lambda function environments and supported runtimes only. Utilities might work with web frameworks and non-Lambda environments, though they are not officially supported.
26-
* **Eases the adoption of best practices** – The main priority of the utilities is to facilitate best practices adoption, as defined in the AWS Well-Architected Serverless Lens; all other functionality is optional.
27-
* **Keep it lean** – Additional dependencies are carefully considered for security and ease of maintenance, and prevent negatively impacting startup time.
28-
* **We strive for backwards compatibility** – New features and changes should keep backwards compatibility. If a breaking change cannot be avoided, the deprecation and migration process should be clearly defined.
29-
* **We work backwards from the community** – We aim to strike a balance of what would work best for 80% of customers. Emerging practices are considered and discussed via Requests for Comment (RFCs)
30-
* **Idiomatic** – Utilities follow programming language idioms and language-specific best practices.
27+
## Features
3128

32-
_`*` Core utilities are Tracer, Logger and Metrics. Optional utilities may vary across languages._
29+
Utility | Description
30+
------------------------------------------------- | ---------------------------------------------------------------------------------
31+
[Tracing](./core/tracer) | Decorators and utilities to trace Lambda function handlers, and both synchronous and asynchronous functions
32+
[Logging](./core/logger) | Structured logging made easier, and decorator to enrich structured logging with key Lambda context details
33+
[Metrics](./core/metrics) | Custom Metrics created asynchronously via CloudWatch Embedded Metric Format (EMF)
34+
[Bring your own middleware](.//utilities/middleware_factory) | Decorator factory to create your own middleware to run logic before, and after each Lambda invocation
35+
[Parameters utility](./utilities/parameters) | Retrieve parameter values from AWS Systems Manager Parameter Store, AWS Secrets Manager, or Amazon DynamoDB, and cache them for a specific amount of time
3336

3437
## Environment variables
3538

3639
**Environment variables** used across suite of utilities.
3740

3841
Environment variable | Description | Utility
3942
------------------------------------------------- | --------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- | -------------------------------------------------
40-
**POWERTOOLS_SERVICE_NAME** | Sets service name used for tracing namespace, metrics dimension and structured logging | all
43+
**POWERTOOLS_SERVICE_NAME** | Sets service name used for tracing namespace, metrics dimension and structured logging | All
4144
**POWERTOOLS_METRICS_NAMESPACE** | Sets namespace used for metrics | [Metrics](./core/metrics)
4245
**POWERTOOLS_TRACE_DISABLED** | Disables tracing | [Tracing](./core/tracer)
43-
**POWERTOOLS_TRACE_MIDDLEWARES** | Creates sub-segment for each custom middleware | [middleware_factory](./utilities/middleware_factory)
46+
**POWERTOOLS_TRACE_MIDDLEWARES** | Creates sub-segment for each custom middleware | [Middleware factory](./utilities/middleware_factory)
4447
**POWERTOOLS_LOGGER_LOG_EVENT** | Logs incoming event | [Logging](./core/logger)
4548
**POWERTOOLS_LOGGER_SAMPLE_RATE** | Debug log sampling | [Logging](./core/logger)
4649
**LOG_LEVEL** | Sets logging level | [Logging](./core/logger)
@@ -54,3 +57,14 @@ from aws_lambda_powertools.logging.logger import set_package_logger
5457

5558
set_package_logger()
5659
```
60+
61+
## Tenets
62+
63+
* **AWS Lambda only** – We optimise for AWS Lambda function environments and supported runtimes only. Utilities might work with web frameworks and non-Lambda environments, though they are not officially supported.
64+
* **Eases the adoption of best practices** – The main priority of the utilities is to facilitate best practices adoption, as defined in the AWS Well-Architected Serverless Lens; all other functionality is optional.
65+
* **Keep it lean** – Additional dependencies are carefully considered for security and ease of maintenance, and prevent negatively impacting startup time.
66+
* **We strive for backwards compatibility** – New features and changes should keep backwards compatibility. If a breaking change cannot be avoided, the deprecation and migration process should be clearly defined.
67+
* **We work backwards from the community** – We aim to strike a balance of what would work best for 80% of customers. Emerging practices are considered and discussed via Requests for Comment (RFCs)
68+
* **Idiomatic** – Utilities follow programming language idioms and language-specific best practices.
69+
70+
_`*` Core utilities are Tracer, Logger and Metrics. Optional utilities may vary across languages._

0 commit comments

Comments
 (0)