Class: Lookout::Batch

Inherits:
StatsdClient show all
Defined in:
lib/lookout/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 StatsdClient

#host, #port, #prefix, #resolve_always

Instance Method Summary collapse

Methods inherited from StatsdClient

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

Constructor Details

#initialize(statsd) ⇒ Batch

Returns a new instance of Batch.

Parameters:

  • requires (Statsd)

    a configured Statsd instance



222
223
224
225
226
227
228
229
230
# File 'lib/lookout/statsd.rb', line 222

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.



219
220
221
# File 'lib/lookout/statsd.rb', line 219

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.



237
238
239
240
241
# File 'lib/lookout/statsd.rb', line 237

def easy
  yield self
ensure
  flush
end

#flushObject



243
244
245
246
247
248
# File 'lib/lookout/statsd.rb', line 243

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

#send_batch_data(message) ⇒ Object



250
251
252
253
254
255
# File 'lib/lookout/statsd.rb', line 250

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

#send_methodObject



257
258
259
260
261
262
263
264
# File 'lib/lookout/statsd.rb', line 257

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