class Statsd::Batch

Batch: A batching statsd proxy

@example Batch a set of instruments using Batch and manual flush:

$statsd = Statsd.new 'localhost', 8125
batch = Statsd::Batch.new($statsd)
batch.increment 'garets'
batch.timing 'glork', 320
batch.gauge 'bork', 100
batch.flush

Batch is a subclass of Statsd, but with a constructor that proxies to a normal Statsd instance. It has it's own batch_size and namespace parameters (that inherit defaults from the supplied Statsd instance). It is recommended that some care is taken if setting very large batch sizes. If the batch size exceeds the allowed packet size for UDP on your network, communication troubles may occur and data will be lost.

Attributes

batch_size[RW]

Public Class Methods

new(statsd) click to toggle source

@param [Statsd] requires a configured Statsd instance

# File lib/statsd.rb, line 58
def initialize(statsd)
  @statsd = statsd
  @batch_size = statsd.batch_size
  @backlog = []
end

Public Instance Methods

easy() { |self| ... } click to toggle source

@yields [Batch] yields itself

A convenience method to ensure that data is not lost in the event of an exception being thrown. Batches will be transmitted on the parent socket as soon as the batch is full, and when the block finishes.

# File lib/statsd.rb, line 69
def easy
  yield self
ensure
  flush
end
flush() click to toggle source
# File lib/statsd.rb, line 75
def flush
  unless @backlog.empty?
    @statsd.send_to_socket @backlog.join("\n")
    @backlog.clear
  end
end

Protected Instance Methods

send_to_socket(message) click to toggle source
# File lib/statsd.rb, line 84
def send_to_socket(message)
  @backlog << message
  if @backlog.size >= @batch_size
    flush
  end
end