Skip to content

Trying to resolve errors in CloudWatch Dashboards widget expression e6 = (e4/e2-e4)*100 where e4 is DIFF(e2) and e2 is (METRICS())

0

Hopefully the title conveys my struggles trying to bundle things up to save creating dozens of expressions long hand to get DIFF expressed as a percentage instead of an actual number. The error in e6 is Unsupported operand type(s).

Essentially I have many metrics, a lot of which will have no data points. If I reference my metrics directly in an exploded version of expression e6 it will work fine, e.g. (DIFF(m1)/(m1-DIFF(m1)))*100. For context I'm using a time range of 14 days, the graphed metrics work on Sample Count statistic with 7 days period to calculate the difference between the most recent week as a percentage of the sample count for the previous week.

Is there any way to handle the differences for each metric within a collective expression that uses the (DIFF(m1)/(m1-DIFF(m1)))*100 logic to render them as percentage values. The collective logic works for absolute differences i.e. e4 is defined as DIFF(e2) where e2 is defined as METRICS()). No matter how I structure my input expressions things fail when trying to apply my percentage logic. My instinct tells me the problem lies in wrapping a DIFF up within a DIFF, but I can't determine whether there's a workaround or my aim here is futile and I'll need to create dozens of expressions using my metrics directly.

1 Answer
0

The issue you're encountering is related to how CloudWatch metric math handles operations on collections of metrics versus individual metrics. When you use METRICS() to create a collection and then apply DIFF() to it, you're working with a collection of time series data. The problem occurs when trying to perform arithmetic operations between collections and individual metrics or between different collections.

For your specific case, where you want to calculate the percentage difference between the current week and previous week, there isn't a direct way to apply the formula (DIFF(m1)/(m1-DIFF(m1)))*100 to a collection of metrics using METRICS().

Instead, you'll need to create individual expressions for each metric. While this might seem tedious, it's the most reliable approach given CloudWatch's metric math limitations.

If you're working with many metrics, consider:

  1. Using the CloudWatch API to programmatically create your dashboard with all the required expressions
  2. Creating a template dashboard with a few key metrics and duplicating the pattern as needed
  3. Using CloudWatch Metric Insights to query and filter your metrics before applying math expressions

The IF function might be useful in cases where you need to handle missing data points, as it can substitute values when certain conditions are met. For example:

IF(metric1, metric2, metric3)

This would use values from metric2 when metric1 is non-zero and non-missing, and values from metric3 otherwise.

Unfortunately, there's no direct shortcut to apply complex percentage calculations across a collection of metrics in a single expression. You'll need to create individual expressions for each metric you want to analyze.
Sources
Using math expressions with CloudWatch metrics - Amazon CloudWatch
Create a target tracking policy using high-resolution metrics for faster response - Amazon EC2 Auto Scaling

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.