Class: AirbrakeHandler

Inherits:
Chef::Handler
  • Object
show all
Defined in:
lib/airbrake_handler.rb

Constant Summary collapse

VERSION =
"0.5.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AirbrakeHandler

Returns a new instance of AirbrakeHandler.



27
28
29
30
31
32
33
# File 'lib/airbrake_handler.rb', line 27

def initialize(options = {})
  @api_key     = options.delete(:api_key)
  @notify_host = options.delete(:notify_host) || nil
  @ignore      = options.delete(:ignore) || []
  @params      = options.delete(:params) || {}
  @options     = options
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



25
26
27
# File 'lib/airbrake_handler.rb', line 25

def api_key
  @api_key
end

#ignoreObject

Returns the value of attribute ignore.



25
26
27
# File 'lib/airbrake_handler.rb', line 25

def ignore
  @ignore
end

#notify_hostObject

Returns the value of attribute notify_host.



25
26
27
# File 'lib/airbrake_handler.rb', line 25

def notify_host
  @notify_host
end

#optionsObject

Returns the value of attribute options.



25
26
27
# File 'lib/airbrake_handler.rb', line 25

def options
  @options
end

#paramsObject

Returns the value of attribute params.



25
26
27
# File 'lib/airbrake_handler.rb', line 25

def params
  @params
end

Instance Method Details

#airbrake_paramsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/airbrake_handler.rb', line 49

def airbrake_params
  {
    :notifier_name    => "Chef Airbrake Notifier",
    :notifier_version => VERSION,
    :notifier_url     => "https://github.com/morgoth/airbrake_handler",
    :component        => run_status.node.name,
    :url              => nil,
    :environment      => {},
    :params           => {
      :start_time   => run_status.start_time,
      :end_time     => run_status.end_time,
      :elapsed_time => run_status.elapsed_time,
      :run_list     => run_status.node.run_list.to_s
    }.merge(params)
  }.merge(options)
end

#clientObject

Raises:

  • (ArgumentError)


66
67
68
69
# File 'lib/airbrake_handler.rb', line 66

def client
  raise ArgumentError.new("You must specify Airbrake api key") unless api_key
  Toadhopper.new(api_key, :notify_host => notify_host)
end

#ignore_exception?(exception) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
# File 'lib/airbrake_handler.rb', line 43

def ignore_exception?(exception)
  ignore.any? do |ignore_case|
    ignore_case[:class] == exception.class.name && (!ignore_case.key?(:message) || !!ignore_case[:message].match(exception.message))
  end
end

#reportObject



35
36
37
38
39
40
41
# File 'lib/airbrake_handler.rb', line 35

def report
  if run_status.failed? && !ignore_exception?(run_status.exception)
    Chef::Log.error("Creating Airbrake exception report")

    client.post!(run_status.exception, airbrake_params)
  end
end