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

#reset_telemetryObject



11
12
13
# File 'lib/datadog/statsd/connection.rb', line 11

def reset_telemetry
  telemetry.reset if telemetry
end

#write(payload) ⇒ Object

not thread safe: ‘Sender` instances that use this are required to properly synchronize or sequence calls to this method



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

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
      connect
      retry
    rescue StandardError => e
      boom = e
    end
  end

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