elasticsearch-transport 7.10.1 → 7.11.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 420122a402e799e70ce7a879b1cabe62cd4750fc356438c0f980a2b78d852fc4
4
- data.tar.gz: 420a016f766aa19fd8c6400ae8dd78c95d76090c8de38cc1658bbd06348c3136
3
+ metadata.gz: 1e364f5d86474b878377249e59488749009a24682da9d35e3eaeb430a0c9d9d4
4
+ data.tar.gz: 9952aede93dd570e563acef81038e9423793f4e0682b9916d5155fa37edb0792
5
5
  SHA512:
6
- metadata.gz: ed848e2746878f56203c6c3a9d73a0f995f65cf5854ef262d3504f98f6a889ee74e4c5e7c67bbe0d0a89b1a03c55ddd74e56c2d6efc65adbaabcd0f557653656
7
- data.tar.gz: c7ba8b088fad4ecb5fd55870f8c50e7fb68b66ca1c157993e69cee0db4205b6371f4b69f83431196e825f22ed08affc35be5c94f5d42b4558d22feb9e0236555
6
+ metadata.gz: be3c7ecd096850addb44e502f471ae2e88e43e3ee93dc342eb4b32b36eb994d66131e35f6458cec5026a02e7edd25e71d058efaa5a675fdacfe38c567a478a7f
7
+ data.tar.gz: 9ed532c04edc174e1eb903bb116add80f946f1610360a0294bed09d6d3f3b26444432745c67431e3611d3c478434e5afb18db9271450854c3255ef67e049e506
data/Gemfile CHANGED
@@ -32,7 +32,7 @@ if File.exist? File.expand_path('../../elasticsearch/elasticsearch.gemspec', __F
32
32
  gem 'elasticsearch', path: File.expand_path('../../elasticsearch', __FILE__), require: false
33
33
  end
34
34
 
35
- group :development do
35
+ group :development, :test do
36
36
  gem 'rspec'
37
37
  if defined?(JRUBY_VERSION)
38
38
  gem 'pry-nav'
data/README.md CHANGED
@@ -187,18 +187,24 @@ Elasticsearch::Client.new(
187
187
 
188
188
  ### Logging
189
189
 
190
- To log requests and responses to standard output with the default logger (an instance of Ruby's {::Logger} class),
191
- set the `log` argument:
190
+ To log requests and responses to standard output with the default logger (an instance of Ruby's {::Logger} class), set the `log` argument to true:
192
191
 
193
192
  ```ruby
194
- Elasticsearch::Client.new log: true
193
+ Elasticsearch::Client.new(log: true)
194
+ ```
195
+
196
+ You can also use [ecs-logging](https://github.com/elastic/ecs-logging-ruby). `ecs-logging` is a set of libraries that allows you to transform your application logs to structured logs that comply with the [Elastic Common Schema (ECS)](https://www.elastic.co/guide/en/ecs/current/ecs-reference.html):
197
+
198
+ ```ruby
199
+ logger = EcsLogging::Logger.new($stdout)
200
+ Elasticsearch::Client.new(logger: logger)
195
201
  ```
196
202
 
197
203
 
198
204
  To trace requests and responses in the _Curl_ format, set the `trace` argument:
199
205
 
200
206
  ```ruby
201
- Elasticsearch::Client.new trace: true
207
+ Elasticsearch::Client.new(trace: true)
202
208
  ```
203
209
 
204
210
  You can customize the default logger or tracer:
@@ -211,7 +217,7 @@ You can customize the default logger or tracer:
211
217
  Or, you can use a custom `::Logger` instance:
212
218
 
213
219
  ```ruby
214
- Elasticsearch::Client.new logger: Logger.new(STDERR)
220
+ Elasticsearch::Client.new(logger: Logger.new(STDERR))
215
221
  ```
216
222
 
217
223
  You can pass the client any conforming logger implementation:
@@ -223,7 +229,7 @@ log = Logging.logger['elasticsearch']
223
229
  log.add_appenders Logging.appenders.stdout
224
230
  log.level = :info
225
231
 
226
- client = Elasticsearch::Client.new logger: log
232
+ client = Elasticsearch::Client.new(logger: log)
227
233
  ```
228
234
 
229
235
  ### Custom HTTP Headers
@@ -120,8 +120,11 @@ module Elasticsearch
120
120
  #
121
121
  # @option api_key [String, Hash] :api_key Use API Key Authentication, either the base64 encoding of `id` and `api_key`
122
122
  # joined by a colon as a String, or a hash with the `id` and `api_key` values.
123
- # @option opaque_id_prefix [String] :opaque_id_prefix set a prefix for X-Opaque-Id when initializing the client. This
124
- # will be prepended to the id you set before each request if you're using X-Opaque-Id
123
+ # @option opaque_id_prefix [String] :opaque_id_prefix set a prefix for X-Opaque-Id when initializing the client.
124
+ # This will be prepended to the id you set before each request
125
+ # if you're using X-Opaque-Id
126
+ # @option enable_meta_header [Boolean] :enable_meta_header Enable sending the meta data header to Cloud.
127
+ # (Default: true)
125
128
  #
126
129
  # @yield [faraday] Access and configure the `Faraday::Connection` instance directly with a block
127
130
  #
@@ -136,6 +139,7 @@ module Elasticsearch
136
139
  @arguments[:randomize_hosts] ||= false
137
140
  @arguments[:transport_options] ||= {}
138
141
  @arguments[:http] ||= {}
142
+ @arguments[:enable_meta_header] = arguments.fetch(:enable_meta_header) { true }
139
143
  @options[:http] ||= {}
140
144
 
141
145
  set_api_key if (@api_key = @arguments[:api_key])
@@ -158,15 +162,18 @@ module Elasticsearch
158
162
  if @arguments[:transport]
159
163
  @transport = @arguments[:transport]
160
164
  else
161
- transport_class = @arguments[:transport_class] || DEFAULT_TRANSPORT_CLASS
162
- if transport_class == Transport::HTTP::Faraday
163
- @transport = transport_class.new(hosts: @seeds, options: @arguments) do |faraday|
164
- faraday.adapter(@arguments[:adapter] || __auto_detect_adapter)
165
- block&.call faraday
166
- end
167
- else
168
- @transport = transport_class.new(hosts: @seeds, options: @arguments)
169
- end
165
+ @transport_class = @arguments[:transport_class] || DEFAULT_TRANSPORT_CLASS
166
+ @transport = if @transport_class == Transport::HTTP::Faraday
167
+ @arguments[:adapter] ||= __auto_detect_adapter
168
+ set_meta_header
169
+ @transport_class.new(hosts: @seeds, options: @arguments) do |faraday|
170
+ faraday.adapter(@arguments[:adapter])
171
+ block&.call faraday
172
+ end
173
+ else
174
+ set_meta_header
175
+ @transport_class.new(hosts: @seeds, options: @arguments)
176
+ end
170
177
  end
171
178
  end
172
179
 
@@ -186,13 +193,79 @@ module Elasticsearch
186
193
 
187
194
  def set_api_key
188
195
  @api_key = __encode(@api_key) if @api_key.is_a? Hash
196
+ add_header('Authorization' => "ApiKey #{@api_key}")
197
+ @arguments.delete(:user)
198
+ @arguments.delete(:password)
199
+ end
200
+
201
+ def add_header(header)
189
202
  headers = @arguments[:transport_options]&.[](:headers) || {}
190
- headers.merge!('Authorization' => "ApiKey #{@api_key}")
203
+ headers.merge!(header)
191
204
  @arguments[:transport_options].merge!(
192
205
  headers: headers
193
206
  )
194
- @arguments.delete(:user)
195
- @arguments.delete(:password)
207
+ end
208
+
209
+ def set_meta_header
210
+ return if @arguments[:enable_meta_header] == false
211
+
212
+ service, version = meta_header_service_version
213
+
214
+ meta_headers = {
215
+ service.to_sym => version,
216
+ rb: RUBY_VERSION,
217
+ t: Elasticsearch::Transport::VERSION
218
+ }
219
+ meta_headers.merge!(meta_header_engine) if meta_header_engine
220
+ meta_headers.merge!(meta_header_adapter) if meta_header_adapter
221
+
222
+ add_header({ 'x-elastic-client-meta' => meta_headers.map { |k, v| "#{k}=#{v}" }.join(',') })
223
+ end
224
+
225
+ def meta_header_service_version
226
+ if defined?(Elastic::META_HEADER_SERVICE_VERSION)
227
+ Elastic::META_HEADER_SERVICE_VERSION
228
+ elsif defined?(Elasticsearch::VERSION)
229
+ ['es', Elasticsearch::VERSION]
230
+ else
231
+ ['es', Elasticsearch::Transport::VERSION]
232
+ end
233
+ end
234
+
235
+ def meta_header_engine
236
+ case RUBY_ENGINE
237
+ when 'ruby'
238
+ {}
239
+ when 'jruby'
240
+ { jv: ENV_JAVA['java.version'], jr: JRUBY_VERSION }
241
+ when 'rbx'
242
+ { rbx: RUBY_VERSION }
243
+ else
244
+ { RUBY_ENGINE.to_sym => RUBY_VERSION }
245
+ end
246
+ end
247
+
248
+ def meta_header_adapter
249
+ if @transport_class == Transport::HTTP::Faraday
250
+ {fd: Faraday::VERSION}.merge(
251
+ case @arguments[:adapter]
252
+ when :patron
253
+ {pt: Patron::VERSION}
254
+ when :net_http
255
+ {nh: defined?(Net::HTTP::VERSION) ? Net::HTTP::VERSION : Net::HTTP::HTTPVersion}
256
+ when :typhoeus
257
+ {ty: Typhoeus::VERSION}
258
+ when :httpclient
259
+ {hc: HTTPClient::VERSION}
260
+ when :net_http_persistent
261
+ {np: Net::HTTP::Persistent::VERSION}
262
+ end
263
+ )
264
+ elsif defined?(Transport::HTTP::Curb) && @transport_class == Transport::HTTP::Curb
265
+ {cl: Curl::CURB_VERSION}
266
+ elsif defined?(Transport::HTTP::Manticore) && @transport_class == Transport::HTTP::Manticore
267
+ {mc: Manticore::VERSION}
268
+ end
196
269
  end
197
270
 
198
271
  def extract_cloud_creds(arguments)
@@ -17,6 +17,6 @@
17
17
 
18
18
  module Elasticsearch
19
19
  module Transport
20
- VERSION = "7.10.1"
20
+ VERSION = '7.11.0.pre.1'.freeze
21
21
  end
22
22
  end
@@ -246,7 +246,7 @@ describe Elasticsearch::Transport::Client do
246
246
  end
247
247
 
248
248
  let(:client) do
249
- described_class.new(adapter: :patron)
249
+ described_class.new(adapter: :patron, enable_meta_header: false)
250
250
  end
251
251
 
252
252
  it 'uses Faraday with the adapter' do
@@ -260,7 +260,7 @@ describe Elasticsearch::Transport::Client do
260
260
  end
261
261
 
262
262
  let(:client) do
263
- described_class.new(adapter: :typhoeus)
263
+ described_class.new(adapter: :typhoeus, enable_meta_header: false)
264
264
  end
265
265
 
266
266
  it 'uses Faraday with the adapter' do
@@ -274,7 +274,7 @@ describe Elasticsearch::Transport::Client do
274
274
  end
275
275
 
276
276
  let(:client) do
277
- described_class.new('adapter' => :patron)
277
+ described_class.new(adapter: :patron, enable_meta_header: false)
278
278
  end
279
279
 
280
280
  it 'uses Faraday with the adapter' do
@@ -1684,7 +1684,7 @@ describe Elasticsearch::Transport::Client do
1684
1684
  context 'when using the HTTPClient adapter' do
1685
1685
 
1686
1686
  let(:client) do
1687
- described_class.new(hosts: ELASTICSEARCH_HOSTS, compression: true, adapter: :httpclient)
1687
+ described_class.new(hosts: ELASTICSEARCH_HOSTS, compression: true, adapter: :httpclient, enable_meta_header: false)
1688
1688
  end
1689
1689
 
1690
1690
  it 'compresses the request and decompresses the response' do
@@ -0,0 +1,203 @@
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
+
18
+ require 'spec_helper'
19
+
20
+ describe Elasticsearch::Transport::Client do
21
+ context 'meta-header' do
22
+ let(:subject) { client.transport.connections.first.connection.headers }
23
+ let(:regexp) { /^[a-z]{1,}=[a-z0-9.\-]{1,}(?:,[a-z]{1,}=[a-z0-9._\-]+)*$/ }
24
+ let(:adapter) { :net_http }
25
+ let(:adapter_code) { "nh=#{defined?(Net::HTTP::VERSION) ? Net::HTTP::VERSION : Net::HTTP::HTTPVersion}" }
26
+ let(:meta_header) do
27
+ if jruby?
28
+ "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION},fd=#{Faraday::VERSION},#{adapter_code}"
29
+ else
30
+ "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},fd=#{Faraday::VERSION},#{adapter_code}"
31
+ end
32
+ end
33
+
34
+ context 'single use of meta header' do
35
+ let(:client) do
36
+ described_class.new(adapter: adapter).tap do |klient|
37
+ allow(klient).to receive(:__build_connections)
38
+ end
39
+ end
40
+
41
+ it 'x-elastic-client-header value matches regexp' do
42
+ expect(subject['x-elastic-client-meta']).to match(regexp)
43
+ expect(subject).to include('x-elastic-client-meta' => meta_header)
44
+ end
45
+ end
46
+
47
+ context 'when using user-agent headers' do
48
+ let(:client) do
49
+ transport_options = { headers: { user_agent: 'My Ruby App' } }
50
+ described_class.new(transport_options: transport_options, adapter: adapter).tap do |klient|
51
+ allow(klient).to receive(:__build_connections)
52
+ end
53
+ end
54
+
55
+ it 'is friendly to previously set headers' do
56
+ expect(subject).to include(user_agent: 'My Ruby App')
57
+ expect(subject['x-elastic-client-meta']).to match(regexp)
58
+ expect(subject).to include('x-elastic-client-meta' => meta_header)
59
+ end
60
+ end
61
+
62
+ context 'when using API Key' do
63
+ let(:client) do
64
+ described_class.new(api_key: 'an_api_key', adapter: adapter)
65
+ end
66
+
67
+ let(:authorization_header) do
68
+ client.transport.connections.first.connection.headers['Authorization']
69
+ end
70
+
71
+ it 'adds the ApiKey header to the connection' do
72
+ expect(authorization_header).to eq('ApiKey an_api_key')
73
+ expect(subject['x-elastic-client-meta']).to match(regexp)
74
+ expect(subject).to include('x-elastic-client-meta' => meta_header)
75
+ end
76
+ end
77
+
78
+ context 'adapters' do
79
+ let(:meta_header) do
80
+ if jruby?
81
+ "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION},fd=#{Faraday::VERSION}"
82
+ else
83
+ "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},fd=#{Faraday::VERSION}"
84
+ end
85
+ end
86
+ let(:client) { described_class.new(adapter: adapter) }
87
+ let(:headers) { client.transport.connections.first.connection.headers }
88
+
89
+ context 'using net/http/persistent' do
90
+ let(:adapter) { :net_http_persistent }
91
+
92
+ it 'sets adapter in the meta header' do
93
+ require 'net/http/persistent'
94
+ expect(headers['x-elastic-client-meta']).to match(regexp)
95
+ meta = "#{meta_header},np=#{Net::HTTP::Persistent::VERSION}"
96
+ expect(headers).to include('x-elastic-client-meta' => meta)
97
+ end
98
+ end
99
+
100
+ context 'using httpclient' do
101
+ let(:adapter) { :httpclient }
102
+
103
+ it 'sets adapter in the meta header' do
104
+ require 'httpclient'
105
+ expect(headers['x-elastic-client-meta']).to match(regexp)
106
+ meta = "#{meta_header},hc=#{HTTPClient::VERSION}"
107
+ expect(headers).to include('x-elastic-client-meta' => meta)
108
+ end
109
+ end
110
+
111
+ context 'using typhoeus' do
112
+ let(:adapter) { :typhoeus }
113
+
114
+ it 'sets adapter in the meta header' do
115
+ require 'typhoeus'
116
+ expect(headers['x-elastic-client-meta']).to match(regexp)
117
+ meta = "#{meta_header},ty=#{Typhoeus::VERSION}"
118
+ expect(headers).to include('x-elastic-client-meta' => meta)
119
+ end
120
+ end
121
+
122
+ unless defined?(JRUBY_VERSION)
123
+ let(:adapter) { :patron }
124
+
125
+ context 'using patron' do
126
+ it 'sets adapter in the meta header' do
127
+ require 'patron'
128
+ expect(headers['x-elastic-client-meta']).to match(regexp)
129
+ meta = "#{meta_header},pt=#{Patron::VERSION}"
130
+ expect(headers).to include('x-elastic-client-meta' => meta)
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+ if defined?(JRUBY_VERSION)
137
+ context 'when using manticore' do
138
+ let(:client) do
139
+ Elasticsearch::Client.new(transport_class: Elasticsearch::Transport::Transport::HTTP::Manticore)
140
+ end
141
+ let(:subject) { client.transport.connections.first.connection.instance_variable_get("@options")[:headers]}
142
+
143
+ it 'sets manticore in the metaheader' do
144
+ expect(subject['x-elastic-client-meta']).to match(regexp)
145
+ expect(subject['x-elastic-client-meta']).to match(/mc=[0-9.]+/)
146
+ end
147
+ end
148
+ else
149
+ context 'when using curb' do
150
+ let(:client) do
151
+ Elasticsearch::Client.new(transport_class: Elasticsearch::Transport::Transport::HTTP::Curb)
152
+ end
153
+
154
+ it 'sets curb in the metaheader' do
155
+ expect(subject['x-elastic-client-meta']).to match(regexp)
156
+ expect(subject['x-elastic-client-meta']).to match(/cl=[0-9.]+/)
157
+ end
158
+ end
159
+ end
160
+
161
+ context 'when using custom transport implementation' do
162
+ class MyTransport
163
+ include Elasticsearch::Transport::Transport::Base
164
+ def initialize(args); end
165
+ end
166
+ let(:client) { Elasticsearch::Client.new(transport_class: MyTransport) }
167
+ let(:subject){ client.instance_variable_get("@arguments")[:transport_options][:headers] }
168
+ let(:meta_header) do
169
+ if jruby?
170
+ "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION}"
171
+ else
172
+ "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION}"
173
+ end
174
+ end
175
+
176
+ it 'doesnae set any info about the implementation in the metaheader' do
177
+ expect(subject['x-elastic-client-meta']).to match(regexp)
178
+ expect(subject).to include('x-elastic-client-meta' => meta_header)
179
+ end
180
+ end
181
+
182
+ context 'when using a different service version' do
183
+ before do
184
+ module Elastic
185
+ META_HEADER_SERVICE_VERSION = [:ent, '8.0.0']
186
+ end
187
+ end
188
+
189
+ after do
190
+ module Elastic
191
+ META_HEADER_SERVICE_VERSION = [:es, Elasticsearch::VERSION]
192
+ end
193
+ end
194
+
195
+ let(:client) { Elasticsearch::Client.new }
196
+
197
+ it 'sets the service version in the metaheader' do
198
+ expect(subject['x-elastic-client-meta']).to match(regexp)
199
+ expect(subject['x-elastic-client-meta']).to start_with('ent=8.0.0')
200
+ end
201
+ end
202
+ end
203
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticsearch-transport
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.10.1
4
+ version: 7.11.0.pre.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karel Minarik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-07 00:00:00.000000000 Z
11
+ date: 2021-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -391,6 +391,7 @@ files:
391
391
  - spec/elasticsearch/connections/selector_spec.rb
392
392
  - spec/elasticsearch/transport/base_spec.rb
393
393
  - spec/elasticsearch/transport/client_spec.rb
394
+ - spec/elasticsearch/transport/meta_header_spec.rb
394
395
  - spec/elasticsearch/transport/sniffer_spec.rb
395
396
  - spec/spec_helper.rb
396
397
  - test/integration/transport_test.rb
@@ -423,9 +424,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
423
424
  version: '2.4'
424
425
  required_rubygems_version: !ruby/object:Gem::Requirement
425
426
  requirements:
426
- - - ">="
427
+ - - ">"
427
428
  - !ruby/object:Gem::Version
428
- version: '0'
429
+ version: 1.3.1
429
430
  requirements: []
430
431
  rubygems_version: 3.1.4
431
432
  signing_key:
@@ -436,6 +437,7 @@ test_files:
436
437
  - spec/elasticsearch/connections/selector_spec.rb
437
438
  - spec/elasticsearch/transport/base_spec.rb
438
439
  - spec/elasticsearch/transport/client_spec.rb
440
+ - spec/elasticsearch/transport/meta_header_spec.rb
439
441
  - spec/elasticsearch/transport/sniffer_spec.rb
440
442
  - spec/spec_helper.rb
441
443
  - test/integration/transport_test.rb