fluent-plugin-elasticsearch 2.10.2 → 2.10.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.coveralls.yml +2 -2
- data/.editorconfig +9 -9
- data/.gitignore +18 -18
- data/.travis.yml +14 -14
- data/Gemfile +8 -8
- data/History.md +239 -236
- data/ISSUE_TEMPLATE.md +25 -25
- data/LICENSE.txt +201 -201
- data/PULL_REQUEST_TEMPLATE.md +10 -10
- data/README.md +807 -795
- data/Rakefile +11 -11
- data/appveyor.yml +35 -35
- data/fluent-plugin-elasticsearch.gemspec +31 -31
- data/lib/fluent/plugin/elasticsearch_constants.rb +13 -13
- data/lib/fluent/plugin/elasticsearch_error_handler.rb +87 -87
- data/lib/fluent/plugin/elasticsearch_index_template.rb +60 -42
- data/lib/fluent/plugin/filter_elasticsearch_genid.rb +25 -25
- data/lib/fluent/plugin/out_elasticsearch.rb +538 -533
- data/lib/fluent/plugin/out_elasticsearch_dynamic.rb +294 -294
- data/test/helper.rb +24 -24
- data/test/plugin/test_elasticsearch_error_handler.rb +195 -195
- data/test/plugin/test_filter_elasticsearch_genid.rb +44 -44
- data/test/plugin/test_out_elasticsearch.rb +2046 -2009
- data/test/plugin/test_out_elasticsearch_dynamic.rb +992 -992
- data/test/plugin/test_template.json +23 -23
- metadata +3 -3
@@ -1,25 +1,25 @@
|
|
1
|
-
require 'securerandom'
|
2
|
-
require 'base64'
|
3
|
-
require 'fluent/plugin/filter'
|
4
|
-
|
5
|
-
module Fluent::Plugin
|
6
|
-
class ElasticsearchGenidFilter < Filter
|
7
|
-
Fluent::Plugin.register_filter('elasticsearch_genid', self)
|
8
|
-
|
9
|
-
config_param :hash_id_key, :string, :default => '_hash'
|
10
|
-
|
11
|
-
def initialize
|
12
|
-
super
|
13
|
-
end
|
14
|
-
|
15
|
-
def configure(conf)
|
16
|
-
super
|
17
|
-
end
|
18
|
-
|
19
|
-
def filter(tag, time, record)
|
20
|
-
record[@hash_id_key] = Base64.strict_encode64(SecureRandom.uuid)
|
21
|
-
record
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
end
|
1
|
+
require 'securerandom'
|
2
|
+
require 'base64'
|
3
|
+
require 'fluent/plugin/filter'
|
4
|
+
|
5
|
+
module Fluent::Plugin
|
6
|
+
class ElasticsearchGenidFilter < Filter
|
7
|
+
Fluent::Plugin.register_filter('elasticsearch_genid', self)
|
8
|
+
|
9
|
+
config_param :hash_id_key, :string, :default => '_hash'
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
super
|
13
|
+
end
|
14
|
+
|
15
|
+
def configure(conf)
|
16
|
+
super
|
17
|
+
end
|
18
|
+
|
19
|
+
def filter(tag, time, record)
|
20
|
+
record[@hash_id_key] = Base64.strict_encode64(SecureRandom.uuid)
|
21
|
+
record
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -1,533 +1,538 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
require 'date'
|
3
|
-
require 'excon'
|
4
|
-
require 'elasticsearch'
|
5
|
-
require 'json'
|
6
|
-
require 'uri'
|
7
|
-
begin
|
8
|
-
require 'strptime'
|
9
|
-
rescue LoadError
|
10
|
-
end
|
11
|
-
|
12
|
-
require 'fluent/plugin/output'
|
13
|
-
require 'fluent/event'
|
14
|
-
require_relative 'elasticsearch_constants'
|
15
|
-
require_relative 'elasticsearch_error_handler'
|
16
|
-
require_relative 'elasticsearch_index_template'
|
17
|
-
|
18
|
-
module Fluent::Plugin
|
19
|
-
class ElasticsearchOutput < Output
|
20
|
-
class ConnectionFailure < StandardError; end
|
21
|
-
|
22
|
-
# RetryStreamError privides a stream to be
|
23
|
-
# put back in the pipeline for cases where a bulk request
|
24
|
-
# failed (e.g some records succeed while others failed)
|
25
|
-
class RetryStreamError < StandardError
|
26
|
-
attr_reader :retry_stream
|
27
|
-
def initialize(retry_stream)
|
28
|
-
@retry_stream = retry_stream
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
helpers :event_emitter, :compat_parameters, :record_accessor
|
33
|
-
|
34
|
-
Fluent::Plugin.register_output('elasticsearch', self)
|
35
|
-
|
36
|
-
DEFAULT_BUFFER_TYPE = "memory"
|
37
|
-
DEFAULT_ELASTICSEARCH_VERSION = 5 # For compatibility.
|
38
|
-
DEFAULT_TYPE_NAME_ES_7x = "_doc".freeze
|
39
|
-
DEFAULT_TYPE_NAME = "fluentd".freeze
|
40
|
-
|
41
|
-
config_param :host, :string, :default => 'localhost'
|
42
|
-
config_param :port, :integer, :default => 9200
|
43
|
-
config_param :user, :string, :default => nil
|
44
|
-
config_param :password, :string, :default => nil, :secret => true
|
45
|
-
config_param :path, :string, :default => nil
|
46
|
-
config_param :scheme, :string, :default => 'http'
|
47
|
-
config_param :hosts, :string, :default => nil
|
48
|
-
config_param :target_index_key, :string, :default => nil
|
49
|
-
config_param :target_type_key, :string, :default => nil,
|
50
|
-
:deprecated => <<EOC
|
51
|
-
Elasticsearch 7.x or above will ignore this config. Please use fixed type_name instead.
|
52
|
-
EOC
|
53
|
-
config_param :time_key_format, :string, :default => nil
|
54
|
-
config_param :time_precision, :integer, :default => 9
|
55
|
-
config_param :include_timestamp, :bool, :default => false
|
56
|
-
config_param :logstash_format, :bool, :default => false
|
57
|
-
config_param :logstash_prefix, :string, :default => "logstash"
|
58
|
-
config_param :logstash_prefix_separator, :string, :default => '-'
|
59
|
-
config_param :logstash_dateformat, :string, :default => "%Y.%m.%d"
|
60
|
-
config_param :utc_index, :bool, :default => true
|
61
|
-
config_param :type_name, :string, :default => DEFAULT_TYPE_NAME
|
62
|
-
config_param :index_name, :string, :default => "fluentd"
|
63
|
-
config_param :id_key, :string, :default => nil
|
64
|
-
config_param :write_operation, :string, :default => "index"
|
65
|
-
config_param :parent_key, :string, :default => nil
|
66
|
-
config_param :routing_key, :string, :default => nil
|
67
|
-
config_param :request_timeout, :time, :default => 5
|
68
|
-
config_param :reload_connections, :bool, :default => true
|
69
|
-
config_param :reload_on_failure, :bool, :default => false
|
70
|
-
config_param :retry_tag, :string, :default=>nil
|
71
|
-
config_param :resurrect_after, :time, :default => 60
|
72
|
-
config_param :time_key, :string, :default => nil
|
73
|
-
config_param :time_key_exclude_timestamp, :bool, :default => false
|
74
|
-
config_param :ssl_verify , :bool, :default => true
|
75
|
-
config_param :client_key, :string, :default => nil
|
76
|
-
config_param :client_cert, :string, :default => nil
|
77
|
-
config_param :client_key_pass, :string, :default => nil
|
78
|
-
config_param :ca_file, :string, :default => nil
|
79
|
-
config_param :ssl_version, :enum, list: [:SSLv23, :TLSv1, :TLSv1_1, :TLSv1_2], :default => :TLSv1
|
80
|
-
config_param :remove_keys, :string, :default => nil
|
81
|
-
config_param :remove_keys_on_update, :string, :default => ""
|
82
|
-
config_param :remove_keys_on_update_key, :string, :default => nil
|
83
|
-
config_param :flatten_hashes, :bool, :default => false
|
84
|
-
config_param :flatten_hashes_separator, :string, :default => "_"
|
85
|
-
config_param :template_name, :string, :default => nil
|
86
|
-
config_param :template_file, :string, :default => nil
|
87
|
-
config_param :template_overwrite, :bool, :default => false
|
88
|
-
config_param :templates, :hash, :default => nil
|
89
|
-
config_param :
|
90
|
-
config_param :
|
91
|
-
config_param :
|
92
|
-
config_param :
|
93
|
-
config_param :
|
94
|
-
config_param :
|
95
|
-
config_param :
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
config_set_default
|
104
|
-
config_set_default :
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
include Fluent::
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
log.
|
178
|
-
end
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
end
|
189
|
-
|
190
|
-
def
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
#
|
217
|
-
#
|
218
|
-
|
219
|
-
Proc.new { |value|
|
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
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
es
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
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
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
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
|
-
header
|
331
|
-
msgs << @dump_proc.call(
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
record
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
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
|
-
if @
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
record[TIMESTAMP_FIELD]
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
target_index =
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
if
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
log.warn "Detected ES 7.x or above: `_doc` will be used as the document `_type`."
|
469
|
-
target_type = '_doc'.freeze
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'date'
|
3
|
+
require 'excon'
|
4
|
+
require 'elasticsearch'
|
5
|
+
require 'json'
|
6
|
+
require 'uri'
|
7
|
+
begin
|
8
|
+
require 'strptime'
|
9
|
+
rescue LoadError
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'fluent/plugin/output'
|
13
|
+
require 'fluent/event'
|
14
|
+
require_relative 'elasticsearch_constants'
|
15
|
+
require_relative 'elasticsearch_error_handler'
|
16
|
+
require_relative 'elasticsearch_index_template'
|
17
|
+
|
18
|
+
module Fluent::Plugin
|
19
|
+
class ElasticsearchOutput < Output
|
20
|
+
class ConnectionFailure < StandardError; end
|
21
|
+
|
22
|
+
# RetryStreamError privides a stream to be
|
23
|
+
# put back in the pipeline for cases where a bulk request
|
24
|
+
# failed (e.g some records succeed while others failed)
|
25
|
+
class RetryStreamError < StandardError
|
26
|
+
attr_reader :retry_stream
|
27
|
+
def initialize(retry_stream)
|
28
|
+
@retry_stream = retry_stream
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
helpers :event_emitter, :compat_parameters, :record_accessor
|
33
|
+
|
34
|
+
Fluent::Plugin.register_output('elasticsearch', self)
|
35
|
+
|
36
|
+
DEFAULT_BUFFER_TYPE = "memory"
|
37
|
+
DEFAULT_ELASTICSEARCH_VERSION = 5 # For compatibility.
|
38
|
+
DEFAULT_TYPE_NAME_ES_7x = "_doc".freeze
|
39
|
+
DEFAULT_TYPE_NAME = "fluentd".freeze
|
40
|
+
|
41
|
+
config_param :host, :string, :default => 'localhost'
|
42
|
+
config_param :port, :integer, :default => 9200
|
43
|
+
config_param :user, :string, :default => nil
|
44
|
+
config_param :password, :string, :default => nil, :secret => true
|
45
|
+
config_param :path, :string, :default => nil
|
46
|
+
config_param :scheme, :string, :default => 'http'
|
47
|
+
config_param :hosts, :string, :default => nil
|
48
|
+
config_param :target_index_key, :string, :default => nil
|
49
|
+
config_param :target_type_key, :string, :default => nil,
|
50
|
+
:deprecated => <<EOC
|
51
|
+
Elasticsearch 7.x or above will ignore this config. Please use fixed type_name instead.
|
52
|
+
EOC
|
53
|
+
config_param :time_key_format, :string, :default => nil
|
54
|
+
config_param :time_precision, :integer, :default => 9
|
55
|
+
config_param :include_timestamp, :bool, :default => false
|
56
|
+
config_param :logstash_format, :bool, :default => false
|
57
|
+
config_param :logstash_prefix, :string, :default => "logstash"
|
58
|
+
config_param :logstash_prefix_separator, :string, :default => '-'
|
59
|
+
config_param :logstash_dateformat, :string, :default => "%Y.%m.%d"
|
60
|
+
config_param :utc_index, :bool, :default => true
|
61
|
+
config_param :type_name, :string, :default => DEFAULT_TYPE_NAME
|
62
|
+
config_param :index_name, :string, :default => "fluentd"
|
63
|
+
config_param :id_key, :string, :default => nil
|
64
|
+
config_param :write_operation, :string, :default => "index"
|
65
|
+
config_param :parent_key, :string, :default => nil
|
66
|
+
config_param :routing_key, :string, :default => nil
|
67
|
+
config_param :request_timeout, :time, :default => 5
|
68
|
+
config_param :reload_connections, :bool, :default => true
|
69
|
+
config_param :reload_on_failure, :bool, :default => false
|
70
|
+
config_param :retry_tag, :string, :default=>nil
|
71
|
+
config_param :resurrect_after, :time, :default => 60
|
72
|
+
config_param :time_key, :string, :default => nil
|
73
|
+
config_param :time_key_exclude_timestamp, :bool, :default => false
|
74
|
+
config_param :ssl_verify , :bool, :default => true
|
75
|
+
config_param :client_key, :string, :default => nil
|
76
|
+
config_param :client_cert, :string, :default => nil
|
77
|
+
config_param :client_key_pass, :string, :default => nil
|
78
|
+
config_param :ca_file, :string, :default => nil
|
79
|
+
config_param :ssl_version, :enum, list: [:SSLv23, :TLSv1, :TLSv1_1, :TLSv1_2], :default => :TLSv1
|
80
|
+
config_param :remove_keys, :string, :default => nil
|
81
|
+
config_param :remove_keys_on_update, :string, :default => ""
|
82
|
+
config_param :remove_keys_on_update_key, :string, :default => nil
|
83
|
+
config_param :flatten_hashes, :bool, :default => false
|
84
|
+
config_param :flatten_hashes_separator, :string, :default => "_"
|
85
|
+
config_param :template_name, :string, :default => nil
|
86
|
+
config_param :template_file, :string, :default => nil
|
87
|
+
config_param :template_overwrite, :bool, :default => false
|
88
|
+
config_param :templates, :hash, :default => nil
|
89
|
+
config_param :max_retry_putting_template, :integer, :default => 10
|
90
|
+
config_param :include_tag_key, :bool, :default => false
|
91
|
+
config_param :tag_key, :string, :default => 'tag'
|
92
|
+
config_param :time_parse_error_tag, :string, :default => 'Fluent::ElasticsearchOutput::TimeParser.error'
|
93
|
+
config_param :reconnect_on_error, :bool, :default => false
|
94
|
+
config_param :pipeline, :string, :default => nil
|
95
|
+
config_param :with_transporter_log, :bool, :default => false
|
96
|
+
config_param :content_type, :enum, list: [:"application/json", :"application/x-ndjson"], :default => :"application/json",
|
97
|
+
:deprecated => <<EOC
|
98
|
+
elasticsearch gem v6.0.2 starts to use correct Content-Type. Please upgrade elasticserach gem and stop to use this option.
|
99
|
+
see: https://github.com/elastic/elasticsearch-ruby/pull/514
|
100
|
+
EOC
|
101
|
+
|
102
|
+
config_section :buffer do
|
103
|
+
config_set_default :@type, DEFAULT_BUFFER_TYPE
|
104
|
+
config_set_default :chunk_keys, ['tag']
|
105
|
+
config_set_default :timekey_use_utc, true
|
106
|
+
end
|
107
|
+
|
108
|
+
include Fluent::ElasticsearchIndexTemplate
|
109
|
+
include Fluent::Plugin::ElasticsearchConstants
|
110
|
+
|
111
|
+
def initialize
|
112
|
+
super
|
113
|
+
end
|
114
|
+
|
115
|
+
def configure(conf)
|
116
|
+
compat_parameters_convert(conf, :buffer)
|
117
|
+
|
118
|
+
super
|
119
|
+
raise Fluent::ConfigError, "'tag' in chunk_keys is required." if not @chunk_key_tag
|
120
|
+
|
121
|
+
@time_parser = create_time_parser
|
122
|
+
|
123
|
+
if @remove_keys
|
124
|
+
@remove_keys = @remove_keys.split(/\s*,\s*/)
|
125
|
+
end
|
126
|
+
|
127
|
+
if @target_index_key && @target_index_key.is_a?(String)
|
128
|
+
@target_index_key = @target_index_key.split '.'
|
129
|
+
end
|
130
|
+
|
131
|
+
if @target_type_key && @target_type_key.is_a?(String)
|
132
|
+
@target_type_key = @target_type_key.split '.'
|
133
|
+
end
|
134
|
+
|
135
|
+
if @remove_keys_on_update && @remove_keys_on_update.is_a?(String)
|
136
|
+
@remove_keys_on_update = @remove_keys_on_update.split ','
|
137
|
+
end
|
138
|
+
|
139
|
+
if @template_name && @template_file
|
140
|
+
retry_install(@max_retry_putting_template) do
|
141
|
+
template_install(@template_name, @template_file, @template_overwrite)
|
142
|
+
end
|
143
|
+
elsif @templates
|
144
|
+
retry_install(@max_retry_putting_template) do
|
145
|
+
templates_hash_install(@templates, @template_overwrite)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
# Consider missing the prefix of "$." in nested key specifiers.
|
150
|
+
@id_key = convert_compat_id_key(@id_key) if @id_key
|
151
|
+
@parent_key = convert_compat_id_key(@parent_key) if @parent_key
|
152
|
+
@routing_key = convert_compat_id_key(@routing_key) if @routing_key
|
153
|
+
|
154
|
+
@meta_config_map = create_meta_config_map
|
155
|
+
|
156
|
+
begin
|
157
|
+
require 'oj'
|
158
|
+
@dump_proc = Oj.method(:dump)
|
159
|
+
rescue LoadError
|
160
|
+
@dump_proc = Yajl.method(:dump)
|
161
|
+
end
|
162
|
+
|
163
|
+
if @user && m = @user.match(/%{(?<user>.*)}/)
|
164
|
+
@user = URI.encode_www_form_component(m["user"])
|
165
|
+
end
|
166
|
+
if @password && m = @password.match(/%{(?<password>.*)}/)
|
167
|
+
@password = URI.encode_www_form_component(m["password"])
|
168
|
+
end
|
169
|
+
|
170
|
+
if @hash_config
|
171
|
+
raise Fluent::ConfigError, "@hash_config.hash_id_key and id_key must be equal." unless @hash_config.hash_id_key == @id_key
|
172
|
+
end
|
173
|
+
@transport_logger = nil
|
174
|
+
if @with_transporter_log
|
175
|
+
@transport_logger = log
|
176
|
+
log_level = conf['@log_level'] || conf['log_level']
|
177
|
+
log.warn "Consider to specify log_level with @log_level." unless log_level
|
178
|
+
end
|
179
|
+
|
180
|
+
@last_seen_major_version = detect_es_major_version rescue DEFAULT_ELASTICSEARCH_VERSION
|
181
|
+
if @last_seen_major_version == 6 && @type_name != DEFAULT_TYPE_NAME_ES_7x
|
182
|
+
log.info "Detected ES 6.x: ES 7.x will only accept `_doc` in type_name."
|
183
|
+
end
|
184
|
+
if @last_seen_major_version >= 7 && @type_name != DEFAULT_TYPE_NAME_ES_7x
|
185
|
+
log.warn "Detected ES 7.x or above: `_doc` will be used as the document `_type`."
|
186
|
+
@type_name = '_doc'.freeze
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def detect_es_major_version
|
191
|
+
@_es_info ||= client.info
|
192
|
+
@_es_info["version"]["number"].to_i
|
193
|
+
end
|
194
|
+
|
195
|
+
def convert_compat_id_key(key)
|
196
|
+
if key.include?('.') && !key.start_with?('$[')
|
197
|
+
key = "$.#{key}" unless key.start_with?('$.')
|
198
|
+
end
|
199
|
+
key
|
200
|
+
end
|
201
|
+
|
202
|
+
def create_meta_config_map
|
203
|
+
result = []
|
204
|
+
result << [record_accessor_create(@id_key), '_id'] if @id_key
|
205
|
+
result << [record_accessor_create(@parent_key), '_parent'] if @parent_key
|
206
|
+
result << [record_accessor_create(@routing_key), '_routing'] if @routing_key
|
207
|
+
result
|
208
|
+
end
|
209
|
+
|
210
|
+
# once fluent v0.14 is released we might be able to use
|
211
|
+
# Fluent::Parser::TimeParser, but it doesn't quite do what we want - if gives
|
212
|
+
# [sec,nsec] where as we want something we can call `strftime` on...
|
213
|
+
def create_time_parser
|
214
|
+
if @time_key_format
|
215
|
+
begin
|
216
|
+
# Strptime doesn't support all formats, but for those it does it's
|
217
|
+
# blazingly fast.
|
218
|
+
strptime = Strptime.new(@time_key_format)
|
219
|
+
Proc.new { |value| strptime.exec(value).to_datetime }
|
220
|
+
rescue
|
221
|
+
# Can happen if Strptime doesn't recognize the format; or
|
222
|
+
# if strptime couldn't be required (because it's not installed -- it's
|
223
|
+
# ruby 2 only)
|
224
|
+
Proc.new { |value| DateTime.strptime(value, @time_key_format) }
|
225
|
+
end
|
226
|
+
else
|
227
|
+
Proc.new { |value| DateTime.parse(value) }
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
def parse_time(value, event_time, tag)
|
232
|
+
@time_parser.call(value)
|
233
|
+
rescue => e
|
234
|
+
router.emit_error_event(@time_parse_error_tag, Fluent::Engine.now, {'tag' => tag, 'time' => event_time, 'format' => @time_key_format, 'value' => value}, e)
|
235
|
+
return Time.at(event_time).to_datetime
|
236
|
+
end
|
237
|
+
|
238
|
+
def client
|
239
|
+
@_es ||= begin
|
240
|
+
excon_options = { client_key: @client_key, client_cert: @client_cert, client_key_pass: @client_key_pass }
|
241
|
+
adapter_conf = lambda {|f| f.adapter :excon, excon_options }
|
242
|
+
transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new(get_connection_options.merge(
|
243
|
+
options: {
|
244
|
+
reload_connections: @reload_connections,
|
245
|
+
reload_on_failure: @reload_on_failure,
|
246
|
+
resurrect_after: @resurrect_after,
|
247
|
+
retry_on_failure: 5,
|
248
|
+
logger: @transport_logger,
|
249
|
+
transport_options: {
|
250
|
+
headers: { 'Content-Type' => @content_type.to_s },
|
251
|
+
request: { timeout: @request_timeout },
|
252
|
+
ssl: { verify: @ssl_verify, ca_file: @ca_file, version: @ssl_version }
|
253
|
+
},
|
254
|
+
http: {
|
255
|
+
user: @user,
|
256
|
+
password: @password
|
257
|
+
}
|
258
|
+
}), &adapter_conf)
|
259
|
+
es = Elasticsearch::Client.new transport: transport
|
260
|
+
|
261
|
+
begin
|
262
|
+
raise ConnectionFailure, "Can not reach Elasticsearch cluster (#{connection_options_description})!" unless es.ping
|
263
|
+
rescue *es.transport.host_unreachable_exceptions => e
|
264
|
+
raise ConnectionFailure, "Can not reach Elasticsearch cluster (#{connection_options_description})! #{e.message}"
|
265
|
+
end
|
266
|
+
|
267
|
+
log.info "Connection opened to Elasticsearch cluster => #{connection_options_description}"
|
268
|
+
es
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
def get_escaped_userinfo(host_str)
|
273
|
+
if m = host_str.match(/(?<scheme>.*)%{(?<user>.*)}:%{(?<password>.*)}(?<path>@.*)/)
|
274
|
+
m["scheme"] +
|
275
|
+
URI.encode_www_form_component(m["user"]) +
|
276
|
+
':' +
|
277
|
+
URI.encode_www_form_component(m["password"]) +
|
278
|
+
m["path"]
|
279
|
+
else
|
280
|
+
host_str
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
def get_connection_options
|
285
|
+
raise "`password` must be present if `user` is present" if @user && !@password
|
286
|
+
|
287
|
+
hosts = if @hosts
|
288
|
+
@hosts.split(',').map do |host_str|
|
289
|
+
# Support legacy hosts format host:port,host:port,host:port...
|
290
|
+
if host_str.match(%r{^[^:]+(\:\d+)?$})
|
291
|
+
{
|
292
|
+
host: host_str.split(':')[0],
|
293
|
+
port: (host_str.split(':')[1] || @port).to_i,
|
294
|
+
scheme: @scheme
|
295
|
+
}
|
296
|
+
else
|
297
|
+
# New hosts format expects URLs such as http://logs.foo.com,https://john:pass@logs2.foo.com/elastic
|
298
|
+
uri = URI(get_escaped_userinfo(host_str))
|
299
|
+
%w(user password path).inject(host: uri.host, port: uri.port, scheme: uri.scheme) do |hash, key|
|
300
|
+
hash[key.to_sym] = uri.public_send(key) unless uri.public_send(key).nil? || uri.public_send(key) == ''
|
301
|
+
hash
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end.compact
|
305
|
+
else
|
306
|
+
[{host: @host, port: @port, scheme: @scheme}]
|
307
|
+
end.each do |host|
|
308
|
+
host.merge!(user: @user, password: @password) if !host[:user] && @user
|
309
|
+
host.merge!(path: @path) if !host[:path] && @path
|
310
|
+
end
|
311
|
+
|
312
|
+
{
|
313
|
+
hosts: hosts
|
314
|
+
}
|
315
|
+
end
|
316
|
+
|
317
|
+
def connection_options_description
|
318
|
+
get_connection_options[:hosts].map do |host_info|
|
319
|
+
attributes = host_info.dup
|
320
|
+
attributes[:password] = 'obfuscated' if attributes.has_key?(:password)
|
321
|
+
attributes.inspect
|
322
|
+
end.join(', ')
|
323
|
+
end
|
324
|
+
|
325
|
+
def append_record_to_messages(op, meta, header, record, msgs)
|
326
|
+
case op
|
327
|
+
when UPDATE_OP, UPSERT_OP
|
328
|
+
if meta.has_key?(ID_FIELD)
|
329
|
+
header[UPDATE_OP] = meta
|
330
|
+
msgs << @dump_proc.call(header) << BODY_DELIMITER
|
331
|
+
msgs << @dump_proc.call(update_body(record, op)) << BODY_DELIMITER
|
332
|
+
end
|
333
|
+
when CREATE_OP
|
334
|
+
if meta.has_key?(ID_FIELD)
|
335
|
+
header[CREATE_OP] = meta
|
336
|
+
msgs << @dump_proc.call(header) << BODY_DELIMITER
|
337
|
+
msgs << @dump_proc.call(record) << BODY_DELIMITER
|
338
|
+
end
|
339
|
+
when INDEX_OP
|
340
|
+
header[INDEX_OP] = meta
|
341
|
+
msgs << @dump_proc.call(header) << BODY_DELIMITER
|
342
|
+
msgs << @dump_proc.call(record) << BODY_DELIMITER
|
343
|
+
end
|
344
|
+
end
|
345
|
+
|
346
|
+
def update_body(record, op)
|
347
|
+
update = remove_keys(record)
|
348
|
+
body = {"doc".freeze => update}
|
349
|
+
if op == UPSERT_OP
|
350
|
+
if update == record
|
351
|
+
body["doc_as_upsert".freeze] = true
|
352
|
+
else
|
353
|
+
body[UPSERT_OP] = record
|
354
|
+
end
|
355
|
+
end
|
356
|
+
body
|
357
|
+
end
|
358
|
+
|
359
|
+
def remove_keys(record)
|
360
|
+
keys = record[@remove_keys_on_update_key] || @remove_keys_on_update || []
|
361
|
+
record.delete(@remove_keys_on_update_key)
|
362
|
+
return record unless keys.any?
|
363
|
+
record = record.dup
|
364
|
+
keys.each { |key| record.delete(key) }
|
365
|
+
record
|
366
|
+
end
|
367
|
+
|
368
|
+
def flatten_record(record, prefix=[])
|
369
|
+
ret = {}
|
370
|
+
if record.is_a? Hash
|
371
|
+
record.each { |key, value|
|
372
|
+
ret.merge! flatten_record(value, prefix + [key.to_s])
|
373
|
+
}
|
374
|
+
elsif record.is_a? Array
|
375
|
+
# Don't mess with arrays, leave them unprocessed
|
376
|
+
ret.merge!({prefix.join(@flatten_hashes_separator) => record})
|
377
|
+
else
|
378
|
+
return {prefix.join(@flatten_hashes_separator) => record}
|
379
|
+
end
|
380
|
+
ret
|
381
|
+
end
|
382
|
+
|
383
|
+
def expand_placeholders(metadata)
|
384
|
+
logstash_prefix = extract_placeholders(@logstash_prefix, metadata)
|
385
|
+
index_name = extract_placeholders(@index_name, metadata)
|
386
|
+
type_name = extract_placeholders(@type_name, metadata)
|
387
|
+
return logstash_prefix, index_name, type_name
|
388
|
+
end
|
389
|
+
|
390
|
+
def multi_workers_ready?
|
391
|
+
true
|
392
|
+
end
|
393
|
+
|
394
|
+
def write(chunk)
|
395
|
+
bulk_message_count = 0
|
396
|
+
bulk_message = ''
|
397
|
+
header = {}
|
398
|
+
meta = {}
|
399
|
+
|
400
|
+
tag = chunk.metadata.tag
|
401
|
+
extracted_values = expand_placeholders(chunk.metadata)
|
402
|
+
@last_seen_major_version = detect_es_major_version rescue DEFAULT_ELASTICSEARCH_VERSION
|
403
|
+
|
404
|
+
chunk.msgpack_each do |time, record|
|
405
|
+
next unless record.is_a? Hash
|
406
|
+
begin
|
407
|
+
process_message(tag, meta, header, time, record, bulk_message, extracted_values)
|
408
|
+
bulk_message_count += 1
|
409
|
+
rescue => e
|
410
|
+
router.emit_error_event(tag, time, record, e)
|
411
|
+
end
|
412
|
+
end
|
413
|
+
|
414
|
+
send_bulk(bulk_message, tag, chunk, bulk_message_count, extracted_values) unless bulk_message.empty?
|
415
|
+
bulk_message.clear
|
416
|
+
end
|
417
|
+
|
418
|
+
def process_message(tag, meta, header, time, record, bulk_message, extracted_values)
|
419
|
+
logstash_prefix, index_name, type_name = extracted_values
|
420
|
+
|
421
|
+
if @flatten_hashes
|
422
|
+
record = flatten_record(record)
|
423
|
+
end
|
424
|
+
|
425
|
+
if @hash_config
|
426
|
+
record = generate_hash_id_key(record)
|
427
|
+
end
|
428
|
+
|
429
|
+
dt = nil
|
430
|
+
if @logstash_format || @include_timestamp
|
431
|
+
if record.has_key?(TIMESTAMP_FIELD)
|
432
|
+
rts = record[TIMESTAMP_FIELD]
|
433
|
+
dt = parse_time(rts, time, tag)
|
434
|
+
elsif record.has_key?(@time_key)
|
435
|
+
rts = record[@time_key]
|
436
|
+
dt = parse_time(rts, time, tag)
|
437
|
+
record[TIMESTAMP_FIELD] = dt.iso8601(@time_precision) unless @time_key_exclude_timestamp
|
438
|
+
else
|
439
|
+
dt = Time.at(time).to_datetime
|
440
|
+
record[TIMESTAMP_FIELD] = dt.iso8601(@time_precision)
|
441
|
+
end
|
442
|
+
end
|
443
|
+
|
444
|
+
target_index_parent, target_index_child_key = @target_index_key ? get_parent_of(record, @target_index_key) : nil
|
445
|
+
if target_index_parent && target_index_parent[target_index_child_key]
|
446
|
+
target_index = target_index_parent.delete(target_index_child_key)
|
447
|
+
elsif @logstash_format
|
448
|
+
dt = dt.new_offset(0) if @utc_index
|
449
|
+
target_index = "#{logstash_prefix}#{@logstash_prefix_separator}#{dt.strftime(@logstash_dateformat)}"
|
450
|
+
else
|
451
|
+
target_index = index_name
|
452
|
+
end
|
453
|
+
|
454
|
+
# Change target_index to lower-case since Elasticsearch doesn't
|
455
|
+
# allow upper-case characters in index names.
|
456
|
+
target_index = target_index.downcase
|
457
|
+
if @include_tag_key
|
458
|
+
record[@tag_key] = tag
|
459
|
+
end
|
460
|
+
|
461
|
+
target_type_parent, target_type_child_key = @target_type_key ? get_parent_of(record, @target_type_key) : nil
|
462
|
+
if target_type_parent && target_type_parent[target_type_child_key]
|
463
|
+
target_type = target_type_parent.delete(target_type_child_key)
|
464
|
+
if @last_seen_major_version == 6
|
465
|
+
log.warn "Detected ES 6.x: `@type_name` will be used as the document `_type`."
|
466
|
+
target_type = type_name
|
467
|
+
elsif @last_seen_major_version >= 7
|
468
|
+
log.warn "Detected ES 7.x or above: `_doc` will be used as the document `_type`."
|
469
|
+
target_type = '_doc'.freeze
|
470
|
+
end
|
471
|
+
else
|
472
|
+
if @last_seen_major_version >= 7 && target_type != DEFAULT_TYPE_NAME_ES_7x
|
473
|
+
log.warn "Detected ES 7.x or above: `_doc` will be used as the document `_type`."
|
474
|
+
target_type = '_doc'.freeze
|
475
|
+
else
|
476
|
+
target_type = type_name
|
477
|
+
end
|
478
|
+
end
|
479
|
+
|
480
|
+
meta.clear
|
481
|
+
meta["_index".freeze] = target_index
|
482
|
+
meta["_type".freeze] = target_type
|
483
|
+
|
484
|
+
if @pipeline
|
485
|
+
meta["pipeline".freeze] = @pipeline
|
486
|
+
end
|
487
|
+
|
488
|
+
@meta_config_map.each do |record_accessor, meta_key|
|
489
|
+
if raw_value = record_accessor.call(record)
|
490
|
+
meta[meta_key] = raw_value
|
491
|
+
end
|
492
|
+
end
|
493
|
+
|
494
|
+
if @remove_keys
|
495
|
+
@remove_keys.each { |key| record.delete(key) }
|
496
|
+
end
|
497
|
+
|
498
|
+
append_record_to_messages(@write_operation, meta, header, record, bulk_message)
|
499
|
+
end
|
500
|
+
|
501
|
+
# returns [parent, child_key] of child described by path array in record's tree
|
502
|
+
# returns [nil, child_key] if path doesnt exist in record
|
503
|
+
def get_parent_of(record, path)
|
504
|
+
parent_object = path[0..-2].reduce(record) { |a, e| a.is_a?(Hash) ? a[e] : nil }
|
505
|
+
[parent_object, path[-1]]
|
506
|
+
end
|
507
|
+
|
508
|
+
# send_bulk given a specific bulk request, the original tag,
|
509
|
+
# chunk, and bulk_message_count
|
510
|
+
def send_bulk(data, tag, chunk, bulk_message_count, extracted_values)
|
511
|
+
retries = 0
|
512
|
+
begin
|
513
|
+
response = client.bulk body: data
|
514
|
+
if response['errors']
|
515
|
+
error = Fluent::Plugin::ElasticsearchErrorHandler.new(self)
|
516
|
+
error.handle_error(response, tag, chunk, bulk_message_count, extracted_values)
|
517
|
+
end
|
518
|
+
rescue RetryStreamError => e
|
519
|
+
emit_tag = @retry_tag ? @retry_tag : tag
|
520
|
+
router.emit_stream(emit_tag, e.retry_stream)
|
521
|
+
rescue *client.transport.host_unreachable_exceptions => e
|
522
|
+
if retries < 2
|
523
|
+
retries += 1
|
524
|
+
@_es = nil
|
525
|
+
@_es_info = nil
|
526
|
+
log.warn "Could not push logs to Elasticsearch, resetting connection and trying again. #{e.message}"
|
527
|
+
sleep 2**retries
|
528
|
+
retry
|
529
|
+
end
|
530
|
+
raise ConnectionFailure, "Could not push logs to Elasticsearch after #{retries} retries. #{e.message}"
|
531
|
+
rescue Exception
|
532
|
+
@_es = nil if @reconnect_on_error
|
533
|
+
@_es_info = nil if @reconnect_on_error
|
534
|
+
raise
|
535
|
+
end
|
536
|
+
end
|
537
|
+
end
|
538
|
+
end
|