Class: Instana::Backend::RequestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/instana/backend/request_client.rb

Overview

Convince wrapper around Net::HTTP.

Since:

  • 1.197.0

Defined Under Namespace

Classes: Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, use_ssl: false) ⇒ RequestClient

Returns a new instance of RequestClient.

Since:

  • 1.197.0



37
38
39
40
41
42
43
44
45
# File 'lib/instana/backend/request_client.rb', line 37

def initialize(host, port, use_ssl: false)
  timeout = Integer(ENV.fetch('INSTANA_TIMEOUT', 500))
  @host = host
  @port = port
  @use_ssl = use_ssl
  @timeout = timeout
  @client_mutex = Mutex.new
  @client = nil
end

Instance Attribute Details

#hostObject (readonly)

Since:

  • 1.197.0



23
24
25
# File 'lib/instana/backend/request_client.rb', line 23

def host
  @host
end

#portObject (readonly)

Since:

  • 1.197.0



23
24
25
# File 'lib/instana/backend/request_client.rb', line 23

def port
  @port
end

Instance Method Details

#send_request(method, path, data = nil, headers = {}) ⇒ Object

Send a request to the backend. If data is a Hash, encode the object as JSON and set the proper headers.

Parameters:

  • method (String)

    request method

  • path (String)

    request path

  • data (Hash, String) (defaults to: nil)

    request body

  • headers (Hash) (defaults to: {})

    extra request headers to send

Since:

  • 1.197.0



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/instana/backend/request_client.rb', line 54

def send_request(method, path, data = nil, headers = {})
  body = if data.is_a?(Hash) || data.is_a?(Array)
           headers['Content-Type'] = 'application/json'
           headers['Accept'] = 'application/json'

           encode_body(data)
         else
           headers['Content-Type'] = 'application/octet-stream'

           data
         end
  begin
    response = @client_mutex.synchronize do
      ensure_connection
      @client.send_request(method, path, body, headers)
    end
    Response.new(response)
  rescue Errno::ECONNREFUSED => e
    Instana.logger.debug("Connection refused to #{@host}:#{@port} - #{e.message}")
    create_error_response('503', 'Connection Refused', 'Connection refused', e.message)
  rescue Errno::EHOSTUNREACH => e
    Instana.logger.debug("Host unreachable #{@host}:#{@port} - #{e.message}")
    create_error_response('503', 'Host Unreachable', 'Host unreachable', e.message)
  rescue Errno::ETIMEDOUT, Net::OpenTimeout, Net::ReadTimeout => e
    Instana.logger.debug("Timeout connecting to #{@host}:#{@port} - #{e.message}")
    create_error_response('408', 'Request Timeout', 'Timeout', e.message)
  rescue SocketError => e
    Instana.logger.debug("Socket error connecting to #{@host}:#{@port} - #{e.message}")
    create_error_response('502', 'Socket Error', 'Socket error', e.message)
  rescue IOError => e
    Instana.logger.debug("IO error sending request to #{@host}:#{@port} - #{e.message}")
    # Reset connection on IO errors and retry once
    @client_mutex.synchronize { reset_connection }
    create_error_response('500', 'IO Error', 'IOError', e.message)
  rescue StandardError => e
    Instana.logger.debug("Error sending request to #{@host}:#{@port} - #{e.class}: #{e.message}")
    create_error_response('500', 'Internal Error', e.class.to_s, e.message)
  end
end