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 transaction messages through HTTP.

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



13
14
15
16
# File 'lib/stackify_apm/transport/agent_http_client.rb', line 13

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.



24
25
26
27
28
# File 'lib/stackify_apm/transport/agent_http_client.rb', line 24

def get_json_headers
  {
    'Content-Type' => 'application/json'
  }.freeze
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.



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

def get_protobuf_headers
  {
    'Content-Type' => 'application/x-protobuf'
  }.freeze
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 transction message to HTTP request. It will accept Array of transactions.



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
61
62
63
64
# File 'lib/stackify_apm/transport/agent_http_client.rb', line 35

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
    message = get_json_message(transactions)
    conn = Faraday.new(ssl: { verify: false })
    response = conn.post do |req|
      req.url URI(@config.transport_http_endpoint + @config.agent_traces_url).to_s
      req.headers = get_json_headers
      req.body = message
    end
    if defined?(response.status) && response.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