Search code examples
amazon-web-servicesamazon-cloudwatchmetrics

How do I combine 2 search metrics with math expression in cloudwatch?


I am trying to get the percentage memory used when running a lambda to display in a graph on cloudwatch. I know there are other ways I can pull the data, but for reasons outside of the scope of this question, I would like to stick to using search to pull the metrics.

I have the following graph

{
    "metrics": [
        [ { "expression": "SEARCH('{SomeMetricNamespace} MetricName=\"MemorySize\"', 'Average', 300)", "id": "m1", "visible": "true" } ],
        [ { "expression": "SEARCH('{SomeMetricNamespace} MetricName=\"MaxMemoryUsed\"', 'Average', 300)", "id": "m2", "visible": "true" } ],
        [ { "expression": "m2/m1*100", "label": "pecentage memory used", "id": "e1", "stat": "Average" } ]
    ],
    "view": "timeSeries",
    "stacked": false,
    "region": "us-west-2",
    "stat": "Average",
    "period": 300,
    "title": "Memory",
    "yAxis": {
        "left": {
            "label": "Percentage Usage",
            "showUnits": false
        }
    },
    "liveData": false
}

The error I am getting

Error in expression e1 [Unsupported operand type(s) for /: '[Array[TimeSeries], Array[TimeSeries]]']

Is there a way to combine the first 2 expressions to give me the percentage memory used?


Solution

  • The result of the expressions are arrays of time series so you can not apply directly operations (+ - * / ^). As a workaround you could transform each time series into single values (average values) for each expression and then calculate the percentage.

    The source should be similar to this:

    {
    "metrics": [
        [ { "expression": "SEARCH('{SomeMetricNamespace} MetricName=\"MemorySize\"', 'Average', 300)", "id": "m1", "visible": "false" } ],
        [ { "expression": "SEARCH('{SomeMetricNamespace} MetricName=\"MaxMemoryUsed\"', 'Average', 300)", "id": "m2", "visible": "false" } ],
        [ { "expression": "AVG(m1)", "label": "AVGMemorySize", "id": "e1", "visible": "false" } ],
        [ { "expression": "AVG(m2)", "label": "AVGMaxMemoryUsed", "id": "e2", "visible": "false" } ],
        [ { "expression": "e2/e1*100", "label": "pecentage memory used", "id": "e3", "stat": "Average" } ]
    
        ],
        "view": "timeSeries",
        "stacked": false,
        "region": "us-west-2",
        "stat": "Average",
        "period": 300,
        "title": "Memory",
        "yAxis": {
            "left": {
                "label": "Percentage Usage",
                "showUnits": false
            }
        },
        "liveData": false
    }