class Invoca::Metrics::StatsdClient

Constants

MILLISECONDS_IN_SECOND

Attributes

log_send_failures[RW]

Public Instance Methods

send_to_socket(message) click to toggle source
# File lib/invoca/metrics/statsd_client.rb, line 24
def send_to_socket(message)
  self.class.logger&.debug { "Statsd: #{message}" }
  socket.send(message, 0)
rescue => ex
  if self.class.log_send_failures
    self.class.logger&.error { "Statsd exception sending: #{ex.class}: #{ex}" }
  end

  nil
end
time(stat, sample_rate = 1) { || ... } click to toggle source
# File lib/invoca/metrics/statsd_client.rb, line 16
def time(stat, sample_rate = 1)
  start = Time.now
  result = yield
  length_of_time = ((Time.now - start) * MILLISECONDS_IN_SECOND).round
  timing(stat, length_of_time, sample_rate)
  [result, length_of_time]
end

Private Instance Methods

new_socket() click to toggle source
# File lib/invoca/metrics/statsd_client.rb, line 43
def new_socket
  self.class.logger&.info { "Statsd client connection info -- [hostname: #{@host}, port: #{@port}]" }
  UDPSocket.new.tap { |udp| udp.connect(@host, @port) }
end
socket() click to toggle source

Socket connection should be Thread local and not Fiber local Can’t use ‘Thread.current ||=` because that will be fiber-local as well.

# File lib/invoca/metrics/statsd_client.rb, line 39
def socket
  Thread.current.thread_variable_get(:statsd_socket) || Thread.current.thread_variable_set(:statsd_socket, new_socket)
end