chef-handler-datadog 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/VERSION +1 -1
  2. data/lib/chef-handler-datadog.rb +60 -20
  3. metadata +4 -4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -3,10 +3,15 @@ require 'chef'
3
3
  require 'chef/handler'
4
4
  require 'dogapi'
5
5
 
6
- class DataDog < Chef::Handler
7
- def initialize(api_key)
8
- @api_key = api_key
9
- @dog = Dogapi::Client.new(api_key)
6
+ class Datadog < Chef::Handler
7
+
8
+ # For the tags to work, the client must have created an Application Key on the
9
+ # "Account Settings" page here: https://app.datadoghq.com/account/settings
10
+ # It should be passed along from the node/role/environemnt attributes, as the default is nil.
11
+ def initialize(opts = {})
12
+ @api_key = opts[:api_key]
13
+ @application_key = opts[:application_key]
14
+ @dog = Dogapi::Client.new(@api_key, application_key = @application_key)
10
15
  end
11
16
 
12
17
  def report
@@ -15,20 +20,21 @@ class DataDog < Chef::Handler
15
20
  @dog.emit_point("chef.resources.total", run_status.all_resources.length, :host => run_status.node.name)
16
21
  @dog.emit_point("chef.resources.updated", run_status.updated_resources.length, :host => run_status.node.name)
17
22
  @dog.emit_point("chef.resources.elapsed_time", run_status.elapsed_time, :host => run_status.node.name)
18
- rescue Errno::ECONNREFUSED => e
19
- Chef::Log.error("Could not connect to DataDog. Connection error:\n" + e)
23
+ rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT => e
24
+ Chef::Log.error("Could not send metrics to Datadog. Connection error:\n" + e)
20
25
  end
21
26
 
22
- event_data = "Chef run for #{run_status.node.name}"
27
+ event_title = ""
28
+ run_time = pluralize(run_status.elapsed_time, "second")
23
29
  if run_status.success?
24
- event_data << " complete in #{run_status.elapsed_time} seconds\n"
30
+ event_title << "Chef completed in #{run_time} on #{run_status.node.name} "
25
31
  else
26
- event_data << " failed in #{run_status.elapsed_time} seconds\n"
32
+ event_title << "Chef failed in #{run_time} on #{run_status.node.name} "
27
33
  end
28
- event_data << "Managed #{run_status.all_resources.length} resources\n"
29
- event_data << "Updated #{run_status.updated_resources.length} resources"
34
+
35
+ event_data = "Chef updated #{run_status.updated_resources.length} resources out of #{run_status.all_resources.length} resources total."
30
36
  if run_status.updated_resources.length.to_i > 0
31
- event_data << "\n\n@@@\n"
37
+ event_data << "\n@@@\n"
32
38
  run_status.updated_resources.each do |r|
33
39
  event_data << "- #{r.to_s} (#{r.defined_at})\n"
34
40
  end
@@ -36,18 +42,52 @@ class DataDog < Chef::Handler
36
42
  end
37
43
 
38
44
  if run_status.failed?
39
- event_data << "\n\n@@@\n#{run_status.formatted_exception}\n@@@\n"
40
- event_data << "\n\n@@@\n#{run_status.backtrace.join("\n")}\n@@@\n"
45
+ event_data << "\n@@@\n#{run_status.formatted_exception}\n@@@\n"
46
+ event_data << "\n@@@\n#{run_status.backtrace.join("\n")}\n@@@\n"
41
47
  end
42
48
 
43
- # Submit the details back to DataDog
49
+ # Submit the details back to Datadog
44
50
  begin
45
- @dog.emit_event(Dogapi::Event.new(event_data), :host => run_status.node.name)
46
- # TODO: add chef roles to set the node's #tags in newsfeed
47
- rescue Errno::ECONNREFUSED => e
48
- Chef::Log.error("Could not connect to DataDog. Connection error:\n" + e)
51
+ # Send the Event data
52
+ @dog.emit_event(Dogapi::Event.new(event_data, :msg_title => event_title), :host => run_status.node.name)
53
+
54
+ # Get the current list of tags, remove any "role:" entries
55
+ host_tags = @dog.host_tags(node.name)[1]["tags"]
56
+ host_tags.delete_if {|tag| tag.start_with?('role:') }
57
+
58
+ # Get list of chef roles, rename them to tag format
59
+ chef_roles = node.run_list.roles
60
+ chef_roles.collect! {|role| "role:" + role }
61
+
62
+ # Combine (union) both arrays. Removes dupes, preserves non-chef tags.
63
+ new_host_tags = host_tags | chef_roles
64
+
65
+ # Replace all tags with the new tags
66
+ @dog.update_tags(node.name, new_host_tags)
67
+
68
+ rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT => e
69
+ Chef::Log.error("Could not connect to Datadog. Connection error:\n" + e)
49
70
  Chef::Log.error("Data to be submitted was:")
71
+ Chef::Log.error(event_title)
50
72
  Chef::Log.error(event_data)
73
+ Chef::Log.error("Tags to be set for this run:")
74
+ Chef::Log.error(new_host_tags)
75
+ end
76
+ end
77
+
78
+ private
79
+
80
+ def pluralize(number, noun)
81
+ begin
82
+ case number
83
+ when 0..1
84
+ "less than 1 #{noun}"
85
+ else
86
+ "#{number.round} #{noun}s"
87
+ end
88
+ rescue
89
+ Chef::Log.warn("Cannot make #{number} more legible")
90
+ "#{number} #{noun}s"
51
91
  end
52
92
  end
53
- end
93
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef-handler-datadog
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mike Fiedler
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-11-17 00:00:00 Z
19
+ date: 2011-11-19 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  requirement: &id001 !ruby/object:Gem::Requirement