Sha256: 65cdb580e3de8d5431242abb1ef2cfceb482899ff5115a87a7ececed3158257a

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Datadog
  class Statsd
    class Connection
      def initialize(telemetry)
        @telemetry = telemetry
      end

      # Close the underlying socket
      def close
        @socket && @socket.close
      end

      def write(payload)
        logger.debug { "Statsd: #{payload}" } if logger
        flush_telemetry = @telemetry.flush?
        if flush_telemetry
          payload += @telemetry.flush()
        end

        send_message(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.message =~ /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

      private

      attr_reader :telemetry
      attr_reader :logger

      def socket
        @socket ||= connect
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dogstatsd-ruby-4.7.0 lib/datadog/statsd/connection.rb