Skip to content

Commit 4090c8c

Browse files
committed
improv: log sampling wording
Signed-off-by: heitorlessa <[email protected]>
1 parent 286bf92 commit 4090c8c

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

docs/content/core/logger.mdx

+25-7
Original file line numberDiff line numberDiff line change
@@ -222,21 +222,24 @@ If you ever forget to use `child` param, we will return an existing `Logger` wit
222222

223223
## Sampling debug logs
224224

225-
You can dynamically set a percentage of your logs to **DEBUG** level using `sample_rate` param or via env var `POWERTOOLS_LOGGER_SAMPLE_RATE`.
225+
Sampling allows you to set your Logger Log Level as DEBUG based on a percentage of your concurrent/cold start invocations. You can set a sampling value of `0.0` to `1` (100%) using either `sample_rate` parameter or `POWERTOOLS_LOGGER_SAMPLE_RATE` env var.
226226

227-
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.
227+
This is useful when you want to troubleshoot an issue, say a sudden increase in concurrency, and you might not have enough information in your logs as Logger log level was understandably set as INFO.
228+
229+
Sampling calculation happens at the Logger class initialization. This means sampling will unlikely if you have a steady low number of invocations, depending on what you configured it.
228230

229231
<Note type="info">
230-
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.
232+
If you want Logger to calculate sampling on every invocation, then please open a feature request.
231233
</Note><br/>
232234

233235
```python:title=collect.py
234236
from aws_lambda_powertools import Logger
235237

236238
# Sample 10% of debug logs e.g. 0.1
237-
logger = Logger(sample_rate=0.1) # highlight-line
239+
logger = Logger(sample_rate=0.1, level="INFO") # highlight-line
238240

239241
def handler(event, context):
242+
logger.debug("Verifying whether order_id is present")
240243
if "order_id" in event:
241244
logger.info("Collecting payment")
242245
...
@@ -245,7 +248,21 @@ def handler(event, context):
245248
<details>
246249
<summary><strong>Excerpt output in CloudWatch Logs</strong></summary>
247250

248-
```json:title=cloudwatch_logs.json
251+
```json:title=sampled_log_request_as_debug.json
252+
{
253+
"timestamp": "2020-05-24 18:17:33,774",
254+
"level": "DEBUG", // highlight-line
255+
"location": "collect.handler:1",
256+
"service": "payment",
257+
"lambda_function_name": "test",
258+
"lambda_function_memory_size": 128,
259+
"lambda_function_arn": "arn:aws:lambda:eu-west-1:12345678910:function:test",
260+
"lambda_request_id": "52fdfc07-2182-154f-163f-5f0f9a621d72",
261+
"cold_start": true,
262+
"sampling_rate": 0.1, // highlight-line
263+
"message": "Verifying whether order_id is present"
264+
}
265+
249266
{
250267
"timestamp": "2020-05-24 18:17:33,774",
251268
"level": "INFO",
@@ -260,6 +277,7 @@ def handler(event, context):
260277
"message": "Collecting payment"
261278
}
262279
```
280+
263281
</details>
264282

265283

@@ -305,7 +323,7 @@ This can be fixed by either ensuring both has the `service` value as `payment`,
305323

306324
You might want to continue to use the same date formatting style, or override `location` to display the `package.function_name:line_number` as you previously had.
307325

308-
Logger allows you to either change the format or suppress the following keys altogether at the initialization: `location`, `timestamp`, `level`, and `datefmt`
326+
Logger allows you to either change the format or suppress the following keys altogether at the initialization: `location`, `timestamp`, `level`, `xray_trace_id`, and `datefmt`
309327

310328
```python
311329
from aws_lambda_powertools import Logger
@@ -317,7 +335,7 @@ logger = Logger(stream=stdout, location="[%(funcName)s] %(module)s", datefmt="fa
317335
logger = Logger(stream=stdout, location=None) # highlight-line
318336
```
319337

320-
Alternatively, you can also change the order of the following log record keys via the `log_record_order` parameter: `level`, `location`, `message`, and `timestamp`
338+
Alternatively, you can also change the order of the following log record keys via the `log_record_order` parameter: `level`, `location`, `message`, `xray_trace_id`, and `timestamp`
321339

322340
```python
323341
from aws_lambda_powertools import Logger

0 commit comments

Comments
 (0)