Class: DatadogChefEvents

Inherits:
Object
  • Object
show all
Includes:
DatadogUtil
Defined in:
lib/chef/handler/datadog_chef_events.rb

Overview

helper class for sending events about chef runs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatadogChefEvents

Returns a new instance of DatadogChefEvents.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/chef/handler/datadog_chef_events.rb', line 16

def initialize
  @hostname = nil
  @run_status = nil
  @failure_notfications = nil

  @alert_type = ''
  @event_priority = ''
  @event_title = ''
  # TODO: refactor how event_body is constructed in the class methods
  #       handling of the event_body is a bit clunky and depends on the order of
  #       method calls
  @event_body = ''
end

Instance Attribute Details

#event_bodyObject (readonly)

Returns the value of attribute event_body.



14
15
16
# File 'lib/chef/handler/datadog_chef_events.rb', line 14

def event_body
  @event_body
end

#event_titleObject (readonly)

Returns the value of attribute event_title.



13
14
15
# File 'lib/chef/handler/datadog_chef_events.rb', line 13

def event_title
  @event_title
end

Instance Method Details

#emit_to_datadog(dog) ⇒ Object

Emit Chef event to Datadog

Parameters:

  • dog (Dogapi::Client)

    Dogapi Client to be used



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
# File 'lib/chef/handler/datadog_chef_events.rb', line 69

def emit_to_datadog(dog)
  @event_body = ''
  build_event_data

  begin
    evt = dog.emit_event(Dogapi::Event.new(@event_body,
                                           msg_title: @event_title,
                                           event_type: 'config_management.run',
                                           event_object: @hostname,
                                           alert_type: @alert_type,
                                           priority: @event_priority,
                                           source_type_name: 'chef',
                                           tags: @tags
                                          ), host: @hostname)
    # FIXME: nice-to-have: abstract format of return value away a bit
    # in dogapi directly. See https://github.com/DataDog/dogapi-rb/issues/18
    if evt.length < 2
      Chef::Log.warn("Unexpected response from Datadog Event API: #{evt}")
    else
      # [http_response_code, {"event" => {"url" => "...", ...}}]
      # 2xx means ok
      if evt[0].to_i / 100 != 2
        Chef::Log.warn("Could not submit event to Datadog (HTTP call failed): #{evt[0]}")
      else
        Chef::Log.debug("Successfully submitted Chef event to Datadog for #{@hostname} at #{evt[1]['event']['url']}")
      end
    end
  rescue StandardError => e
    Chef::Log.warn("Could not determine whether Chef event was successfully submitted to Datadog: #{evt}. Error:\n#{e}")
  end
end

#with_failure_notifications(failure_notifications) ⇒ DatadogChefTags

set the failure notification list

Parameters:

  • failure_notifications (Array)

    set of datadog notification handles

Returns:

  • (DatadogChefTags)

    instance reference to self enabling method chaining



52
53
54
55
# File 'lib/chef/handler/datadog_chef_events.rb', line 52

def with_failure_notifications(failure_notifications)
  @failure_notifications = failure_notifications
  self
end

#with_hostname(hostname) ⇒ DatadogChefTags

set the target hostname (chef node name)

Parameters:

  • hostname (String)

    hostname to use for the handler report

Returns:

  • (DatadogChefTags)

    instance reference to self enabling method chaining



34
35
36
37
# File 'lib/chef/handler/datadog_chef_events.rb', line 34

def with_hostname(hostname)
  @hostname = hostname
  self
end

#with_run_status(run_status) ⇒ DatadogChefTags

set the chef run status used for the report

Parameters:

  • run_status (Chef::RunStatus)

    current chef run status

Returns:

  • (DatadogChefTags)

    instance reference to self enabling method chaining



43
44
45
46
# File 'lib/chef/handler/datadog_chef_events.rb', line 43

def with_run_status(run_status)
  @run_status = run_status
  self
end

#with_tags(tags) ⇒ DatadogChefTags

set the datadog host tags associated with the event

Parameters:

  • the (Array)

    set of host tags

Returns:

  • (DatadogChefTags)

    instance reference to self enabling method chaining



61
62
63
64
# File 'lib/chef/handler/datadog_chef_events.rb', line 61

def with_tags(tags)
  @tags = tags
  self
end