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
* 'develop' of https://github.com/awslabs/aws-lambda-powertools-python:
chore: correct docstring for log_metrics
chore: fix typo in metrics doc
chore: Correct test comment
chore: remove unused import
chore: formatting
feat: update Metrics interface to resemble tracer & logger: use "service" as its namespace.
We recommend you use your application or main service as a metric namespace.
34
+
You can explicitly set a namespace name via `service` param or via `POWERTOOLS_SERVICE_NAME` env var. This sets **namespace** key that will be used for all metrics.
34
35
35
36
```python:title=app.py
36
37
from aws_lambda_powertools.metrics import Metrics, MetricUnit
37
38
38
-
metrics = Metrics()
39
-
# metrics.add_namespace("ServerlessAirline") # optionally if you set via env var
39
+
# POWERTOOLS_SERVICE_NAME defined
40
+
metrics = Metrics() # highlight-line
41
+
42
+
# Explicit definition
43
+
Metrics(service="ServerlessAirline") # sets namespace to "ServerlessAirline"
44
+
45
+
40
46
```
41
47
42
48
You can initialize Metrics anywhere in your code as many time as you need - It'll keep track of your aggregate metrics in memory.
@@ -48,7 +54,7 @@ You can create metrics using `add_metric`, and set dimensions for all your aggre
48
54
```python:title=app.py
49
55
from aws_lambda_powertools.metrics import Metrics, MetricUnit
Use `POWERTOOLS_METRICS_NAMESPACE` env var when unit testing your code to ensure a metric namespace object is created, and your code doesn't fail validation.
152
+
Use `POWERTOOLS_SERVICE_NAME` env var when unit testing your code to ensure a metric namespace object is created, and your code doesn't fail validation.
You can ignore that if you are explicitly creating metric namespace within your own code `metrics.add_namespace()`.
158
+
You can ignore this if you are explicitly setting namespace by passing a service name when initializing Metrics: `metrics = Metrics(service=ServiceName)`.
- Both are necessary because `app.py` initializes them in the global scope, since both Tracer and Metrics will be initialized and configured during import time. For unit tests, we could always patch and explicitly config but env vars do just fine for this example.
0 commit comments