class Statsd::Base

Attributes

logger[RW]

Set to a standard logger instance to enable debug logging.

counter[RW]
host[RW]

StatsD host. Defaults to 127.0.0.1.

namespace[RW]
port[RW]

StatsD port. Defaults to 8125.

Public Class Methods

new(host='127.0.0.1', port=8125) click to toggle source
# File lib/statsd.rb, line 24
def initialize(host='127.0.0.1', port=8125)
  @socket     = UDPSocket.new
  @host       = host
  @port       = port
  @counter    = 1
end

Public Instance Methods

count(stat, count, sample_rate=1) click to toggle source
# File lib/statsd.rb, line 39
def count(stat, count, sample_rate=1)
  send_stat(stat, count, :c, sample_rate)
end
decrement(stat, sample_rate=1) click to toggle source
# File lib/statsd.rb, line 35
def decrement(stat, sample_rate=1)
  count stat, -1, sample_rate
end
gauge(stat, value, sample_rate=1) click to toggle source

Sends an arbitary gauge value for the given stat to the statsd server. This is useful for recording things like available disk space, memory usage, and the like, which have different semantics than counters.

@example Report the current user count:

$statsd.gauge('user.count', User.count)
# File lib/statsd.rb, line 50
def gauge(stat, value, sample_rate=1)
  send_stat(stat, value, :g, sample_rate)
end
increment(stat, sample_rate=1) click to toggle source
# File lib/statsd.rb, line 31
def increment(stat, sample_rate=1)
  count stat, 1, sample_rate
end
send_to_socket(message) click to toggle source
# File lib/statsd.rb, line 73
def send_to_socket(message)
  self.class.logger.debug { "Statsd: #{message}" } if self.class.logger
  @socket.send(message, 0, @host, @port)
rescue => boom
  self.class.logger.error { "Statsd: #{boom.class} #{boom}" } if self.class.logger
  nil
end
time(stat, sample_rate=1) { || ... } click to toggle source

Reports execution time of the provided block using {#timing}.

@example Report the time (in ms) taken to activate an account

$statsd.time('account.activate') { @account.activate! }
# File lib/statsd.rb, line 66
def time(stat, sample_rate=1)
  start = Time.now
  result = yield
  timing(stat, ((Time.now - start) * 1000).round, sample_rate)
  result
end
timing(stat, ms, sample_rate=1) click to toggle source

Sends a timing (in ms) for the given stat to the statsd server. The sample_rate determines what percentage of the time this report is sent. The statsd server then uses the sample_rate to correctly track the average timing for the stat.

# File lib/statsd.rb, line 58
def timing(stat, ms, sample_rate=1)
  send_stat(stat, ms, :ms, sample_rate)
end

Protected Instance Methods

send_stat(stat, delta, type, sample_rate=1) click to toggle source
# File lib/statsd.rb, line 83
def send_stat(stat, delta, type, sample_rate=1)
  if sample_rate == 1 or rand < sample_rate
    stat   = stat.to_s.gsub('::', '.').tr(':|@', '_')
    prefix = "#{@namespace}." unless @namespace.nil?
    rate   = "|@#{sample_rate}" unless sample_rate == 1
    send_to_socket("#{prefix}#{stat}:#{delta}|#{type}#{rate}")
  end
end