Class: LogStash::Outputs::ElasticSearch::HttpClient::ManticoreAdapter
- Inherits:
-
Object
- Object
- LogStash::Outputs::ElasticSearch::HttpClient::ManticoreAdapter
- Defined in:
- lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#manticore ⇒ Object
readonly
Returns the value of attribute manticore.
Instance Method Summary collapse
- #client ⇒ Object
- #close ⇒ Object
-
#format_url(url, path_and_query = nil) ⇒ Object
Returned urls from this method should be checked for double escaping.
-
#initialize(logger, options) ⇒ ManticoreAdapter
constructor
A new instance of ManticoreAdapter.
- #log_java_exception(e, level = :debug) ⇒ Object
- #log_request_error(e) ⇒ Object
-
#manticore_proxy_hash(proxy_uri) ⇒ Object
Transform the proxy option to a hash.
-
#perform_request(url, method, path, params = {}, body = nil) ⇒ Response
Performs the request by invoking Transport::Base#perform_request with a block.
-
#remove_double_escaping(url) ⇒ Object
Later versions of SafeURI will also escape the ‘%’ sign in an already escaped URI.
Constructor Details
#initialize(logger, options) ⇒ ManticoreAdapter
Returns a new instance of ManticoreAdapter.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb', line 10 def initialize(logger, ) @logger = logger = .dup [:ssl] = [:ssl] || {} # We manage our own retries directly, so let's disable them here [:automatic_retries] = 0 # We definitely don't need cookies [:cookies] = false @client_params = {:headers => DEFAULT_HEADERS.merge([:headers] || {})} if [:proxy] [:proxy] = manticore_proxy_hash([:proxy]) end @manticore = ::Manticore::Client.new() end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
8 9 10 |
# File 'lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb', line 8 def logger @logger end |
#manticore ⇒ Object (readonly)
Returns the value of attribute manticore.
8 9 10 |
# File 'lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb', line 8 def manticore @manticore end |
Instance Method Details
#client ⇒ Object
40 41 42 |
# File 'lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb', line 40 def client @manticore end |
#close ⇒ Object
147 148 149 |
# File 'lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb', line 147 def close @manticore.close end |
#format_url(url, path_and_query = nil) ⇒ Object
Returned urls from this method should be checked for double escaping.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb', line 114 def format_url(url, path_and_query=nil) request_uri = url.clone # We excise auth info from the URL in case manticore itself tries to stick # sensitive data in a thrown exception or log data request_uri.user = nil request_uri.password = nil return request_uri.to_s if path_and_query.nil? parsed_path_and_query = java.net.URI.new(path_and_query) new_query_parts = [request_uri.query, parsed_path_and_query.query].select do |part| part && !part.empty? # Skip empty nil and "" end request_uri.query = new_query_parts.join("&") unless new_query_parts.empty? # use `raw_path`` as `path` will unescape any escaped '/' in the path request_uri.path = "#{request_uri.path}/#{parsed_path_and_query.raw_path}".gsub(/\/{2,}/, "/") request_uri end |
#log_java_exception(e, level = :debug) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb', line 105 def log_java_exception(e, level = :debug) return unless e.is_a?(java.lang.Exception) # @logger.name using the same convention as LS does logger = self.class.name.gsub('::', '.').downcase logger = org.apache.logging.log4j.LogManager.getLogger(logger) logger.send(level, '', e) # logger.error('', e) - prints nested causes end |
#log_request_error(e) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb', line 87 def log_request_error(e) details = { message: e., exception: e.class } details[:cause] = e.cause if e.respond_to?(:cause) details[:backtrace] = e.backtrace if @logger.debug? level = case e when ::Manticore::Timeout :debug when ::Manticore::UnknownException :warn else :info end @logger.send level, "Failed to perform request", details log_java_exception(details[:cause], :debug) if details[:cause] && @logger.debug? end |
#manticore_proxy_hash(proxy_uri) ⇒ Object
Transform the proxy option to a hash. Manticore’s support for non-hash proxy options is broken. This was fixed in github.com/cheald/manticore/commit/34a00cee57a56148629ed0a47c329181e7319af5 but this is not yet released
32 33 34 35 36 37 38 |
# File 'lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb', line 32 def manticore_proxy_hash(proxy_uri) [:scheme, :port, :user, :password, :path].reduce(:host => proxy_uri.host) do |acc,opt| value = proxy_uri.send(opt) acc[opt] = value unless value.nil? || (value.is_a?(String) && value.empty?) acc end end |
#perform_request(url, method, path, params = {}, body = nil) ⇒ Response
Performs the request by invoking Transport::Base#perform_request with a block.
49 50 51 52 53 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 |
# File 'lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb', line 49 def perform_request(url, method, path, params={}, body=nil) # Perform 2-level deep merge on the params, so if the passed params and client params will both have hashes stored on a key they # will be merged as well, instead of choosing just one of the values params = (params || {}).merge(@client_params) { |key, oldval, newval| (oldval.is_a?(Hash) && newval.is_a?(Hash)) ? oldval.merge(newval) : newval } params[:body] = body if body if url.user params[:auth] = { :user => CGI.unescape(url.user), # We have to unescape the password here since manticore won't do it # for us unless its part of the URL :password => CGI.unescape(url.password), :eager => true } end request_uri = format_url(url, path) request_uri_as_string = remove_double_escaping(request_uri.to_s) begin resp = @manticore.send(method.downcase, request_uri_as_string, params) # Manticore returns lazy responses by default # We want to block for our usage, this will wait for the response to finish resp.call rescue ::Manticore::ManticoreException => e log_request_error(e) raise ::LogStash::Outputs::ElasticSearch::HttpClient::Pool::HostUnreachableError.new(e, request_uri_as_string) end code = resp.code if code < 200 || code > 299 # assume anything not 2xx is an error that the layer above needs to interpret raise ::LogStash::Outputs::ElasticSearch::HttpClient::Pool::BadResponseCodeError.new(code, request_uri, body, resp.body) end resp end |
#remove_double_escaping(url) ⇒ Object
Later versions of SafeURI will also escape the ‘%’ sign in an already escaped URI. (If the path variable is used, it constructs a new java.net.URI object using the multi-arg constructor, which will escape any ‘%’ characters in the path, as opposed to the single-arg constructor which requires illegal characters to be already escaped, and will throw otherwise) The URI needs to have been previously escaped, as it does not play nice with an escaped ‘/’ in the middle of a URI, as required by date math, treating it as a path separator
143 144 145 |
# File 'lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb', line 143 def remove_double_escaping(url) url.gsub(/%25([0-9A-F]{2})/i, '%\1') end |