Class: Instana::Backend::RequestClient
- Inherits:
-
Object
- Object
- Instana::Backend::RequestClient
- Defined in:
- lib/instana/backend/request_client.rb
Overview
Convince wrapper around Net::HTTP.
Defined Under Namespace
Classes: Response
Instance Attribute Summary collapse
- #host ⇒ Object readonly
- #port ⇒ Object readonly
Instance Method Summary collapse
-
#initialize(host, port, use_ssl: false) ⇒ RequestClient
constructor
A new instance of RequestClient.
-
#send_request(method, path, data = nil, headers = {}) ⇒ Object
Send a request to the backend.
Constructor Details
#initialize(host, port, use_ssl: false) ⇒ RequestClient
Returns a new instance of RequestClient.
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
#host ⇒ Object (readonly)
23 24 25 |
# File 'lib/instana/backend/request_client.rb', line 23 def host @host end |
#port ⇒ Object (readonly)
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.
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.}") create_error_response('503', 'Connection Refused', 'Connection refused', e.) rescue Errno::EHOSTUNREACH => e Instana.logger.debug("Host unreachable #{@host}:#{@port} - #{e.}") create_error_response('503', 'Host Unreachable', 'Host unreachable', e.) rescue Errno::ETIMEDOUT, Net::OpenTimeout, Net::ReadTimeout => e Instana.logger.debug("Timeout connecting to #{@host}:#{@port} - #{e.}") create_error_response('408', 'Request Timeout', 'Timeout', e.) rescue SocketError => e Instana.logger.debug("Socket error connecting to #{@host}:#{@port} - #{e.}") create_error_response('502', 'Socket Error', 'Socket error', e.) rescue IOError => e Instana.logger.debug("IO error sending request to #{@host}:#{@port} - #{e.}") # Reset connection on IO errors and retry once @client_mutex.synchronize { reset_connection } create_error_response('500', 'IO Error', 'IOError', e.) rescue StandardError => e Instana.logger.debug("Error sending request to #{@host}:#{@port} - #{e.class}: #{e.}") create_error_response('500', 'Internal Error', e.class.to_s, e.) end end |