From 0767306c0889e4b3e3502a2adafeecc7a9bb95b9 Mon Sep 17 00:00:00 2001 From: Olivier Vielpeau Date: Wed, 6 Mar 2019 20:44:01 +0100 Subject: [PATCH 1/2] Improve error handling when submitting events/metrics/tags * rescue errors that may be raised when sending events (typically, raised by the API client) * improve log messages for all submission types when errors are caught --- lib/chef/handler/datadog_chef_events.rb | 22 +++++++++++----------- lib/chef/handler/datadog_chef_metrics.rb | 4 +++- lib/chef/handler/datadog_chef_tags.rb | 4 ++-- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/chef/handler/datadog_chef_events.rb b/lib/chef/handler/datadog_chef_events.rb index 9cdbf66..98aef42 100644 --- a/lib/chef/handler/datadog_chef_events.rb +++ b/lib/chef/handler/datadog_chef_events.rb @@ -66,17 +66,17 @@ def with_tags(tags) def emit_to_datadog(dog) @event_body = '' build_event_data - 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) 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 @@ -90,8 +90,8 @@ def emit_to_datadog(dog) Chef::Log.debug("Successfully submitted Chef event to Datadog for #{@hostname} at #{evt[1]['event']['url']}") end end - rescue - Chef::Log.warn("Could not determine whether chef run was successfully submitted to Datadog: #{evt}") + rescue StandardError => e + Chef::Log.warn("Could not determine whether Chef event was successfully submitted to Datadog: #{evt}. Error:\n#{e}") end end diff --git a/lib/chef/handler/datadog_chef_metrics.rb b/lib/chef/handler/datadog_chef_metrics.rb index 1a08138..20d4597 100644 --- a/lib/chef/handler/datadog_chef_metrics.rb +++ b/lib/chef/handler/datadog_chef_metrics.rb @@ -48,6 +48,8 @@ def emit_to_datadog(dog) 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.error("Could not send metrics to Datadog. Connection error:\n" + 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: #{evt}. Error:\n#{e}") end end # end class DatadogChefMetrics diff --git a/lib/chef/handler/datadog_chef_tags.rb b/lib/chef/handler/datadog_chef_tags.rb index 9d3169e..6f2faa8 100644 --- a/lib/chef/handler/datadog_chef_tags.rb +++ b/lib/chef/handler/datadog_chef_tags.rb @@ -113,8 +113,8 @@ def send_update_to_datadog(dog) end break unless should_retry end - rescue - Chef::Log.warn("Could not determine whether #{@hostname}'s tags were successfully submitted to Datadog: #{rc}") + rescue StandardError => e + Chef::Log.warn("Could not determine whether #{@hostname}'s tags were successfully submitted to Datadog: #{rc}. Error:\n#{e}") end end From 35d15cd3448d971a2d0dbff36782f1f78361f0b0 Mon Sep 17 00:00:00 2001 From: Olivier Vielpeau Date: Wed, 6 Mar 2019 21:05:04 +0100 Subject: [PATCH 2/2] Add debug log entry that includes which DD host we're sending to Helps understanding which endpoint is failing, when multiple are set up --- chef-handler-datadog.gemspec | 2 +- lib/chef/handler/datadog.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/chef-handler-datadog.gemspec b/chef-handler-datadog.gemspec index 24e969c..d522c6b 100644 --- a/chef-handler-datadog.gemspec +++ b/chef-handler-datadog.gemspec @@ -14,7 +14,7 @@ Gem::Specification.new do |gem| gem.require_paths = ['lib'] gem.extra_rdoc_files = ['README.md', 'LICENSE.txt'] - gem.add_dependency 'dogapi', '>= 1.23' + gem.add_dependency 'dogapi', '>= 1.31' gem.add_development_dependency 'appraisal', '~> 2.0.1' gem.add_development_dependency 'bundler' diff --git a/lib/chef/handler/datadog.rb b/lib/chef/handler/datadog.rb index 29823cb..a06bdc5 100644 --- a/lib/chef/handler/datadog.rb +++ b/lib/chef/handler/datadog.rb @@ -31,6 +31,7 @@ def report @dogs.each do |dog| # post the report information to the datadog service + Chef::Log.debug("Sending Chef report to #{dog.datadog_host}") send_report_to_datadog dog end ensure