Class: Statsd::Batch

Inherits:
Client show all
Defined in:
lib/statsd.rb

Overview

Some more unabashed borrowing: github.com/reinh/statsd/blob/master/lib/statsd.rb#L410 The big difference between this implementation and reinh’s is that we don’t support namespaces, and we have a bunch of hacks for introducing prefixes to the namespaces we’re acting against.

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 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 Client

#host, #port, #prefix, #resolve_always

Instance Method Summary collapse

Methods inherited from Client

#batch, #decrement, #gauge, #host_ip_addr, #increment, #send_data, #timing, #update_counter

Constructor Details

#initialize(statsd) ⇒ Batch

Returns a new instance of Batch.

Parameters:

  • requires (Statsd)

    a configured Statsd instance



178
179
180
181
182
183
184
185
186
# File 'lib/statsd.rb', line 178

def initialize(statsd)
  @statsd = statsd
  @batch_size = statsd.batch_size
  @backlog = []
  @send_data = send_method
  @host = statsd.host
  @port = statsd.port
  @prefix = statsd.prefix
end

Instance Attribute Details

#batch_sizeObject

Returns the value of attribute batch_size.



175
176
177
# File 'lib/statsd.rb', line 175

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.



193
194
195
196
197
# File 'lib/statsd.rb', line 193

def easy
  yield self
ensure
  flush
end

#flushObject



199
200
201
202
203
204
# File 'lib/statsd.rb', line 199

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

#send_batch_data(message) ⇒ Object



206
207
208
209
210
211
# File 'lib/statsd.rb', line 206

def send_batch_data(message)
  @backlog << message
  if @backlog.size >= @batch_size
    flush
  end
end

#send_methodObject



213
214
215
216
217
218
219
220
# File 'lib/statsd.rb', line 213

def send_method
  lambda { |data|
    @backlog << data
    if @backlog.size >= @batch_size
      flush
    end
  }
end