AWS CloudWatch GetMetricsData:“Sum”在没有值的时间段丢失点数

Mat*_*oli 1 amazon-web-services amazon-cloudwatch aws-sdk amazon-cloudwatch-metrics aws-sdk-js

GetMetricsData从 AWS CloudWatch查询:

                 {
                    StartTime: lastWeek ,
                    EndTime: today,
                    MetricDataQueries: [
                        {
                            Id: 'invocations',
                            Label: 'Invocations',
                            MetricStat: {
                                Metric: {
                                    Dimensions: [
                                        {
                                            Name: 'FunctionName',
                                            Value: /* FunctionName */,
                                        },
                                    ],
                                    MetricName: 'Invocations',
                                    Namespace: 'AWS/Lambda'
                                },
                                Period: 60*60*24, // day
                                Stat: 'Sum',
                                Unit: 'Count',
                            },
                        },
                    ],
                }
Run Code Online (Sandbox Code Playgroud)

这就是我得到的:

在此处输入图片说明

我没有获得 7 天(即一周)的数据,而是获得了 5 天。我错过了 2 天(如您在图表中所见)。

那些失踪的日子没有任何数据。

CloudWatch 不会返回没有数据的点。我怎样才能让Sum操作返回实际计数 ( 0) 呢?

Unk*_*nts 5

You can use metric math and FILL function to default missing values to 0.

Id of your metric is invocations so the expression would be:

FILL(invocations, 0) 
Run Code Online (Sandbox Code Playgroud)

Full query would be something like:

             {
                StartTime: lastWeek ,
                EndTime: today,
                MetricDataQueries: [
                    {
                        Id: 'result',
                        Label: 'Sums with zeros',
                        Expression: 'FILL(invocations, 0)'
                    },
                    {
                        Id: 'invocations',
                        Label: 'Invocations',
                        MetricStat: {
                            Metric: {
                                Dimensions: [
                                    {
                                        Name: 'FunctionName',
                                        Value: /* FunctionName */,
                                    },
                                ],
                                MetricName: 'Invocations',
                                Namespace: 'AWS/Lambda'
                            },
                            Period: 60*60*24, // day
                            Stat: 'Sum',
                            Unit: 'Count',
                        },
                    },
                ],
            }
Run Code Online (Sandbox Code Playgroud)

This will return 2 metrics, with zeros and without. You can then hide the original metric by setting ReturnData: false in that MetricDataQuery.

See here for more details: