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
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 requests. This is sometimes used if you are proxying Elasticsearch access through a special http path, such as using mod_rewrite.
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 49
def initialize(options={})
@logger = options[:logger]
@options = options
@url_template = build_url_template
@pool = build_pool(@options)
end
|
Instance Attribute Details
#action_count ⇒ Object
Returns the value of attribute action_count.
25
26
27
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 25
def action_count
@action_count
end
|
#client ⇒ Object
Returns the value of attribute client.
25
26
27
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 25
def client
@client
end
|
#logger ⇒ Object
Returns the value of attribute logger.
25
26
27
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 25
def logger
@logger
end
|
#options ⇒ Object
Returns the value of attribute options.
25
26
27
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 25
def options
@options
end
|
#pool ⇒ Object
Returns the value of attribute pool.
25
26
27
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 25
def pool
@pool
end
|
#recv_count ⇒ Object
Returns the value of attribute recv_count.
25
26
27
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 25
def recv_count
@recv_count
end
|
Instance Method Details
#build_adapter(options) ⇒ Object
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 218
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_class = ::LogStash::Outputs::ElasticSearch::HttpClient::ManticoreAdapter
adapter = adapter_class.new(@logger, adapter_options)
end
|
#build_pool(options) ⇒ Object
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 246
def build_pool(options)
adapter = build_adapter(options)
pool_options = {
:sniffing => sniffing,
:sniffer_delay => options[:sniffer_delay],
:healthcheck_path => options[:healthcheck_path],
:absolute_healthcheck_path => options[:absolute_healthcheck_path],
:sniffing_path => options[:sniffing_path],
:absolute_sniffing_path => options[:absolute_sniffing_path],
:resurrect_delay => options[:resurrect_delay],
:url_normalizer => self.method(:host_to_url)
}
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
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 63
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
87
88
89
90
91
92
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
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 87
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
bulk_body = ""
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 (bulk_body.bytesize + as_json.bytesize) > TARGET_BULK_BYTES
bulk_responses << bulk_send(bulk_body)
bulk_body = as_json
else
bulk_body << as_json
end
end
bulk_responses << bulk_send(bulk_body) if bulk_body.size > 0
join_bulk_responses(bulk_responses)
end
|
#bulk_send(bulk_body) ⇒ Object
131
132
133
134
135
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 131
def bulk_send(bulk_body)
url, response = @pool.post("_bulk", nil, bulk_body)
LogStash::Json.load(response.body)
end
|
#calculate_property(uris, property, default, sniff_check) ⇒ Object
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 142
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
210
211
212
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 210
def client_settings
@options[:client_settings] || {}
end
|
#close ⇒ Object
137
138
139
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 137
def close
@pool.close
end
|
#get_version ⇒ Object
82
83
84
85
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 82
def get_version
url, response = @pool.get("")
LogStash::Json.load(response.body)["version"]
end
|
#host_to_url(h) ⇒ Object
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 268
def host_to_url(h)
uri_klass = @url_template[:scheme] == 'https' ? URI::HTTPS : URI::HTTP
uri = uri_klass.build(@url_template)
uri.user = h.user || user
uri.password = h.password || password
uri.host = h.host if h.host
uri.port = h.port if h.port
uri.path = h.path if !h.path.nil? && !h.path.empty? && h.path != "/"
uri.query = h.query
parameters = client_settings[:parameters]
if parameters && !parameters.empty?
combined = uri.query ?
Hash[URI::decode_www_form(uri.query)].merge(parameters) :
parameters
query_str = combined.flat_map {|k,v|
values = Array(v)
values.map {|av| "#{k}=#{av}"}
}.join("&")
uri.query = query_str
end
::LogStash::Util::SafeURI.new(uri.normalize)
end
|
#join_bulk_responses(bulk_responses) ⇒ Object
124
125
126
127
128
129
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 124
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
|
#password ⇒ Object
169
170
171
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 169
def password
calculate_property(uris, :password, @options[:password], sniffing)
end
|
#path ⇒ Object
173
174
175
176
177
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 173
def path
calculated = calculate_property(uris, :path, client_settings[:path], sniffing)
calculated = "/#{calculated}" if calculated && !calculated.start_with?("/")
calculated
end
|
#port ⇒ Object
199
200
201
202
203
204
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 199
def port
calculate_property(uris, :port, nil, sniffing) || 9200
end
|
#scheme ⇒ Object
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 179
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
161
162
163
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 161
def sniffing
@options[:sniffing]
end
|
#ssl_options ⇒ Object
214
215
216
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 214
def ssl_options
client_settings.fetch(:ssl, {})
end
|
#template_exists?(name) ⇒ Boolean
297
298
299
300
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 297
def template_exists?(name)
url, response = @pool.head("/_template/#{name}")
response.code >= 200 && response.code <= 299
end
|
#template_install(name, template, force = false) ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 74
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
302
303
304
305
306
307
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 302
def template_put(name, template)
path = "_template/#{name}"
logger.info("Installing elasticsearch template to #{path}")
url, response = @pool.put(path, nil, LogStash::Json.dump(template))
response
end
|
#update_action_builder(args, source) ⇒ Object
Build a bulk item for an elasticsearch update action
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
339
340
341
342
343
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 310
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
206
207
208
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 206
def uris
@options[:hosts]
end
|
#user ⇒ Object
165
166
167
|
# File 'lib/logstash/outputs/elasticsearch/http_client.rb', line 165
def user
calculate_property(uris, :user, @options[:user], sniffing)
end
|