Skip to content

Commit

Permalink
Fixes tracing for Manticore
Browse files Browse the repository at this point in the history
  • Loading branch information
picandocodigo committed Sep 15, 2021
1 parent 50dc021 commit 98c81d1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 17 deletions.
29 changes: 13 additions & 16 deletions lib/elastic/transport/transport/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ def perform_request(method, path, params = {}, body = nil, headers = nil, opts =
end

params = params.clone

ignore = Array(params.delete(:ignore)).compact.map { |s| s.to_i }

begin
Expand All @@ -293,9 +292,7 @@ def perform_request(method, path, params = {}, body = nil, headers = nil, opts =
end

url = connection.full_url(path, params)

response = block.call(connection, url)

connection.healthy! if connection.failures > 0

# Raise an exception so we can catch it for `retry_on_status`
Expand Down Expand Up @@ -345,26 +342,18 @@ def perform_request(method, path, params = {}, body = nil, headers = nil, opts =
duration = Time.now - start

if response.status.to_i >= 300
__log_response method, path, params, body, url, response, nil, 'N/A', duration
__trace method, path, params, connection.connection.headers, body, url, response, nil, 'N/A', duration if tracer
__log_response(method, path, params, body, url, response, nil, 'N/A', duration)
__trace(method, path, params, connection_headers(connection), body, url, response, nil, 'N/A', duration) if tracer

# Log the failure only when `ignore` doesn't match the response status
unless ignore.include?(response.status.to_i)
log_fatal "[#{response.status}] #{response.body}"
end

log_fatal("[#{response.status}] #{response.body}") unless ignore.include?(response.status.to_i)
__raise_transport_error response unless ignore.include?(response.status.to_i)
end

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'

unless ignore.include?(response.status.to_i)
__log_response method, path, params, body, url, response, json, took, duration
end

__trace method, path, params, connection.connection.headers, body, url, response, nil, 'N/A', duration if tracer

__log_response(method, path, params, body, url, response, json, took, duration) unless ignore.include?(response.status.to_i)
__trace(method, path, params, connection_headers(connection), body, url, response, nil, 'N/A', duration) if tracer
warnings(response.headers['warning']) if response.headers&.[]('warning')

Response.new response.status, json || response.body, response.headers
Expand Down Expand Up @@ -444,6 +433,14 @@ def user_agent_header(client)
def warnings(warning)
warn("warning: #{warning}")
end

def connection_headers(connection)
if defined?(Elastic::Transport::Transport::HTTP::Manticore) && self.class == Elastic::Transport::Transport::HTTP::Manticore
@request_options[:headers]
else
connection.connection.headers
end
end
end
end
end
Expand Down
46 changes: 46 additions & 0 deletions test/integration/jruby_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

require 'test_helper'

if JRUBY
require 'elastic/transport/transport/http/manticore'

class Elastic::Transport::ClientManticoreIntegrationTest < Minitest::Test
context "Transport" do
setup do
uri = URI(HOST)
@host = {
host: uri.host,
port: uri.port,
user: uri.user,
password: uri.password
}
end

should 'allow to customize the Faraday adapter to Manticore' do
client = Elastic::Transport::Client.new(
transport_class: Elastic::Transport::Transport::HTTP::Manticore,
trace: true,
hosts: [@host]
)
response = client.perform_request 'GET', ''
assert_respond_to(response.body, :to_hash)
end
end
end
end
2 changes: 1 addition & 1 deletion test/integration/transport_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ class Elastic::Transport::ClientIntegrationTest < Minitest::Test
assert_respond_to(response.body, :to_hash)
assert_not_nil response.body['name']
assert_equal 'application/json', response.headers['content-type']
end unless JRUBY
end unless jruby?
end
end

0 comments on commit 98c81d1

Please sign in to comment.