Class: StackifyRubyAPM::AgentHTTPClient Private

Inherits:
AgentBaseTransport show all
Includes:
Log
Defined in:
lib/stackify_apm/transport/agent_http_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 HTTP.

Constant Summary collapse

HEADERS =

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

{
  'Content-Type' => 'application/x-protobuf'
}.freeze

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) ⇒ AgentHTTPClient

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 AgentHTTPClient.



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

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 HTTP request. It will accept Array of transactions.



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
54
55
56
57
58
# File 'lib/stackify_apm/transport/agent_http_client.rb', line 27

def post(transactions = [])
  debug '[AgentHTTPClient] 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
    protobuf_obj = build_message(transactions)
    message = StackifyProtoBuf::Traces.encode(protobuf_obj)
    # Convert message into binary and send it to http request
    conn = Faraday.new(ssl: { verify: false })
    response = conn.post do |req|
      req.url URI(@config.transport_http_endpoint + @config.agent_traces_url)
      req.headers = HEADERS
      req.body = message
    end
    if response.try(:status) == 200
      debug '[AgentHTTPClient] Successfully send message via http request.' if ENV['STACKIFY_TRANSPORT_LOG_LEVEL'] == '0'
    elsif ENV['STACKIFY_TRANSPORT_LOG_LEVEL'] == '0'
      debug "[AgentHTTPClient] Failure sending via http request: #{response.inspect}"
    end
  rescue StandardError => e
    debug '[AgentHTTPClient] All retries are exhausted!' if retry_count >= max_retries
    retry_count += 1
    if retry_count < max_retries
      debug "[AgentHTTPClient] post() exception: #{e.inspect}"
      debug "[AgentHTTPClient] An error occured. Retries left: #{max_retries - retry_count}"
    end
    sleep delay += retry_count
    retry if retry_count < max_retries + 1
  end
end