lib/elasticsearch/transport/transport/base.rb in elasticsearch-transport-6.0.1 vs lib/elasticsearch/transport/transport/base.rb in elasticsearch-transport-6.0.2
- old
+ new
@@ -182,15 +182,18 @@
# Trace the request in the `curl` format
#
# @api private
#
- def __trace(method, path, params, body, url, response, json, took, duration)
+ def __trace(method, path, params, headers, body, url, response, json, took, duration)
trace_url = "http://localhost:9200/#{path}?pretty" +
( params.empty? ? '' : "&#{::Faraday::Utils::ParamsHash[params].to_query}" )
trace_body = body ? " -d '#{__convert_to_json(body, :pretty => true)}'" : ''
- tracer.info "curl -X #{method.to_s.upcase} '#{trace_url}'#{trace_body}\n"
+ trace_command = "curl -X #{method.to_s.upcase}"
+ trace_command += " -H '#{headers.inject('') { |memo,item| memo << item[0] + ': ' + item[1] }}'" if headers && !headers.empty?
+ trace_command += " '#{trace_url}'#{trace_body}\n"
+ tracer.info trace_command
tracer.debug "# #{Time.now.iso8601} [#{response.status}] (#{format('%.3f', duration)}s)\n#"
tracer.debug json ? serializer.dump(json, :pretty => true).gsub(/^/, '# ').sub(/\}$/, "\n# }")+"\n" : "# #{response.body}\n"
end
# Raise error specific for the HTTP response status or a generic server error
@@ -231,18 +234,19 @@
#
# @param method [String] Request method
# @param path [String] The API endpoint
# @param params [Hash] Request parameters (will be serialized by {Connections::Connection#full_url})
# @param body [Hash] Request body (will be serialized by the {#serializer})
+ # @param headers [Hash] Request headers (will be serialized by the {#serializer})
# @param block [Proc] Code block to evaluate, passed from the implementation
#
# @return [Response]
# @raise [NoMethodError] If no block is passed
# @raise [ServerError] If request failed on server
# @raise [Error] If no connection is available
#
- def perform_request(method, path, params={}, body=nil, &block)
+ def perform_request(method, path, params={}, body=nil, headers=nil, &block)
raise NoMethodError, "Implement this method in your transport class" unless block_given?
start = Time.now if logger || tracer
tries = 0
params = params.clone
@@ -309,11 +313,11 @@
duration = Time.now-start if logger || tracer
if response.status.to_i >= 300
__log method, path, params, body, url, response, nil, 'N/A', duration if logger
- __trace method, path, params, body, url, response, nil, 'N/A', duration if tracer
+ __trace method, path, params, headers, body, url, response, nil, 'N/A', duration if tracer
# Log the failure only when `ignore` doesn't match the response status
__log_failed response if logger && !ignore.include?(response.status.to_i)
__raise_transport_error response unless ignore.include?(response.status.to_i)
@@ -321,10 +325,10 @@
json = serializer.load(response.body) if response.body && !response.body.empty? && response.headers && response.headers["content-type"] =~ /json/
took = (json['took'] ? sprintf('%.3fs', json['took']/1000.0) : 'n/a') rescue 'n/a' if logger || tracer
__log method, path, params, body, url, response, json, took, duration if logger && !ignore.include?(response.status.to_i)
- __trace method, path, params, body, url, response, json, took, duration if tracer
+ __trace method, path, params, headers, body, url, response, json, took, duration if tracer
Response.new response.status, json || response.body, response.headers
ensure
@last_request_at = Time.now
end