Class: StackDriverHandler

Inherits:
Chef::Handler
  • Object
show all
Defined in:
lib/chef/handler/stackdriver.rb,
lib/chef/handler/stackdriver/version.rb

Constant Summary collapse

VERSION =
"0.1.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ StackDriverHandler

Returns a new instance of StackDriverHandler.



24
25
26
27
28
29
30
# File 'lib/chef/handler/stackdriver.rb', line 24

def initialize(opts = {})
  @options = {
    :instance_id => nil,
    :stackdriver_api => nil
  }
  @options.merge! opts
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



23
24
25
# File 'lib/chef/handler/stackdriver.rb', line 23

def options
  @options
end

Instance Method Details

#reportObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chef/handler/stackdriver.rb', line 32

def report
  if !options[:instance_id]
    Chef::Log.error "StackDriverHandler: No instance_id provided.  No information sent to StackDriver."
  elsif !options[:stackdriver_api]
    Chef::Log.error "StackDriverHandler: No stackdriver_api provided.  No information sent to StackDriver."
  else
    status = success? ? 1 : 0
    date = Time.now.to_i
    StackDriver.init options[:stackdriver_api]
    StackDriver.send_metric 'chef_status', status, date, options[:instance_id]
    if success?
      StackDriver.send_metric 'chef_run_time', run_data[:elapsed_time].to_s, date, options[:instance_id]
      StackDriver.send_metric 'chef_resources_updated', run_data[:updated_resources].length, date, options[:instance_id]
    end
    Chef::Log.debug "StackDriverHandler: Metrics sent."
  end
end