Sabo Boz
Sabo Boz

Reputation: 2575

cannot get cloudwatch metric data with getMetricStatistics

I currently have a node application where I am trying to use the getMetricStatistics function of the AWS Cloudwatch sdk to retrieve data from a metric into my application.

In order to troubleshoot this I have run the listMetrics function as follows:

var params = {
        MetricName: 'Open',
        Namespace: 'AWS/SES',
    };
    cloudwatch.listMetrics(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else     console.log(data);           // successful response
    });

After running the above code, I get output written via console.log:

Output of listMetrics

After this I try to run the getMetricStatistics function as follows:

    var cloudwatch = new aws.CloudWatch({apiVersion: '2010-08-01'});
    var params = {
        EndTime: new Date(2022,1,31), /* required */
        MetricName: 'Open', /* required */
        Namespace: 'AWS/SES', /* required */
        Period: '3600', /* required */
        StartTime: new Date(2022,1,27), /* required */
        Dimensions: [
          {
            Name: 'test-open-key', /* required */
            Value: 'test-open-value' /* required */
          }
          /* more items */
        ],
        Statistics: [
          'Average',
          /* more items */
        ]
      };
      cloudwatch.getMetricStatistics(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else     console.log(data);           // successful response
      });

However, the output of the above code is as follows, showing no datapoints: Output of 2nd code snippet

I have based my input parameters based on what I got from listMetrics, and in my console I can see the following graph, meaning there should be at least one datapoint retrieved on the 29th January.

Cloudwatch graph

Would anyone be able to advise on what I'm doing wrong/any further avenues for troubleshooting?

Upvotes: 0

Views: 1099

Answers (1)

When creating the date object the month is 0-indexed(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#several_ways_to_create_a_date_object)

Change your time values like this:

StartTime: new Date(2022,0,27)
EndTime: new Date(2022,0,31),

Upvotes: 1

Related Questions