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.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/metriks-addons/cloudwatch_reporter.rb', line 26

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.



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

def cw
  @cw
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#prefixObject

Returns the value of attribute prefix.



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

def prefix
  @prefix
end

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

#tagsObject

Returns the value of attribute tags.



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

def tags
  @tags
end

Instance Method Details

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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/metriks-addons/cloudwatch_reporter.rb', line 103

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



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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/metriks-addons/cloudwatch_reporter.rb', line 46

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



133
134
135
136
137
138
139
140
141
142
# File 'lib/metriks-addons/cloudwatch_reporter.rb', line 133

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

#submit(datapoints) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/metriks-addons/cloudwatch_reporter.rb', line 38

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