fluent-plugin-elasticsearch-sm 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +2 -0
- data/.editorconfig +9 -0
- data/.gitignore +17 -0
- data/.travis.yml +11 -0
- data/Gemfile +8 -0
- data/History.md +81 -0
- data/LICENSE.txt +22 -0
- data/PULL_REQUEST_TEMPLATE.md +10 -0
- data/README.md +417 -0
- data/Rakefile +10 -0
- data/lib/fluent/plugin/out_elasticsearch.rb +249 -0
- data/lib/fluent/plugin/out_elasticsearch_dynamic.rb +239 -0
- data/test/helper.rb +24 -0
- data/test/plugin/test_out_elasticsearch.rb +636 -0
- data/test/plugin/test_out_elasticsearch_dynamic.rb +537 -0
- metadata +163 -0
data/test/helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start do
|
3
|
+
add_filter do |src|
|
4
|
+
!(src.filename =~ /^#{SimpleCov.root}\/lib/)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'coveralls'
|
9
|
+
Coveralls.wear!
|
10
|
+
|
11
|
+
# needs to be after simplecov but before test/unit, because fluentd sets default
|
12
|
+
# encoding to ASCII-8BIT, but coverall might load git data which could contain a
|
13
|
+
# UTF-8 character
|
14
|
+
at_exit do
|
15
|
+
Encoding.default_internal = 'UTF-8' if defined?(Encoding) && Encoding.respond_to?(:default_internal)
|
16
|
+
Encoding.default_external = 'UTF-8' if defined?(Encoding) && Encoding.respond_to?(:default_external)
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'test/unit'
|
20
|
+
require 'fluent/test'
|
21
|
+
require 'minitest/pride'
|
22
|
+
|
23
|
+
require 'webmock/test_unit'
|
24
|
+
WebMock.disable_net_connect!
|
@@ -0,0 +1,636 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
class ElasticsearchOutput < Test::Unit::TestCase
|
5
|
+
attr_accessor :index_cmds, :index_command_counts
|
6
|
+
|
7
|
+
def setup
|
8
|
+
Fluent::Test.setup
|
9
|
+
require 'fluent/plugin/out_elasticsearch'
|
10
|
+
@driver = nil
|
11
|
+
log = Fluent::Engine.log
|
12
|
+
log.out.logs.slice!(0, log.out.logs.length)
|
13
|
+
end
|
14
|
+
|
15
|
+
def driver(tag='test', conf='')
|
16
|
+
@driver ||= Fluent::Test::BufferedOutputTestDriver.new(Fluent::ElasticsearchOutput, tag).configure(conf)
|
17
|
+
end
|
18
|
+
|
19
|
+
def sample_record
|
20
|
+
{'age' => 26, 'request_id' => '42', 'parent_id' => 'parent', 'routing_id' => 'routing'}
|
21
|
+
end
|
22
|
+
|
23
|
+
def stub_elastic_ping(url="http://localhost:9200")
|
24
|
+
stub_request(:head, url).to_return(:status => 200, :body => "", :headers => {})
|
25
|
+
end
|
26
|
+
|
27
|
+
def stub_elastic(url="http://localhost:9200/_bulk")
|
28
|
+
stub_request(:post, url).with do |req|
|
29
|
+
@index_cmds = req.body.split("\n").map {|r| JSON.parse(r) }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def stub_elastic_unavailable(url="http://localhost:9200/_bulk")
|
34
|
+
stub_request(:post, url).to_return(:status => [503, "Service Unavailable"])
|
35
|
+
end
|
36
|
+
|
37
|
+
def stub_elastic_with_store_index_command_counts(url="http://localhost:9200/_bulk")
|
38
|
+
if @index_command_counts == nil
|
39
|
+
@index_command_counts = {}
|
40
|
+
@index_command_counts.default = 0
|
41
|
+
end
|
42
|
+
|
43
|
+
stub_request(:post, url).with do |req|
|
44
|
+
index_cmds = req.body.split("\n").map {|r| JSON.parse(r) }
|
45
|
+
@index_command_counts[url] += index_cmds.size
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_configure
|
50
|
+
config = %{
|
51
|
+
host logs.google.com
|
52
|
+
port 777
|
53
|
+
scheme https
|
54
|
+
path /es/
|
55
|
+
user john
|
56
|
+
password doe
|
57
|
+
}
|
58
|
+
instance = driver('test', config).instance
|
59
|
+
|
60
|
+
assert_equal 'logs.google.com', instance.host
|
61
|
+
assert_equal 777, instance.port
|
62
|
+
assert_equal 'https', instance.scheme
|
63
|
+
assert_equal '/es/', instance.path
|
64
|
+
assert_equal 'john', instance.user
|
65
|
+
assert_equal 'doe', instance.password
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_legacy_hosts_list
|
69
|
+
config = %{
|
70
|
+
hosts host1:50,host2:100,host3
|
71
|
+
scheme https
|
72
|
+
path /es/
|
73
|
+
port 123
|
74
|
+
}
|
75
|
+
instance = driver('test', config).instance
|
76
|
+
|
77
|
+
assert_equal 3, instance.get_connection_options[:hosts].length
|
78
|
+
host1, host2, host3 = instance.get_connection_options[:hosts]
|
79
|
+
|
80
|
+
assert_equal 'host1', host1[:host]
|
81
|
+
assert_equal 50, host1[:port]
|
82
|
+
assert_equal 'https', host1[:scheme]
|
83
|
+
assert_equal '/es/', host2[:path]
|
84
|
+
assert_equal 'host3', host3[:host]
|
85
|
+
assert_equal 123, host3[:port]
|
86
|
+
assert_equal 'https', host3[:scheme]
|
87
|
+
assert_equal '/es/', host3[:path]
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_hosts_list
|
91
|
+
config = %{
|
92
|
+
hosts https://john:password@host1:443/elastic/,http://host2
|
93
|
+
path /default_path
|
94
|
+
user default_user
|
95
|
+
password default_password
|
96
|
+
}
|
97
|
+
instance = driver('test', config).instance
|
98
|
+
|
99
|
+
assert_equal 2, instance.get_connection_options[:hosts].length
|
100
|
+
host1, host2 = instance.get_connection_options[:hosts]
|
101
|
+
|
102
|
+
assert_equal 'host1', host1[:host]
|
103
|
+
assert_equal 443, host1[:port]
|
104
|
+
assert_equal 'https', host1[:scheme]
|
105
|
+
assert_equal 'john', host1[:user]
|
106
|
+
assert_equal 'password', host1[:password]
|
107
|
+
assert_equal '/elastic/', host1[:path]
|
108
|
+
|
109
|
+
assert_equal 'host2', host2[:host]
|
110
|
+
assert_equal 'http', host2[:scheme]
|
111
|
+
assert_equal 'default_user', host2[:user]
|
112
|
+
assert_equal 'default_password', host2[:password]
|
113
|
+
assert_equal '/default_path', host2[:path]
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_single_host_params_and_defaults
|
117
|
+
config = %{
|
118
|
+
host logs.google.com
|
119
|
+
user john
|
120
|
+
password doe
|
121
|
+
}
|
122
|
+
instance = driver('test', config).instance
|
123
|
+
|
124
|
+
assert_equal 1, instance.get_connection_options[:hosts].length
|
125
|
+
host1 = instance.get_connection_options[:hosts][0]
|
126
|
+
|
127
|
+
assert_equal 'logs.google.com', host1[:host]
|
128
|
+
assert_equal 9200, host1[:port]
|
129
|
+
assert_equal 'http', host1[:scheme]
|
130
|
+
assert_equal 'john', host1[:user]
|
131
|
+
assert_equal 'doe', host1[:password]
|
132
|
+
assert_equal nil, host1[:path]
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_writes_to_default_index
|
136
|
+
stub_elastic_ping
|
137
|
+
stub_elastic
|
138
|
+
driver.emit(sample_record)
|
139
|
+
driver.run
|
140
|
+
assert_equal('fluentd', index_cmds.first['index']['_index'])
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_writes_to_default_type
|
144
|
+
stub_elastic_ping
|
145
|
+
stub_elastic
|
146
|
+
driver.emit(sample_record)
|
147
|
+
driver.run
|
148
|
+
assert_equal('fluentd', index_cmds.first['index']['_type'])
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_writes_to_speficied_index
|
152
|
+
driver.configure("index_name myindex\n")
|
153
|
+
stub_elastic_ping
|
154
|
+
stub_elastic
|
155
|
+
driver.emit(sample_record)
|
156
|
+
driver.run
|
157
|
+
assert_equal('myindex', index_cmds.first['index']['_index'])
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_writes_to_target_index_key
|
161
|
+
driver.configure("target_index_key @target_index\n")
|
162
|
+
stub_elastic_ping
|
163
|
+
stub_elastic
|
164
|
+
record = sample_record.clone
|
165
|
+
driver.emit(sample_record.merge('@target_index' => 'local-override'))
|
166
|
+
driver.run
|
167
|
+
assert_equal('local-override', index_cmds.first['index']['_index'])
|
168
|
+
assert_nil(index_cmds[1]['@target_index'])
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_writes_to_target_index_key_logstash
|
172
|
+
driver.configure("target_index_key @target_index\n")
|
173
|
+
driver.configure("logstash_format true\n")
|
174
|
+
time = Time.parse Date.today.to_s
|
175
|
+
stub_elastic_ping
|
176
|
+
stub_elastic
|
177
|
+
driver.emit(sample_record.merge('@target_index' => 'local-override'), time)
|
178
|
+
driver.run
|
179
|
+
assert_equal('local-override', index_cmds.first['index']['_index'])
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_writes_to_target_index_key_fallack
|
183
|
+
driver.configure("target_index_key @target_index\n")
|
184
|
+
stub_elastic_ping
|
185
|
+
stub_elastic
|
186
|
+
driver.emit(sample_record)
|
187
|
+
driver.run
|
188
|
+
assert_equal('fluentd', index_cmds.first['index']['_index'])
|
189
|
+
end
|
190
|
+
|
191
|
+
def test_writes_to_target_index_key_fallack_logstash
|
192
|
+
driver.configure("target_index_key @target_index\n")
|
193
|
+
driver.configure("logstash_format true\n")
|
194
|
+
time = Time.parse Date.today.to_s
|
195
|
+
logstash_index = "logstash-#{time.getutc.strftime("%Y.%m.%d")}"
|
196
|
+
stub_elastic_ping
|
197
|
+
stub_elastic
|
198
|
+
driver.emit(sample_record, time)
|
199
|
+
driver.run
|
200
|
+
assert_equal(logstash_index, index_cmds.first['index']['_index'])
|
201
|
+
end
|
202
|
+
|
203
|
+
def test_writes_to_speficied_type
|
204
|
+
driver.configure("type_name mytype\n")
|
205
|
+
stub_elastic_ping
|
206
|
+
stub_elastic
|
207
|
+
driver.emit(sample_record)
|
208
|
+
driver.run
|
209
|
+
assert_equal('mytype', index_cmds.first['index']['_type'])
|
210
|
+
end
|
211
|
+
|
212
|
+
def test_writes_to_speficied_host
|
213
|
+
driver.configure("host 192.168.33.50\n")
|
214
|
+
stub_elastic_ping("http://192.168.33.50:9200")
|
215
|
+
elastic_request = stub_elastic("http://192.168.33.50:9200/_bulk")
|
216
|
+
driver.emit(sample_record)
|
217
|
+
driver.run
|
218
|
+
assert_requested(elastic_request)
|
219
|
+
end
|
220
|
+
|
221
|
+
def test_writes_to_speficied_port
|
222
|
+
driver.configure("port 9201\n")
|
223
|
+
stub_elastic_ping("http://localhost:9201")
|
224
|
+
elastic_request = stub_elastic("http://localhost:9201/_bulk")
|
225
|
+
driver.emit(sample_record)
|
226
|
+
driver.run
|
227
|
+
assert_requested(elastic_request)
|
228
|
+
end
|
229
|
+
|
230
|
+
def test_writes_to_multi_hosts
|
231
|
+
hosts = [['192.168.33.50', 9201], ['192.168.33.51', 9201], ['192.168.33.52', 9201]]
|
232
|
+
hosts_string = hosts.map {|x| "#{x[0]}:#{x[1]}"}.compact.join(',')
|
233
|
+
|
234
|
+
driver.configure("hosts #{hosts_string}")
|
235
|
+
|
236
|
+
hosts.each do |host_info|
|
237
|
+
host, port = host_info
|
238
|
+
stub_elastic_ping("http://#{host}:#{port}")
|
239
|
+
stub_elastic_with_store_index_command_counts("http://#{host}:#{port}/_bulk")
|
240
|
+
end
|
241
|
+
|
242
|
+
1000.times do
|
243
|
+
driver.emit(sample_record.merge('age'=>rand(100)))
|
244
|
+
end
|
245
|
+
|
246
|
+
driver.run
|
247
|
+
|
248
|
+
# @note: we cannot make multi chunks with options (flush_interval, buffer_chunk_limit)
|
249
|
+
# it's Fluentd test driver's constraint
|
250
|
+
# so @index_command_counts.size is always 1
|
251
|
+
|
252
|
+
assert(@index_command_counts.size > 0, "not working with hosts options")
|
253
|
+
|
254
|
+
total = 0
|
255
|
+
@index_command_counts.each do |url, count|
|
256
|
+
total += count
|
257
|
+
end
|
258
|
+
assert_equal(2000, total)
|
259
|
+
end
|
260
|
+
|
261
|
+
def test_makes_bulk_request
|
262
|
+
stub_elastic_ping
|
263
|
+
stub_elastic
|
264
|
+
driver.emit(sample_record)
|
265
|
+
driver.emit(sample_record.merge('age' => 27))
|
266
|
+
driver.run
|
267
|
+
assert_equal(4, index_cmds.count)
|
268
|
+
end
|
269
|
+
|
270
|
+
def test_all_records_are_preserved_in_bulk
|
271
|
+
stub_elastic_ping
|
272
|
+
stub_elastic
|
273
|
+
driver.emit(sample_record)
|
274
|
+
driver.emit(sample_record.merge('age' => 27))
|
275
|
+
driver.run
|
276
|
+
assert_equal(26, index_cmds[1]['age'])
|
277
|
+
assert_equal(27, index_cmds[3]['age'])
|
278
|
+
end
|
279
|
+
|
280
|
+
def test_writes_to_logstash_index
|
281
|
+
driver.configure("logstash_format true\n")
|
282
|
+
#
|
283
|
+
# This is 1 second past midnight in BST, so the UTC index should be the day before
|
284
|
+
dt = DateTime.new(2015, 6, 1, 0, 0, 1, "+01:00")
|
285
|
+
logstash_index = "logstash-2015.05.31"
|
286
|
+
stub_elastic_ping
|
287
|
+
stub_elastic
|
288
|
+
driver.emit(sample_record, dt.to_time)
|
289
|
+
driver.run
|
290
|
+
assert_equal(logstash_index, index_cmds.first['index']['_index'])
|
291
|
+
end
|
292
|
+
|
293
|
+
def test_writes_to_logstash_non_utc_index
|
294
|
+
driver.configure("logstash_format true
|
295
|
+
utc_index false")
|
296
|
+
# When using `utc_index false` the index time will be the local day of
|
297
|
+
# ingestion time
|
298
|
+
time = Date.today.to_time
|
299
|
+
index = "logstash-#{time.strftime("%Y.%m.%d")}"
|
300
|
+
stub_elastic_ping
|
301
|
+
stub_elastic
|
302
|
+
driver.emit(sample_record, time)
|
303
|
+
driver.run
|
304
|
+
assert_equal(index, index_cmds.first['index']['_index'])
|
305
|
+
end
|
306
|
+
|
307
|
+
def test_writes_to_logstash_index_with_specified_prefix
|
308
|
+
driver.configure("logstash_format true
|
309
|
+
logstash_prefix myprefix")
|
310
|
+
time = Time.parse Date.today.to_s
|
311
|
+
logstash_index = "myprefix-#{time.getutc.strftime("%Y.%m.%d")}"
|
312
|
+
stub_elastic_ping
|
313
|
+
stub_elastic
|
314
|
+
driver.emit(sample_record, time)
|
315
|
+
driver.run
|
316
|
+
assert_equal(logstash_index, index_cmds.first['index']['_index'])
|
317
|
+
end
|
318
|
+
|
319
|
+
def test_writes_to_logstash_index_with_specified_dateformat
|
320
|
+
driver.configure("logstash_format true
|
321
|
+
logstash_dateformat %Y.%m")
|
322
|
+
time = Time.parse Date.today.to_s
|
323
|
+
logstash_index = "logstash-#{time.getutc.strftime("%Y.%m")}"
|
324
|
+
stub_elastic_ping
|
325
|
+
stub_elastic
|
326
|
+
driver.emit(sample_record, time)
|
327
|
+
driver.run
|
328
|
+
assert_equal(logstash_index, index_cmds.first['index']['_index'])
|
329
|
+
end
|
330
|
+
|
331
|
+
def test_writes_to_logstash_index_with_specified_prefix_and_dateformat
|
332
|
+
driver.configure("logstash_format true
|
333
|
+
logstash_prefix myprefix
|
334
|
+
logstash_dateformat %Y.%m")
|
335
|
+
time = Time.parse Date.today.to_s
|
336
|
+
logstash_index = "myprefix-#{time.getutc.strftime("%Y.%m")}"
|
337
|
+
stub_elastic_ping
|
338
|
+
stub_elastic
|
339
|
+
driver.emit(sample_record, time)
|
340
|
+
driver.run
|
341
|
+
assert_equal(logstash_index, index_cmds.first['index']['_index'])
|
342
|
+
end
|
343
|
+
|
344
|
+
def test_doesnt_add_logstash_timestamp_by_default
|
345
|
+
stub_elastic_ping
|
346
|
+
stub_elastic
|
347
|
+
driver.emit(sample_record)
|
348
|
+
driver.run
|
349
|
+
assert_nil(index_cmds[1]['@timestamp'])
|
350
|
+
end
|
351
|
+
|
352
|
+
def test_adds_logstash_timestamp_when_configured
|
353
|
+
driver.configure("logstash_format true\n")
|
354
|
+
stub_elastic_ping
|
355
|
+
stub_elastic
|
356
|
+
ts = DateTime.now.to_s
|
357
|
+
driver.emit(sample_record)
|
358
|
+
driver.run
|
359
|
+
assert(index_cmds[1].has_key? '@timestamp')
|
360
|
+
assert_equal(index_cmds[1]['@timestamp'], ts)
|
361
|
+
end
|
362
|
+
|
363
|
+
def test_uses_custom_timestamp_when_included_in_record
|
364
|
+
driver.configure("logstash_format true\n")
|
365
|
+
stub_elastic_ping
|
366
|
+
stub_elastic
|
367
|
+
ts = DateTime.new(2001,2,3).to_s
|
368
|
+
driver.emit(sample_record.merge!('@timestamp' => ts))
|
369
|
+
driver.run
|
370
|
+
assert(index_cmds[1].has_key? '@timestamp')
|
371
|
+
assert_equal(index_cmds[1]['@timestamp'], ts)
|
372
|
+
end
|
373
|
+
|
374
|
+
def test_uses_custom_time_key
|
375
|
+
driver.configure("logstash_format true
|
376
|
+
time_key vtm\n")
|
377
|
+
stub_elastic_ping
|
378
|
+
stub_elastic
|
379
|
+
ts = DateTime.new(2001,2,3).to_s
|
380
|
+
driver.emit(sample_record.merge!('vtm' => ts))
|
381
|
+
driver.run
|
382
|
+
assert(index_cmds[1].has_key? '@timestamp')
|
383
|
+
assert_equal(index_cmds[1]['@timestamp'], ts)
|
384
|
+
end
|
385
|
+
|
386
|
+
def test_uses_custom_time_key_exclude_timekey
|
387
|
+
driver.configure("logstash_format true
|
388
|
+
time_key vtm
|
389
|
+
time_key_exclude_timestamp true\n")
|
390
|
+
stub_elastic_ping
|
391
|
+
stub_elastic
|
392
|
+
ts = DateTime.new(2001,2,3).to_s
|
393
|
+
driver.emit(sample_record.merge!('vtm' => ts))
|
394
|
+
driver.run
|
395
|
+
assert(!index_cmds[1].key?('@timestamp'), '@timestamp should be messing')
|
396
|
+
end
|
397
|
+
|
398
|
+
def test_uses_custom_time_key_format
|
399
|
+
driver.configure("logstash_format true
|
400
|
+
time_key_format %Y-%m-%dT%H:%M:%S.%N%z\n")
|
401
|
+
stub_elastic_ping
|
402
|
+
stub_elastic
|
403
|
+
ts = "2001-02-03T13:14:01.673+02:00"
|
404
|
+
driver.emit(sample_record.merge!('@timestamp' => ts))
|
405
|
+
driver.run
|
406
|
+
assert_equal("logstash-2001.02.03", index_cmds[0]['index']['_index'])
|
407
|
+
assert(index_cmds[1].has_key? '@timestamp')
|
408
|
+
assert_equal(index_cmds[1]['@timestamp'], ts)
|
409
|
+
end
|
410
|
+
|
411
|
+
def test_uses_custom_time_key_format_logs_an_error
|
412
|
+
driver.configure("logstash_format true
|
413
|
+
time_key_format %Y-%m-%dT%H:%M:%S.%N%z\n")
|
414
|
+
stub_elastic_ping
|
415
|
+
stub_elastic
|
416
|
+
|
417
|
+
ts = "2001/02/03 13:14:01,673+02:00"
|
418
|
+
index = "logstash-#{Date.today.strftime("%Y.%m.%d")}"
|
419
|
+
|
420
|
+
driver.emit(sample_record.merge!('@timestamp' => ts))
|
421
|
+
driver.run
|
422
|
+
|
423
|
+
log = driver.instance.router.emit_error_handler.log
|
424
|
+
errors = log.out.logs.grep /tag="Fluent::ElasticsearchOutput::TimeParser.error"/
|
425
|
+
assert_equal(1, errors.length, "Error was logged for timestamp parse failure")
|
426
|
+
|
427
|
+
assert_equal(index, index_cmds[0]['index']['_index'])
|
428
|
+
assert(index_cmds[1].has_key? '@timestamp')
|
429
|
+
assert_equal(index_cmds[1]['@timestamp'], ts)
|
430
|
+
end
|
431
|
+
|
432
|
+
|
433
|
+
def test_uses_custom_time_key_format_obscure_format
|
434
|
+
driver.configure("logstash_format true
|
435
|
+
time_key_format %a %b %d %H:%M:%S %Z %Y\n")
|
436
|
+
stub_elastic_ping
|
437
|
+
stub_elastic
|
438
|
+
ts = "Thu Nov 29 14:33:20 GMT 2001"
|
439
|
+
driver.emit(sample_record.merge!('@timestamp' => ts))
|
440
|
+
driver.run
|
441
|
+
assert_equal("logstash-2001.11.29", index_cmds[0]['index']['_index'])
|
442
|
+
assert(index_cmds[1].has_key? '@timestamp')
|
443
|
+
assert_equal(index_cmds[1]['@timestamp'], ts)
|
444
|
+
end
|
445
|
+
|
446
|
+
def test_doesnt_add_tag_key_by_default
|
447
|
+
stub_elastic_ping
|
448
|
+
stub_elastic
|
449
|
+
driver.emit(sample_record)
|
450
|
+
driver.run
|
451
|
+
assert_nil(index_cmds[1]['tag'])
|
452
|
+
end
|
453
|
+
|
454
|
+
def test_adds_tag_key_when_configured
|
455
|
+
driver('mytag').configure("include_tag_key true\n")
|
456
|
+
stub_elastic_ping
|
457
|
+
stub_elastic
|
458
|
+
driver.emit(sample_record)
|
459
|
+
driver.run
|
460
|
+
assert(index_cmds[1].has_key?('tag'))
|
461
|
+
assert_equal(index_cmds[1]['tag'], 'mytag')
|
462
|
+
end
|
463
|
+
|
464
|
+
def test_adds_id_key_when_configured
|
465
|
+
driver.configure("id_key request_id\n")
|
466
|
+
stub_elastic_ping
|
467
|
+
stub_elastic
|
468
|
+
driver.emit(sample_record)
|
469
|
+
driver.run
|
470
|
+
assert_equal(index_cmds[0]['index']['_id'], '42')
|
471
|
+
end
|
472
|
+
|
473
|
+
def test_doesnt_add_id_key_if_missing_when_configured
|
474
|
+
driver.configure("id_key another_request_id\n")
|
475
|
+
stub_elastic_ping
|
476
|
+
stub_elastic
|
477
|
+
driver.emit(sample_record)
|
478
|
+
driver.run
|
479
|
+
assert(!index_cmds[0]['index'].has_key?('_id'))
|
480
|
+
end
|
481
|
+
|
482
|
+
def test_adds_id_key_when_not_configured
|
483
|
+
stub_elastic_ping
|
484
|
+
stub_elastic
|
485
|
+
driver.emit(sample_record)
|
486
|
+
driver.run
|
487
|
+
assert(!index_cmds[0]['index'].has_key?('_id'))
|
488
|
+
end
|
489
|
+
|
490
|
+
def test_adds_parent_key_when_configured
|
491
|
+
driver.configure("parent_key parent_id\n")
|
492
|
+
stub_elastic_ping
|
493
|
+
stub_elastic
|
494
|
+
driver.emit(sample_record)
|
495
|
+
driver.run
|
496
|
+
assert_equal(index_cmds[0]['index']['_parent'], 'parent')
|
497
|
+
end
|
498
|
+
|
499
|
+
def test_doesnt_add_parent_key_if_missing_when_configured
|
500
|
+
driver.configure("parent_key another_parent_id\n")
|
501
|
+
stub_elastic_ping
|
502
|
+
stub_elastic
|
503
|
+
driver.emit(sample_record)
|
504
|
+
driver.run
|
505
|
+
assert(!index_cmds[0]['index'].has_key?('_parent'))
|
506
|
+
end
|
507
|
+
|
508
|
+
def test_adds_parent_key_when_not_configured
|
509
|
+
stub_elastic_ping
|
510
|
+
stub_elastic
|
511
|
+
driver.emit(sample_record)
|
512
|
+
driver.run
|
513
|
+
assert(!index_cmds[0]['index'].has_key?('_parent'))
|
514
|
+
end
|
515
|
+
|
516
|
+
def test_adds_routing_key_when_configured
|
517
|
+
driver.configure("routing_key routing_id\n")
|
518
|
+
stub_elastic_ping
|
519
|
+
stub_elastic
|
520
|
+
driver.emit(sample_record)
|
521
|
+
driver.run
|
522
|
+
assert_equal(index_cmds[0]['index']['_routing'], 'routing')
|
523
|
+
end
|
524
|
+
|
525
|
+
def test_doesnt_add_routing_key_if_missing_when_configured
|
526
|
+
driver.configure("routing_key another_routing_id\n")
|
527
|
+
stub_elastic_ping
|
528
|
+
stub_elastic
|
529
|
+
driver.emit(sample_record)
|
530
|
+
driver.run
|
531
|
+
assert(!index_cmds[0]['index'].has_key?('_routing'))
|
532
|
+
end
|
533
|
+
|
534
|
+
def test_adds_routing_key_when_not_configured
|
535
|
+
stub_elastic_ping
|
536
|
+
stub_elastic
|
537
|
+
driver.emit(sample_record)
|
538
|
+
driver.run
|
539
|
+
assert(!index_cmds[0]['index'].has_key?('_routing'))
|
540
|
+
end
|
541
|
+
|
542
|
+
def test_request_error
|
543
|
+
stub_elastic_ping
|
544
|
+
stub_elastic_unavailable
|
545
|
+
driver.emit(sample_record)
|
546
|
+
assert_raise(Elasticsearch::Transport::Transport::Errors::ServiceUnavailable) {
|
547
|
+
driver.run
|
548
|
+
}
|
549
|
+
end
|
550
|
+
|
551
|
+
def test_garbage_record_error
|
552
|
+
stub_elastic_ping
|
553
|
+
stub_elastic
|
554
|
+
driver.emit("some garbage string")
|
555
|
+
driver.run
|
556
|
+
end
|
557
|
+
|
558
|
+
def test_connection_failed_retry
|
559
|
+
connection_resets = 0
|
560
|
+
|
561
|
+
stub_elastic_ping(url="http://localhost:9200").with do |req|
|
562
|
+
connection_resets += 1
|
563
|
+
end
|
564
|
+
|
565
|
+
stub_request(:post, "http://localhost:9200/_bulk").with do |req|
|
566
|
+
raise Faraday::ConnectionFailed, "Test message"
|
567
|
+
end
|
568
|
+
|
569
|
+
driver.emit(sample_record)
|
570
|
+
|
571
|
+
assert_raise(Fluent::ElasticsearchOutput::ConnectionFailure) {
|
572
|
+
driver.run
|
573
|
+
}
|
574
|
+
assert_equal(connection_resets, 3)
|
575
|
+
end
|
576
|
+
|
577
|
+
def test_update_should_not_write_if_theres_no_id
|
578
|
+
driver.configure("write_operation update\n")
|
579
|
+
stub_elastic_ping
|
580
|
+
stub_elastic
|
581
|
+
driver.emit(sample_record)
|
582
|
+
driver.run
|
583
|
+
assert_nil(index_cmds)
|
584
|
+
end
|
585
|
+
|
586
|
+
def test_upsert_should_not_write_if_theres_no_id
|
587
|
+
driver.configure("write_operation upsert\n")
|
588
|
+
stub_elastic_ping
|
589
|
+
stub_elastic
|
590
|
+
driver.emit(sample_record)
|
591
|
+
driver.run
|
592
|
+
assert_nil(index_cmds)
|
593
|
+
end
|
594
|
+
|
595
|
+
def test_create_should_not_write_if_theres_no_id
|
596
|
+
driver.configure("write_operation create\n")
|
597
|
+
stub_elastic_ping
|
598
|
+
stub_elastic
|
599
|
+
driver.emit(sample_record)
|
600
|
+
driver.run
|
601
|
+
assert_nil(index_cmds)
|
602
|
+
end
|
603
|
+
|
604
|
+
def test_update_should_write_update_op_and_doc_as_upsert_is_false
|
605
|
+
driver.configure("write_operation update
|
606
|
+
id_key request_id")
|
607
|
+
stub_elastic_ping
|
608
|
+
stub_elastic
|
609
|
+
driver.emit(sample_record)
|
610
|
+
driver.run
|
611
|
+
assert(index_cmds[0].has_key?("update"))
|
612
|
+
assert(!index_cmds[1]["doc_as_upsert"])
|
613
|
+
end
|
614
|
+
|
615
|
+
def test_upsert_should_write_update_op_and_doc_as_upsert_is_true
|
616
|
+
driver.configure("write_operation upsert
|
617
|
+
id_key request_id")
|
618
|
+
stub_elastic_ping
|
619
|
+
stub_elastic
|
620
|
+
driver.emit(sample_record)
|
621
|
+
driver.run
|
622
|
+
assert(index_cmds[0].has_key?("update"))
|
623
|
+
assert(index_cmds[1]["doc_as_upsert"])
|
624
|
+
end
|
625
|
+
|
626
|
+
def test_create_should_write_create_op
|
627
|
+
driver.configure("write_operation create
|
628
|
+
id_key request_id")
|
629
|
+
stub_elastic_ping
|
630
|
+
stub_elastic
|
631
|
+
driver.emit(sample_record)
|
632
|
+
driver.run
|
633
|
+
assert(index_cmds[0].has_key?("create"))
|
634
|
+
end
|
635
|
+
|
636
|
+
end
|