Search code examples
amazon-web-servicesamazon-cloudwatchamazon-cloudwatchlogs

Using CloudWatch Metric Filter to report memory consumption percentage


We have wide use of AWS Lambda as part of our infrastructure. To gain better visibility, I'd like to be able to report a custom CloudWatch metric which value is memory_used/memory_allocated, this can be achieved using CloudWatch logs and metric filter.

Consider the following log message:

REPORT RequestId: de96230a-70c1-491f-97f5-f76805227173  Duration: 811.71 ms Billed Duration: 900 ms Memory Size: 256 MB Max Memory Used: 122 MB

For the above message the calculated value for the metric should be 122/256=0.476.

At the time, I couldn't overcome the metric filter syntax. Any help with this would be highly appreciated.


Solution

  • I don't think you can do it directly with one metric filter. You could do something like this:

    1. Extract MemorySize and MaxMemoryUsed into 2 separate metrics

    Create 2 metric filters, both with the same rule [type=REPORT, ...]. This will filter out lines that start with REPORT and it will create variables for each column of the line. In this case, it will create variables $type, $2, $3, ... up to $19 (there are 19 space-separated words in the logline).

    Values you need are in variables $13 and $18, so when creating the 2 metric filters, use $13 for the first metric and $18 for the second metric in the Metric value field. Metric names and namespace can be whatever you want. I'll use MemorySize and MaxMemoryUsed for names and CustomLambdaMetrics as the namespace.

    2. Use metric math to calculate the value you need.

    Now that you have the 2 metrics, you can create a widget with a source like this to get the average usage (region may be different in your case):

    {
        "metrics": [
            [ { "expression": "m2/m1", "label": "Average memory usage", "id": "e1" } ],
            [ "CustomLambdaMetrics", "MemorySize", { "id": "m1", "visible": false } ],
            [ ".", "MaxMemoryUsed", { "id": "m2", "visible": false } ]
        ],
        "view": "timeSeries",
        "stacked": false,
        "region": "us-east-1",
        "stat": "Sum",
        "period": 60
    }
    

    See here for more info on metric math: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/using-metric-math.html