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/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