Skip to content

Conversation

sleepdeprecation
Copy link
Contributor

I am writing some code that uses the Cloudwatch GetMetricData API's math expression feature, specifically in trying to add two metrics together.

This PR adds support for adding two metrics together using the expression feature.

There are some additional features and validation I would like to add to this (specifically, checking that there aren't any other special characters, so that m1 + m2 - m3 would fail, and the ability to add more than two metrics together, ex m1 + m2 + m3), but I wanted to open the PR at this point to see if this feature would be considered useful and check if this seems like a reasonable approach to the problem.

Copy link

codecov bot commented Dec 17, 2024

Codecov Report

Attention: Patch coverage is 92.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 94.59%. Comparing base (3c4c72e) to head (c22754c).

Files with missing lines Patch % Lines
moto/cloudwatch/metric_data_expression_parser.py 92.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8416      +/-   ##
==========================================
- Coverage   94.59%   94.59%   -0.01%     
==========================================
  Files        1163     1163              
  Lines      101636   101661      +25     
==========================================
+ Hits        96147    96170      +23     
- Misses       5489     5491       +2     
Flag Coverage Δ
servertests 28.85% <92.00%> (+0.01%) ⬆️
unittests 94.57% <92.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@bblommers bblommers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @sleepdeprecation, thank you for the PR!

Looking at the docs, these expressions can get quite advanced - so I don't think that this is a long-term solution.

If you mostly want the simple addition for now, then I'm willing to merge it (after the comments are addressed), but future extensions should use a proper parser, as that will make it much easier to extend/maintain the solution.

There are quite a few parsers out there, but my preference would be to use pyparsing, just because we're already using it in the Glue-module (see here) - so if we use that we don't have to add yet another dependency.

expression = expression.replace(" ", "")
plus_splits = expression.split("+")
if len(plus_splits) != 2:
raise ValidationError(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should fail silently. The result may be wrong, but that's preferable to throwing an exception and completely stopping people from using this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants