Skip to content

Commit 2b8c8db

Browse files
committed
[CLIENT] Fixes tracing for Manticore
Closes #1426
1 parent 2c47a9c commit 2b8c8db

File tree

3 files changed

+73
-40
lines changed

3 files changed

+73
-40
lines changed

elasticsearch-transport/lib/elasticsearch/transport/transport/base.rb

+12-13
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ def perform_request(method, path, params = {}, body = nil, headers = nil, opts =
272272
end
273273

274274
params = params.clone
275-
276275
ignore = Array(params.delete(:ignore)).compact.map { |s| s.to_i }
277276

278277
begin
@@ -284,9 +283,7 @@ def perform_request(method, path, params = {}, body = nil, headers = nil, opts =
284283
end
285284

286285
url = connection.full_url(path, params)
287-
288286
response = block.call(connection, url)
289-
290287
connection.healthy! if connection.failures > 0
291288

292289
# Raise an exception so we can catch it for `retry_on_status`
@@ -336,14 +333,10 @@ def perform_request(method, path, params = {}, body = nil, headers = nil, opts =
336333
duration = Time.now - start
337334

338335
if response.status.to_i >= 300
339-
__log_response method, path, params, body, url, response, nil, 'N/A', duration
340-
__trace method, path, params, connection.connection.headers, body, url, response, nil, 'N/A', duration if tracer
341-
336+
__log_response(method, path, params, body, url, response, nil, 'N/A', duration)
337+
__trace(method, path, params, connection_headers(connection), body, url, response, nil, 'N/A', duration) if tracer
342338
# Log the failure only when `ignore` doesn't match the response status
343-
unless ignore.include?(response.status.to_i)
344-
log_fatal "[#{response.status}] #{response.body}"
345-
end
346-
339+
log_fatal "[#{response.status}] #{response.body}" unless ignore.include?(response.status.to_i)
347340
__raise_transport_error response unless ignore.include?(response.status.to_i)
348341
end
349342

@@ -354,10 +347,8 @@ def perform_request(method, path, params = {}, body = nil, headers = nil, opts =
354347
__log_response method, path, params, body, url, response, json, took, duration
355348
end
356349

357-
__trace method, path, params, connection.connection.headers, body, url, response, nil, 'N/A', duration if tracer
358-
350+
__trace(method, path, params, connection_headers(connection), body, url, response, nil, 'N/A', duration) if tracer
359351
warnings(response.headers['warning']) if response.headers&.[]('warning')
360-
361352
Response.new response.status, json || response.body, response.headers
362353
ensure
363354
@last_request_at = Time.now
@@ -435,6 +426,14 @@ def user_agent_header(client)
435426
def warnings(warning)
436427
warn("warning: #{warning}")
437428
end
429+
430+
def connection_headers(connection)
431+
if defined?(Elasticsearch::Transport::Transport::HTTP::Manticore) && self.class == Elasticsearch::Transport::Transport::HTTP::Manticore
432+
@request_options[:headers]
433+
else
434+
connection.connection.headers
435+
end
436+
end
438437
end
439438
end
440439
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
require 'test_helper'
18+
19+
if JRUBY
20+
require 'elasticsearch/transport/transport/http/manticore'
21+
22+
class Elasticsearch::Transport::ClientManticoreIntegrationTest < Elasticsearch::Test::IntegrationTestCase
23+
context "Transport" do
24+
setup do
25+
@host, @port = ELASTICSEARCH_HOSTS.first.split(':')
26+
end
27+
28+
shutdown do
29+
begin; Object.send(:remove_const, :Manticore); rescue NameError; end
30+
end
31+
32+
should 'allow to customize the Faraday adapter to Manticore' do
33+
client = Elasticsearch::Transport::Client.new(
34+
transport_class: Elasticsearch::Transport::Transport::HTTP::Manticore,
35+
trace: true,
36+
hosts: [ { host: @host, port: @port } ]
37+
)
38+
response = client.perform_request 'GET', ''
39+
assert_respond_to(response.body, :to_hash)
40+
end
41+
end
42+
end
43+
end

elasticsearch-transport/test/integration/transport_test.rb

+18-27
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ class Elasticsearch::Transport::ClientIntegrationTest < Elasticsearch::Test::Int
2929
context "Transport" do
3030
setup do
3131
@host, @port = ELASTICSEARCH_HOSTS.first.split(':')
32+
@hosts = { hosts: [ { host: @host, port: @port } ] }
3233
begin; Object.send(:remove_const, :Patron); rescue NameError; end
3334
end
3435

3536
should "allow to customize the Faraday adapter to Typhoeus" do
3637
require 'typhoeus'
3738
require 'typhoeus/adapters/faraday'
3839

39-
transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new \
40-
:hosts => [ { host: @host, port: @port } ] do |f|
41-
f.response :logger
42-
f.adapter :typhoeus
43-
end
40+
transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new(@hosts) do |f|
41+
f.response :logger
42+
f.adapter :typhoeus
43+
end
4444

4545
client = Elasticsearch::Transport::Client.new transport: transport
4646
client.perform_request 'GET', ''
@@ -49,8 +49,7 @@ class Elasticsearch::Transport::ClientIntegrationTest < Elasticsearch::Test::Int
4949
should "allow to customize the Faraday adapter to NetHttpPersistent" do
5050
require 'net/http/persistent'
5151

52-
transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new \
53-
:hosts => [ { host: @host, port: @port } ] do |f|
52+
transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new(@hosts) do |f|
5453
f.response :logger
5554
f.adapter :net_http_persistent
5655
end
@@ -60,13 +59,10 @@ class Elasticsearch::Transport::ClientIntegrationTest < Elasticsearch::Test::Int
6059
end
6160

6261
should "allow to define connection parameters and pass them" do
63-
transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new \
64-
:hosts => [ { host: @host, port: @port } ],
65-
:options => { :transport_options => {
66-
:params => { :format => 'yaml' }
67-
}
68-
}
69-
62+
transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new(
63+
hosts: [ { host: @host, port: @port } ],
64+
options: { transport_options: { params: { :format => 'yaml' } } }
65+
)
7066
client = Elasticsearch::Transport::Client.new transport: transport
7167
response = client.perform_request 'GET', ''
7268

@@ -76,32 +72,27 @@ class Elasticsearch::Transport::ClientIntegrationTest < Elasticsearch::Test::Int
7672
should "use the Curb client" do
7773
require 'curb'
7874
require 'elasticsearch/transport/transport/http/curb'
79-
80-
transport = Elasticsearch::Transport::Transport::HTTP::Curb.new \
81-
:hosts => [ { host: @host, port: @port } ] do |curl|
82-
curl.verbose = true
83-
end
75+
transport = Elasticsearch::Transport::Transport::HTTP::Curb.new(@hosts) do |curl|
76+
curl.verbose = true
77+
end
8478

8579
client = Elasticsearch::Transport::Client.new transport: transport
8680
client.perform_request 'GET', ''
87-
end unless JRUBY
81+
end unless jruby?
8882

8983
should "deserialize JSON responses in the Curb client" do
9084
require 'curb'
9185
require 'elasticsearch/transport/transport/http/curb'
92-
93-
transport = Elasticsearch::Transport::Transport::HTTP::Curb.new \
94-
:hosts => [ { host: @host, port: @port } ] do |curl|
95-
curl.verbose = true
96-
end
86+
transport = Elasticsearch::Transport::Transport::HTTP::Curb.new(@hosts) do |curl|
87+
curl.verbose = true
88+
end
9789

9890
client = Elasticsearch::Transport::Client.new transport: transport
9991
response = client.perform_request 'GET', ''
10092

10193
assert_respond_to(response.body, :to_hash)
10294
assert_not_nil response.body['name']
10395
assert_equal 'application/json', response.headers['content-type']
104-
end unless JRUBY
96+
end unless jruby?
10597
end
106-
10798
end

0 commit comments

Comments
 (0)