Skip to content

Commit 6a435f7

Browse files
committed
Opaque-Id: Set it per request instead of with the setter
1 parent 8c0dba6 commit 6a435f7

File tree

4 files changed

+23
-29
lines changed

4 files changed

+23
-29
lines changed

elasticsearch-api/lib/elasticsearch/api.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ module API
2828
:format, # Search, Cat, ...
2929
:pretty, # Pretty-print the response
3030
:human, # Return numeric values in human readable format
31-
:filter_path # Filter the JSON response
31+
:filter_path, # Filter the JSON response
32+
:opaque_id # Use X-Opaque-Id
3233
]
3334

3435
HTTP_GET = 'GET'.freeze

elasticsearch-transport/README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,25 +196,21 @@ You can pass the client any conforming logger implementation:
196196

197197
### Identifying running tasks with X-Opaque-Id
198198

199-
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:
199+
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:
200200

201201
```ruby
202202
client = Elasticsearch::Client.new
203-
client.opaque_id = '123456'
204-
client.search(index: 'myindex', q: 'title:test')
203+
client.search(index: 'myindex', q: 'title:test', opaque_id: '123456')
205204
```
206205
The search request will include the following HTTP Header:
207206
```
208207
X-Opaque-Id: 123456
209208
```
210209

211-
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.
212-
213210
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:
214211
```ruby
215212
client = Elasticsearch::Client.new(opaque_id_prefix: 'eu-west1')
216-
client.opaque_id = '123456'
217-
client.search(index: 'myindex', q: 'title:test')
213+
client.search(index: 'myindex', q: 'title:test', opaque_id: '123456')
218214
```
219215
The request will include the following HTTP Header:
220216
```

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,14 @@ def initialize(arguments={}, &block)
149149
#
150150
def perform_request(method, path, params = {}, body = nil, headers = nil)
151151
method = @send_get_body_as if 'GET' == method && body
152-
if @opaque_id
152+
if (opaque_id = params.delete(:opaque_id))
153153
headers = {} if headers.nil?
154-
opaque_id = @opaque_id_prefix ? "#{@opaque_id_prefix}#{@opaque_id}" : @opaque_id
154+
opaque_id = @opaque_id_prefix ? "#{@opaque_id_prefix}#{opaque_id}" : opaque_id
155155
headers.merge!('X-Opaque-Id' => opaque_id)
156-
@opaque_id = nil # Remove Opaque id after each request
157156
end
158157
transport.perform_request(method, path, params, body, headers)
159158
end
160159

161-
attr_accessor :opaque_id
162-
163160
private
164161

165162
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
@@ -653,18 +653,11 @@
653653
end
654654
end
655655

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

659659
it 'uses x-opaque-id on a request' do
660-
client.opaque_id = '12345'
661-
expect(client.perform_request('GET', '/').headers['x-opaque-id']).to eq('12345')
662-
end
663-
664-
it 'deletes x-opaque-id on a second request' do
665-
client.opaque_id = 'asdfg'
666-
expect(client.perform_request('GET', '/').headers['x-opaque-id']).to eq('asdfg')
667-
expect(client.perform_request('GET', '/').headers).not_to include('x-opaque-id')
660+
expect(client.perform_request('GET', '/', { opaque_id: '12345' }).headers['x-opaque-id']).to eq('12345')
668661
end
669662
end
670663

@@ -675,20 +668,27 @@
675668
end
676669

677670
it 'uses x-opaque-id on a request' do
678-
client.opaque_id = '12345'
679-
expect(client.perform_request('GET', '/').headers['x-opaque-id']).to eq("#{prefix}12345")
671+
expect(client.perform_request('GET', '/', { opaque_id: '12345' }).headers['x-opaque-id']).to eq("#{prefix}12345")
680672
end
681673

682-
it 'deletes x-opaque-id on a second request' do
683-
client.opaque_id = 'asdfg'
684-
expect(client.perform_request('GET', '/').headers['x-opaque-id']).to eq("#{prefix}_asdfg")
685-
expect(client.perform_request('GET', '/').headers).not_to include('x-opaque-id')
674+
context 'when using an API call' do
675+
let(:client) { described_class.new(host: hosts) }
676+
677+
it 'doesnae raise an ArgumentError' do
678+
expect { client.search(opaque_id: 'no_error') }.not_to raise_error
679+
end
680+
681+
it 'uses X-Opaque-Id in the header' do
682+
allow(client).to receive(:perform_request) { OpenStruct.new(body: '') }
683+
expect { client.search(opaque_id: 'opaque_id') }.not_to raise_error
684+
expect(client).to have_received(:perform_request)
685+
.with('GET', '_search', { opaque_id: 'opaque_id' }, nil)
686+
end
686687
end
687688
end
688689
end
689690

690691
context 'when the client connects to Elasticsearch' do
691-
692692
let(:logger) do
693693
Logger.new(STDERR).tap do |logger|
694694
logger.formatter = proc do |severity, datetime, progname, msg|

0 commit comments

Comments
 (0)