Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cd9f064

Browse files
committedMar 26, 2024
Adding examples + doc
1 parent a36df37 commit cd9f064

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
 

‎docs/core/metrics.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@ If you'd like to remove them at some point, you can use `clear_default_dimension
131131
--8<-- "examples/metrics/src/set_default_dimensions_log_metrics.py"
132132
```
133133

134+
### Changing default timestamp
135+
136+
When creating metrics, we use the current timestamp. If you want to change the timestamp of all the metrics you create, utilize the `set_timestamp` function.
137+
138+
???+ info
139+
If you need to use different timestamps across multiple metrics, opt for [single_metric](#working-with-different-timestamp).
140+
141+
=== "set_custom_timestamp_log_metrics.py"
142+
143+
```python hl_lines="15"
144+
--8<-- "examples/metrics/src/set_custom_timestamp_log_metrics.py"
145+
```
146+
134147
### Flushing metrics
135148

136149
As you finish adding all your metrics, you need to serialize and flush them to standard output. You can do that automatically with the `log_metrics` decorator.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import datetime
2+
3+
from aws_lambda_powertools import Metrics
4+
from aws_lambda_powertools.metrics import MetricUnit
5+
from aws_lambda_powertools.utilities.typing import LambdaContext
6+
7+
metrics = Metrics()
8+
9+
10+
@metrics.log_metrics # ensures metrics are flushed upon request completion/failure
11+
def lambda_handler(event: dict, context: LambdaContext):
12+
metrics.add_metric(name="SuccessfulBooking", unit=MetricUnit.Count, value=1)
13+
14+
metric_timestamp = int((datetime.datetime.now() - datetime.timedelta(days=2)).timestamp() * 1000)
15+
metrics.set_timestamp(metric_timestamp)

0 commit comments

Comments
 (0)
Please sign in to comment.