Class: StatsD::Instrument::UDPSink

Inherits:
Object
  • Object
show all
Defined in:
lib/statsd/instrument/udp_sink.rb

Overview

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.

Constant Summary collapse

FINALIZER =
->(object_id) do
  Thread.list.each do |thread|
    if (store = thread["StatsD::UDPSink"])
      store.delete(object_id)&.close
    end
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ UDPSink

Returns a new instance of UDPSink.



25
26
27
28
29
# File 'lib/statsd/instrument/udp_sink.rb', line 25

def initialize(host, port)
  ObjectSpace.define_finalizer(self, FINALIZER)
  @host = host
  @port = port
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



15
16
17
# File 'lib/statsd/instrument/udp_sink.rb', line 15

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



15
16
17
# File 'lib/statsd/instrument/udp_sink.rb', line 15

def port
  @port
end

Class Method Details

.for_addr(addr) ⇒ Object



9
10
11
12
# File 'lib/statsd/instrument/udp_sink.rb', line 9

def for_addr(addr)
  host, port_as_string = addr.split(":", 2)
  new(host, Integer(port_as_string))
end

Instance Method Details

#<<(datagram) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/statsd/instrument/udp_sink.rb', line 35

def <<(datagram)
  retried = false
  begin
    socket.send(datagram, 0)
  rescue SocketError, IOError, SystemCallError => error
    StatsD.logger.debug do
      "[StatsD::Instrument::UDPSink] Resetting connection because of #{error.class}: #{error.message}"
    end
    invalidate_socket
    if retried
      StatsD.logger.warn do
        "[#{self.class.name}] Events were dropped because of #{error.class}: #{error.message}"
      end
    else
      retried = true
      retry
    end
  end
  self
end

#sample?(sample_rate) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/statsd/instrument/udp_sink.rb', line 31

def sample?(sample_rate)
  sample_rate == 1.0 || rand < sample_rate
end