Skip to content

Commit a36df37

Browse files
Adding examples + doc
1 parent 9c15b5b commit a36df37

File tree

5 files changed

+68
-10
lines changed

5 files changed

+68
-10
lines changed

aws_lambda_powertools/metrics/base.py

-4
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,6 @@ def single_metric(
557557
resolution: MetricResolution | int = 60,
558558
namespace: str | None = None,
559559
default_dimensions: Dict[str, str] | None = None,
560-
timestamp: int | datetime.datetime | None = None,
561560
) -> Generator[SingleMetric, None, None]:
562561
"""Context manager to simplify creation of a single metric
563562
@@ -620,9 +619,6 @@ def single_metric(
620619
metric: SingleMetric = SingleMetric(namespace=namespace)
621620
metric.add_metric(name=name, unit=unit, value=value, resolution=resolution)
622621

623-
if timestamp:
624-
metric.set_timestamp(timestamp)
625-
626622
if default_dimensions:
627623
for dim_name, dim_value in default_dimensions.items():
628624
metric.add_dimension(name=dim_name, value=dim_value)

docs/core/metrics.md

+22-5
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,15 @@ You can add high-cardinality data as part of your Metrics log with `add_metadata
224224
--8<-- "examples/metrics/src/add_metadata_output.json"
225225
```
226226

227-
### Single metric with a different dimension
227+
### Single metric
228228

229-
CloudWatch EMF uses the same dimensions across all your metrics. Use `single_metric` if you have a metric that should have different dimensions.
229+
CloudWatch EMF uses the same dimensions and timestamp across all your metrics. Use `single_metric` if you have a metric that should have different dimensions or timestamp.
230230

231-
???+ info
232-
Generally, this would be an edge case since you [pay for unique metric](https://aws.amazon.com/cloudwatch/pricing){target="_blank"}. Keep the following formula in mind:
231+
#### Working with different dimensions
232+
233+
Generally, using different dimensions would be an edge case since you [pay for unique metric](https://aws.amazon.com/cloudwatch/pricing){target="_blank"}.
233234

234-
**unique metric = (metric_name + dimension_name + dimension_value)**
235+
Keep the following formula in mind: **unique metric = (metric_name + dimension_name + dimension_value)**
235236

236237
=== "single_metric.py"
237238

@@ -259,6 +260,22 @@ By default it will skip all previously defined dimensions including default dime
259260
--8<-- "examples/metrics/src/single_metric_default_dimensions.py"
260261
```
261262

263+
#### Working with different timestamp
264+
265+
When working with multiple metrics, customers may need different timestamps between them. In such cases, utilize `single_metric` to flush individual metrics with specific timestamps.
266+
267+
=== "single_metric_with_different_timestamp.py"
268+
269+
```python hl_lines="15 17"
270+
--8<-- "examples/metrics/src/single_metric_with_different_timestamp.py"
271+
```
272+
273+
=== "single_metric_with_different_timestamp_payload.json"
274+
275+
```json hl_lines="5 10 15 20 25"
276+
--8<-- "examples/metrics/src/single_metric_with_different_timestamp_payload.json"
277+
```
278+
262279
### Flushing metrics manually
263280

264281
If you are using the [AWS Lambda Web Adapter](https://github.com/awslabs/aws-lambda-web-adapter){target="_blank"} project, or a middleware with custom metric logic, you can use `flush_metrics()`. This method will serialize, print metrics available to standard output, and clear in-memory metrics data.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from aws_lambda_powertools import Logger, single_metric
2+
from aws_lambda_powertools.metrics import MetricUnit
3+
from aws_lambda_powertools.utilities.typing import LambdaContext
4+
5+
logger = Logger()
6+
7+
8+
def lambda_handler(event: dict, context: LambdaContext):
9+
10+
for record in event:
11+
12+
record_id: str = record.get("record_id")
13+
amount: int = record.get("amount")
14+
timestamp: int = record.get("timestamp")
15+
16+
with single_metric(name="Orders", unit=MetricUnit.Count, value=amount, namespace="Powertools") as metric:
17+
logger.info(f"Processing record id {record_id}")
18+
metric.set_timestamp(timestamp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[
2+
{
3+
"record_id": "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
4+
"amount": 10,
5+
"timestamp": 1648195200000
6+
},
7+
{
8+
"record_id": "6ba7b811-9dad-11d1-80b4-00c04fd430c8",
9+
"amount": 30,
10+
"timestamp": 1648224000000
11+
},
12+
{
13+
"record_id": "6ba7b812-9dad-11d1-80b4-00c04fd430c8",
14+
"amount": 25,
15+
"timestamp": 1648209600000
16+
},
17+
{
18+
"record_id": "6ba7b813-9dad-11d1-80b4-00c04fd430c8",
19+
"amount": 40,
20+
"timestamp": 1648177200000
21+
},
22+
{
23+
"record_id": "6ba7b814-9dad-11d1-80b4-00c04fd430c8",
24+
"amount": 32,
25+
"timestamp": 1648216800000
26+
}
27+
]

tests/functional/metrics/test_metrics_cloudwatch_emf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ def test_single_metric_with_custom_timestamp(capsys, metric, dimension, namespac
158158
namespace=namespace,
159159
default_dimensions=default_dimensions,
160160
**metric,
161-
timestamp=timestamp,
162161
) as my_metric:
162+
my_metric.set_timestamp(timestamp)
163163
my_metric.add_metric(name="second_metric", unit="Count", value=1)
164164

165165
output = capture_metrics_output(capsys)

0 commit comments

Comments
 (0)