class StatsD::Instrument::Backends::UDPBackend

Constants

BASE_SUPPORTED_METRIC_TYPES
DEFAULT_IMPLEMENTATION

Attributes

host[R]
implementation[R]
port[R]

Public Class Methods

new(server = nil, implementation = nil) click to toggle source
Calls superclass method
# File lib/statsd/instrument/backends/udp_backend.rb, line 76
def initialize(server = nil, implementation = nil)
  super()
  self.server = server || "localhost:8125"
  self.implementation = (implementation || DEFAULT_IMPLEMENTATION).to_sym
end

Public Instance Methods

collect_metric(metric) click to toggle source
# File lib/statsd/instrument/backends/udp_backend.rb, line 94
def collect_metric(metric)
  unless @packet_factory.class::SUPPORTED_METRIC_TYPES[metric.type]
    StatsD.logger.warn("[StatsD] Metric type #{metric.type.inspect} is not supported " \
      "on #{implementation} implementation.")
    return false
  end

  if metric.sample_rate < 1.0 && rand > metric.sample_rate
    return false
  end

  write_packet(@packet_factory.generate_packet(metric))
end
host=(host) click to toggle source
# File lib/statsd/instrument/backends/udp_backend.rb, line 114
def host=(host)
  @host = host
  invalidate_socket
end
implementation=(value) click to toggle source
# File lib/statsd/instrument/backends/udp_backend.rb, line 82
def implementation=(value)
  @packet_factory = case value
  when :datadog
    DogStatsDProtocol.new
  when :statsite
    StatsiteStatsDProtocol.new
  else
    StatsDProtocol.new
  end
  @implementation = value
end
invalidate_socket() click to toggle source
# File lib/statsd/instrument/backends/udp_backend.rb, line 146
def invalidate_socket
  synchronize do
    @socket = nil
  end
end
port=(port) click to toggle source
# File lib/statsd/instrument/backends/udp_backend.rb, line 119
def port=(port)
  @port = port
  invalidate_socket
end
server=(connection_string) click to toggle source
# File lib/statsd/instrument/backends/udp_backend.rb, line 108
def server=(connection_string)
  @host, @port = connection_string.split(':', 2)
  @port = @port.to_i
  invalidate_socket
end
socket() click to toggle source
# File lib/statsd/instrument/backends/udp_backend.rb, line 124
def socket
  if @socket.nil?
    @socket = UDPSocket.new
    @socket.connect(host, port)
  end
  @socket
end
write_packet(command) click to toggle source
# File lib/statsd/instrument/backends/udp_backend.rb, line 132
def write_packet(command)
  synchronize do
    socket.send(command, 0) > 0
  end
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 command without a lock.
  socket.send(command, 0) > 0
rescue SocketError, IOError, SystemCallError => e
  StatsD.logger.error("[StatsD] #{e.class.name}: #{e.message}")
  invalidate_socket
end