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:
improv: include example tests in `make tests` (#63)
chore: rename Makefile target docs-dev to docs-local (#65)
improv: better namespace/dimension handling for Metrics (#62)
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
40
-
metrics = Metrics() # highlight-line
41
+
#POWERTOOLS_METRICS_NAMESPACE and POWERTOOLS_SERVICE_NAME defined
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)`.
Copy file name to clipboardExpand all lines: example/README.md
+5-5
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Summary
2
2
3
-
This example uses both [tracing](https://github.com/awslabs/aws-lambda-powertools/tree/develop/python#tracing)and [logging](https://github.com/awslabs/aws-lambda-powertools/tree/develop/python#logging) features, includes all environment variables that can be used, and demonstrates how to explicitly disable tracing while running unit tests - That is not necessary when running within SAM CLI as it detects the local env automatically.
3
+
This example uses [tracer](https://awslabs.github.io/aws-lambda-powertools-python/core/tracer/), [metrics](https://awslabs.github.io/aws-lambda-powertools-python/core/metrics/),and [logger](https://awslabs.github.io/aws-lambda-powertools-python/core/logger/) features, includes all environment variables that can be used, and demonstrates how to explicitly disable tracing while running unit tests - That is not necessary when running within SAM CLI as it detects the local env automatically.
4
4
5
5
**Quick commands**
6
6
@@ -9,9 +9,9 @@ This example uses both [tracing](https://github.com/awslabs/aws-lambda-powertool
9
9
***Deploy**: `sam deploy --guided`
10
10
***Unit Tests**: We recommend proceeding with the following commands in a virtual environment
-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.
15
15
16
16
# Example code
17
17
@@ -118,7 +118,7 @@ Tests are defined in the `tests` folder in this project. Use PIP to install the
0 commit comments