=begin = datadog.rb *Copyright*:: (C) 2013 by Novu, LLC *Author(s)*:: Tamara Temple *Since*:: 2013-05-01 == Description Manage the connection and communication from the app to DataDog service. == Usage require '.../datadog.rb' class MyClass include DataDog api_key = 'Your DD API Key' dogger "Hi from MyClass!" begin # some operations rescue Exception => e dogger "Woops, an Error", :type => :error, :body => "#{e.class}: #{e}\n#{e.backtrace.join("\n")}" end # ... other class code end == Caveats This module integrates with methadone, using it's logging features in addition to sending the log message to data dog. This isn't necessarily the most robust intermingling... =end require 'dogapi' require 'methadone' module NewBackup module DataDog include Methadone::CLILogging DataDogger = Struct.new :api_key, :environment, :client module_function def api_key $dogger ||= DataDogger.new $dogger.api_key end def api_key=(api_key) $dogger ||= DataDogger.new $dogger.api_key=api_key debug "#{self.class}##{__method__}@#{File.basename(__FILE__)}:#{__LINE__}: $dogger.api_key: #{$dogger.api_key}" end def dogger(msg, options={}) return if msg.nil? || msg.empty? if options[:type] == :error error msg else info msg end debug "#{self.class}##{__method__}@#{File.basename(__FILE__)}:#{__LINE__}: $dogger exists? #{$dogger.inspect}" debug "#{self.class}##{__method__}@#{File.basename(__FILE__)}:#{__LINE__}: $dogger.api_key: #{$dogger.api_key}" return if $dogger.api_key.nil? || $dogger.api_key.empty? $dogger.client ||= Dogapi::Client.new($dogger.api_key) debug "#{self.class}##{__method__}@#{File.basename(__FILE__)}:#{__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' : 'success', :tags => [ "host:#{hostname}", "env:#{$dogger.environment}", "new_backup" ], :priority => 'normal', :host => hostname, :source => 'MyApps'} body = options[:body] ||= msg this_event = Dogapi::Event.new(body, emit_options) debug "#{self.class}##{__method__}@#{File.basename(__FILE__)}:#{__LINE__}: emit_options: #{emit_options.to_yaml}\nbody: #{body}\nthis_event: #{this_event.inspect}" $dogger.client.emit_event(this_event) end end end