elasticsearch 8.18.0 → 9.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of elasticsearch might be problematic. Click here for more details.

@@ -1,206 +0,0 @@
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_relative 'helpers_spec_helper'
18
- require 'elasticsearch/helpers/bulk_helper'
19
- require 'tempfile'
20
-
21
- context 'Elasticsearch client helpers' do
22
- context 'Bulk helper' do
23
- let(:index) { 'bulk_animals' }
24
- let(:index_slice) { 'bulk_animals_slice' }
25
- let(:params) { { refresh: 'wait_for' } }
26
- let(:bulk_helper) { Elasticsearch::Helpers::BulkHelper.new(client, index, params) }
27
- let(:docs) do
28
- [
29
- { scientific_name: 'Lama guanicoe', name:'Guanaco' },
30
- { scientific_name: 'Tayassu pecari', name:'White-lipped peccary' },
31
- { scientific_name: 'Snycerus caffer', name:'Buffalo, african' },
32
- { scientific_name: 'Coluber constrictor', name:'Snake, racer' },
33
- { scientific_name: 'Thalasseus maximus', name:'Royal tern' },
34
- { scientific_name: 'Centrocercus urophasianus', name:'Hen, sage' },
35
- { scientific_name: 'Sitta canadensis', name:'Nuthatch, red-breasted' },
36
- { scientific_name: 'Aegypius tracheliotus', name:'Vulture, lappet-faced' },
37
- { scientific_name: 'Bucephala clangula', name:'Common goldeneye' },
38
- { scientific_name: 'Felis pardalis', name:'Ocelot' }
39
- ]
40
- end
41
-
42
- after do
43
- client.indices.delete(index: index, ignore: 404)
44
- client.indices.delete(index: index_slice, ignore: 404)
45
- end
46
-
47
- it 'Ingests documents' do
48
- response = bulk_helper.ingest(docs)
49
- expect(response).to be_an_instance_of Elasticsearch::API::Response
50
- expect(response.status).to eq(200)
51
- expect(response['items'].map { |a| a['index']['status'] }.uniq.first).to eq 201
52
- end
53
-
54
- it 'Updates documents' do
55
- docs = [
56
- { scientific_name: 'Otocyon megalotos', name: 'Bat-eared fox' },
57
- { scientific_name: 'Herpestes javanicus', name: 'Small Indian mongoose' }
58
- ]
59
- bulk_helper.ingest(docs)
60
- # Get the ingested documents, add id and modify them to update them:
61
- animals = client.search(index: index)['hits']['hits']
62
- # Add id to each doc
63
- docs = animals.map { |animal| animal['_source'].merge({'id' => animal['_id'] }) }
64
- docs.map { |doc| doc['scientific_name'].upcase! }
65
- response = bulk_helper.update(docs)
66
- expect(response.status).to eq(200)
67
- expect(response['items'].map { |i| i['update']['result'] }.uniq.first).to eq('updated')
68
- end
69
-
70
- it 'Deletes documents' do
71
- response = bulk_helper.ingest(docs)
72
- ids = response.body['items'].map { |a| a['index']['_id'] }
73
- response = bulk_helper.delete(ids)
74
- expect(response.status).to eq 200
75
- expect(response['items'].map { |item| item['delete']['result'] }.uniq.first).to eq('deleted')
76
- expect(client.count(index: index)['count']).to eq(0)
77
- end
78
-
79
- it 'Ingests documents and yields response and docs' do
80
- slice = 2
81
- bulk_helper = Elasticsearch::Helpers::BulkHelper.new(client, index_slice, params)
82
- response = bulk_helper.ingest(docs, {slice: slice}) do |response, docs|
83
- expect(response).to be_an_instance_of Elasticsearch::API::Response
84
- expect(docs.count).to eq slice
85
- end
86
- response = client.search(index: index_slice, size: 200)
87
- expect(response['hits']['hits'].map { |a| a['_source'].transform_keys(&:to_sym) }).to eq docs
88
- end
89
-
90
- context 'JSON File helper' do
91
- let(:file) { Tempfile.new('test-data.json') }
92
- let(:json) do
93
- json = <<~JSON
94
- [
95
- {
96
- "character_name": "Anallese Lonie",
97
- "species": "mouse",
98
- "catchphrase": "Seamless regional definition",
99
- "favorite_food": "pizza"
100
- },
101
- {
102
- "character_name": "Janey Davidovsky",
103
- "species": "cat",
104
- "catchphrase": "Down-sized responsive pricing structure",
105
- "favorite_food": "pizza"
106
- },
107
- {
108
- "character_name": "Morse Mountford",
109
- "species": "cat",
110
- "catchphrase": "Ameliorated modular data-warehouse",
111
- "favorite_food": "carrots"
112
- },
113
- {
114
- "character_name": "Saundra Kauble",
115
- "species": "dog",
116
- "catchphrase": "Synchronised 24/7 support",
117
- "favorite_food": "carrots"
118
- },
119
- {
120
- "character_name": "Kain Viggars",
121
- "species": "cat",
122
- "catchphrase": "Open-architected asymmetric circuit",
123
- "favorite_food": "carrots"
124
- }
125
- ]
126
- JSON
127
- end
128
-
129
- before do
130
- file.write(json)
131
- file.rewind
132
- end
133
-
134
- after do
135
- file.close
136
- file.unlink
137
- end
138
-
139
- it 'Ingests a JSON file' do
140
- response = bulk_helper.ingest_json(file)
141
-
142
- expect(response).to be_an_instance_of Elasticsearch::API::Response
143
- expect(response.status).to eq(200)
144
- end
145
-
146
- context 'with data not in root of JSON file' do
147
- let(:json) do
148
- json = <<~JSON
149
- {
150
- "field": "value",
151
- "status": 200,
152
- "data": {
153
- "items": [
154
- {
155
- "character_name": "Anallese Lonie",
156
- "species": "mouse",
157
- "catchphrase": "Seamless regional definition",
158
- "favorite_food": "pizza"
159
- },
160
- {
161
- "character_name": "Janey Davidovsky",
162
- "species": "cat",
163
- "catchphrase": "Down-sized responsive pricing structure",
164
- "favorite_food": "pizza"
165
- },
166
- {
167
- "character_name": "Morse Mountford",
168
- "species": "cat",
169
- "catchphrase": "Ameliorated modular data-warehouse",
170
- "favorite_food": "carrots"
171
- },
172
- {
173
- "character_name": "Saundra Kauble",
174
- "species": "dog",
175
- "catchphrase": "Synchronised 24/7 support",
176
- "favorite_food": "carrots"
177
- },
178
- {
179
- "character_name": "Kain Viggars",
180
- "species": "cat",
181
- "catchphrase": "Open-architected asymmetric circuit",
182
- "favorite_food": "carrots"
183
- }
184
- ]
185
- }
186
- }
187
- JSON
188
- end
189
-
190
- it 'Ingests a JSON file passing keys as Array' do
191
- response = bulk_helper.ingest_json(file, { keys: ['data', 'items'] })
192
- expect(response).to be_an_instance_of Elasticsearch::API::Response
193
- expect(response.status).to eq(200)
194
- expect(response['items'].map { |a| a['index']['status'] }.uniq.first).to eq 201
195
- end
196
-
197
- it 'Ingests a JSON file passing keys as String' do
198
- response = bulk_helper.ingest_json(file, { keys: 'data,items' })
199
- expect(response).to be_an_instance_of Elasticsearch::API::Response
200
- expect(response.status).to eq(200)
201
- expect(response['items'].map { |a| a['index']['status'] }.uniq.first).to eq 201
202
- end
203
- end
204
- end
205
- end
206
- end
@@ -1,118 +0,0 @@
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_relative 'helpers_spec_helper'
18
- require 'elasticsearch/helpers/esql_helper'
19
- require 'ipaddr'
20
-
21
- context 'Elasticsearch client helpers' do
22
- let(:index) { 'esql_helper_test' }
23
- let(:body) { { size: 12, query: { match_all: {} } } }
24
- let(:esql_helper) { Elasticsearch::Helpers::ESQLHelper }
25
- let(:query) do
26
- <<~ESQL
27
- FROM #{index}
28
- | EVAL duration_ms = ROUND(event.duration / 1000000.0, 1)
29
- ESQL
30
- end
31
-
32
- before do
33
- client.indices.create(
34
- index: index,
35
- body: {
36
- mappings: {
37
- properties: { 'client.ip' => { type: 'ip' }, message: { type: 'keyword' } }
38
- }
39
- }
40
- )
41
- client.bulk(
42
- index: index,
43
- body: [
44
- {'index': {}},
45
- {'@timestamp' => '2023-10-23T12:15:03.360Z', 'client.ip' => '172.21.2.162', message: 'Connected to 10.1.0.3', 'event.duration' => 3450233},
46
- {'index': {}},
47
- {'@timestamp' => '2023-10-23T12:27:28.948Z', 'client.ip' => '172.21.2.113', message: 'Connected to 10.1.0.2', 'event.duration' => 2764889},
48
- {'index': {}},
49
- {'@timestamp' => '2023-10-23T13:33:34.937Z', 'client.ip' => '172.21.0.5', message: 'Disconnected', 'event.duration' => 1232382},
50
- {'index': {}},
51
- {'@timestamp' => '2023-10-23T13:51:54.732Z', 'client.ip' => '172.21.3.15', message: 'Connection error', 'event.duration' => 725448},
52
- {'index': {}},
53
- {'@timestamp' => '2023-10-23T13:52:55.015Z', 'client.ip' => '172.21.3.15', message: 'Connection error', 'event.duration' => 8268153},
54
- {'index': {}},
55
- {'@timestamp' => '2023-10-23T13:53:55.832Z', 'client.ip' => '172.21.3.15', message: 'Connection error', 'event.duration' => 5033755},
56
- {'index': {}},
57
- {'@timestamp' => '2023-10-23T13:55:01.543Z', 'client.ip' => '172.21.3.15', message: 'Connected to 10.1.0.1', 'event.duration' => 1756467}
58
- ],
59
- refresh: true
60
- )
61
- end
62
-
63
- after do
64
- client.indices.delete(index: index)
65
- end
66
-
67
- it 'returns an ESQL response as a relational key/value object' do
68
- response = esql_helper.query(client, query)
69
- expect(response.count).to eq 7
70
- expect(response.first.keys).to eq ['duration_ms', 'message', 'event.duration', 'client.ip', '@timestamp']
71
- response.each do |r|
72
- expect(r['@timestamp']).to be_a String
73
- expect(r['client.ip']).to be_a String
74
- expect(r['message']).to be_a String
75
- expect(r['event.duration']).to be_a Integer
76
- end
77
- end
78
-
79
- it 'parses iterated objects when procs are passed in' do
80
- parser = {
81
- '@timestamp' => Proc.new { |t| DateTime.parse(t) },
82
- 'client.ip' => Proc.new { |i| IPAddr.new(i) },
83
- 'event.duration' => Proc.new { |d| d.to_s }
84
- }
85
- response = esql_helper.query(client, query, parser: parser)
86
- response.each do |r|
87
- expect(r['@timestamp']).to be_a DateTime
88
- expect(r['client.ip']).to be_a IPAddr
89
- expect(r['message']).to be_a String
90
- expect(r['event.duration']).to be_a String
91
- end
92
- end
93
-
94
- it 'parser does not error when value is nil, leaves nil' do
95
- client.index(
96
- index: index,
97
- body: {
98
- '@timestamp' => nil,
99
- 'client.ip' => nil,
100
- message: 'Connected to 10.1.0.1',
101
- 'event.duration' => 1756465
102
- },
103
- refresh: true
104
- )
105
- parser = {
106
- '@timestamp' => Proc.new { |t| DateTime.parse(t) },
107
- 'client.ip' => Proc.new { |i| IPAddr.new(i) },
108
- 'event.duration' => Proc.new { |d| d.to_s }
109
- }
110
- response = esql_helper.query(client, query, parser: parser)
111
- response.each do |r|
112
- expect [DateTime, NilClass].include?(r['@timestamp'].class)
113
- expect [IPAddr, NilClass].include?(r['client.ip'].class)
114
- expect(r['message']).to be_a String
115
- expect(r['event.duration']).to be_a String
116
- end
117
- end
118
- end
@@ -1,29 +0,0 @@
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
- ELASTICSEARCH_URL = ENV['TEST_ES_SERVER'] || "http://localhost:#{(ENV['PORT'] || 9200)}"
21
- raise URI::InvalidURIError unless ELASTICSEARCH_URL =~ /\A#{URI::DEFAULT_PARSER.make_regexp}\z/
22
-
23
- def client
24
- @client ||= Elasticsearch::Client.new(
25
- host: ELASTICSEARCH_URL,
26
- user: 'elastic',
27
- password: 'changeme'
28
- )
29
- end
@@ -1,83 +0,0 @@
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_relative 'helpers_spec_helper'
18
- require 'elasticsearch/helpers/scroll_helper'
19
-
20
- context 'Elasticsearch client helpers' do
21
- context 'ScrollHelper' do
22
- let(:index) { 'books' }
23
- let(:body) { { size: 12, query: { match_all: {} } } }
24
- let(:scroll_helper) { Elasticsearch::Helpers::ScrollHelper.new(client, index, body) }
25
-
26
- before do
27
- documents = [
28
- { index: { _index: index, data: {name: "Leviathan Wakes", "author": "James S.A. Corey", "release_date": "2011-06-02", "page_count": 561} } },
29
- { index: { _index: index, data: {name: "Hyperion", "author": "Dan Simmons", "release_date": "1989-05-26", "page_count": 482} } },
30
- { index: { _index: index, data: {name: "Dune", "author": "Frank Herbert", "release_date": "1965-06-01", "page_count": 604} } },
31
- { index: { _index: index, data: {name: "Dune Messiah", "author": "Frank Herbert", "release_date": "1969-10-15", "page_count": 331} } },
32
- { index: { _index: index, data: {name: "Children of Dune", "author": "Frank Herbert", "release_date": "1976-04-21", "page_count": 408} } },
33
- { index: { _index: index, data: {name: "God Emperor of Dune", "author": "Frank Herbert", "release_date": "1981-05-28", "page_count": 454} } },
34
- { index: { _index: index, data: {name: "Consider Phlebas", "author": "Iain M. Banks", "release_date": "1987-04-23", "page_count": 471} } },
35
- { index: { _index: index, data: {name: "Pandora's Star", "author": "Peter F. Hamilton", "release_date": "2004-03-02", "page_count": 768} } },
36
- { index: { _index: index, data: {name: "Revelation Space", "author": "Alastair Reynolds", "release_date": "2000-03-15", "page_count": 585} } },
37
- { index: { _index: index, data: {name: "A Fire Upon the Deep", "author": "Vernor Vinge", "release_date": "1992-06-01", "page_count": 613} } },
38
- { index: { _index: index, data: {name: "Ender's Game", "author": "Orson Scott Card", "release_date": "1985-06-01", "page_count": 324} } },
39
- { index: { _index: index, data: {name: "1984", "author": "George Orwell", "release_date": "1985-06-01", "page_count": 328} } },
40
- { index: { _index: index, data: {name: "Fahrenheit 451", "author": "Ray Bradbury", "release_date": "1953-10-15", "page_count": 227} } },
41
- { index: { _index: index, data: {name: "Brave New World", "author": "Aldous Huxley", "release_date": "1932-06-01", "page_count": 268} } },
42
- { index: { _index: index, data: {name: "Foundation", "author": "Isaac Asimov", "release_date": "1951-06-01", "page_count": 224} } },
43
- { index: { _index: index, data: {name: "The Giver", "author": "Lois Lowry", "release_date": "1993-04-26", "page_count": 208} } },
44
- { index: { _index: index, data: {name: "Slaughterhouse-Five", "author": "Kurt Vonnegut", "release_date": "1969-06-01", "page_count": 275} } },
45
- { index: { _index: index, data: {name: "The Hitchhiker's Guide to the Galaxy", "author": "Douglas Adams", "release_date": "1979-10-12", "page_count": 180} } },
46
- { index: { _index: index, data: {name: "Snow Crash", "author": "Neal Stephenson", "release_date": "1992-06-01", "page_count": 470} } },
47
- { index: { _index: index, data: {name: "Neuromancer", "author": "William Gibson", "release_date": "1984-07-01", "page_count": 271} } },
48
- { index: { _index: index, data: {name: "The Handmaid's Tale", "author": "Margaret Atwood", "release_date": "1985-06-01", "page_count": 311} } },
49
- { index: { _index: index, data: {name: "Starship Troopers", "author": "Robert A. Heinlein", "release_date": "1959-12-01", "page_count": 335} } },
50
- { index: { _index: index, data: {name: "The Left Hand of Darkness", "author": "Ursula K. Le Guin", "release_date": "1969-06-01", "page_count": 304} } },
51
- { index: { _index: index, data: {name: "The Moon is a Harsh Mistress", "author": "Robert A. Heinlein", "release_date": "1966-04-01", "page_count": 288 } } }
52
- ]
53
- client.bulk(body: documents, refresh: 'wait_for')
54
- end
55
-
56
- after do
57
- client.indices.delete(index: index)
58
- end
59
-
60
- it 'instantiates a scroll helper' do
61
- expect(scroll_helper).to be_an_instance_of Elasticsearch::Helpers::ScrollHelper
62
- end
63
-
64
- it 'searches an index' do
65
- my_documents = []
66
- while !(documents = scroll_helper.results).empty?
67
- my_documents << documents
68
- end
69
-
70
- expect(my_documents.flatten.size).to eq 24
71
- end
72
-
73
- it 'uses enumerable' do
74
- count = 0
75
- scroll_helper.each { count += 1 }
76
- expect(count).to eq 24
77
- expect(scroll_helper).to respond_to(:count)
78
- expect(scroll_helper).to respond_to(:reject)
79
- expect(scroll_helper).to respond_to(:uniq)
80
- expect(scroll_helper.map { |a| a['_id'] }.uniq.count).to eq 24
81
- end
82
- end
83
- end
@@ -1,55 +0,0 @@
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
- if ENV['TEST_WITH_OTEL'] == 'true'
19
- ELASTICSEARCH_URL = ENV['TEST_ES_SERVER'] || "http://localhost:#{(ENV['PORT'] || 9200)}"
20
- raise URI::InvalidURIError unless ELASTICSEARCH_URL =~ /\A#{URI::DEFAULT_PARSER.make_regexp}\z/
21
-
22
- require 'spec_helper'
23
-
24
- context 'OpenTelemetry' do
25
- let(:exporter) { EXPORTER }
26
- before { exporter.reset }
27
- after { exporter.reset }
28
- let(:span) { exporter.finished_spans[0] }
29
-
30
- let(:client) do
31
- Elasticsearch::Client.new(
32
- host: ELASTICSEARCH_URL,
33
- user: 'elastic',
34
- password: 'changeme'
35
- )
36
- end
37
-
38
- after do
39
- client.delete(index: 'myindex', id: 1); rescue
40
- end
41
-
42
- context 'when a request is instrumented' do
43
- it 'sets the span name to the endpoint id' do
44
- client.search(body: { query: { match: { a: 1 } } })
45
- expect(span.name).to eq 'search'
46
- end
47
-
48
- it 'sets the path parts' do
49
- client.index(index: 'myindex', id: 1, body: { title: 'Test' })
50
- expect(span.attributes['db.elasticsearch.path_parts.index']).to eq 'myindex'
51
- expect(span.attributes['db.elasticsearch.path_parts.id']).to eq 1
52
- end
53
- end
54
- end
55
- end
data/spec/spec_helper.rb DELETED
@@ -1,43 +0,0 @@
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 'elasticsearch'
19
- require 'rspec'
20
-
21
- RSpec.configure do |config|
22
- config.formatter = :documentation
23
- end
24
-
25
- def meta_version
26
- client.send(:client_meta_version, Elasticsearch::VERSION)
27
- end
28
-
29
- def jruby?
30
- defined?(JRUBY_VERSION)
31
- end
32
-
33
- if ENV['TEST_WITH_OTEL'] == 'true'
34
- require 'opentelemetry-sdk'
35
- EXPORTER = OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new
36
- span_processor = OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(EXPORTER)
37
-
38
- OpenTelemetry::SDK.configure do |c|
39
- c.error_handler = ->(exception:, message:) { raise(exception || message) }
40
- c.logger = Logger.new($stderr, level: ENV.fetch('OTEL_LOG_LEVEL', 'fatal').to_sym)
41
- c.add_span_processor span_processor
42
- end
43
- end
@@ -1,138 +0,0 @@
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
- require 'base64'
20
-
21
- describe Elasticsearch::Client do
22
- context 'when using API Key' do
23
- let(:authorization_header) do
24
- client.transport.connections.first.connection.headers['Authorization']
25
- end
26
-
27
- context 'when an encoded api_key is provided' do
28
- let(:client) do
29
- described_class.new(api_key: 'an_api_key')
30
- end
31
-
32
- it 'Adds the ApiKey header to the connection' do
33
- expect(authorization_header).to eq('ApiKey an_api_key')
34
- end
35
- end
36
-
37
- context 'when an un-encoded api_key is provided' do
38
- let(:client) do
39
- described_class.new(api_key: { id: 'my_id', api_key: 'my_api_key' })
40
- end
41
-
42
- it 'Adds the ApiKey header to the connection' do
43
- expect(authorization_header).to eq("ApiKey #{Base64.strict_encode64('my_id:my_api_key')}")
44
- end
45
- end
46
-
47
- context 'when basic auth and api_key are provided' do
48
- let(:client) do
49
- described_class.new(
50
- api_key: { id: 'my_id', api_key: 'my_api_key' },
51
- host: 'http://elastic:password@localhost:9200'
52
- )
53
- end
54
-
55
- it 'removes basic auth credentials' do
56
- expect(authorization_header).not_to match(/^Basic/)
57
- expect(authorization_header).to match(/^ApiKey/)
58
- end
59
- end
60
-
61
- context 'when other headers were specified' do
62
- let(:client) do
63
- described_class.new(
64
- api_key: 'elasticsearch_api_key',
65
- transport_options: { headers: { 'x-test-header' => 'test' } }
66
- )
67
- end
68
-
69
- it 'Adds the ApiKey header to the connection and keeps the header' do
70
- header = client.transport.connections.first.connection.headers
71
- expect(header['Authorization']).to eq('ApiKey elasticsearch_api_key')
72
- expect(header['X-Test-Header']).to eq('test')
73
- end
74
- end
75
-
76
- context 'when sending transport_options but no headers were specified' do
77
- let(:client) do
78
- described_class.new(
79
- api_key: 'elasticsearch_api_key',
80
- transport_options: { ssl: { verify: false } }
81
- )
82
- end
83
-
84
- it 'Adds the ApiKey header to the connection and keeps the options' do
85
- header = client.transport.connections.first.connection.headers
86
- expect(header['Authorization']).to eq('ApiKey elasticsearch_api_key')
87
- expect(client.transport.options[:transport_options]).to include({ ssl: { verify: false } })
88
- expect(client.transport.options[:transport_options][:headers]).to include('Authorization' => 'ApiKey elasticsearch_api_key')
89
- end
90
- end
91
-
92
- context 'when other headers and options were specified' do
93
- let(:client) do
94
- described_class.new(
95
- api_key: 'elasticsearch_api_key',
96
- transport_options: {
97
- headers: { 'x-test-header' => 'test' },
98
- ssl: { verify: false }
99
- }
100
- )
101
- end
102
-
103
- it 'Adds the ApiKey header to the connection and keeps the header' do
104
- header = client.transport.connections.first.connection.headers
105
- expect(header['X-Test-Header']).to eq('test')
106
- expect(header['Authorization']).to eq('ApiKey elasticsearch_api_key')
107
- expect(client.transport.options[:transport_options]).to include({ ssl: { verify: false } })
108
- expect(client.transport.options[:transport_options][:headers]).to include('Authorization' => 'ApiKey elasticsearch_api_key')
109
- end
110
- end
111
-
112
- context 'Metaheader' do
113
- let(:adapter_code) { "nh=#{defined?(Net::HTTP::VERSION) ? Net::HTTP::VERSION : Net::HTTP::HTTPVersion}" }
114
- let(:meta_header) do
115
- if jruby?
116
- "es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elastic::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION},fd=#{Faraday::VERSION},#{adapter_code}"
117
- else
118
- "es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elastic::Transport::VERSION},fd=#{Faraday::VERSION},#{adapter_code}"
119
- end
120
- end
121
-
122
- context 'when using API Key' do
123
- let(:client) do
124
- described_class.new(api_key: 'an_api_key')
125
- end
126
-
127
- let(:headers) do
128
- client.transport.connections.first.connection.headers
129
- end
130
-
131
- it 'adds the ApiKey header to the connection' do
132
- expect(authorization_header).to eq('ApiKey an_api_key')
133
- expect(headers).to include('x-elastic-client-meta' => meta_header)
134
- end
135
- end
136
- end
137
- end
138
- end