Class: Datadog::Statsd::Connection

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

Constant Summary collapse

DEFAULT_HOST =
'127.0.0.1'
DEFAULT_PORT =
8125

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, socket_path, logger) ⇒ Connection

Returns a new instance of Connection.



36
37
38
39
40
41
# File 'lib/datadog/statsd.rb', line 36

def initialize(host, port, socket_path, logger)
  @host = host || DEFAULT_HOST
  @port = port || DEFAULT_PORT
  @socket_path = socket_path
  @logger = logger
end

Instance Attribute Details

#hostObject (readonly)

StatsD host. Defaults to 127.0.0.1.



28
29
30
# File 'lib/datadog/statsd.rb', line 28

def host
  @host
end

#portObject (readonly)

StatsD port. Defaults to 8125.



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

def port
  @port
end

#socket_pathObject (readonly)

DogStatsd unix socket path. Not used by default.



34
35
36
# File 'lib/datadog/statsd.rb', line 34

def socket_path
  @socket_path
end

Instance Method Details

#closeObject

Close the underlying socket



80
81
82
# File 'lib/datadog/statsd.rb', line 80

def close
  @socket && @socket.close
end

#write(message) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/datadog/statsd.rb', line 43

def write(message)
  @logger.debug { "Statsd: #{message}" } if @logger
  if @socket_path.nil?
    socket.send(message, 0)
  else
    socket.sendmsg_nonblock(message)
  end
rescue StandardError => boom
  # Give up on this socket if it looks like it is bad
  bad_socket = !@socket_path.nil? && (
    boom.is_a?(Errno::ECONNREFUSED) ||
    boom.is_a?(Errno::ECONNRESET) ||
    boom.is_a?(Errno::ENOENT)
  )
  if bad_socket
    @socket = nil
    return
  end

  # Try once to reconnect if the socket has been closed
  retries ||= 1
  if retries <= 1 && boom.is_a?(Errno::ENOTCONN) or
     retries <= 1 && boom.is_a?(IOError) && boom.message =~ /closed stream/i
    retries += 1
    begin
      @socket = connect
      retry
    rescue StandardError => e
      boom = e
    end
  end

  @logger.error { "Statsd: #{boom.class} #{boom}" } if @logger
  nil
end