Skip to content

Commit fa2631b

Browse files
committed
Opaque-Id: Set it per request instead of with the setter
1 parent 1667c8e commit fa2631b

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
@@ -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: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,25 +211,21 @@ You can pass the client any conforming logger implementation:
211211
client = Elasticsearch::Client.new logger: log
212212
### Identifying running tasks with X-Opaque-Id
213213

214-
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:
214+
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:
215215

216216
```ruby
217217
client = Elasticsearch::Client.new
218-
client.opaque_id = '123456'
219-
client.search(index: 'myindex', q: 'title:test')
218+
client.search(index: 'myindex', q: 'title:test', opaque_id: '123456')
220219
```
221220
The search request will include the following HTTP Header:
222221
```
223222
X-Opaque-Id: 123456
224223
```
225224

226-
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.
227-
228225
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:
229226
```ruby
230227
client = Elasticsearch::Client.new(opaque_id_prefix: 'eu-west1')
231-
client.opaque_id = '123456'
232-
client.search(index: 'myindex', q: 'title:test')
228+
client.search(index: 'myindex', q: 'title:test', opaque_id: '123456')
233229
```
234230
The request will include the following HTTP Header:
235231
```

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,14 @@ def initialize(arguments={}, &block)
157157
#
158158
def perform_request(method, path, params = {}, body = nil, headers = nil)
159159
method = @send_get_body_as if 'GET' == method && body
160-
if @opaque_id
160+
if (opaque_id = params.delete(:opaque_id))
161161
headers = {} if headers.nil?
162-
opaque_id = @opaque_id_prefix ? "#{@opaque_id_prefix}#{@opaque_id}" : @opaque_id
162+
opaque_id = @opaque_id_prefix ? "#{@opaque_id_prefix}#{opaque_id}" : opaque_id
163163
headers.merge!('X-Opaque-Id' => opaque_id)
164-
@opaque_id = nil # Remove Opaque id after each request
165164
end
166165
transport.perform_request(method, path, params, body, headers)
167166
end
168167

169-
attr_accessor :opaque_id
170-
171168
private
172169

173170
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
@@ -1096,18 +1096,11 @@
10961096
end
10971097
end
10981098

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

11021102
it 'uses x-opaque-id on a request' do
1103-
client.opaque_id = '12345'
1104-
expect(client.perform_request('GET', '/').headers['x-opaque-id']).to eq('12345')
1105-
end
1106-
1107-
it 'deletes x-opaque-id on a second request' do
1108-
client.opaque_id = 'asdfg'
1109-
expect(client.perform_request('GET', '/').headers['x-opaque-id']).to eq('asdfg')
1110-
expect(client.perform_request('GET', '/').headers).not_to include('x-opaque-id')
1103+
expect(client.perform_request('GET', '/', { opaque_id: '12345' }).headers['x-opaque-id']).to eq('12345')
11111104
end
11121105
end
11131106

@@ -1118,20 +1111,27 @@
11181111
end
11191112

11201113
it 'uses x-opaque-id on a request' do
1121-
client.opaque_id = '12345'
1122-
expect(client.perform_request('GET', '/').headers['x-opaque-id']).to eq("#{prefix}12345")
1114+
expect(client.perform_request('GET', '/', { opaque_id: '12345' }).headers['x-opaque-id']).to eq("#{prefix}12345")
11231115
end
11241116

1125-
it 'deletes x-opaque-id on a second request' do
1126-
client.opaque_id = 'asdfg'
1127-
expect(client.perform_request('GET', '/').headers['x-opaque-id']).to eq("#{prefix}_asdfg")
1128-
expect(client.perform_request('GET', '/').headers).not_to include('x-opaque-id')
1117+
context 'when using an API call' do
1118+
let(:client) { described_class.new(host: hosts) }
1119+
1120+
it 'doesnae raise an ArgumentError' do
1121+
expect { client.search(opaque_id: 'no_error') }.not_to raise_error
1122+
end
1123+
1124+
it 'uses X-Opaque-Id in the header' do
1125+
allow(client).to receive(:perform_request) { OpenStruct.new(body: '') }
1126+
expect { client.search(opaque_id: 'opaque_id') }.not_to raise_error
1127+
expect(client).to have_received(:perform_request)
1128+
.with('GET', '_search', { opaque_id: 'opaque_id' }, nil)
1129+
end
11291130
end
11301131
end
11311132
end
11321133

11331134
context 'when the client connects to Elasticsearch' do
1134-
11351135
let(:logger) do
11361136
Logger.new(STDERR).tap do |logger|
11371137
logger.formatter = proc do |severity, datetime, progname, msg|

0 commit comments

Comments
 (0)