Class: Datadog::Statsd::Connection

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

Direct Known Subclasses

UDPConnection, UDSConnection

Instance Method Summary collapse

Constructor Details

#initialize(telemetry: nil, logger: nil) ⇒ Connection

Returns a new instance of Connection.


6
7
8
9
# File 'lib/datadog/statsd/connection.rb', line 6

def initialize(telemetry: nil, logger: nil)
  @telemetry = telemetry
  @logger = logger
end

Instance Method Details

#closeObject

Close the underlying socket


12
13
14
15
16
17
18
19
# File 'lib/datadog/statsd/connection.rb', line 12

def close
  begin
    @socket && @socket.close if instance_variable_defined?(:@socket)
  rescue StandardError => boom
    logger.error { "Statsd: #{boom.class} #{boom}" } if logger
  end
  @socket = nil
end

#write(payload) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/datadog/statsd/connection.rb', line 21

def write(payload)
  logger.debug { "Statsd: #{payload}" } if logger

  send_message(payload)

  telemetry.sent(packets: 1, bytes: payload.length) if telemetry

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

  telemetry.dropped(packets: 1, bytes: payload.length) if telemetry
  logger.error { "Statsd: #{boom.class} #{boom}" } if logger
  nil
end