Class: DatadogChefMetrics

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

Overview

helper class for sending datadog metrics from a chef run

Instance Method Summary collapse

Constructor Details

#initializeDatadogChefMetrics

Returns a new instance of DatadogChefMetrics.



10
11
12
13
# File 'lib/chef/handler/datadog_chef_metrics.rb', line 10

def initialize
  @hostname = ''
  @run_status = nil
end

Instance Method Details

#emit_to_datadog(dog) ⇒ Object

Emit Chef metrics to Datadog

Parameters:

  • dog (Dogapi::Client)

    Dogapi Client to be used



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chef/handler/datadog_chef_metrics.rb', line 36

def emit_to_datadog(dog)
  # Send base success/failure metric
  dog.emit_point('chef.run.success', @run_status.success? ? 1 : 0, host: @hostname, type: 'counter')
  dog.emit_point('chef.run.failure', @run_status.success? ? 0 : 1, host: @hostname, type: 'counter')

  # If there is a failure during compile phase, a large portion of
  # run_status may be unavailable. Bail out here
  warn_msg = 'Error during compile phase, no Datadog metrics available.'
  return Chef::Log.warn(warn_msg) if compile_error?

  dog.emit_point('chef.resources.total', @run_status.all_resources.length, host: @hostname)
  dog.emit_point('chef.resources.updated', @run_status.updated_resources.length, host: @hostname)
  dog.emit_point('chef.resources.elapsed_time', @run_status.elapsed_time, host: @hostname)
  Chef::Log.debug('Submitted Chef metrics back to Datadog')
rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT => e
  Chef::Log.warn("Could not send metrics to Datadog. Connection error:\n" + e)
rescue StandardError => e
  Chef::Log.warn("Could not determine whether chef run metrics were successfully submitted to Datadog. Error:\n#{e}")
end

#with_hostname(hostname) ⇒ DatadogChefMetrics

set the target hostname (chef node name)

Parameters:

  • hostname (String)

    hostname used for reporting metrics

Returns:



19
20
21
22
# File 'lib/chef/handler/datadog_chef_metrics.rb', line 19

def with_hostname(hostname)
  @hostname = hostname
  self
end

#with_run_status(run_status) ⇒ DatadogChefMetrics

set the chef run status used for the report

Parameters:

  • run_status (Chef::RunStatus)

    current run status

Returns:



28
29
30
31
# File 'lib/chef/handler/datadog_chef_metrics.rb', line 28

def with_run_status(run_status)
  @run_status = run_status
  self
end