Skip to content

Commit 96723cf

Browse files
committed
[Client] Document custom headers
1 parent ce4ff84 commit 96723cf

File tree

1 file changed

+51
-21
lines changed

1 file changed

+51
-21
lines changed

elasticsearch-transport/README.md

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Full documentation is available at <http://rubydoc.info/gems/elasticsearch-trans
7878
* [Connect using an Elastic Cloud ID](#connect-using-an-elastic-cloud-id)
7979
* [Authentication](#authentication)
8080
* [Logging](#logging)
81+
* [Custom HTTP Headers](#custom-http-headers)
8182
* [Identifying running tasks with X-Opaque-Id](#identifying-running-tasks-with-x-opaque-id)
8283
* [Setting Timeouts](#setting-timeouts)
8384
* [Randomizing Hosts](#randomizing-hosts)
@@ -189,30 +190,55 @@ Elasticsearch::Client.new(
189190
To log requests and responses to standard output with the default logger (an instance of Ruby's {::Logger} class),
190191
set the `log` argument:
191192

192-
Elasticsearch::Client.new log: true
193+
```ruby
194+
Elasticsearch::Client.new log: true
195+
```
196+
193197

194198
To trace requests and responses in the _Curl_ format, set the `trace` argument:
195199

196-
Elasticsearch::Client.new trace: true
200+
```ruby
201+
Elasticsearch::Client.new trace: true
202+
```
197203

198204
You can customize the default logger or tracer:
199205

206+
```ruby
200207
client.transport.logger.formatter = proc { |s, d, p, m| "#{s}: #{m}\n" }
201208
client.transport.logger.level = Logger::INFO
209+
```
202210

203211
Or, you can use a custom `::Logger` instance:
204212

205-
Elasticsearch::Client.new logger: Logger.new(STDERR)
213+
```ruby
214+
Elasticsearch::Client.new logger: Logger.new(STDERR)
215+
```
206216

207217
You can pass the client any conforming logger implementation:
208218

209-
require 'logging' # https://github.com/TwP/logging/
219+
```ruby
220+
require 'logging' # https://github.com/TwP/logging/
221+
222+
log = Logging.logger['elasticsearch']
223+
log.add_appenders Logging.appenders.stdout
224+
log.level = :info
225+
226+
client = Elasticsearch::Client.new logger: log
227+
```
228+
229+
### Custom HTTP Headers
230+
231+
You can set a custom HTTP header on the client's initializer:
210232

211-
log = Logging.logger['elasticsearch']
212-
log.add_appenders Logging.appenders.stdout
213-
log.level = :info
233+
```ruby
234+
client = Elasticsearch::Client.new(
235+
transport_options: {
236+
headers:
237+
{user_agent: "My Ruby App"}
238+
}
239+
)
240+
```
214241

215-
client = Elasticsearch::Client.new logger: log
216242
### Identifying running tasks with X-Opaque-Id
217243

218244
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:
@@ -330,25 +356,29 @@ preferring HTTP clients with support for persistent connections.
330356

331357
To use the [_Patron_](https://github.com/toland/patron) HTTP, for example, just require it:
332358

333-
require 'patron'
359+
```ruby
360+
require 'patron'
361+
```
334362

335363
Then, create a new client, and the _Patron_ gem will be used as the "driver":
336364

337-
client = Elasticsearch::Client.new
365+
```ruby
366+
client = Elasticsearch::Client.new
338367

339-
client.transport.connections.first.connection.builder.handlers
340-
# => [Faraday::Adapter::Patron]
368+
client.transport.connections.first.connection.builder.adapter
369+
# => Faraday::Adapter::Patron
341370

342-
10.times do
343-
client.nodes.stats(metric: 'http')['nodes'].values.each do |n|
344-
puts "#{n['name']} : #{n['http']['total_opened']}"
345-
end
346-
end
371+
10.times do
372+
client.nodes.stats(metric: 'http')['nodes'].values.each do |n|
373+
puts "#{n['name']} : #{n['http']['total_opened']}"
374+
end
375+
end
347376

348-
# => Stiletoo : 24
349-
# => Stiletoo : 24
350-
# => Stiletoo : 24
351-
# => ...
377+
# => Stiletoo : 24
378+
# => Stiletoo : 24
379+
# => Stiletoo : 24
380+
# => ...
381+
```
352382

353383
To use a specific adapter for _Faraday_, pass it as the `adapter` argument:
354384

0 commit comments

Comments
 (0)