本文介绍了使用 CloudWatch 指标筛选器报告内存消耗百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们广泛使用 AWS Lambda 作为我们基础设施的一部分.为了获得更好的可见性,我希望能够报告一个自定义 CloudWatch 指标,其值为 memory_used/memory_allocated,这可以使用 CloudWatch 日志和指标过滤器来实现.

考虑以下日志消息:

REPORT RequestId:de96230a-70c1-491f-97f5-f76805227173 持续时间:811.71 ms 计费持续时间:900 ms 内存大小:256 MB 已用最大内存:122 MB

对于上述消息,度量的计算值应为 122/256=0.476.

当时,我无法克服指标过滤器语法.对此的任何帮助将不胜感激.

解决方案

我认为您无法直接使用一个指标过滤器来完成.你可以这样做:

1.将 MemorySize 和 MaxMemoryUsed 提取为 2 个单独的指标

创建 2 个指标过滤器,都具有相同的规则 [type=REPORT, ...].这将过滤掉以 REPORT 开头的行,并将为该行的每一列创建变量.在这种情况下,它将创建变量 $type, $2, $3, ...$19 (日志行中有 19 个空格分隔的单词).>

您需要的值在变量 $13$18 中,因此在创建 2 个指标过滤器时,使用 $13 作为第一个指标,$18 用于 Metric value 字段中的第二个指标.指标名称和命名空间可以是任何你想要的.我将使用 MemorySizeMaxMemoryUsed 作为名称,使用 CustomLambdaMetrics 作为命名空间.

2.使用度量数学计算您需要的值.

既然您有 2 个指标,您可以使用这样的来源创建一个小部件以获取平均使用情况(您的情况可能因地区而异):

{指标":[[ { 表达式":m2/m1",标签":平均内存使用",id":e1";}],[ CustomLambdaMetrics", MemorySize", { id": m1", visible": false } ],[ ".", "MaxMemoryUsed", { "id": "m2", "visible": false }]],视图":时间序列",堆叠":假,地区":us-east-1",统计":总和",期间":60}

有关度量数学的更多信息,请参见此处:https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/using-metric-math.html

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.

解决方案

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

这篇关于使用 CloudWatch 指标筛选器报告内存消耗百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 06:54