chef-handler-datadog 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZjkzOTQ0NTMxMmMyNDkyZDg1NTI4YmZjYzc1ODk4ZDI4ZGFlOTNkMg==
5
- data.tar.gz: !binary |-
6
- MjlhNGU4MjZjYTZiZTFlMDEyMmFmOThlMjcyZjcyZTNhODAzOWM5NQ==
2
+ SHA1:
3
+ metadata.gz: 345202254b0a6790a4ee591adfeceb1b6fe4987b
4
+ data.tar.gz: 95b4da8f05b6179952e61a984ad683e83c11eae0
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- ZDcyMGViMDc4YTNmN2MyNDVkZTRmODY0OWRhZWQ5MzVkMGEyMzAzZjViOTIw
10
- MWY0NDg2YjZlYmVlYTVmZjEyODI1MWNhOGI0NDM3MzNmNGFiNzNmZDA4ZGRj
11
- NDA1YThjMjliMzg3MGE2ZDQ2MmM1MDg0OWI0YWQ5MWFkY2YwNTk=
12
- data.tar.gz: !binary |-
13
- Y2I2MmZhNzY0MWQ0YmEwNGE4ODZlOGY5YzM1NDhhZjczYWFkYjIxOTUxYWY2
14
- NmNjODE0NmVjNDkxOGU1NDk4NjBmMjY3NmM1OTdlOTdlZGY0ZWMxY2U4NGJm
15
- NTFlYTllY2I3M2E3M2I4ZDI0MzA4YzhjYTJhNjc3ZmQ2Y2Q0ODE=
6
+ metadata.gz: 672bc2ade623d4afda862b27b66d7301ea05ce1efe0404811bd9c99a36ce69c900e9cdd233165317c946f9891c23d313328c7effd8bb95185236b3043368ea00
7
+ data.tar.gz: 1565a34fa7063abe4db33ee64b724c1a7189b2d8a5478bdb46e18d00ebf8ad07497f6862f772e3b97bb2859bae4f4ccd2847b36093565263c47f22456b69321f
@@ -1,42 +1,44 @@
1
1
  AllCops:
2
2
  Include:
3
3
  - chef-handler-datadog.gemspec
4
- - Appraisals
5
- - Gemfile
6
- - Rakefile
4
+ - '**/Appraisals'
5
+ - '**/Gemfile'
6
+ - '**/Rakefile'
7
7
  Exclude:
8
- - spec/*.rb
9
-
10
- # We want to continue to support Ruby 1.8.x for now
11
- HashSyntax:
12
- EnforcedStyle: hash_rockets
8
+ - 'spec/*.rb'
9
+ - '*/vendor/**/*'
13
10
 
14
11
  # 80 characters is a nice goal, but not worth currently changing in existing
15
12
  # code for the sake of changing it to conform to a length set in 1928 (IBM).
16
- LineLength:
13
+ Metrics/LineLength:
17
14
  Max: 150
18
15
 
19
16
  #####
20
17
  # These exceptions are good goals to attain, and probably will over time,
21
18
  # so periodic disabling and re-running to inspect values is suggested.
22
19
 
20
+ # TODO: main method `report` does a lot of work, which drives this number up.
21
+ Metrics/AbcSize:
22
+ Max: 38
23
+
23
24
  # TODO: Main class is currently over 100 lines of code, making comprehending
24
25
  # it harder. With refactors and simplifications, we can bring this down,
25
26
  # but for now, let's not make it too much worse.
26
- ClassLength:
27
- Max: 160
27
+ Metrics/ClassLength:
28
+ Max: 158
28
29
 
29
- # TODO: this is currently down to 12 from 22, and should be a constant
30
+ # TODO: this is currently down to 7 from 22, and should be a constant
30
31
  # goal to reduce method complexity. The accepted goal is 6.
31
- CyclomaticComplexity:
32
- Max: 12
32
+ # Method `select_hostname`
33
+ Metrics/CyclomaticComplexity:
34
+ Max: 7
33
35
 
