Class: Statsd::Batch

Inherits:
Statsd show all
Extended by:
Forwardable
Defined in:
lib/statsd.rb

Overview

Batch: A batching statsd proxy

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.

Examples:

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

Instance Attribute Summary collapse

Attributes inherited from Statsd

#delimiter, #host, #namespace, #port, #postfix, #prefix, #stat_delimiter

Instance Method Summary collapse

Methods inherited from Statsd

#batch, #connect, #count, #decrement, #gauge, #increment, #set, #time, #timing

Constructor Details

#initialize(statsd) ⇒ Batch

Returns a new instance of Batch.

Parameters:

  • requires (Statsd)

    a configured Statsd instance



59
60
61
62
63
# File 'lib/statsd.rb', line 59

def initialize(statsd)
  @statsd = statsd
  @batch_size = statsd.batch_size
  @backlog = []
end

Instance Attribute Details

#batch_sizeObject

Returns the value of attribute batch_size.



56
57
58
# File 'lib/statsd.rb', line 56

def batch_size
  @batch_size
end

Instance Method Details

#easyObject

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.



70
71
72
73
74
# File 'lib/statsd.rb', line 70

def easy
  yield self
ensure
  flush
end

#flushObject



76
77
78
79
80
81
# File 'lib/statsd.rb', line 76

def flush
  unless @backlog.empty?
    @statsd.send_to_socket @backlog.join("\n")
    @backlog.clear
  end
end