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
improv: better namespace/dimension handling for Metrics (#62)
* improv: namespace/dimension changes
Use service name for default dimension instead of namespace
Add namespace parameter
* docs: document changes to namespace/service
* chore: fix typo in docstring
* chore: fix typo in docs
* chore: fix test comments
* chore: Add env var for namespace back into example app
* chore: correct typo in docs
* chore: update example in README
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.
35
+
You can explicitly set a namespace name via `namespace` param or via `POWERTOOLS_METRICS_NAMESPACE` env var. This sets **namespace** key that will be used for all metrics.
36
+
You can also pass a service name via `service` param or `POWERTOOLS_SERVICE_NAME` env var. This will create a dimension with the service name.
35
37
36
38
```python:title=app.py
37
39
from aws_lambda_powertools.metrics import Metrics, MetricUnit
38
40
39
-
# POWERTOOLS_SERVICE_NAME defined
41
+
#POWERTOOLS_METRICS_NAMESPACE and POWERTOOLS_SERVICE_NAME defined
40
42
metrics = Metrics() # highlight-line
41
43
42
44
# Explicit definition
43
-
Metrics(service="ServerlessAirline") #sets namespace to "ServerlessAirline"
45
+
Metrics(namespace="ServerlessAirline", service="orders") #creates a default dimension {"service": "orders"} under the namespace "ServerlessAirline"
44
46
45
47
46
48
```
47
49
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.
50
+
You can initialize Metrics anywhere in your code as many times as you need - It'll keep track of your aggregate metrics in memory.
49
51
50
52
## Creating metrics
51
53
52
-
You can create metrics using `add_metric`, and set dimensions for all your aggregate metrics using `add_dimension`.
54
+
You can create metrics using `add_metric`, and manually create dimensions for all your aggregate metrics using `add_dimension`.
53
55
54
56
```python:title=app.py
55
57
from aws_lambda_powertools.metrics import Metrics, MetricUnit
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.
152
+
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 namespaceby passing a service name when initializing Metrics: `metrics = Metrics(service=ServiceName)`.
159
+
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)`.
-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.
12
+
-**Run tests with namespace and service set, and tracing disabled**
-These 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