class StackifyRubyAPM::UnixSocketClient

This class will handle the sending of transaction messages through unix domain socket. @api private

Public Class Methods

new(config) click to toggle source
# File lib/stackify_apm/transport/unix_socket_client.rb, line 12
def initialize(config)
  @config = config
  super(config)
end

Public Instance Methods

get_json_headers() click to toggle source
# File lib/stackify_apm/transport/unix_socket_client.rb, line 21
def get_json_headers
  'application/json'
end
get_protobuf_headers() click to toggle source
# File lib/stackify_apm/transport/unix_socket_client.rb, line 17
def get_protobuf_headers
  'application/x-protobuf'
end
post(transactions = []) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity

This method will send a transaction message to the unix domain socket. It will accept Array of transactions.

# File lib/stackify_apm/transport/unix_socket_client.rb, line 30
def post(transactions = [])
  debug '[UnixSocketClient] post()' if ENV['STACKIFY_TRANSPORT_LOG_LEVEL'] == '0'
  return unless ENV['STACKIFY_RUBY_ENV'] != 'rspec'
  max_retries = @config.max_retries
  retry_count = 0
  delay = @config.delay_seconds
  begin
    # Convert message into binary and send it to unix domain socket
    message = get_json_message(transactions)
    client = NetX::HTTPUnix.new('unix://' + @config.unix_socket_path)
    req = Net::HTTP::Post.new(@config.agent_traces_url)
    req.set_content_type(get_json_headers)
    req.body = message
    response = client.request(req)
    debug "[UnixSocketClient] status_code = #{response.code}" if ENV['STACKIFY_TRANSPORT_LOG_LEVEL'] == '0'
    if response.code.to_i == 200
      debug '[UnixSocketClient] Successfully sent message via unix domain socket.' if ENV['STACKIFY_TRANSPORT_LOG_LEVEL'] == '0'
    elsif ENV['STACKIFY_TRANSPORT_LOG_LEVEL'] == '0'
      debug "[UnixSocketClient] Failure sending via unix domain socket: #{response.inspect}"
    end
  rescue StandardError => e
    debug '[UnixSocketClient] All retries are exhausted!' if retry_count >= max_retries
    retry_count += 1
    if retry_count < max_retries
      debug "[UnixSocketClient] post() exception: #{e.inspect}"
      debug "[UnixSocketClient] An error occured. Retries left: #{max_retries - retry_count}"
    end
    sleep delay += retry_count
    retry if retry_count < max_retries + 1
  end
end