lib/new_backup/datadog.rb in new_backup-1.0.2 vs lib/new_backup/datadog.rb in new_backup-1.0.3
- old
+ new
@@ -45,21 +45,23 @@
module DataDog
include Methadone::CLILogging
- @api_key = nil
- @environment = (ENV['RAILS_ENV'].nil? || ENV['RAILS_ENV'].empty?) ? 'development' : ENV['RAILS_ENV']
+ DataDogger = Struct.new :api_key, :environment, :client
module_function
def api_key
- @api_key
+ $dogger ||= DataDogger.new
+ $dogger.api_key
end
def api_key=(api_key)
- @api_key=api_key
+ $dogger ||= DataDogger.new
+ $dogger.api_key=api_key
+ debug "#{self.class}##{__method__}@#{__LINE__}: $dogger.api_key: #{$dogger.api_key}"
end
def dogger(msg, options={})
return if msg.nil? || msg.empty?
@@ -67,24 +69,33 @@
if options[:type] == :error
error msg
else
info msg
end
+ debug "#{self.class}##{__method__}@#{__LINE__}: $dogger exists? #{$dogger.inspect}"
+ debug "#{self.class}##{__method__}@#{__LINE__}: $dogger.api_key: #{$dogger.api_key}"
+ return if $dogger.api_key.nil? || $dogger.api_key.empty?
- return if @api_key.nil? || @api_key.empty?
-
- @client ||= Datadog::Client.new(@api_key)
+ $dogger.client ||= Dogapi::Client.new($dogger.api_key)
+ debug "#{self.class}##{__method__}@#{__LINE__}: $dogger.client: #{$dogger.client.inspect}"
+ $dogger.environment ||= (ENV['RAILS_ENV'].nil? || ENV['RAILS_ENV'].empty?) ? 'development' : ENV['RAILS_ENV']
+
+ hostname = `hostname`.chomp
+
emit_options = {
:msg_title => msg,
- :alert_type => (options[:type] == :error) ? 'Error' : 'Normal',
- :tags => [ "host:#{`hostname`}", "env:#{@environment}" ],
+ :alert_type => (options[:type] == :error) ? 'error' : 'success',
+ :tags => [ "host:#{hostname}", "env:#{$dogger.environment}", "new_backup" ],
:priority => 'normal',
+ :host => hostname,
:source => 'MyApps'}
body = options[:body] ||= msg
- @client.emit_event(Dogapi::Event.new(body, emit_options))
+ this_event = Dogapi::Event.new(body, emit_options)
+ debug "#{self.class}##{__method__}@#{__LINE__}: emit_options: #{emit_options.to_yaml}\nbody: #{body}\nthis_event: #{this_event.inspect}"
+ $dogger.client.emit_event(this_event)
end
end