-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metrics): add flush_metrics() method to allow manual flushing of…
… metrics (#2171) Co-authored-by: Heitor Lessa <lessa@amazon.co.uk> Co-authored-by: Leandro Damascena <leandro.damascena@gmail.com>
- Loading branch information
1 parent
14147cb
commit d4607f3
Showing
4 changed files
with
59 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 10 additions & 6 deletions
16
examples/metrics/src/manual_flush.py → examples/metrics/src/flush_metrics.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
import json | ||
|
||
from aws_lambda_powertools import Metrics | ||
from aws_lambda_powertools.metrics import MetricUnit | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
metrics = Metrics() | ||
|
||
|
||
def lambda_handler(event: dict, context: LambdaContext): | ||
def book_flight(flight_id: str, **kwargs): | ||
# logic to book flight | ||
... | ||
metrics.add_metric(name="SuccessfulBooking", unit=MetricUnit.Count, value=1) | ||
your_metrics_object = metrics.serialize_metric_set() | ||
metrics.clear_metrics() | ||
print(json.dumps(your_metrics_object)) | ||
|
||
|
||
def lambda_handler(event: dict, context: LambdaContext): | ||
try: | ||
book_flight(flight_id=event.get("flight_id", "")) | ||
finally: | ||
metrics.flush_metrics() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters