Description
Use case
I'm creating a lambda to report metrics from an external source to Cloudwatch for observability within AWS. I'm looking for a way to set a custom timestamp for the metrics emitted each invocation.
The metrics I'm querying from the external source will basically be a list of (metric name, value) tuples for a specific time. This specific time will be in the recent past, maybe a few minutes old, but I'm not sure that matters here.
I can easily add the key and values to metrics, however I cannot set an explicit time because Metrics does not expose a way and the the time ultimately always set to datetime.now()
when rendered to emf.
Solution/User Experience
I'm imagining a simple api to set the timestamp on the Metrics object. If a timestamp is not specific then the current time should be used.
metrics = Metrics()
@metrics.log_metrics
def lambda_handler(event: dict, context: LambdaContext):
time = ...
metric_values: list[(string, string)] = ...
for (name, value) in metric_values:
metrics.add_metric(name=name unit=MetricUnit.Count, value=value)
metrics.set_timestamp(time) # <---
Alternative solutions
aws-embedded-metrics-python supports setting the timestamp on their MetricLogger class.
Acknowledgment
- This feature request meets Powertools for AWS Lambda (Python) Tenets
- Should this be considered in other Powertools for AWS Lambda languages? i.e. Java, TypeScript, and .NET
Metadata
Metadata
Assignees
Type
Projects
Status