Class: Datadog::Statsd::Connection
- Inherits:
-
Object
- Object
- Datadog::Statsd::Connection
- Defined in:
- lib/datadog/statsd/connection.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#close ⇒ Object
Close the underlying socket.
-
#initialize(telemetry) ⇒ Connection
constructor
A new instance of Connection.
- #write(payload) ⇒ Object
Constructor Details
#initialize(telemetry) ⇒ Connection
Returns a new instance of Connection.
6 7 8 |
# File 'lib/datadog/statsd/connection.rb', line 6 def initialize(telemetry) @telemetry = telemetry end |
Instance Method Details
#close ⇒ Object
Close the underlying socket
11 12 13 |
# File 'lib/datadog/statsd/connection.rb', line 11 def close @socket && @socket.close end |
#write(payload) ⇒ Object
15 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 45 46 47 48 49 50 |
# File 'lib/datadog/statsd/connection.rb', line 15 def write(payload) logger.debug { "Statsd: #{payload}" } if logger flush_telemetry = @telemetry.flush? if flush_telemetry payload += @telemetry.flush() end (payload) if flush_telemetry @telemetry.reset end telemetry.bytes_sent += payload.length telemetry.packets_sent += 1 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. =~ /closed stream/i) retries += 1 begin @socket = connect retry rescue StandardError => e boom = e end end telemetry.bytes_dropped += payload.length telemetry.packets_dropped += 1 logger.error { "Statsd: #{boom.class} #{boom}" } if logger nil end |