Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show CloudWatch metric with unit Seconds in Hours

I have a custom cloud watch metric with unit Seconds. (representing the age of a cache)

As usual values are around 125,000 I'd like to convert them into Hours - for better readability.

Is that possible?

like image 727
Niko Sams Avatar asked Dec 07 '22 09:12

Niko Sams


1 Answers

This has changed with the addition of Metrics Math. You can do all sorts of transformations on your data, both manually (from the console) and from CloudFormation dashboard templates.


From the console: see the link above, which says:

To add a math expression to a graph

  1. Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/.

  2. Create or edit a graph or line widget.

  3. Choose Graphed metrics.

  4. Choose Add a math expression. A new line appears for the expression.

  5. For the Details column, type the math expression. The tables in the following section list the functions you can use in the expression.

    To use a metric or the result of another expression as part of the formula for this expression, use the value shown in the Id column. For example, m1+m2 or e1-MIN(e1).


From a CloudFormation Template

You can add new metrics which are Metrics Math expressions, transforming existing metrics. You can add, subtract, multiply, etc. metrics and scalars. In your case, you probably just want to use divide, like in this example:

Say you have the following bucket request latency metrics object in your template:

"metrics":[
  ["AWS/S3","TotalRequestLatency","BucketName","MyBucketName"]
]

The latency default is in milliseconds. Let's plot it in seconds, just for fun. 1s = 1,000ms so we'll add the following:

"metrics":[
  ["AWS/S3","TotalRequestLatency","BucketName","MyBucketName",{"id": "timeInMillis"}],
  [{"expression":"timeInMillis / 1000", "label":"LatencyInSeconds","id":"timeInSeconds"}]
]

Note that the expression has access to the ID of the other metrics. Helpful naming can be useful when things get more complicated, but the key thing is just to match the variables you put in the expression to the ID you assign to the corresponding metric.

This leaves us with a graph with two metrics on it: one milliseconds, the other seconds. If we want to lose the milliseconds, we can, but we need to keep the metric values around to compute the math expression, so we use the following work-around:

"metrics":[
  ["AWS/S3","TotalRequestLatency","BucketName","MyBucketName",{"id": "timeInMillis","visible":false}],
  [{"expression":"timeInMillis / 1000", "label":"LatencyInSeconds","id":"timeInSeconds"}]
]

Making the metric invisible takes it off the graph while still allowing us to compute our expression off of it.

like image 98
MyStackRunnethOver Avatar answered Jan 04 '23 17:01

MyStackRunnethOver