Module: AsCombinedMetrics::Cli::CloudWatch

Defined in:
lib/as-combined-metrics/cloudwatch.rb

Instance Method Summary collapse

Instance Method Details

#fetch_metric(metric) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/as-combined-metrics/cloudwatch.rb', line 12

def fetch_metric(metric)
  logger.progname = "#{Module.nesting.first.to_s} #{__method__}"

  # Read from CloudWatch  
  data = {
      :start_time => (Time.now.utc - options[:period]).iso8601,
      :end_time   => (Time.now.utc).iso8601,
      :period     => options[:period]
  }

  # # removing the keys that are not used in fetch metric 
  metric = metric.merge(data).reject { |k,v| [:comparison_operator, :threshold, :aggregate_as_group].include? k }
  
  logger.info { set_color "Polling data for metric: #{metric[:metric_name]}", :white }
  logger.info { "Metric info: #{metric}" }
  
  begin
    result = @cw.get_metric_statistics(metric).to_hash
  rescue Exception => e
    logger.error { set_color "An error occured #{e}, SDK will retry", :red }
  end

  if result[:datapoints].empty?
    logger.info { set_color "No datapoints found for #{metric[:metric_name]} - Will publish a value of 0 (Don't DownScale) to CloudWatch", :red }
    logger.info { "Result: #{result}" }
    return -1
  else
    datapoint = result[:datapoints][0][metric[:statistics][0].downcase.to_sym]
    logger.info { set_color "Result: #{datapoint} #{result[:datapoints][0][:unit]}", :white }
    datapoint
  end
end

#publish_metric(mode, asg, combined_metric_value) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/as-combined-metrics/cloudwatch.rb', line 45

def publish_metric(mode, asg, combined_metric_value)
  logger.progname = "#{Module.nesting.first.to_s} #{__method__}"

  cw_options = {
    namespace: 'combined_metrics',
    metric_data: [
      metric_name: combined_metrics_name(mode),
       dimensions: [{
        name: 'AutoScalingGroupName',
        value: asg
      }],
      value: combined_metric_value,
      unit: 'None'
    ]
  }
  logger.info { set_color "Options to be sent to cloudwatch: #{cw_options}", :white }
  
  begin
    if options[:dryrun]
      logger.info { set_color "DryRun! Not Publishing Combined Metrics to CloudWatch...", :white }
    else
      logger.info { set_color "Publishing Combined Metrics to CloudWatch...", :white }
      @cw.put_metric_data(cw_options)
    end
  rescue Exception => e
    logger.error { set_color "An error occured #{e}, SDK will retry up to 3 times", :red }
  end
end

#set_default_options(hash, member) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/as-combined-metrics/cloudwatch.rb', line 3

def set_default_options(hash, member)
  logger.progname = "#{Module.nesting.first.to_s} #{__method__}"
  if hash[member] && !hash[member].nil?
    default = hash[member]
  else
    default =  self.config["default_options"][member.to_s.gsub("_", "-")] if !self.config["default_options"][member.to_s.gsub("_", "-")].nil?
  end
end