class StatsD::Instrument::UDPSink
@note This class is part of the new Client implementation that is intended
to become the new default in the next major release of this library.
Attributes
host[R]
port[R]
Public Class Methods
for_addr(addr)
click to toggle source
# File lib/statsd/instrument/udp_sink.rb, line 6 def self.for_addr(addr) host, port_as_string = addr.split(':', 2) new(host, Integer(port_as_string)) end
new(host, port)
click to toggle source
# File lib/statsd/instrument/udp_sink.rb, line 13 def initialize(host, port) @host = host @port = port @mutex = Mutex.new @socket = nil end
Public Instance Methods
<<(datagram)
click to toggle source
# File lib/statsd/instrument/udp_sink.rb, line 24 def <<(datagram) with_socket { |socket| socket.send(datagram, 0) > 0 } self rescue ThreadError # In cases where a TERM or KILL signal has been sent, and we send stats as # part of a signal handler, locks cannot be acquired, so we do our best # to try and send the datagram without a lock. socket.send(datagram, 0) > 0 rescue SocketError, IOError, SystemCallError # TODO: log? invalidate_socket end
addr()
click to toggle source
# File lib/statsd/instrument/udp_sink.rb, line 39 def addr "#{host}:#{port}" end
sample?(sample_rate)
click to toggle source
# File lib/statsd/instrument/udp_sink.rb, line 20 def sample?(sample_rate) sample_rate == 1 || rand < sample_rate end
Private Instance Methods
invalidate_socket()
click to toggle source
# File lib/statsd/instrument/udp_sink.rb, line 57 def invalidate_socket @mutex.synchronize do @socket = nil end end
socket()
click to toggle source
# File lib/statsd/instrument/udp_sink.rb, line 49 def socket if @socket.nil? @socket = UDPSocket.new @socket.connect(@host, @port) end @socket end
with_socket() { |socket| ... }
click to toggle source
# File lib/statsd/instrument/udp_sink.rb, line 45 def with_socket @mutex.synchronize { yield(socket) } end