-
Notifications
You must be signed in to change notification settings - Fork 436
feat(metrics): allow custom timestamps for metrics #4006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
leandrodamascena
merged 11 commits into
aws-powertools:develop
from
leandrodamascena:cloudwatch-custom-timestamp
Mar 27, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6569743
Initial commit
leandrodamascena dbc9f2e
Refactoring logic + mypy still failling
leandrodamascena 1de4de9
Metrics should raise a warning when outside of constraints
leandrodamascena 45f9a85
Merge branch 'develop' into cloudwatch-custom-timestamp
leandrodamascena dee6313
Adding timestamp to single_metric
leandrodamascena c651b6f
Adding timestamp to single_metric
leandrodamascena 9c15b5b
Adding test for wrong type
leandrodamascena a36df37
Adding examples + doc
leandrodamascena cd9f064
Adding examples + doc
leandrodamascena 05d1efc
Wording + TZ
leandrodamascena cb85059
Improving doc
leandrodamascena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import datetime | ||
|
||
from aws_lambda_powertools import Metrics | ||
from aws_lambda_powertools.metrics import MetricUnit | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
metrics = Metrics() | ||
|
||
|
||
@metrics.log_metrics # ensures metrics are flushed upon request completion/failure | ||
def lambda_handler(event: dict, context: LambdaContext): | ||
metrics.add_metric(name="SuccessfulBooking", unit=MetricUnit.Count, value=1) | ||
|
||
metric_timestamp = int((datetime.datetime.now() - datetime.timedelta(days=2)).timestamp() * 1000) | ||
metrics.set_timestamp(metric_timestamp) |
18 changes: 18 additions & 0 deletions
18
examples/metrics/src/single_metric_with_different_timestamp.py
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from aws_lambda_powertools import Logger, single_metric | ||
from aws_lambda_powertools.metrics import MetricUnit | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
logger = Logger() | ||
|
||
|
||
def lambda_handler(event: dict, context: LambdaContext): | ||
|
||
for record in event: | ||
|
||
record_id: str = record.get("record_id") | ||
amount: int = record.get("amount") | ||
timestamp: int = record.get("timestamp") | ||
|
||
with single_metric(name="Orders", unit=MetricUnit.Count, value=amount, namespace="Powertools") as metric: | ||
logger.info(f"Processing record id {record_id}") | ||
metric.set_timestamp(timestamp) |
27 changes: 27 additions & 0 deletions
27
examples/metrics/src/single_metric_with_different_timestamp_payload.json
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[ | ||
{ | ||
"record_id": "6ba7b810-9dad-11d1-80b4-00c04fd430c8", | ||
"amount": 10, | ||
"timestamp": 1648195200000 | ||
}, | ||
{ | ||
"record_id": "6ba7b811-9dad-11d1-80b4-00c04fd430c8", | ||
"amount": 30, | ||
"timestamp": 1648224000000 | ||
}, | ||
{ | ||
"record_id": "6ba7b812-9dad-11d1-80b4-00c04fd430c8", | ||
"amount": 25, | ||
"timestamp": 1648209600000 | ||
}, | ||
{ | ||
"record_id": "6ba7b813-9dad-11d1-80b4-00c04fd430c8", | ||
"amount": 40, | ||
"timestamp": 1648177200000 | ||
}, | ||
{ | ||
"record_id": "6ba7b814-9dad-11d1-80b4-00c04fd430c8", | ||
"amount": 32, | ||
"timestamp": 1648216800000 | ||
} | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.