class StatsdServer::Output::Tcp

Attributes

logger[RW]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/statsdserver/output/tcp.rb, line 9
def initialize(opts = {})
  if opts["host"].nil?
    raise ArgumentError, "missing host in [output:tcp] config section"
  end

  if opts["port"].nil?
    raise ArgumentError, "missing port in [output:tcp] config section"
  end

  @opts = opts
  @logger = Logger.new(STDOUT)
end

Public Instance Methods

send(str) click to toggle source
# File lib/statsdserver/output/tcp.rb, line 23
def send(str)
  @socket ||= connect
  begin
    @socket.write("#{str}")
  rescue => e
    # set @socket to nil to force a re-connect, then pass up the exception
    @socket.close rescue nil
    @socket = nil
    raise
  end
end

Private Instance Methods

connect() click to toggle source
# File lib/statsdserver/output/tcp.rb, line 36
def connect
  TCPSocket.new(@opts["host"], @opts["port"].to_i)
end