Search code examples
amazon-web-servicesaws-sdkamazon-cloudwatchaws-sdk-jsamazon-cloudwatch-metrics

AWS CloudWatch GetMetricsData: "Sum" misses points on periods without values


I am querying GetMetricsData from 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',
                            },
                        },
                    ],
                }

This is what I get:

enter image description here

Instead of getting data for 7 days (i.e. a week) I get 5 days. I have 2 missing days (as you can see in the graph).

Those missing days did not have any data.

CloudWatch is not returning points which have no data. How can I make the Sum operation return the actual count (0) instead?


Solution

  • 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) 
    

    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',
                            },
                        },
                    ],
                }
    

    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: