class Rack::StatsdBatch::Recorder

Attributes

data[R]

Public Class Methods

new() click to toggle source
# File lib/rack/statsd_batch.rb, line 22
def initialize
  @data = []
end

Public Instance Methods

count(key, diff) click to toggle source
# File lib/rack/statsd_batch.rb, line 26
def count key, diff
  @data << "#{key}:#{diff}|c"
end
gauge(key, value) click to toggle source
# File lib/rack/statsd_batch.rb, line 34
def gauge key, value
  @data << "#{key}:#{value}|g"
end
gauge_diff(key, diff) click to toggle source
# File lib/rack/statsd_batch.rb, line 38
def gauge_diff key, diff
  value = "#{diff}"
  value = "+#{value}" if diff > 0
  @data << "#{key}:#{value}|g"
end
publish(host, port, mtu) click to toggle source
# File lib/rack/statsd_batch.rb, line 48
def publish host, port, mtu
  return if @data.empty?
  c = connection(host, port)
  build_packets(mtu).each {|p| c.send(p) }
end
sets(key, set_entry) click to toggle source
# File lib/rack/statsd_batch.rb, line 44
def sets key, set_entry
  @data << "#{key}:#{set_entry}|s"
end
timing(key, ms) click to toggle source
# File lib/rack/statsd_batch.rb, line 30
def timing key, ms
  @data << "#{key}:#{ms}|ms"
end

Private Instance Methods

build_packets(mtu) click to toggle source
# File lib/rack/statsd_batch.rb, line 60
def build_packets mtu
  rv = []
  packet = ''
  while !data.empty?
    message = data.shift
    newline_length = packet.empty? ? 0 : 1
    if packet.length + message.length + newline_length > mtu
      rv << packet
      packet = ''
    end
    message = "\n#{message}" unless packet.empty?
    packet << message
  end
  (rv << packet) unless packet.empty?
  rv
end
connection(host, port) click to toggle source
# File lib/rack/statsd_batch.rb, line 56
def connection host, port
  Rack::StatsdBatch::Connection.new(UDPSocket.new, host, port)
end