Class: StackifyRubyAPM::UnixSocketClient Private

Inherits:
AgentBaseTransport show all
Includes:
Log
Defined in:
lib/stackify_apm/transport/unix_socket_client.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This class will handle the sending of protobuf messages through unix domain socket.

Constant Summary

Constants included from Log

Log::PREFIX

Instance Method Summary collapse

Methods included from Log

#debug, #error, #fatal, #info, #log, #warn

Methods inherited from AgentBaseTransport

#build_message

Constructor Details

#initialize(config) ⇒ UnixSocketClient

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of UnixSocketClient.



12
13
14
15
# File 'lib/stackify_apm/transport/unix_socket_client.rb', line 12

def initialize(config)
  @config = config
  super(config)
end

Instance Method Details

#post(transactions = []) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

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



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
51
52
53
# File 'lib/stackify_apm/transport/unix_socket_client.rb', line 22

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
    protobuf_obj = build_message(transactions)
    message = StackifyProtoBuf::Traces.encode(protobuf_obj)
    client = NetX::HTTPUnix.new('unix://' + @config.unix_socket_path)
    req = Net::HTTP::Post.new(@config.agent_traces_url)
    req.set_content_type('application/x-protobuf')
    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