fluent-plugin-elasticsearch 1.8.0 → 1.9.0.rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f279ccf2181eb8582234fb5b417439dfd8f22ac9
4
- data.tar.gz: e3b8e02a2aa972ac0e07971369dd9381b58401e3
3
+ metadata.gz: ac0ae4b2396f3c809f7cb7e59fe8dbe1b26bf516
4
+ data.tar.gz: 6354f35f3a2ef593c5d1e4e3bf5213f6d0d17efa
5
5
  SHA512:
6
- metadata.gz: a576bf1cd9ff05130d1f71cabfc6b264ffc53f86cb9780af42ee0aa4f647a24905e5d41c22dd446474896bf2ee17b7ad942011bc2f6dd10cf4231fde348e35aa
7
- data.tar.gz: b938736ba3ed0fbf8846e864c5f7273c1b059a930a4b91165d2513e6038de6bdf48968570479047dce33a7ad3fa727a02f48215d3be724a6d60414c024010ae4
6
+ metadata.gz: 55af560dafa19e24f62819c95944673113ec8b9541f0bf338938fadfabf8b9cad48850c201f829365761739278fdde2356887eef1ebe13ae62f267b7946efc6b
7
+ data.tar.gz: 71cd49d06e9e664d0143117669b8aab67efa228271b952bc2cae296ab69011fbe749553611f306a2cf44d7e7001ca5b08da7d23ddd8bc7892225d292f6f0553e
data/History.md CHANGED
@@ -1,6 +1,11 @@
1
1
  ## Changelog [[tags]](https://github.com/uken/fluent-plugin-elasticsearch/tags)
2
2
 
3
3
  ### [Unreleased]
