lib/instrumental/agent.rb in instrumental_agent-0.6.1 vs lib/instrumental/agent.rb in instrumental_agent-0.7

- old
+ new

@@ -18,12 +18,16 @@ def self.logger=(l) @logger = l end - def self.logger(force = false) - @logger ||= Logger.new(File.open('/dev/null', 'a')) # append mode so it's forksafe + def self.logger + if !@logger + @logger = Logger.new(STDERR) + @logger.level = Logger::WARN + end + @logger end def self.all @agents ||= [] end @@ -38,22 +42,24 @@ # # Instrumental::Agent.new(API_KEY) # Instrumental::Agent.new(API_KEY, :collector => 'hostname:port') def initialize(api_key, options = {}) default_options = { - :collector => 'instrumentalapp.com:8000', - :enabled => true, - :test_mode => false, + :collector => 'instrumentalapp.com:8000', + :enabled => true, + :test_mode => false, + :synchronous => false } options = default_options.merge(options) collector = options[:collector].split(':') - @api_key = api_key - @host = collector[0] - @port = (collector[1] || 8000).to_i - @enabled = options[:enabled] - @test_mode = options[:test_mode] + @api_key = api_key + @host = collector[0] + @port = (collector[1] || 8000).to_i + @enabled = options[:enabled] + @test_mode = options[:test_mode] + @synchronous = options[:synchronous] @pid = Process.pid if @enabled @failures = 0 @@ -91,10 +97,25 @@ rescue Exception => e report_exception(e) nil end + # Send a notice to the server (deploys, downtime, etc.) + # + # agent.notice('A notice') + def notice(note, time = Time.now, duration = 0) + if valid_note?(note) + send_command("notice", time.to_i, duration.to_i, note) + note + else + nil + end + rescue Exception => e + report_exception(e) + nil + end + def enabled? @enabled end def connected? @@ -109,10 +130,14 @@ @logger ||= self.class.logger end private + def valid_note?(note) + note !~ /[\n\r]/ + end + def valid?(metric, value, time) valid_metric = metric =~ /^([\d\w\-_]+\.)*[\d\w\-_]+$/i valid_value = value.to_s =~ /^-?\d+(\.\d+)?$/ return true if valid_metric && valid_value @@ -207,10 +232,10 @@ end rescue Exception => err logger.error err.to_s if command_and_args logger.debug "requeueing: #{command_and_args}" - @queue << command_and_args + @queue << command_and_args end disconnect @failures += 1 delay = [(@failures - 1) ** BACKOFF, MAX_RECONNECT_DELAY].min logger.info "disconnected, reconnect in #{delay}..."