Class: ChefMetrics

Inherits:
Chef::Handler
  • Object
show all
Defined in:
lib/chef-metrics.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action = nil, &action_block) ⇒ ChefMetrics

Returns a new instance of ChefMetrics.



7
8
9
10
11
12
13
# File 'lib/chef-metrics.rb', line 7

def initialize(action=nil, &action_block)
  @metric_scheme = "chef.#{Chef::Config.node_name}"
  @measure_time = Time.now.to_i
  @metrics = Hash.new
  @use_run_state = true
  @action = action || action_block
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



5
6
7
# File 'lib/chef-metrics.rb', line 5

def action
  @action
end

#measure_timeObject

Returns the value of attribute measure_time.



5
6
7
# File 'lib/chef-metrics.rb', line 5

def measure_time
  @measure_time
end

#metric_schemeObject

Returns the value of attribute metric_scheme.



5
6
7
# File 'lib/chef-metrics.rb', line 5

def metric_scheme
  @metric_scheme
end

#metricsObject

Returns the value of attribute metrics.



5
6
7
# File 'lib/chef-metrics.rb', line 5

def metrics
  @metrics
end

#use_run_stateObject

Returns the value of attribute use_run_state.



5
6
7
# File 'lib/chef-metrics.rb', line 5

def use_run_state
  @use_run_state
end

Instance Method Details

#graphite_formattedObject



21
22
23
24
25
26
# File 'lib/chef-metrics.rb', line 21

def graphite_formatted
  @metrics.inject("") do |result, (metric, value)|
    result << "#{@metric_scheme}.#{metric} #{value} #{@measure_time}\n"
    result
  end
end

#reportObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/chef-metrics.rb', line 28

def report
  @measure_time = Time.now.to_i
  unless run_status.all_resources.nil?
    @metrics[:all_resources] = run_status.all_resources.length
    @metrics[:updated_resources] = run_status.updated_resources.length
    @metrics[:elapsed_time] = run_status.elapsed_time
  end
  if run_status.success?
    @metrics[:success] = 1
    @metrics[:fail] = 0
  else
    @metrics[:success] = 0
    @metrics[:fail] = 1
  end
  if @use_run_state
    run_state_metrics!
  end
  if @action
    self.instance_eval(&@action)
  else
    Chef::Log.info("Chef Metrics report handler was not provided an action")
  end
end

#run_state_metrics!Object



15
16
17
18
19
# File 'lib/chef-metrics.rb', line 15

def run_state_metrics!
  if node.run_state[:metrics].is_a?(Hash)
    @metrics.merge!(node.run_state[:metrics])
  end
end