lib/instrumental/agent.rb in instrumental_agent-0.1.0 vs lib/instrumental/agent.rb in instrumental_agent-0.1.2
- old
+ new
@@ -7,11 +7,11 @@
#
# Instrumental::Agent.new(API_KEY)
module Instrumental
class Agent
attr_accessor :host, :port
- attr_reader :connection
+ attr_reader :connection, :enabled
def self.start_reactor
unless EM.reactor_running?
logger.debug 'Starting EventMachine reactor'
Thread.new { EM.run }
@@ -96,25 +96,31 @@
# Sets up a connection to the collector.
#
# Instrumental::Agent.new(API_KEY)
# Instrumental::Agent.new(API_KEY, :collector => 'hostname:port')
def initialize(api_key, options = {})
- default_options = { :start_reactor => true }
+ default_options = { :start_reactor => true, :enabled => true }
options = default_options.merge(options)
@api_key = api_key
if options[:collector]
@host, @port = options[:collector].split(':')
@port = (@port || 8000).to_i
else
@host = 'instrumentalapp.com'
@port = 8000
end
- self.class.start_reactor if options[:start_reactor]
+ @enabled = options[:enabled]
- EM.next_tick do
- @connection = EM.connect host, port, ServerConnection, self, api_key
+ if @enabled
+ if options[:start_reactor]
+ self.class.start_reactor
+ end
+
+ EM.next_tick do
+ @connection = EM.connect host, port, ServerConnection, self, api_key
+ end
end
end
# Store a gauge for a metric, optionally at a specific time.
#
@@ -131,10 +137,14 @@
def increment(metric, value = 1, time = Time.now)
valid?(metric, value, time)
send_command("increment", metric, value, time.to_i)
end
+ def enabled?
+ @enabled
+ end
+
def connected?
connection && connection.connected
end
def logger
@@ -156,13 +166,15 @@
end
true
end
def send_command(cmd, *args)
- cmd = "%s %s\n" % [cmd, args.collect(&:to_s).join(" ")]
- logger.debug "Sending: #{cmd.chomp}"
- EM.next_tick do
- connection.send_data(cmd)
+ if enabled?
+ cmd = "%s %s\n" % [cmd, args.collect(&:to_s).join(" ")]
+ logger.debug "Sending: #{cmd.chomp}"
+ EM.next_tick do
+ connection.send_data(cmd)
+ end
end
end
end