Skip to content

Feature request: allow custom timestamps for metrics #3429

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

Closed
2 tasks done
pseidel-kcf opened this issue Nov 28, 2023 · 10 comments · Fixed by #4006
Closed
2 tasks done

Feature request: allow custom timestamps for metrics #3429

pseidel-kcf opened this issue Nov 28, 2023 · 10 comments · Fixed by #4006
Assignees
Labels
feature-request feature request help wanted Could use a second pair of eyes/hands metrics

Comments

@pseidel-kcf
Copy link

pseidel-kcf commented Nov 28, 2023

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

@pseidel-kcf pseidel-kcf added feature-request feature request triage Pending triage from maintainers labels Nov 28, 2023
Copy link

boring-cyborg bot commented Nov 28, 2023

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@pseidel-kcf pseidel-kcf changed the title Feature request: allow custom time for metrics Feature request: allow custom timestamps for metrics Nov 28, 2023
@heitorlessa
Copy link
Contributor

heitorlessa commented Nov 30, 2023 via email

@rubenfonseca
Copy link
Contributor

@pseidel-kcf just checking to see if you would like to work on this one?

@rubenfonseca rubenfonseca added help wanted Could use a second pair of eyes/hands and removed triage Pending triage from maintainers labels Dec 19, 2023
@rubenfonseca rubenfonseca moved this from Triage to Ideas in Powertools for AWS Lambda (Python) Dec 19, 2023
@pseidel-kcf
Copy link
Author

Thanks for the follow up.

I don't have capacity to work on this.

This is no longer a blocker for me because the external source from my example is implementing a built-in way to export metrics.

@rubenfonseca
Copy link
Contributor

No problem! We'll keep it in the backlog to see if there's more interest in this feature. Thank you so much for opening this!

@MatasNed
Copy link

@rubenfonseca Would it be possible for me to contribute to this? I'd be interested. Let me know

@leandrodamascena
Copy link
Contributor

Hey everyone! I submitted a PullRequest to add this new feature, but I have some questions.

1 - Timestamp type

According to the AWS Documentation timestamp is a number representing the time stamp used for metrics extracted from the event. Values MUST be expressed as the number of milliseconds after Jan 1, 1970 00:00:00 UTC.

That said, should we only allow int inputs with epoch value, or should customers be able to pass datetime objects and have them converted internally?

2 - Invalid values

If a customer passes an invalid number, we will emit a warning and fallback to the current timestamp instead of raising an exception.

I'm still working on the pull request and I expect to have it merged by next week.

Thank you.

@rubenfonseca
Copy link
Contributor

1 - I agree that we should start simple with "int", as we can always overload the function later if there's the demand for other types

Great job picking this up Leandro

Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added the pending-release Fix or implementation already in dev waiting to be released label Mar 27, 2024
Copy link
Contributor

This is now released under 2.36.0 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Mar 27, 2024
@leandrodamascena leandrodamascena moved this from Coming soon to Shipped in Powertools for AWS Lambda (Python) Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request feature request help wanted Could use a second pair of eyes/hands metrics
Projects
Status: Shipped
Development

Successfully merging a pull request may close this issue.

5 participants