Skip to content

Commit

Permalink
Merge pull request #102 from DataDog/olivielpeau/catch-event-emit-errors
Browse files Browse the repository at this point in the history
Improve error handling and logging when submitting data
  • Loading branch information
remeh authored May 13, 2019
2 parents 87bf803 + af8cdc9 commit f3afbf1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion chef-handler-datadog.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,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'
Expand Down
1 change: 1 addition & 0 deletions lib/chef/handler/datadog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions lib/chef/handler/datadog_chef_events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 3 additions & 1 deletion lib/chef/handler/datadog_chef_metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/chef/handler/datadog_chef_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit f3afbf1

Please sign in to comment.