34
36
  # TODO: The main method `report` is now down from 85 lines. As refactors
35
37
  # continue, this should drop. However, the goal of 10 lines in a method may
36
38
  # be a little lofty.
37
- MethodLength:
38
- Max: 78
39
+ Metrics/MethodLength:
40
+ Max: 36
39
41
 
40
42
  # TODO: select_hostname, report are both higher than the default of 7
41
- PerceivedComplexity:
43
+ Metrics/PerceivedComplexity:
42
44
  Max: 9
@@ -1,20 +1,25 @@
1
1
  language: ruby
2
+ cache: bundler
3
+ sudo: false
4
+
2
5
  rvm:
3
6
  - 1.9.3
4
- - 2.1.2
7
+ - 2.1.5
8
+
5
9
  bundler_args: --without=localdev
10
+
6
11
  gemfile:
7
- - gemfiles/chef_10.26.0.gemfile
8
- - gemfiles/chef_10.32.2.gemfile
9
- - gemfiles/chef_11.8.2.gemfile
10
- - gemfiles/chef_11.10.4.gemfile
11
- - gemfiles/chef_11.12.8.gemfile
12
- - gemfiles/chef_11.14.2.gemfile
13
- - gemfiles/chef_11.16.0.gemfile
14
- - gemfiles/chef_12.0.0.alpha.1.gemfile
12
+ - gemfiles/chef_10.14.4.gemfile
13
+ - gemfiles/chef_10.gemfile
14
+ - gemfiles/chef_11.gemfile
15
+ - gemfiles/chef_12.gemfile
16
+
15
17
  env:
16
18
  - API_KEY=somefakeapikey APPLICATION_KEY=somefakeapplicationkey
19
+
17
20
  matrix:
21
+ exclude:
22
+ # Chef 12.x with Ohai 8.2.x doesn't work on Chef 1.9.3
23
+ - rvm: 1.9.3
24
+ gemfile: gemfiles/chef_12.gemfile
18
25
  allow_failures:
19
- - rvm: 2.1.2
20
- - gemfile: gemfiles/chef_12.0.0.alpha.1.gemfile
data/Appraisals CHANGED
@@ -1,27 +1,16 @@
1
1
  # encoding: utf-8
2
2
 
3
- # Describe any version dependencies here.
4
- %w(
5
- 11.8.2
6
- 11.10.4
7
- 11.12.8
8
- 11.14.2
9
- 11.16.0
10
- 12.0.0.alpha.1
11
- ).each do |tv|
3
+ # Latest release of mainline Chef versions here.
4
+ %w(10 11 12).each do |tv|
12
5
  appraise "chef-#{tv}" do
13
- gem 'chef', tv
6
+ gem 'chef', "~> #{tv}.0"
14
7
  end
15
8
  end
16
9
 
17
- # Due to some oddity in json gem version pinning for versions that have
18
- # conflicts, specify best version here.
19
- %w(
20
- 10.26.0
21
- 10.32.2
22
- ).each do |tv|
23
- appraise "chef-#{tv}" do
24
- gem 'chef', tv
25
- gem 'json', '1.7.7'
26
- end
10
+ # Describe any specific Chef versions here.
11
+ appraise 'chef-10.14.4' do
12
+ gem 'chef', '10.14.4'
13
+ # Old versions of Chef didn't pin the max version of Ohai they supported.
14
+ # See: http://git.io/vecAn
15
+ gem 'ohai', '< 8.0'
27
16
  end
@@ -1,6 +1,13 @@
1
1
  Changes
2
2
  =======
3
3
 