4
+
5
+ ### 1.9.0.rc.1
6
+ - Optimize output plugins (#203)
7
+
8
+ ### 1.8.0
4
9
  - fix typo in defaults for ssl_verify on elasticsearch_dynamic (#202)
5
10
  - add support for `templates` (#196)
6
11
  - rename `send` method to `send_bulk` (#206)
@@ -3,7 +3,7 @@ $:.push File.expand_path('../lib', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = 'fluent-plugin-elasticsearch'
6
- s.version = '1.8.0'
6
+ s.version = '1.9.0.rc.1'
7
7
  s.authors = ['diogo', 'pitr']
8
8
  s.email = ['pitr.vern@gmail.com', 'me@diogoterror.com']
9
9
  s.description = %q{ElasticSearch output plugin for Fluent event collector}
@@ -9,9 +9,10 @@ begin
9
9
  rescue LoadError
10
10
  end
11
11
 
12
+ require 'fluent/output'
12
13
  require_relative 'elasticsearch_index_template'
13
14
 
14
- class Fluent::ElasticsearchOutput < Fluent::BufferedOutput
15
+ class Fluent::ElasticsearchOutput < Fluent::ObjectBufferedOutput
15
16
  class ConnectionFailure < StandardError; end
16
17
 
17
18
  Fluent::Plugin.register_output('elasticsearch', self)
@@ -55,10 +56,10 @@ class Fluent::ElasticsearchOutput < Fluent::BufferedOutput
55
56
  config_param :template_name, :string, :default => nil
56
57
  config_param :template_file, :string, :default => nil
57
58
  config_param :templates, :hash, :default => nil
59
+ config_param :include_tag_key, :bool, :default => false
60
+ config_param :tag_key, :string, :default => 'tag'
58
61
 
59
- include Fluent::SetTagKeyMixin
60
62
  include Fluent::ElasticsearchIndexTemplate
61
- config_set_default :include_tag_key, false
62
63
 
63
64
  def initialize
64
65
  super
@@ -91,6 +92,22 @@ class Fluent::ElasticsearchOutput < Fluent::BufferedOutput
91
92
  templates_hash_install (@templates)
92
93
  end
93
94
 
95
+ @meta_config_map = create_meta_config_map
96
+
97
+ begin
98
+ require 'oj'
99
+ @dump_proc = Oj.method(:dump)
100
+ rescue LoadError
101
+ @dump_proc = Yajl.method(:dump)
102
+ end
103
+ end
104
+
105
+ def create_meta_config_map
106
+ result = []
107
+ result << [@id_key, '_id'] if @id_key
108
+ result << [@parent_key, '_parent'] if @parent_key
109
+ result << [@routing_key, '_routing'] if @routing_key
110
+ result
94
111
  end
95
112
 
96
113
  def start
@@ -198,40 +215,47 @@ class Fluent::ElasticsearchOutput < Fluent::BufferedOutput
198
215
  end.join(', ')
199
216
  end
200
217
 
201
- def format(tag, time, record)
202
- [tag, time, record].to_msgpack
203
- end
204
-
205
218
  def shutdown
206
219
  super
207
220
  end
208
221
 
209
- def append_record_to_messages(op, meta, record, msgs)
222
+ BODY_DELIMITER = "\n".freeze
223
+ UPDATE_OP = "update".freeze
224
+ UPSERT_OP = "upsert".freeze
225
+ CREATE_OP = "create".freeze
226
+ INDEX_OP = "index".freeze
227
+ ID_FIELD = "_id".freeze
228
+ TIMESTAMP_FIELD = "@timestamp".freeze
229
+
230
+ def append_record_to_messages(op, meta, header, record, msgs)
210
231
  case op
211
- when "update", "upsert"
212
- if meta.has_key?("_id")
213
- msgs << { "update" => meta }
214
- msgs << update_body(record, op)
232
+ when UPDATE_OP, UPSERT_OP
233
+ if meta.has_key?(ID_FIELD)
234
+ header[UPDATE_OP] = meta
235
+ msgs << @dump_proc.call(header) << BODY_DELIMITER
236
+ msgs << @dump_proc.call(update_body(record, op)) << BODY_DELIMITER
215
237
  end
216
- when "create"
217
- if meta.has_key?("_id")
218
- msgs << { "create" => meta }
219
- msgs << record
238
+ when CREATE_OP
239
+ if meta.has_key?(ID_FIELD)
240
+ header[CREATE_OP] = meta
241
+ msgs << @dump_proc.call(header) << BODY_DELIMITER
242
+ msgs << @dump_proc.call(record) << BODY_DELIMITER
220
243
  end
221
- when "index"
222
- msgs << { "index" => meta }
223
- msgs << record
244
+ when INDEX_OP
245
+ header[INDEX_OP] = meta
246
+ msgs << @dump_proc.call(header) << BODY_DELIMITER
247
+ msgs << @dump_proc.call(record) << BODY_DELIMITER
224
248
  end
225
249
  end
226
250
 
227
251
  def update_body(record, op)
228
252
  update = remove_keys(record)
229
- body = { "doc" => update }
230
- if op == "upsert"
253
+ body = {"doc".freeze => update}
254
+ if op == UPSERT_OP
231
255
  if update == record
232
- body["doc_as_upsert"] = true
256
+ body["doc_as_upsert".freeze] = true
233
257
  else
234
- body["upsert"] = record
258
+ body[UPSERT_OP] = record
235
259
  end
236
260
  end
237
261
  body
@@ -261,28 +285,31 @@ class Fluent::ElasticsearchOutput < Fluent::BufferedOutput
261
285
  ret
262
286
  end
263
287
 
264
- def write(chunk)
265
- bulk_message = []
288
+ def write_objects(tag, chunk)
289
+ bulk_message = ''
290
+ header = {}
291
+ meta = {}
292
+
293
+ chunk.msgpack_each do |time, record|
294
+ next unless record.is_a? Hash
266
295
 
267
- chunk.msgpack_each do |tag, time, record|
268
296
  if @flatten_hashes
269
297
  record = flatten_record(record)
270
298
  end
271
299
 
272
- next unless record.is_a? Hash
273
- target_index_parent, target_index_child_key = get_parent_of(record, @target_index_key)
300
+ target_index_parent, target_index_child_key = @target_index_key ? get_parent_of(record, @target_index_key) : nil
274
301
  if target_index_parent && target_index_parent[target_index_child_key]
275
302
  target_index = target_index_parent.delete(target_index_child_key)
276
303
  elsif @logstash_format
277
- if record.has_key?("@timestamp")
278
- dt = record["@timestamp"]
279
- dt = @time_parser.parse(record["@timestamp"], time)
304
+ if record.has_key?(TIMESTAMP_FIELD)
305
+ dt = record[TIMESTAMP_FIELD]
306
+ dt = @time_parser.parse(record[TIMESTAMP_FIELD], time)
280
307
  elsif record.has_key?(@time_key)
281
308
  dt = @time_parser.parse(record[@time_key], time)
282
- record['@timestamp'] = record[@time_key] unless time_key_exclude_timestamp
309
+ record[TIMESTAMP_FIELD] = record[@time_key] unless time_key_exclude_timestamp
283
310
  else
284
311
  dt = Time.at(time).to_datetime
285
- record.merge!({"@timestamp" => dt.to_s})
312
+ record[TIMESTAMP_FIELD] = dt.to_s
286
313
  end
287
314
  dt = dt.new_offset(0) if @utc_index
288
315
  target_index = "#{@logstash_prefix}-#{dt.strftime(@logstash_dateformat)}"
@@ -294,29 +321,29 @@ class Fluent::ElasticsearchOutput < Fluent::BufferedOutput
294
321
  # allow upper-case characters in index names.
295
322
  target_index = target_index.downcase
296
323
  if @include_tag_key
297
- record.merge!(@tag_key => tag)
324
+ record[@tag_key] = tag
298
325
  end
299
326
 
300
- target_type_parent, target_type_child_key = get_parent_of(record, @target_type_key)
327
+ target_type_parent, target_type_child_key = @target_type_key ? get_parent_of(record, @target_type_key) : nil
301
328
  if target_type_parent && target_type_parent[target_type_child_key]
302
329
  target_type = target_type_parent.delete(target_type_child_key)
303
330
  else
304
331
  target_type = @type_name
305
332
  end
306
333
 
307
- meta = {"_index" => target_index, "_type" => target_type}
334
+ meta.clear
335
+ meta["_index".freeze] = target_index
336
+ meta["_type".freeze] = target_type
308
337
 
309
- @meta_config_map ||= { 'id_key' => '_id', 'parent_key' => '_parent', 'routing_key' => '_routing' }
310
- @meta_config_map.each_pair do |config_name, meta_key|
311
- record_key = self.instance_variable_get("@#{config_name}")
312
- meta[meta_key] = record[record_key] if record_key && record[record_key]
338
+ @meta_config_map.each do |record_key, meta_key|
339
+ meta[meta_key] = record[record_key] if record[record_key]
313
340
  end
314
341
 
315
342
  if @remove_keys
316
343
  @remove_keys.each { |key| record.delete(key) }
317
344
  end
318
345
 
319
- append_record_to_messages(@write_operation, meta, record, bulk_message)
346
+ append_record_to_messages(@write_operation, meta, header, record, bulk_message)
320
347
  end
321
348
 
322
349
  send_bulk(bulk_message) unless bulk_message.empty?
@@ -326,8 +353,6 @@ class Fluent::ElasticsearchOutput < Fluent::BufferedOutput
326
353
  # returns [parent, child_key] of child described by path array in record's tree
327
354
  # returns [nil, child_key] if path doesnt exist in record
328
355
  def get_parent_of(record, path)
329
- return [nil, nil] unless path
330
-
331
356
  parent_object = path[0..-2].reduce(record) { |a, e| a.is_a?(Hash) ? a[e] : nil }
332
357
  [parent_object, path[-1]]
333
358
  end
@@ -33,6 +33,10 @@ class Fluent::ElasticsearchOutputDynamic < Fluent::ElasticsearchOutput
33
33
  @current_config = nil
34
34
  end
35
35
 
36
+ def create_meta_config_map
37
+ {'id_key' => '_id', 'parent_key' => '_parent', 'routing_key' => '_routing'}
38
+ end
39
+
36
40
  def client(host)
37
41
 
38
42
  # check here to see if we already have a client connection for the given host
@@ -109,11 +113,18 @@ class Fluent::ElasticsearchOutputDynamic < Fluent::ElasticsearchOutput
109
113
  end.join(', ')
110
114
  end
111
115
 
112
- def write(chunk)
113
- bulk_message = Hash.new { |h,k| h[k] = [] }
116
+ def write_objects(tag, chunk)
117
+ bulk_message = Hash.new { |h,k| h[k] = '' }
114
118
  dynamic_conf = @dynamic_config.clone
115
119
 
116
- chunk.msgpack_each do |tag, time, record|
120
+ headers = {
121
+ UPDATE_OP => {},
122
+ UPSERT_OP => {},
123
+ CREATE_OP => {},
124
+ INDEX_OP => {}
125
+ }
126
+
127
+ chunk.msgpack_each do |time, record|
117
128
  next unless record.is_a? Hash
118
129
 
119
130
  # evaluate all configurations here
@@ -157,7 +168,6 @@ class Fluent::ElasticsearchOutputDynamic < Fluent::ElasticsearchOutput
157
168
 
158
169
  meta = {"_index" => target_index, "_type" => dynamic_conf['type_name']}
159
170
 
160
- @meta_config_map ||= { 'id_key' => '_id', 'parent_key' => '_parent', 'routing_key' => '_routing' }
161
171
  @meta_config_map.each_pair do |config_name, meta_key|
162
172
  if dynamic_conf[config_name] && record[dynamic_conf[config_name]]
163
173
  meta[meta_key] = record[dynamic_conf[config_name]]
@@ -174,12 +184,13 @@ class Fluent::ElasticsearchOutputDynamic < Fluent::ElasticsearchOutput
174
184
  @remove_keys.each { |key| record.delete(key) }
175
185
  end
176
186
 
177
- append_record_to_messages(dynamic_conf["write_operation"], meta, record, bulk_message[host])
187
+ write_op = dynamic_conf["write_operation"]
188
+ append_record_to_messages(write_op, meta, headers[write_op], record, bulk_message[host])
178
189
  end
179
190
 
180
- bulk_message.each do | hKey, array |
181
- send_bulk(array, hKey) unless array.empty?
182
- array.clear
191
+ bulk_message.each do |hKey, msgs|
192
+ send_bulk(msgs, hKey) unless msgs.empty?
193
+ msgs.clear
183
194
  end
184
195
  end
185
196
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-elasticsearch
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.0
4
+ version: 1.9.0.rc.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - diogo
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-10-24 00:00:00.000000000 Z
12
+ date: 2016-11-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fluentd
@@ -151,9 +151,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
151
151
  version: '2.0'
152
152
  required_rubygems_version: !ruby/object:Gem::Requirement
153
153
  requirements:
154
- - - ">="
154
+ - - ">"
155
155
  - !ruby/object:Gem::Version
156
- version: '0'
156
+ version: 1.3.1
157
157
  requirements: []
158
158
  rubyforge_project:
159
159
  rubygems_version: 2.5.1