Class: LogStash::Outputs::ElasticSearch::HttpClient
- Inherits:
-
Object
- Object
- LogStash::Outputs::ElasticSearch::HttpClient
show all
- Defined in:
- lib/logstash/outputs/elasticsearch/http_client.rb,
lib/logstash/outputs/elasticsearch/http_client/pool.rb,
lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb
Overview
Defined Under Namespace
Classes: ManticoreAdapter, Pool
Constant Summary
collapse
{ "Content-Type" => "application/json" }
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#build_adapter(options) ⇒ Object
-
#build_pool(options) ⇒ Object
-
#build_url_template ⇒ Object
-
#bulk(actions) ⇒ Object
-
#bulk_send(body_stream) ⇒ Object
-
#calculate_property(uris, property, default, sniff_check) ⇒ Object
-
#client_settings ⇒ Object
-
#close ⇒ Object
-
#exists?(path, use_get = false) ⇒ Boolean
-
#get(path) ⇒ Object
-
#get_ilm_endpoint ⇒ Object
-
#get_xpack_info ⇒ Object
-
#host_to_url(h) ⇒ Object
-
#http_compression ⇒ Object
-
#ilm_policy_exists?(name) ⇒ Boolean
-
#ilm_policy_put(name, policy) ⇒ Object
-
#initialize(options = {}) ⇒ HttpClient
constructor
The ‘options` is a hash where the following symbol keys have meaning:.
-
#join_bulk_responses(bulk_responses) ⇒ Object
-
#maximum_seen_major_version ⇒ Object
-
#password ⇒ Object
-
#path ⇒ Object
-
#port ⇒ Object
-
#post(path, params = {}, body_string) ⇒ Object
-
#rollover_alias_exists?(name) ⇒ Boolean
check whether rollover alias already exists.
-
#rollover_alias_put(alias_name, alias_definition) ⇒ Object
Create a new rollover alias.
-
#scheme ⇒ Object
-
#sniffing ⇒ Object
-
#ssl_options ⇒ Object
-
#template_exists?(name) ⇒ Boolean
-
#template_install(name, template, force = false) ⇒ Object
-
#template_put(name, template) ⇒ Object
-
#update_action_builder(args, source) ⇒ Object
Build a bulk item for an elasticsearch update action.
-
#uris ⇒ Object
-
#user ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ HttpClient
The ‘options` is a hash where the following symbol keys have meaning:
-
‘:hosts` - array of String. Set a list of hosts to use for communication.
-
‘:port` - number. set the port to use to communicate with Elasticsearch
-
‘:user` - String. The user to use for authentication.
-
‘:password` - String. The password to use for authentication.
-
‘:timeout` - Float. A duration value, in seconds, after which a socket
operation or request will be aborted if not yet successfull
-
‘:client_settings` - a hash; see below for keys.
The ‘client_settings` key is a has that can contain other settings:
-
‘:ssl` - Boolean. Enable or disable SSL/TLS.
-
‘:proxy` - String. Choose a HTTP HTTProxy to use.
-
‘:path` - String. The leading path for prefixing Elasticsearch
-
‘:headers` - Hash. Pairs of headers and their values requests. This is sometimes used if you are proxying Elasticsearch access through a special http path, such as using mod_rewrite.
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 52
def initialize(options={})
@logger = options[:logger]
@metric = options[:metric]
@bulk_request_metrics = @metric.namespace(:bulk_requests)
@bulk_response_metrics = @bulk_request_metrics.namespace(:responses)
@options = options
@url_template = build_url_template
@pool = build_pool(@options)
@bulk_path = @options[:bulk_path]
end
|
Instance Attribute Details
#action_count ⇒ Object
Returns the value of attribute action_count.
27
28
29
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 27
def action_count
@action_count
end
|
#client ⇒ Object
Returns the value of attribute client.
27
28
29
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 27
def client
@client
end
|
#logger ⇒ Object
Returns the value of attribute logger.
27
28
29
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 27
def logger
@logger
end
|
#options ⇒ Object
Returns the value of attribute options.
27
28
29
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 27
def options
@options
end
|
#pool ⇒ Object
Returns the value of attribute pool.
27
28
29
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 27
def pool
@pool
end
|
#recv_count ⇒ Object
Returns the value of attribute recv_count.
27
28
29
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 27
def recv_count
@recv_count
end
|
Instance Method Details
#build_adapter(options) ⇒ Object
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 255
def build_adapter(options)
timeout = options[:timeout] || 0
adapter_options = {
:socket_timeout => timeout,
:request_timeout => timeout,
}
adapter_options[:proxy] = client_settings[:proxy] if client_settings[:proxy]
adapter_options[:check_connection_timeout] = client_settings[:check_connection_timeout] if client_settings[:check_connection_timeout]
if client_settings[:pool_max]
adapter_options[:pool_max] = client_settings[:pool_max]
end
if client_settings[:pool_max_per_route]
adapter_options[:pool_max_per_route] = client_settings[:pool_max_per_route]
end
adapter_options[:ssl] = ssl_options if self.scheme == 'https'
adapter_options[:headers] = client_settings[:headers] if client_settings[:headers]
adapter_class = ::LogStash::Outputs::ElasticSearch::HttpClient::ManticoreAdapter
adapter = adapter_class.new(@logger, adapter_options)
end
|
#build_pool(options) ⇒ Object
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 285
def build_pool(options)
adapter = build_adapter(options)
pool_options = {
:sniffing => sniffing,
:sniffer_delay => options[:sniffer_delay],
:sniffing_path => options[:sniffing_path],
:healthcheck_path => options[:healthcheck_path],
:resurrect_delay => options[:resurrect_delay],
:url_normalizer => self.method(:host_to_url),
:metric => options[:metric]
}
pool_options[:scheme] = self.scheme if self.scheme
pool_class = ::LogStash::Outputs::ElasticSearch::HttpClient::Pool
full_urls = @options[:hosts].map {|h| host_to_url(h) }
pool = pool_class.new(@logger, adapter, full_urls, pool_options)
pool.start
pool
end
|
#build_url_template ⇒ Object
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 70
def build_url_template
{
:scheme => self.scheme,
:user => self.user,
:password => self.password,
:host => "URLTEMPLATE",
:port => self.port,
:path => self.path
}
end
|
#bulk(actions) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 93
def bulk(actions)
@action_count ||= 0
@action_count += actions.size
return if actions.empty?
bulk_actions = actions.collect do |action, args, source|
args, source = update_action_builder(args, source) if action == 'update'
if source && action != 'delete'
next [ { action => args }, source ]
else
next { action => args }
end
end
body_stream = StringIO.new
if http_compression
body_stream.set_encoding "BINARY"
stream_writer = Zlib::GzipWriter.new(body_stream, Zlib::DEFAULT_COMPRESSION, Zlib::DEFAULT_STRATEGY)
else
stream_writer = body_stream
end
bulk_responses = []
bulk_actions.each do |action|
as_json = action.is_a?(Array) ?
action.map {|line| LogStash::Json.dump(line)}.join("\n") :
LogStash::Json.dump(action)
as_json << "\n"
if (body_stream.size + as_json.bytesize) > TARGET_BULK_BYTES
bulk_responses << bulk_send(body_stream) unless body_stream.size == 0
end
stream_writer.write(as_json)
end
stream_writer.close if http_compression
bulk_responses << bulk_send(body_stream) if body_stream.size > 0
body_stream.close if !http_compression
join_bulk_responses(bulk_responses)
end
|
#bulk_send(body_stream) ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 140
def bulk_send(body_stream)
params = http_compression ? {:headers => {"Content-Encoding" => "gzip"}} : {}
response = @pool.post(@bulk_path, params, body_stream.string)
if !body_stream.closed?
body_stream.truncate(0)
body_stream.seek(0)
end
@bulk_response_metrics.increment(response.code.to_s)
if response.code != 200
url = ::LogStash::Util::SafeURI.new(response.final_url)
raise ::LogStash::Outputs::ElasticSearch::HttpClient::Pool::BadResponseCodeError.new(
response.code, url, body_stream.to_s, response.body
)
end
LogStash::Json.load(response.body)
end
|
#calculate_property(uris, property, default, sniff_check) ⇒ Object
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 175
def calculate_property(uris, property, default, sniff_check)
values = uris.map(&property).uniq
if sniff_check && values.size > 1
raise LogStash::ConfigurationError, "Cannot have multiple values for #{property} in hosts when sniffing is enabled!"
end
uri_value = values.first
default = nil if default.is_a?(String) && default.empty? uri_value = nil if uri_value.is_a?(String) && uri_value.empty?
if default && uri_value && (default != uri_value)
raise LogStash::ConfigurationError, "Explicit value for '#{property}' was declared, but it is different in one of the URLs given! Please make sure your URLs are inline with explicit values. The URLs have the property set to '#{uri_value}', but it was also set to '#{default}' explicitly"
end
uri_value || default
end
|
#client_settings ⇒ Object
243
244
245
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 243
def client_settings
@options[:client_settings] || {}
end
|
#close ⇒ Object
171
172
173
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 171
def close
@pool.close
end
|
#exists?(path, use_get = false) ⇒ Boolean
340
341
342
343
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 340
def exists?(path, use_get=false)
response = use_get ? @pool.get(path) : @pool.head(path)
response.code >= 200 && response.code <= 299
end
|
#get(path) ⇒ Object
161
162
163
164
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 161
def get(path)
response = @pool.get(path, nil)
LogStash::Json.load(response.body)
end
|
#get_ilm_endpoint ⇒ Object
381
382
383
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 381
def get_ilm_endpoint
@pool.get("/_ilm/policy")
end
|
#get_xpack_info ⇒ Object
377
378
379
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 377
def get_xpack_info
get("/_xpack")
end
|
#host_to_url(h) ⇒ Object
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 306
def host_to_url(h)
raw_scheme = @url_template[:scheme] || 'http'
raw_user = h.user || @url_template[:user]
raw_password = h.password || @url_template[:password]
postfixed_userinfo = raw_user && raw_password ? "#{raw_user}:#{raw_password}@" : nil
raw_host = h.host raw_port = h.port || @url_template[:port]
raw_path = !h.path.nil? && !h.path.empty? && h.path != "/" ? h.path : @url_template[:path]
prefixed_raw_path = raw_path && !raw_path.empty? ? raw_path : "/"
parameters = client_settings[:parameters]
raw_query = if parameters && !parameters.empty?
combined = h.query ?
Hash[URI::decode_www_form(h.query)].merge(parameters) :
parameters
query_str = combined.flat_map {|k,v|
values = Array(v)
values.map {|av| "#{k}=#{av}"}
}.join("&")
query_str
else
h.query
end
prefixed_raw_query = raw_query && !raw_query.empty? ? "?#{raw_query}" : nil
raw_url = "#{raw_scheme}://#{postfixed_userinfo}#{raw_host}:#{raw_port}#{prefixed_raw_path}#{prefixed_raw_query}"
::LogStash::Util::SafeURI.new(raw_url)
end
|
#http_compression ⇒ Object
251
252
253
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 251
def http_compression
client_settings.fetch(:http_compression, false)
end
|
#ilm_policy_exists?(name) ⇒ Boolean
385
386
387
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 385
def ilm_policy_exists?(name)
exists?("/_ilm/policy/#{name}", true)
end
|
#ilm_policy_put(name, policy) ⇒ Object
389
390
391
392
393
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 389
def ilm_policy_put(name, policy)
path = "_ilm/policy/#{name}"
logger.info("Installing ILM policy #{policy} to #{path}")
@pool.put(path, nil, LogStash::Json.dump(policy))
end
|
#join_bulk_responses(bulk_responses) ⇒ Object
133
134
135
136
137
138
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 133
def join_bulk_responses(bulk_responses)
{
"errors" => bulk_responses.any? {|r| r["errors"] == true},
"items" => bulk_responses.reduce([]) {|m,r| m.concat(r.fetch("items", []))}
}
end
|
#maximum_seen_major_version ⇒ Object
89
90
91
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 89
def maximum_seen_major_version
@pool.maximum_seen_major_version
end
|
#password ⇒ Object
202
203
204
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 202
def password
calculate_property(uris, :password, @options[:password], sniffing)
end
|
#path ⇒ Object
206
207
208
209
210
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 206
def path
calculated = calculate_property(uris, :path, client_settings[:path], sniffing)
calculated = "/#{calculated}" if calculated && !calculated.start_with?("/")
calculated
end
|
#port ⇒ Object
232
233
234
235
236
237
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 232
def port
calculate_property(uris, :port, nil, sniffing) || 9200
end
|
#post(path, params = {}, body_string) ⇒ Object
166
167
168
169
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 166
def post(path, params = {}, body_string)
response = @pool.post(path, params, body_string)
LogStash::Json.load(response.body)
end
|
#rollover_alias_exists?(name) ⇒ Boolean
check whether rollover alias already exists
358
359
360
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 358
def rollover_alias_exists?(name)
exists?(name)
end
|
#rollover_alias_put(alias_name, alias_definition) ⇒ Object
Create a new rollover alias
363
364
365
366
367
368
369
370
371
372
373
374
375
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 363
def rollover_alias_put(alias_name, alias_definition)
logger.info("Creating rollover alias #{alias_name}")
begin
@pool.put(CGI::escape(alias_name), nil, LogStash::Json.dump(alias_definition))
rescue ::LogStash::Outputs::ElasticSearch::HttpClient::Pool::BadResponseCodeError => e
if e.response_code == 400
logger.info("Rollover Alias #{alias_name} already exists. Skipping")
return
end
raise e
end
end
|
#scheme ⇒ Object
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 212
def scheme
explicit_scheme = if ssl_options && ssl_options.has_key?(:enabled)
ssl_options[:enabled] ? 'https' : 'http'
else
nil
end
calculated_scheme = calculate_property(uris, :scheme, explicit_scheme, sniffing)
if calculated_scheme && calculated_scheme !~ /https?/
raise LogStash::ConfigurationError, "Bad scheme '#{calculated_scheme}' found should be one of http/https"
end
if calculated_scheme && explicit_scheme && calculated_scheme != explicit_scheme
raise LogStash::ConfigurationError, "SSL option was explicitly set to #{ssl_options[:enabled]} but a URL was also declared with a scheme of '#{explicit_scheme}'. Please reconcile this"
end
calculated_scheme end
|
#sniffing ⇒ Object
194
195
196
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 194
def sniffing
@options[:sniffing]
end
|
#ssl_options ⇒ Object
247
248
249
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 247
def ssl_options
client_settings.fetch(:ssl, {})
end
|
#template_exists?(name) ⇒ Boolean
345
346
347
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 345
def template_exists?(name)
exists?("/_template/#{name}")
end
|
#template_install(name, template, force = false) ⇒ Object
81
82
83
84
85
86
87
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 81
def template_install(name, template, force=false)
if template_exists?(name) && !force
@logger.debug("Found existing Elasticsearch template. Skipping template management", :name => name)
return
end
template_put(name, template)
end
|
#template_put(name, template) ⇒ Object
349
350
351
352
353
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 349
def template_put(name, template)
path = "_template/#{name}"
logger.info("Installing elasticsearch template to #{path}")
@pool.put(path, nil, LogStash::Json.dump(template))
end
|
#update_action_builder(args, source) ⇒ Object
Build a bulk item for an elasticsearch update action
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 397
def update_action_builder(args, source)
if args[:_script]
source_orig = source
source = { 'script' => {'params' => { @options[:script_var_name] => source_orig }} }
if @options[:scripted_upsert]
source['scripted_upsert'] = true
source['upsert'] = {}
elsif @options[:doc_as_upsert]
source['upsert'] = source_orig
else
source['upsert'] = args.delete(:_upsert) if args[:_upsert]
end
case @options[:script_type]
when 'indexed'
source['script']['id'] = args.delete(:_script)
when 'file'
source['script']['file'] = args.delete(:_script)
when 'inline'
source['script']['inline'] = args.delete(:_script)
end
source['script']['lang'] = @options[:script_lang] if @options[:script_lang] != ''
else
source = { 'doc' => source }
if @options[:doc_as_upsert]
source['doc_as_upsert'] = true
else
source['upsert'] = args.delete(:_upsert) if args[:_upsert]
end
end
[args, source]
end
|
#uris ⇒ Object
239
240
241
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 239
def uris
@options[:hosts]
end
|
#user ⇒ Object
198
199
200
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 198
def user
calculate_property(uris, :user, @options[:user], sniffing)
end
|