Skip to content

Commit f877b6c

Browse files
committed
Opaque-Id: Set it per request instead of with the setter
1 parent 4a16e29 commit f877b6c

File tree

4 files changed

+24
-30
lines changed

4 files changed

+24
-30
lines changed

elasticsearch-api/lib/elasticsearch/api.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ module API
3232
:format, # Search, Cat, ...
3333
:pretty, # Pretty-print the response
3434
:human, # Return numeric values in human readable format
35-
:filter_path # Filter the JSON response
35+
:filter_path, # Filter the JSON response
36+
:opaque_id # Use X-Opaque-Id
3637
]
3738

3839
HTTP_GET = 'GET'.freeze

elasticsearch-transport/README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,25 +246,21 @@ client = Elasticsearch::Client.new logger: log
246246

247247
### Identifying running tasks with X-Opaque-Id
248248

249-
The X-Opaque-Id header allows to track certain calls, or associate certain tasks with the client that started them ([more on the Elasticsearch docs](https://www.elastic.co/guide/en/elasticsearch/reference/master/tasks.html#_identifying_running_tasks)). To use this feature, you need to set an id for `opaque_id` on the client before each request. Example:
249+
The X-Opaque-Id header allows to track certain calls, or associate certain tasks with the client that started them ([more on the Elasticsearch docs](https://www.elastic.co/guide/en/elasticsearch/reference/master/tasks.html#_identifying_running_tasks)). To use this feature, you need to set an id for `opaque_id` on the client on each request. Example:
250250

251251
```ruby
252252
client = Elasticsearch::Client.new
253-
client.opaque_id = '123456'
254-
client.search(index: 'myindex', q: 'title:test')
253+
client.search(index: 'myindex', q: 'title:test', opaque_id: '123456')
255254
```
256255
The search request will include the following HTTP Header:
257256
```
258257
X-Opaque-Id: 123456
259258
```
260259

261-
Please note that `opaque_id` will be set to nil after every request, so you need to set it on the client for every individual request.
262-
263260
You can also set a prefix for X-Opaque-Id when initializing the client. This will be prepended to the id you set before each request if you're using X-Opaque-Id. Example:
264261
```ruby
265-
client = Elasticsearch::Client.new(opaque_id_prefix: 'eu-west1')
266-
client.opaque_id = '123456'
267-
client.search(index: 'myindex', q: 'title:test')
262+
client = Elasticsearch::Client.new(opaque_id_prefix: 'eu-west1_')
263+
client.search(index: 'myindex', q: 'title:test', opaque_id: '123456')
268264
```
269265
The request will include the following HTTP Header:
270266
```

elasticsearch-transport/lib/elasticsearch/transport/client.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,14 @@ def initialize(arguments={}, &block)
162162
#
163163
def perform_request(method, path, params = {}, body = nil, headers = nil)
164164
method = @send_get_body_as if 'GET' == method && body
165-
if @opaque_id
165+
if (opaque_id = params.delete(:opaque_id))
166166
headers = {} if headers.nil?
167-
opaque_id = @opaque_id_prefix ? "#{@opaque_id_prefix}#{@opaque_id}" : @opaque_id
167+
opaque_id = @opaque_id_prefix ? "#{@opaque_id_prefix}#{opaque_id}" : opaque_id
168168
headers.merge!('X-Opaque-Id' => opaque_id)
169-
@opaque_id = nil # Remove Opaque id after each request
170169
end
171170
transport.perform_request(method, path, params, body, headers)
172171
end
173172

174-
attr_accessor :opaque_id
175-
176173
private
177174

178175
def set_api_key

elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,18 +1282,11 @@
12821282
end
12831283
end
12841284

1285-
context 'when x-opaque-id is set before calling a method' do
1285+
context 'when x-opaque-id is set' do
12861286
let(:client) { described_class.new(host: hosts) }
12871287

12881288
it 'uses x-opaque-id on a request' do
1289-
client.opaque_id = '12345'
1290-
expect(client.perform_request('GET', '/').headers['x-opaque-id']).to eq('12345')
1291-
end
1292-
1293-
it 'deletes x-opaque-id on a second request' do
1294-
client.opaque_id = 'asdfg'
1295-
expect(client.perform_request('GET', '/').headers['x-opaque-id']).to eq('asdfg')
1296-
expect(client.perform_request('GET', '/').headers).not_to include('x-opaque-id')
1289+
expect(client.perform_request('GET', '/', { opaque_id: '12345' }).headers['x-opaque-id']).to eq('12345')
12971290
end
12981291
end
12991292

@@ -1304,20 +1297,27 @@
13041297
end
13051298

13061299
it 'uses x-opaque-id on a request' do
1307-
client.opaque_id = '12345'
1308-
expect(client.perform_request('GET', '/').headers['x-opaque-id']).to eq("#{prefix}12345")
1300+
expect(client.perform_request('GET', '/', { opaque_id: '12345' }).headers['x-opaque-id']).to eq("#{prefix}12345")
13091301
end
13101302

1311-
it 'deletes x-opaque-id on a second request' do
1312-
client.opaque_id = 'asdfg'
1313-
expect(client.perform_request('GET', '/').headers['x-opaque-id']).to eq("#{prefix}_asdfg")
1314-
expect(client.perform_request('GET', '/').headers).not_to include('x-opaque-id')
1303+
context 'when using an API call' do
1304+
let(:client) { described_class.new(host: hosts) }
1305+
1306+
it 'doesnae raise an ArgumentError' do
1307+
expect { client.search(opaque_id: 'no_error') }.not_to raise_error
1308+
end
1309+
1310+
it 'uses X-Opaque-Id in the header' do
1311+
allow(client).to receive(:perform_request) { OpenStruct.new(body: '') }
1312+
expect { client.search(opaque_id: 'opaque_id') }.not_to raise_error
1313+
expect(client).to have_received(:perform_request)
1314+
.with('GET', '_search', { opaque_id: 'opaque_id' }, nil)
1315+
end
13151316
end
13161317
end
13171318
end
13181319

13191320
context 'when the client connects to Elasticsearch' do
1320-
13211321
let(:logger) do
13221322
Logger.new(STDERR).tap do |logger|
13231323
logger.formatter = proc do |severity, datetime, progname, msg|

0 commit comments

Comments
 (0)