4
+ # 0.7.0 / 2015-10-22
5
+
6
+ * [FEATURE] Support passing `DATADOG_PROXY` env var to control `dogapi` behavior, [#65][] [@datwiz][]
7
+ * [OPTIMIZE] Only map `tags` if non-nil, [#66][] [@owen][]
8
+ * [MISC] Style updates, [#60][] [@miketheman][]
9
+ * [MISC] Update test matrix to handle latest released major versions, [#59][] [@miketheman][]
10
+
4
11
  # 0.6.0 / 2014-09-17
5
12
 
6
13
  * [FEATURE] Submit Chef-related tags to Datadog for Events, [#52][] [@miketheman][]
@@ -62,7 +69,13 @@ And all other versions were prior to this. See git history for more.
62
69
  [#50]: https://github.com/DataDog/chef-handler-datadog/issues/50
63
70
  [#51]: https://github.com/DataDog/chef-handler-datadog/issues/51
64
71
  [#52]: https://github.com/DataDog/chef-handler-datadog/issues/52
72
+ [#59]: https://github.com/DataDog/chef-handler-datadog/issues/59
73
+ [#60]: https://github.com/DataDog/chef-handler-datadog/issues/60
74
+ [#65]: https://github.com/DataDog/chef-handler-datadog/issues/65
75
+ [#66]: https://github.com/DataDog/chef-handler-datadog/issues/66
65
76
  [@alq]: https://github.com/alq
77
+ [@datwiz]: https://github.com/datwiz
66
78
  [@dwradcliffe]: https://github.com/dwradcliffe
67
79
  [@miketheman]: https://github.com/miketheman
80
+ [@owen]: https://github.com/owen
68
81
  [@remh]: https://github.com/remh
data/README.md CHANGED
@@ -15,6 +15,18 @@ This can be installed by using the `dd-handler` recipe from the [datadog cookboo
15
15
  run_list 'foo::bar', 'datadog::dd-handler'
16
16
  ```
17
17
 
18
+ ### Windows support
19
+
20
+ The chef handler does work on Microsoft Windows however limitations with SSL + Ruby on Windows require extra setup. One solution is to set the `SSL_CERT_FILE` environmental variable to the one that chef uses on the machine to fix this issue. Here is how [chef](https://github.com/chef/omnibus-chef/blob/master/files/openssl-customization/windows/ssl_env_hack.rb) fixes the issue.
21
+
22
+ ```ruby
23
+ # Setup the certs for ruby in windows
24
+ env 'SSL_CERT_FILE' do
25
+ action :create
26
+ value "C:\\opscode\\chef\\embedded\\ssl\\certs\\cacert.pem"
27
+ end
28
+ ```
29
+
18
30
  The Datadog Docs on [Chef](http://docs.datadoghq.com/guides/chef/#deployhandler) has detailed instructions.
19
31
 
20
32
  ## Contributing to chef-handler-datadog
data/Rakefile CHANGED
@@ -3,12 +3,11 @@
3
3
  require 'rubygems'
4
4
  require 'bundler/gem_tasks'
5
5
 
6
- require 'appraisal'
7
6
  require 'rake/clean'
8
7
  require 'rspec/core/rake_task'
9
8
  require 'rubocop/rake_task'
10
9
 
11
- task :default => [:cops, :spec]
10
+ task default: [:cops, :spec]
12
11
 
13
12
  CLEAN.include(['coverage/', 'doc/', 'pkg/'])
14
13
 
@@ -9,20 +9,20 @@ Gem::Specification.new do |gem|
9
9
  gem.version = ChefHandlerDatadog::VERSION
10
10
 
11
11
  gem.files = `git ls-files`.split($\) # rubocop:disable SpecialGlobalVars
12
- gem.executables = gem.files.grep(/^bin\//).map { |f| File.basename(f) }
13
- gem.test_files = gem.files.grep(/^(test|spec|features)\//)
12
+ gem.executables = gem.files.grep(%r{^bin\/}).map { |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)\/})
14
14
  gem.require_paths = ['lib']
15
15
  gem.extra_rdoc_files = ['README.md', 'LICENSE.txt']
16
16
 
17
17
  gem.add_dependency 'dogapi', '>= 1.6'
18
18
 
19
- gem.add_development_dependency 'appraisal', '~> 1.0.0'
19
+ gem.add_development_dependency 'appraisal', '~> 2.0.1'
20
20
  gem.add_development_dependency 'bundler'
21
- gem.add_development_dependency 'chef', '>= 10', '<= 12'
21
+ gem.add_development_dependency 'chef', '>= 10.14.4', '<= 13'
22
22
  gem.add_development_dependency 'dotenv'
23
23
  gem.add_development_dependency 'rake'
24
- gem.add_development_dependency 'rspec', '~> 3.0.0'
25
- gem.add_development_dependency 'rubocop', '~> 0.25.0'
24
+ gem.add_development_dependency 'rspec', '~> 3.2.0'
25
+ gem.add_development_dependency 'rubocop', '~> 0.34.2'
26
26
  gem.add_development_dependency 'simplecov'
27
27
  gem.add_development_dependency 'vcr'
28
28
  gem.add_development_dependency 'webmock'
@@ -2,8 +2,8 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "chef", "10.26.0"
6
- gem "json", "1.7.7"
5
+ gem "chef", "10.14.4"
6
+ gem "ohai", "< 8.0"
7
7
 
8
8
  group :localdev do
9
9
  gem "guard"
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "chef", "11.14.2"
5
+ gem "chef", "~> 10.0"
6
6
 
7
7
  group :localdev do
8
8
  gem "guard"
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "chef", "11.16.0"
5
+ gem "chef", "~> 11.0"
6
6
 
7
7
  group :localdev do
8
8
  gem "guard"
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "chef", "11.10.4"
5
+ gem "chef", "~> 12.0"
6
6
 
7
7
  group :localdev do
8
8
  gem "guard"
@@ -3,6 +3,9 @@ require 'rubygems'
3
3
  require 'chef/handler'
4
4
  require 'chef/mash'
5
5
  require 'dogapi'
6
+ require_relative 'datadog_chef_metrics'
7
+ require_relative 'datadog_chef_tags'
8
+ require_relative 'datadog_chef_events'
6
9
 
7
10
  class Chef
8
11
  class Handler
@@ -20,241 +23,75 @@ class Chef
20
23
  end
21
24
 
22
25
  def report
23
- # resolve correct hostname
24
- hostname = select_hostname(run_status.node, config)
25
-
26
- # Send the metrics
27
- emit_metrics_to_datadog(hostname, run_status)
28
-
29
- # Build the correct event
30
- event_data = build_event_data(hostname, run_status)
31
-
32
- # Submit the details back to Datadog
33
- begin
34
- # Collect tags
35
- new_host_tags = get_combined_tags(node)
36
-
37
- # Send the Event data
38
- emit_event_to_datadog(hostname, event_data, new_host_tags)
39
-
40
- # Update tags
41
- if config[:application_key].nil?
42
- Chef::Log.warn('You need an application key to let Chef tag your nodes ' \
43
- 'in Datadog. Visit https://app.datadoghq.com/account/settings#api to ' \
44
- 'create one and update your datadog attributes in the datadog cookbook.'
45
- )
46
- fail ArgumentError, 'Missing Datadog Application Key'
47
- else
48
-
49
- # Replace all Chef tags with the found Chef tags
50
- rc = @dog.update_tags(hostname, new_host_tags, 'chef')
51
- begin
52
- # See FIXME above about why I feel dirty repeating this code here
53
- if rc.length < 2
54
- Chef::Log.warn("Unexpected response from Datadog Event API: #{rc}")
55
- else
56
- if rc[0].to_i / 100 != 2
57
- Chef::Log.warn("Could not submit #{new_host_tags} tags for #{hostname} to Datadog: #{rc}")
58
- else
59
- Chef::Log.debug("Successfully updated #{hostname}'s tags to #{new_host_tags.join(', ')}")
60
- end
61
- end
62
- rescue
63
- Chef::Log.warn("Could not determine whether #{hostname}'s tags were successfully submitted to Datadog: #{rc}")
64
- end
65
- end
66
- rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT => e
67
- Chef::Log.error("Could not connect to Datadog. Connection error:\n" + e)
68
- Chef::Log.error('Data to be submitted was:')
69
- Chef::Log.error(event_title)
70
- Chef::Log.error(event_body)
71
- Chef::Log.error('Tags to be set for this run:')
72
- Chef::Log.error(new_host_tags)
73
- end
26
+ # use datadog agent proxy settings, if available
27
+ use_agent_proxy unless ENV['DATADOG_PROXY'].nil?
28
+
29
+ # prepare the metrics, event, and tags information to be reported
30
+ prepare_report_for_datadog
31
+ # post the report information to the datadog service
32
+ send_report_to_datadog
33
+ ensure
34
+ # restore the env proxy settings before leaving to avoid downstream side-effects
35
+ restore_env_proxies unless ENV['DATADOG_PROXY'].nil?
74
36
  end
75
37
 
76
38
  private
77
39
 
78
- # Build the Event data for submission
79
- #
80
- # @param hostname [String] resolved hostname to attach to Event
81
- # @param run_status [Chef::RunStatus] current run status
82
- # @return [Array] alert_type, event_priority, event_title, event_body
83
- def build_event_data(hostname, run_status)
84
- # bail early in case of a compiletime failure
85
- # OPTIMIZE: Use better inspectors to handle failure scenarios, refactor needed.
86
- if run_status.elapsed_time.nil?
87
-
88
- alert_type = 'error'
89
- event_title = "Chef failed during compile phase on #{hostname} "
90
- event_priority = 'normal'
91
- event_body = 'Chef was unable to complete a run, an error during compilation may have occurred.'
92
-
93
- return [alert_type, event_priority, event_title, event_body]
94
- end
95
-
96
- run_time = pluralize(run_status.elapsed_time, 'second')
97
-
98
- # This is the first line of the Event body, the rest is appended here.
99
- event_body = "Chef updated #{run_status.updated_resources.length} resources out of #{run_status.all_resources.length} resources total."
100
-
101
- # Show the updated resource list, truncated when failed to 5
102
- event_body << updated_resource_list(run_status)
103
-
104
- if run_status.success?
105
- alert_type = 'success'
106
- event_priority = 'low'
107
- event_title = "Chef completed in #{run_time} on #{hostname} "
108
- else
109
- alert_type = 'error'
110
- event_priority = 'normal'
111
- event_title = "Chef failed in #{run_time} on #{hostname} "
112
-
113
- if @config[:notify_on_failure]
114
- handles = @config[:notify_on_failure]
115
- # convert the notification handle array to a string
116
- event_body << "\nAlerting: #{handles.join(' ')}\n"
117
- end
118
-
119
- event_body << "\n$$$\n#{run_status.formatted_exception}\n$$$\n"
120
- event_body << "\n$$$\n#{run_status.backtrace.join("\n")}\n$$$\n"
121
- end
122
-
123
- # Return resolved data
124
- [alert_type, event_priority, event_title, event_body]
125
- end
126
-
127
- # Compose a list of resources updated during a run.
128
- # Shorten the list when there is a failure for stacktrace debugging
129
- #
130
- # @param run_status [Chef::RunStatus] current run status
131
- # @return [String] formatted list of resources updated, truncated on failure
132
- def updated_resource_list(run_status)
133
- # No resources updated? Go away.
134
- return '' unless run_status.updated_resources.length.to_i > 0
135
-
136
- if run_status.failed?
137
- report_resources = run_status.updated_resources.last(5)
138
- else
139
- report_resources = run_status.updated_resources
140
- end
141
-
142
- event_body = "\n$$$\n"
143
- report_resources.each do |r|
144
- event_body << "- #{r} (#{r.defined_at})\n"
145
- end
146
- event_body << "\n$$$\n"
147
- end
148
-
149
- # Emit Event to Datadog Event Stream
150
- #
151
- # @param hostname [String] resolved hostname to attach to Event
152
- # @param event_params [Array] all the configurables to build a valid Event
153
- # @param tags [Array] Chef env/roles/tags to be set as Datadog tags
154
- def emit_event_to_datadog(hostname, event_data, tags)
155
- alert_type, event_priority, event_title, event_body = event_data
156
-
157
- evt = @dog.emit_event(Dogapi::Event.new(event_body,
158
- :msg_title => event_title,
159
- :event_type => 'config_management.run',
160
- :event_object => hostname,
161
- :alert_type => alert_type,
162
- :priority => event_priority,
163
- :source_type_name => 'chef',
164
- :tags => tags
165
- ), :host => hostname)
166
-
167
- begin
168
- # FIXME: nice-to-have: abstract format of return value away a bit
169
- # in dogapi directly. See https://github.com/DataDog/dogapi-rb/issues/18
170
- if evt.length < 2
171
- Chef::Log.warn("Unexpected response from Datadog Event API: #{evt}")
172
- else
173
- # [http_response_code, {"event" => {"url" => "...", ...}}]
174
- # 2xx means ok
175
- if evt[0].to_i / 100 != 2
176
- Chef::Log.warn("Could not submit event to Datadog (HTTP call failed): #{evt[0]}")
177
- else
178
- Chef::Log.debug("Successfully submitted Chef event to Datadog for #{hostname} at #{evt[1]['event']['url']}")
179
- end
180
- end
181
- rescue
182
- Chef::Log.warn("Could not determine whether chef run was successfully submitted to Datadog: #{evt}")
183
- end
40
+ # prepare metrics, event, and tags data for posting to datadog
41
+ def prepare_report_for_datadog
42
+ # uses class method accessors for run_status and config
43
+ hostname = resolve_correct_hostname
44
+ # prepare chef run metrics
45
+ @metrics =
46
+ DatadogChefMetrics.new
47
+ .with_dogapi_client(@dog)
48
+ .with_hostname(hostname)
49
+ .with_run_status(run_status)
50
+
51
+ # Collect and prepare tags
52
+ @tags =
53
+ DatadogChefTags.new
54
+ .with_dogapi_client(@dog)
55
+ .with_hostname(hostname)
56
+ .with_run_status(run_status)
57
+ .with_application_key(config[:application_key])
58
+
59
+ # Build the chef event information
60
+ @event =
61
+ DatadogChefEvents.new
62
+ .with_dogapi_client(@dog)
63
+ .with_hostname(hostname)
64
+ .with_run_status(run_status)
65
+ .with_failure_notifications(@config['notify_on_failure'])
66
+ .with_tags(@tags.combined_host_tags)
184
67
  end
185
68
 
186
- # Emit Chef metrics to Datadog
187
- #
188
- # @param hostname [String] resolved hostname to attach to series
189
- # @param run_status [Chef::RunStatus] current run status
190
- def emit_metrics_to_datadog(hostname, run_status)
191
- # If there is a failure during compile phase, a large portion of
192
- # run_status may be unavailable. Bail out here
193
- warn_msg = 'Error during compile phase, no Datadog metrics available.'
194
- return Chef::Log.warn(warn_msg) if run_status.elapsed_time.nil?
195
-
196
- @dog.emit_point('chef.resources.total', run_status.all_resources.length, :host => hostname)
197
- @dog.emit_point('chef.resources.updated', run_status.updated_resources.length, :host => hostname)
198
- @dog.emit_point('chef.resources.elapsed_time', run_status.elapsed_time, :host => hostname)
199
- Chef::Log.debug('Submitted Chef metrics back to Datadog')
69
+ # Submit metrics, event, and tags information to datadog
70
+ def send_report_to_datadog
71
+ @metrics.emit_to_datadog
72
+ @event.emit_to_datadog
73
+ @tags.send_update_to_datadog
200
74
  rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT => e
201
- Chef::Log.error("Could not send metrics to Datadog. Connection error:\n" + e)
202
- end
203
-
204
- # Build up an array of Chef tags to send back
205
- #
206
- # Selects all [env, roles, tags] from the Node's object and reformats
207
- # them to `key:value` e.g. `role:database-master`.
208
- #
209
- # @param node [Chef::Node]
210
- # @return [Array] current Chef env, roles, tags
211
- def get_combined_tags(node)
212
- chef_env = get_node_env(node).split # converts a string into an array
213
-
214
- chef_roles = get_node_roles(node)
215
- chef_tags = get_node_tags(node)
216
-
217
- # Combine (union) all arrays. Removes duplicates if found.
218
- chef_env | chef_roles | chef_tags
219
- end
220
-
221
- def get_node_roles(node)
222
- node.run_list.roles.map! { |role| 'role:' + role }
223
- end
224
-
225
- def get_node_env(node)
226
- 'env:' + node.chef_environment if node.respond_to?('chef_environment')
227
- end
228
-
229
- def get_node_tags(node)
230
- node.tags.map! { |tag| 'tag:' + tag }
231
- end
232
-
233
- def pluralize(number, noun)
234
- case number
235
- when 0..1
236
- "less than 1 #{noun}"
237
- else
238
- "#{number.round} #{noun}s"
239
- end
240
- rescue
241
- Chef::Log.warn("Cannot make #{number} more legible")
242
- "#{number} #{noun}s"
75
+ Chef::Log.error("Could not connect to Datadog. Connection error:\n" + e)
76
+ Chef::Log.error('Data to be submitted was:')
77
+ Chef::Log.error(@event.event_title)
78
+ Chef::Log.error(@event.event_body)
79
+ Chef::Log.error('Tags to be set for this run:')
80
+ Chef::Log.error(@tags.combined_host_tags)
243
81
  end
244
82
 
245
83
  # Select which hostname to report back to Datadog.
246
84
  # Makes decision based on inputs from `config` and when absent, use the
247
85
  # node's `ec2` attribute existence to make the decision.
248
86
  #
249
- # @param node [Chef::Node] from `run_status`, can feasibly any `node`
250
- # @param config [Hash] config object passed in to handler
251
87
  # @return [String] the hostname decided upon
252
- def select_hostname(node, config)
88
+ def resolve_correct_hostname
89
+ node = run_status.node
253
90
  use_ec2_instance_id = !config.key?(:use_ec2_instance_id) ||
254
- (config.key?(:use_ec2_instance_id) &&
255
- config[:use_ec2_instance_id])
91
+ (config.key?(:use_ec2_instance_id) && config[:use_ec2_instance_id])
256
92
 
257
93
  if config[:hostname]
94
+ puts "found hostname #{config[:hostname]} in config object"
258
95
  config[:hostname]
259
96
  elsif use_ec2_instance_id && node.attribute?('ec2') && node.ec2.attribute?('instance_id')
260
97
  node.ec2.instance_id
@@ -262,6 +99,23 @@ class Chef
262
99
  node.name
263
100
  end
264
101
  end
102
+
103
+ # Using the agent proxy settings requires setting http(s)_proxy
104
+ # env vars. However, original env var settings need to be
105
+ # preserved for restoration at the end of the handler.
106
+ def use_agent_proxy
107
+ Chef::Log.info('Using agent proxy settings')
108
+ @env_http_proxy = ENV['http_proxy']
109
+ @env_https_proxy = ENV['https_proxy']
110
+ ENV['http_proxy'] = ENV['DATADOG_PROXY']
111
+ ENV['https_proxy'] = ENV['DATADOG_PROXY']
112
+ end
113
+
114
+ # Restore environment proxy settings to pre-report values
115
+ def restore_env_proxies
116
+ ENV['http_proxy'] = @env_http_proxy
117
+ ENV['https_proxy'] = @env_https_proxy
118
+ end
265
119
  end # end class Datadog
266
120
  end # end class Handler
267
121
  end # end class Chef