Skip to content

Commit b503137

Browse files
Adding docs and examples
1 parent 7b0fd1a commit b503137

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

docs/core/logger.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -627,24 +627,27 @@ Notice in the CloudWatch Logs output how `payment_id` appears as expected when l
627627

628628
### Sampling debug logs
629629

630-
Use sampling when you want to dynamically change your log level to **DEBUG** based on a **percentage of your concurrent/cold start invocations**.
630+
Use sampling when you want to dynamically change your log level to **DEBUG** based on a **percentage of the Lambda function executions**.
631631

632-
You can use values ranging from `0.0` to `1` (100%) when setting `POWERTOOLS_LOGGER_SAMPLE_RATE` env var, or `sample_rate` parameter in Logger.
632+
You can use values ranging from `0.0` to `1` (100%) when setting `POWERTOOLS_LOGGER_SAMPLE_RATE` env var, or `sampling_rate` parameter in Logger.
633633

634634
???+ tip "Tip: When is this useful?"
635-
Let's imagine a sudden spike increase in concurrency triggered a transient issue downstream. When looking into the logs you might not have enough information, and while you can adjust log levels it might not happen again.
635+
Log sampling allows you to capture debug information for a fraction of your requests, helping you diagnose rare or intermittent issues without drowning your logs in unnecessary details.
636636

637-
This feature takes into account transient issues where additional debugging information can be useful.
637+
Example: Imagine an e-commerce checkout process where you want to understand rare payment gateway errors. With 10% sampling, you'll log detailed information for a small subset of transactions, making troubleshooting easier without generating excessive logs.
638638

639-
Sampling decision happens at the Logger initialization. This means sampling may happen significantly more or less than depending on your traffic patterns, for example a steady low number of invocations and thus few cold starts.
639+
Sampling decision happens at every execution when using `@logger.inject_lambda_context` decorator or `refresh_sample_rate_calculation` method. If you don't use any of both, you may end up with undesired sampling behavior.
640640

641-
???+ note
642-
Open a [feature request](https://github.com/aws-powertools/powertools-lambda-python/issues/new?assignees=&labels=feature-request%2C+triage&template=feature_request.md&title=){target="_blank"} if you want Logger to calculate sampling for every invocation
641+
=== "sampling_debug_logs_with_decorator.py"
643642

644-
=== "sampling_debug_logs.py"
643+
```python hl_lines="5 8"
644+
--8<-- "examples/logger/src/sampling_debug_logs_with_decorator.py"
645+
```
645646

646-
```python hl_lines="6 10"
647-
--8<-- "examples/logger/src/sampling_debug_logs.py"
647+
=== "sampling_debug_logs_with_standalone_function.py"
648+
649+
```python hl_lines="5 12"
650+
--8<-- "examples/logger/src/sampling_debug_logs_with_standalone_function.py"
648651
```
649652

650653
=== "sampling_debug_logs_output.json"

examples/logger/src/sampling_debug_logs.py renamed to examples/logger/src/sampling_debug_logs_with_decorator.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
from aws_lambda_powertools.utilities.typing import LambdaContext
33

44
# Sample 10% of debug logs e.g. 0.1
5-
# NOTE: this evaluation will only occur at cold start
65
logger = Logger(service="payment", sample_rate=0.1)
76

87

8+
@logger.inject_lambda_context
99
def lambda_handler(event: dict, context: LambdaContext):
1010
logger.debug("Verifying whether order_id is present")
1111
logger.info("Collecting payment")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from aws_lambda_powertools import Logger
2+
from aws_lambda_powertools.utilities.typing import LambdaContext
3+
4+
# Sample 10% of debug logs e.g. 0.1
5+
logger = Logger(service="payment", sample_rate=0.1)
6+
7+
8+
def lambda_handler(event: dict, context: LambdaContext):
9+
logger.debug("Verifying whether order_id is present")
10+
logger.info("Collecting payment")
11+
12+
logger.refresh_sample_rate_calculation()
13+
14+
return "hello world"

0 commit comments

Comments
 (0)