Class: MetriksAddons::CloudWatchReporter

Inherits:
BaseReporter show all
Defined in:
lib/metriks-addons/cloudwatch_reporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseReporter

#flush, #log, #restart, #start, #stop

Constructor Details

#initialize(access_key, secret_key, namespace, tags, options = {}) ⇒ CloudWatchReporter

Returns a new instance of CloudWatchReporter.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/metriks-addons/cloudwatch_reporter.rb', line 11

def initialize(access_key, secret_key, namespace, tags, options = {})
  super(options)
  @cw = AWS::CloudWatch.new(:access_key_id => access_key, :secret_access_key => secret_key)
  @namespace = namespace
  @dimensions = get_dimensions(tags)

  @prefix = options[:prefix]
  @source = options[:source]

  @batch_size   = options[:batch_size] || 50
end

Instance Attribute Details

#cwObject

Returns the value of attribute cw.



9
10
11
# File 'lib/metriks-addons/cloudwatch_reporter.rb', line 9

def cw
  @cw
end

#loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/metriks-addons/cloudwatch_reporter.rb', line 9

def logger
  @logger
end

#prefixObject

Returns the value of attribute prefix.



9
10
11
# File 'lib/metriks-addons/cloudwatch_reporter.rb', line 9

def prefix
  @prefix
end

#sourceObject

Returns the value of attribute source.



9
10
11
# File 'lib/metriks-addons/cloudwatch_reporter.rb', line 9

def source
  @source
end

#tagsObject

Returns the value of attribute tags.



9
10
11
# File 'lib/metriks-addons/cloudwatch_reporter.rb', line 9

def tags
  @tags
end

Instance Method Details

#create_datapoints(base_name, metric, time, keys, keys_unit, snapshot_keys = [], snapshot_keys_unit = []) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/metriks-addons/cloudwatch_reporter.rb', line 88

def create_datapoints(base_name, metric, time, keys, keys_unit, snapshot_keys = [], snapshot_keys_unit = [])
  datapoints = []

  keys.flatten.zip(keys_unit.flatten).each do |key, key_unit|
    name = key.to_s.gsub(/^get_/, '')
    datapoints << {
      :metric_name => "#{base_name}.#{name}",
      :timestamp => time,
      :value => metric.send(key),
      :dimensions => @dimensions,
      :unit => key_unit
    }
  end

  unless snapshot_keys.empty?
    snapshot = metric.snapshot
    snapshot_keys.flatten.zip(snapshot_keys_unit.flatten).each do |key, key_unit|
      name = key.to_s.gsub(/^get_/, '')
      datapoints << {
        :metric_name => "#{base_name}.#{name}",
        :timestamp => time,
        :value => snapshot.send(key),
        :dimensions => @dimensions,
        :unit => key_unit
      }
    end
  end
  datapoints
end

#get_datapointsObject



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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/metriks-addons/cloudwatch_reporter.rb', line 31

def get_datapoints
  time = Time.at(@time_tracker.now_floored).iso8601

  datapoints = []
  @registry.each do |name, metric|
    next if name.nil? || name.empty?
    name = name.to_s.gsub(/ +/, '_')
    if @prefix
      name = "#{@prefix}.#{name}"
    end

    case metric
    when Metriks::Meter
      datapoints |= create_datapoints name, metric, time, [
        :count, :mean_rate
      ], [
        'Count', 'Count/Second'
      ]
    when Metriks::Counter
      datapoints |= create_datapoints name, metric, time, [
        :count
      ], [
        'Count'
      ]
    when Metriks::Gauge
      datapoints |= create_datapoints name, metric, time, [
        :value
      ], [
        'Count'
      ]
    when Metriks::Timer
      datapoints |= create_datapoints name, metric, time, [
        :count, :mean_rate, :min, :max, :mean, :stddev
      ], [
        'Count', 'Count/Second', 'Seconds', 'Seconds',
        'Seconds', 'Seconds'
      ], [
        :median
      ], [
        'Seconds'
      ]
    when Metriks::Histogram
      datapoints |= create_datapoints name, metric, time, [
        :count, :min, :max, :mean, :stddev
      ], [
        'Count', 'Count', 'Count', 'Count',
        'Count'
      ], [
        :median
      ], [
        'Count'
      ]
    end
  end
  datapoints
end

#get_dimensions(tags) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/metriks-addons/cloudwatch_reporter.rb', line 118

def get_dimensions(tags)
  dimensions =[]
  tags.each do |name, value|
    dimensions << {
      :name => "#{name}",
      :value => value
    }
  end
  dimensions
end

#submit(datapoints) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/metriks-addons/cloudwatch_reporter.rb', line 23

def submit(datapoints)
  return if datapoints.empty?
  datapoints.each do |datapoint|
    response = @cw.put_metric_data({:namespace => @namespace, :metric_data => [datapoint]})
  end
  log "info", "Sent #{datapoints.size} metrics to CloudWatch"
end