Class: Ruboty::SqsMonitor::Aws::CloudWatch

Inherits:
Object
  • Object
show all
Defined in:
lib/ruboty/sqs_monitor/aws/cloudwatch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue_name, options) ⇒ CloudWatch

Returns a new instance of CloudWatch.



7
8
9
10
11
12
13
14
# File 'lib/ruboty/sqs_monitor/aws/cloudwatch.rb', line 7

def initialize(queue_name, options)
  @queue_name = queue_name
  @start_time = options[:start_time]
  @end_time   = options[:end_time]

  opts = options.select {|k, v| CLIENT_OPTIONS.include? k.to_sym }
  @client = ::Aws::CloudWatch::Client.new(opts)
end

Instance Attribute Details

#end_timeObject (readonly)

Returns the value of attribute end_time.



5
6
7
# File 'lib/ruboty/sqs_monitor/aws/cloudwatch.rb', line 5

def end_time
  @end_time
end

#queue_nameObject (readonly)

Returns the value of attribute queue_name.



5
6
7
# File 'lib/ruboty/sqs_monitor/aws/cloudwatch.rb', line 5

def queue_name
  @queue_name
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



5
6
7
# File 'lib/ruboty/sqs_monitor/aws/cloudwatch.rb', line 5

def start_time
  @start_time
end

Instance Method Details

#get_statistics(metric_name, stats: nil, start_time: nil, end_time: nil, period: nil) ⇒ Object



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
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ruboty/sqs_monitor/aws/cloudwatch.rb', line 16

def get_statistics(metric_name,
                   stats:      nil,
                   start_time: nil,
                   end_time:   nil,
                   period:     nil)
  stats      ||= default_stats(metric_name)
  end_time   ||= @end_time   || Time.now
  start_time ||= @start_time || (end_time - 3600)
  period     ||= end_time - start_time

  opts = {
    namespace:   "AWS/SQS",
    metric_name: metric_name,
    dimensions:  [ {name: "QueueName", value: @queue_name} ],
    start_time:  start_time,
    end_time:    end_time,
    period:      period.to_i,
    statistics:  [stats]
  }
  resp = @client.get_metric_statistics(opts)

  datapoints = default_datapoints(start_time, end_time, period)
  resp.datapoints.each do |datapoint|
    ts = datapoint.timestamp
    dp = datapoints[ts.to_i]
    dp.value = datapoint_to_value(datapoint)
    dp.unit  = datapoint.unit
    dp.freeze
  end

  OpenStruct.new(
    queue_name: @queue_name,
    start_time: start_time,
    end_time:   end_time,
    metric:     resp.label,
    stats:      stats,
    label:      "#{resp.label}(#{stats})",
    datapoints: datapoints.sort_by{|k,v| k}.map(&:last)
  ).freeze
end