Search code examples
ruby-on-railsrubyamazon-cloudwatchaws-sdk-ruby

How to get Ruby SDK Cloudwatch metics


I am trying to get CPU metrics from cloudwatch using the Ruby SDK v2 in Rails.

metric = Aws::CloudWatch::Metric.new( 'AWS/EC2', 'CPUUtilization', region: 'eu-west-1')


@stats = metric.get_statistics({
  dimensions: [{ :name => 'InstanceID', :value => 'i-14b21212' }],
  start_time: 2.days.ago.utc.iso8601,
  end_time: Time.now.utc.iso8601,
  period: 300,
  statistics: ["Average"]

})

This doesn't error, but doesn't return any data points. I have try this with an instance with advance monitoring enabled and one with out and nothing.

The above returns:

[Aws::CloudWatch::Client 200 0.639127 0 retries] get_metric_statistics(dimensions:[{name:"InstanceID",value:"[FILTERED]"}],start_time:2016-12-13 17:17:53 UTC,end_time:2016-12-15 17:17:53 UTC,period:300,statistics:["Average"],namespace:"AWS/EC2",metric_name:"CPUUtilization")

=> #<struct Aws::CloudWatch::Types::GetMetricStatisticsOutput label="CPUUtilization", datapoints=[]>

I have also tried this and still get nothing:

metric = Aws::CloudWatch::Metric.new( 'AWS/EC2', 'CPUUtilization', region: 'eu-west-1')


@stats = metric.get_statistics({
  dimensions: [{ :name => 'InstanceID', :value => 'i-14b21212' }],
  start_time: 2.days.ago.utc.iso8601,
  end_time: Time.now.utc.iso8601,
  period: 300,
  extended_statistics: ["p10", "p20", "p30", "p40", "p50", "p60", "p70", "p80", "p90", "p100"]

})

Any help on what I am doing wrong would be great!


Solution

  • According to documentation:

    Metric filters are case sensitive.

    So this would be the correct call:

    dimensions: [{ :name => 'InstanceId', :value => 'i-14b21212' }],