You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/content/core/logger.mdx
+49-7
Original file line number
Diff line number
Diff line change
@@ -222,21 +222,24 @@ If you ever forget to use `child` param, we will return an existing `Logger` wit
222
222
223
223
## Sampling debug logs
224
224
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.
226
226
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 decision happens at the Logger class initialization, which only happens during a cold start. This means sampling may happen significant more or less than you expect if you have a steady low number of invocations and thus few cold starts.
228
230
229
231
<Notetype="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.
"message": "Verifying whether order_id is present"
264
+
}
265
+
249
266
{
250
267
"timestamp": "2020-05-24 18:17:33,774",
251
268
"level": "INFO",
@@ -260,6 +277,7 @@ def handler(event, context):
260
277
"message": "Collecting payment"
261
278
}
262
279
```
280
+
263
281
</details>
264
282
265
283
@@ -305,7 +323,7 @@ This can be fixed by either ensuring both has the `service` value as `payment`,
305
323
306
324
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.
307
325
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`
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`
321
339
322
340
```python
323
341
from aws_lambda_powertools import Logger
@@ -358,3 +376,27 @@ except Exception:
358
376
}
359
377
```
360
378
</details>
379
+
380
+
381
+
## Testing your code
382
+
383
+
When unit testing your code that makes use of `inject_lambda_context` decorator, you need to pass a dummy Lambda Context, or else Logger will fail.
384
+
385
+
This is a Pytest sample that provides the minimum information necessary for Logger to succeed:
Copy file name to clipboardExpand all lines: docs/content/core/metrics.mdx
+28-2
Original file line number
Diff line number
Diff line change
@@ -251,11 +251,37 @@ This has the advantage of keeping cold start metric separate from your applicati
251
251
252
252
## Testing your code
253
253
254
+
### Environment variables
255
+
254
256
Use `POWERTOOLS_METRICS_NAMESPACE` and `POWERTOOLS_SERVICE_NAME` env vars when unit testing your code to ensure metric namespace and dimension objects are created, and your code doesn't fail validation.
You can ignore this if you are explicitly setting namespace/default dimension by passing the `namespace` and `service` parameters when initializing Metrics: `metrics = Metrics(namespace=ApplicationName, service=ServiceName)`.
262
+
If you prefer setting environment variable for specific tests, and are using Pytest, you can use [monkeypatch](https://docs.pytest.org/en/latest/monkeypatch.html) fixture:
263
+
264
+
```python:title=pytest_env_var.py
265
+
deftest_namespace_env_var(monkeypatch):
266
+
# Set POWERTOOLS_METRICS_NAMESPACE before initializating Metrics
> Ignore this, if you are explicitly setting namespace/default dimension via `namespace` and `service` parameters: `metrics = Metrics(namespace=ApplicationName, service=ServiceName)`
274
+
275
+
### Clearing metrics
276
+
277
+
`Metrics` keep metrics in memory across multiple instances. If you need to test this behaviour, you can use the following Pytest fixture to ensure metrics are reset incl. cold start:
278
+
279
+
```python:title=pytest_metrics_reset_fixture.py
280
+
@pytest.fixture(scope="function", autouse=True)
281
+
defreset_metric_set():
282
+
# Clear out every metric data prior to every test
283
+
metrics = Metrics()
284
+
metrics.clear_metrics()
285
+
metrics_global.is_cold_start =True# ensure each test has cold start
0 commit comments