Class: Weatherman::Report

Inherits:
Object
  • Object
show all
Includes:
AWS
Defined in:
lib/weatherman/report.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AWS

aws_access_key_id, aws_access_key_id=, aws_secret_access_key, aws_secret_access_key=, #cloud_watch, ec2_attributes, instance_id, region, region=

Constructor Details

#initialize(name, options = {}, &collector) ⇒ Report

Returns a new instance of Report.



14
15
16
17
18
19
20
21
22
# File 'lib/weatherman/report.rb', line 14

def initialize(name, options = {}, &collector)
  @name       = name
  @collector  = collector
  @namespace  = options[:namespace] || 'Custom/Weatherman'
  @period     = options[:period] || 12
  @dimensions = options[:dimensions] || {}

  @dimensions.merge! 'InstanceId' => AWS.instance_id
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/weatherman/report.rb', line 6

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/weatherman/report.rb', line 6

def options
  @options
end

Class Method Details

.run(name, options = {}, &collector) ⇒ Object



8
9
10
11
12
# File 'lib/weatherman/report.rb', line 8

def self.run(name, options = {}, &collector)
  report = new(name, options, &collector)
  report.run
  report
end

Instance Method Details

#broadcast(value) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/weatherman/report.rb', line 43

def broadcast(value)
  begin
    cloud_watch.put_metric_data @namespace, [{
      'MetricName' => @name,
      'Value'      => value,
      'Dimensions' => @dimensions.map { |k, v| { k.to_s => v } }
    }]
  rescue
    #TODO: Add loggging here, and throughout the gem
  end
end

#runObject



24
25
26
27
28
# File 'lib/weatherman/report.rb', line 24

def run
  @thread ||= Thread.new do
    broadcast sample_metric while sleep @period
  end
end

#running?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/weatherman/report.rb', line 35

def running?
  !@thread.nil?
end

#sample_metricObject



39
40
41
# File 'lib/weatherman/report.rb', line 39

def sample_metric
  @collector.call
end

#stopObject



30
31
32
33
# File 'lib/weatherman/report.rb', line 30

def stop
  @thread.kill if @thread
  @thread = nil
end