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 transaction 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_json_message, #build_message, #get_json_message, #get_protobuf_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

#get_json_headersObject

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.



21
22
23
# File 'lib/stackify_apm/transport/unix_socket_client.rb', line 21

def get_json_headers
  'application/json'
end

#get_protobuf_headersObject

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.



17
18
19
# File 'lib/stackify_apm/transport/unix_socket_client.rb', line 17

def get_protobuf_headers
  'application/x-protobuf'
end

#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 transaction message to the unix domain socket. It will accept Array of transactions.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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