fluent-plugin-record-reformer 0.3.0 → 0.4.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bd21069087496f3c30e1599b22b8847d6429323d
4
+ data.tar.gz: 31026f14e6ed73d41ca4ad80ed5f7caca6da72a2
5
+ SHA512:
6
+ metadata.gz: 504c18888e9e06f93fc2eba073399271b558e1a63811de988ecac867429b8e8c06caf9f096210568cbfdfc028d20a1c760301ebaa90f106649776f9f60f9305b
7
+ data.tar.gz: a4d047dbc29a96c2eed77e804ec5e08a99d1bef0055f866c1886c7e7facf2ca227058533fb7ec0e87bf26b960d6b19581c7671aa40dd455570d250d8629bca71
@@ -1,3 +1,11 @@
1
+ ## 0.4.0 (2014/10/31)
2
+
3
+ Changes:
4
+
5
+ * accept numbers as a record key
6
+ * rescue if ruby code expansion failed, and log.warn
7
+ * use newly test-unit gem instead of rspec
8
+
1
9
  ## 0.3.0 (2014/10/01)
2
10
 
3
11
  Fixes:
data/Rakefile CHANGED
@@ -1,12 +1,15 @@
1
1
  # encoding: utf-8
2
2
  require "bundler/gem_tasks"
3
3
 
4
- require 'rspec/core'
5
- require 'rspec/core/rake_task'
6
- RSpec::Core::RakeTask.new(:spec) do |spec|
7
- spec.pattern = FileList['spec/**/*_spec.rb']
4
+ require 'rake/testtask'
5
+ desc 'Run test_unit based test'
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.test_files = Dir["test/**/test_*.rb"].sort
9
+ t.verbose = true
10
+ #t.warning = true
8
11
  end
9
- task :default => :spec
12
+ task :default => :test
10
13
 
11
14
  desc 'Open an irb session preloaded with the gem library'
12
15
  task :console do
@@ -3,7 +3,7 @@ $:.push File.expand_path('../lib', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
5
  gem.name = "fluent-plugin-record-reformer"
6
- gem.version = "0.3.0"
6
+ gem.version = "0.4.0"
7
7
  gem.authors = ["Naotoshi Seo"]
8
8
  gem.email = "sonots@gmail.com"
9
9
  gem.homepage = "https://github.com/sonots/fluent-plugin-record-reformer"
@@ -19,7 +19,9 @@ Gem::Specification.new do |gem|
19
19
 
20
20
  gem.add_dependency "fluentd"
21
21
  gem.add_development_dependency "rake"
22
- gem.add_development_dependency "rspec"
23
22
  gem.add_development_dependency "pry"
24
23
  gem.add_development_dependency "pry-nav"
24
+ gem.add_development_dependency "test-unit"
25
+ gem.add_development_dependency "rr"
26
+ gem.add_development_dependency "timecop"
25
27
  end
@@ -1,4 +1,3 @@
1
- require 'socket'
2
1
  require 'ostruct'
3
2
 
4
3
  module Fluent
@@ -6,6 +5,7 @@ module Fluent
6
5
  Fluent::Plugin.register_output('record_reformer', self)
7
6
 
8
7
  def initialize
8
+ require 'socket'
9
9
  super
10
10
  end
11
11
 
@@ -87,7 +87,7 @@ module Fluent
87
87
  es.each {|time, record|
88
88
  last_record = record # for debug log
89
89
  new_tag, new_record = reform(@tag, time, record, placeholders)
90
- Engine.emit(new_tag, time, new_record)
90
+ Engine.emit(new_tag, time, new_record) if new_tag
91
91
  }
92
92
  chain.next
93
93
  rescue => e
@@ -125,7 +125,7 @@ module Fluent
125
125
  1.upto(tag_parts.size-1).each do |i|
126
126
  rev_tag_suffix[i] = "#{rev_tag_parts[i]}.#{rev_tag_suffix[i-1]}"
127
127
  end
128
- rev_tag_suffix.reverse
128
+ rev_tag_suffix.reverse!
129
129
  end
130
130
 
131
131
  class PlaceholderExpander
@@ -155,7 +155,7 @@ module Fluent
155
155
  end
156
156
 
157
157
  def expand(str)
158
- str.gsub(/(\${[a-zA-Z_]+(\[-?[0-9]+\])?}|__[A-Z_]+__)/) {
158
+ str.gsub(/(\${[a-zA-Z0-9_]+(\[-?[0-9]+\])?}|__[A-Z_]+__)/) {
159
159
  log.warn "record_reformer: unknown placeholder `#{$1}` found" unless @placeholders.include?($1)
160
160
  @placeholders[$1]
161
161
  }
@@ -185,8 +185,14 @@ module Fluent
185
185
  #
186
186
  # @param [String] str the string to be replaced
187
187
  def expand(str)
188
- str = str.gsub(/\$\{([^}]+)\}/, '#{\1}') # ${..} => #{..}
189
- eval "\"#{str}\"", @placeholders.instance_eval { binding }
188
+ interpolated = str.gsub(/\$\{([^}]+)\}/, '#{\1}') # ${..} => #{..}
189
+ begin
190
+ eval "\"#{interpolated}\"", @placeholders.instance_eval { binding }
191
+ rescue => e
192
+ log.warn "record_reformer: failed to expand `#{str}`", :error_class => e.class, :error => e.message
193
+ log.warn_backtrace
194
+ nil
195
+ end
190
196
  end
191
197
 
192
198
  class UndefOpenStruct < OpenStruct
@@ -1,5 +1,6 @@
1
1
  # encoding: UTF-8
2
- require_relative 'spec_helper'
2
+ require_relative 'helper'
3
+ require 'fluent/plugin/out_record_reformer'
3
4
  require 'benchmark'
4
5
  Fluent::Test.setup
5
6
 
@@ -0,0 +1,10 @@
1
+ require 'test/unit'
2
+ require 'fluent/log'
3
+ require 'fluent/test'
4
+
5
+ unless defined?(Test::Unit::AssertionFailedError)
6
+ class Test::Unit::AssertionFailedError < StandardError
7
+ end
8
+ end
9
+
10
+ $log = Fluent::Log.new(Fluent::Test::DummyLogDevice.new, Fluent::Log::LEVEL_WARN)
@@ -0,0 +1,320 @@
1
+ require_relative 'helper'
2
+ require 'rr'
3
+ require 'timecop'
4
+ require 'fluent/plugin/out_record_reformer'
5
+
6
+ Fluent::Test.setup
7
+
8
+ class RecordReformerOutputTest < Test::Unit::TestCase
9
+ setup do
10
+ @hostname = Socket.gethostname.chomp
11
+ @tag = 'test.tag'
12
+ @tag_parts = @tag.split('.')
13
+ @time = Time.local(1,2,3,4,5,2010,nil,nil,nil,nil)
14
+ Timecop.freeze(@time)
15
+ end
16
+
17
+ teardown do
18
+ Timecop.return
19
+ end
20
+
21
+ def create_driver(conf)
22
+ Fluent::Test::OutputTestDriver.new(Fluent::RecordReformerOutput, @tag).configure(conf)
23
+ end
24
+
25
+ def emit(config, msgs = [''])
26
+ d = create_driver(config)
27
+ msgs.each do |msg|
28
+ d.run { d.emit({'eventType0' => 'bar', 'message' => msg}, @time) }
29
+ end
30
+
31
+ @instance = d.instance
32
+ d.emits
33
+ end
34
+
35
+ CONFIG = %[
36
+ tag reformed.${tag}
37
+
38
+ hostname ${hostname}
39
+ input_tag ${tag}
40
+ time ${time.to_s}
41
+ message ${hostname} ${tag_parts.last} ${URI.escape(message)}
42
+ ]
43
+
44
+ sub_test_case 'configure' do
45
+ test 'typical usage' do
46
+ assert_nothing_raised do
47
+ create_driver(CONFIG)
48
+ end
49
+ end
50
+
51
+ test "tag is not specified" do
52
+ assert_raise(Fluent::ConfigError) do
53
+ create_driver('')
54
+ end
55
+ end
56
+
57
+ test "keep_keys must be specified together with renew_record true" do
58
+ assert_raise(Fluent::ConfigError) do
59
+ create_driver(%[keep_keys a])
60
+ end
61
+ end
62
+ end
63
+
64
+ sub_test_case "test options" do
65
+ test 'typical usage' do
66
+ msgs = ['1', '2']
67
+ emits = emit(CONFIG, msgs)
68
+ assert_equal 2, emits.size
69
+ emits.each_with_index do |(tag, time, record), i|
70
+ assert_equal("reformed.#{@tag}", tag)
71
+ assert_equal('bar', record['eventType0'])
72
+ assert_equal(@hostname, record['hostname'])
73
+ assert_equal(@tag, record['input_tag'])
74
+ assert_equal(@time.to_s, record['time'])
75
+ assert_equal("#{@hostname} #{@tag_parts[-1]} #{msgs[i]}", record['message'])
76
+ end
77
+ end
78
+
79
+ test '(obsolete) output_tag' do
80
+ config = %[output_tag reformed.${tag}]
81
+ msgs = ['1']
82
+ emits = emit(config, msgs)
83
+ emits.each_with_index do |(tag, time, record), i|
84
+ assert_equal("reformed.#{@tag}", tag)
85
+ end
86
+ end
87
+
88
+ test 'record directive' do
89
+ config = %[
90
+ tag reformed.${tag}
91
+
92
+ <record>
93
+ hostname ${hostname}
94
+ tag ${tag}
95
+ time ${time.to_s}
96
+ message ${hostname} ${tag_parts.last} ${message}
97
+ </record>
98
+ ]
99
+ msgs = ['1', '2']
100
+ emits = emit(config, msgs)
101
+ emits.each_with_index do |(tag, time, record), i|
102
+ assert_equal("reformed.#{@tag}", tag)
103
+ assert_equal('bar', record['eventType0'])
104
+ assert_equal(@hostname, record['hostname'])
105
+ assert_equal(@tag, record['tag'])
106
+ assert_equal(@time.to_s, record['time'])
107
+ assert_equal("#{@hostname} #{@tag_parts[-1]} #{msgs[i]}", record['message'])
108
+ end
109
+ end
110
+
111
+ test 'remove_keys' do
112
+ config = CONFIG + %[remove_keys eventType0,message]
113
+ emits = emit(config)
114
+ emits.each_with_index do |(tag, time, record), i|
115
+ assert_equal("reformed.#{@tag}", tag)
116
+ assert_not_include(record, 'eventType0')
117
+ assert_equal(@hostname, record['hostname'])
118
+ assert_equal(@tag, record['input_tag'])
119
+ assert_equal(@time.to_s, record['time'])
120
+ assert_not_include(record, 'message')
121
+ end
122
+ end
123
+
124
+ test 'renew_record' do
125
+ config = CONFIG + %[renew_record true]
126
+ msgs = ['1', '2']
127
+ emits = emit(config, msgs)
128
+ emits.each_with_index do |(tag, time, record), i|
129
+ assert_equal("reformed.#{@tag}", tag)
130
+ assert_not_include(record, 'eventType0')
131
+ assert_equal(@hostname, record['hostname'])
132
+ assert_equal(@tag, record['input_tag'])
133
+ assert_equal(@time.to_s, record['time'])
134
+ assert_equal("#{@hostname} #{@tag_parts[-1]} #{msgs[i]}", record['message'])
135
+ end
136
+ end
137
+
138
+ test 'keep_keys' do
139
+ config = %[tag reformed.${tag}\nrenew_record true\nkeep_keys eventType0,message]
140
+ msgs = ['1', '2']
141
+ emits = emit(config, msgs)
142
+ emits.each_with_index do |(tag, time, record), i|
143
+ assert_equal("reformed.#{@tag}", tag)
144
+ assert_equal('bar', record['eventType0'])
145
+ assert_equal(msgs[i], record['message'])
146
+ end
147
+ end
148
+
149
+ test 'enable_ruby no' do
150
+ config = %[
151
+ tag reformed.${tag}
152
+ enable_ruby no
153
+ <record>
154
+ message ${hostname} ${tag_parts.last} ${URI.encode(message)}
155
+ </record>
156
+ ]
157
+ msgs = ['1', '2']
158
+ emits = emit(config, msgs)
159
+ emits.each_with_index do |(tag, time, record), i|
160
+ assert_equal("reformed.#{@tag}", tag)
161
+ assert_equal("#{@hostname} ${tag_parts.last} ${URI.encode(message)}", record['message'])
162
+ end
163
+ end
164
+ end
165
+
166
+ sub_test_case 'test placeholders' do
167
+ %w[yes no].each do |enable_ruby|
168
+ test "hostname with enble_ruby #{enable_ruby}" do
169
+ config = %[
170
+ tag tag
171
+ enable_ruby #{enable_ruby}
172
+ <record>
173
+ message ${hostname}
174
+ </record>
175
+ ]
176
+ emits = emit(config)
177
+ emits.each do |(tag, time, record)|
178
+ assert_equal(@hostname, record['message'])
179
+ end
180
+ end
181
+
182
+ test "tag with enable_ruby #{enable_ruby}" do
183
+ config = %[
184
+ tag tag
185
+ enable_ruby #{enable_ruby}
186
+ <record>
187
+ message ${tag}
188
+ </record>
189
+ ]
190
+ emits = emit(config)
191
+ emits.each do |(tag, time, record)|
192
+ assert_equal(@tag, record['message'])
193
+ end
194
+ end
195
+
196
+ test "tag_parts with enable_ruby #{enable_ruby}" do
197
+ config = %[
198
+ tag tag
199
+ enable_ruby #{enable_ruby}
200
+ <record>
201
+ message ${tag_parts[0]} ${tag_parts[-1]}
202
+ </record>
203
+ ]
204
+ expected = "#{@tag.split('.').first} #{@tag.split('.').last}"
205
+ emits = emit(config)
206
+ emits.each do |(tag, time, record)|
207
+ assert_equal(expected, record['message'])
208
+ end
209
+ end
210
+
211
+ test "(obsolete) tags with enable_ruby #{enable_ruby}" do
212
+ config = %[
213
+ tag tag
214
+ enable_ruby #{enable_ruby}
215
+ <record>
216
+ message ${tags[0]} ${tags[-1]}
217
+ </record>
218
+ ]
219
+ expected = "#{@tag.split('.').first} #{@tag.split('.').last}"
220
+ emits = emit(config)
221
+ emits.each do |(tag, time, record)|
222
+ assert_equal(expected, record['message'])
223
+ end
224
+ end
225
+
226
+ test "${tag_prefix[N]} and ${tag_suffix[N]} with enable_ruby #{enable_ruby}" do
227
+ config = %[
228
+ tag tag
229
+ enable_ruby #{enable_ruby}
230
+ <record>
231
+ message ${tag_prefix[1]} ${tag_prefix[-2]} ${tag_suffix[2]} ${tag_suffix[-3]}
232
+ </record>
233
+ ]
234
+ @tag = 'prefix.test.tag.suffix'
235
+ expected = "prefix.test prefix.test.tag tag.suffix test.tag.suffix"
236
+ emits = emit(config)
237
+ emits.each do |(tag, time, record)|
238
+ assert_equal(expected, record['message'])
239
+ end
240
+ end
241
+
242
+ test "time with enable_ruby #{enable_ruby}" do
243
+ config = %[
244
+ tag tag
245
+ enable_ruby #{enable_ruby}
246
+ <record>
247
+ message ${time}
248
+ </record>
249
+ ]
250
+ emits = emit(config)
251
+ emits.each do |(tag, time, record)|
252
+ assert_equal(@time.to_s, record['message'])
253
+ end
254
+ end
255
+
256
+ test "record keys with enable_ruby #{enable_ruby}" do
257
+ config = %[
258
+ tag tag
259
+ enable_ruby #{enable_ruby}
260
+ remove_keys eventType0
261
+ <record>
262
+ message bar ${message}
263
+ eventtype ${eventType0}
264
+ </record>
265
+ ]
266
+ msgs = ['1', '2']
267
+ emits = emit(config, msgs)
268
+ emits.each_with_index do |(tag, time, record), i|
269
+ assert_not_include(record, 'eventType0')
270
+ assert_equal("bar", record['eventtype'])
271
+ assert_equal("bar #{msgs[i]}", record['message'])
272
+ end
273
+ end
274
+ end
275
+
276
+ test 'unknown placeholder (enable_ruby no)' do
277
+ config = %[
278
+ tag tag
279
+ enable_ruby no
280
+ <record>
281
+ message ${unknown}
282
+ </record>
283
+ ]
284
+ d = create_driver(config)
285
+ mock(d.instance.log).warn("record_reformer: unknown placeholder `${unknown}` found")
286
+ d.run { d.emit({}, @time) }
287
+ assert_equal 1, d.emits.size
288
+ end
289
+
290
+ test 'failed to expand record field (enable_ruby yes)' do
291
+ config = %[
292
+ tag tag
293
+ enable_ruby yes
294
+ <record>
295
+ message ${unknown['bar']}
296
+ </record>
297
+ ]
298
+ d = create_driver(config)
299
+ mock(d.instance.log).warn("record_reformer: failed to expand `${unknown['bar']}`", anything)
300
+ d.run { d.emit({}, @time) }
301
+ # emit, but nil value
302
+ assert_equal 1, d.emits.size
303
+ d.emits.each do |(tag, time, record)|
304
+ assert_nil(record['message'])
305
+ end
306
+ end
307
+
308
+ test 'failed to expand tag (enable_ruby yes)' do
309
+ config = %[
310
+ tag ${unknown['bar']}
311
+ enable_ruby yes
312
+ ]
313
+ d = create_driver(config)
314
+ mock(d.instance.log).warn("record_reformer: failed to expand `${unknown['bar']}`", anything)
315
+ d.run { d.emit({}, @time) }
316
+ # nil tag message should not be emitted
317
+ assert_equal 0, d.emits.size
318
+ end
319
+ end
320
+ end
metadata CHANGED
@@ -1,94 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-record-reformer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
5
- prerelease:
4
+ version: 0.4.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Naotoshi Seo
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-09-30 00:00:00.000000000 Z
11
+ date: 2014-10-31 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: fluentd
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
- name: rspec
42
+ name: pry
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
- name: pry
56
+ name: pry-nav
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - ">="
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - ">="
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0'
78
69
  - !ruby/object:Gem::Dependency
79
- name: pry-nav
70
+ name: test-unit
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: timecop
80
99
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
100
  requirements:
83
- - - ! '>='
101
+ - - ">="
84
102
  - !ruby/object:Gem::Version
85
103
  version: '0'
86
104
  type: :development
87
105
  prerelease: false
88
106
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
107
  requirements:
91
- - - ! '>='
108
+ - - ">="
92
109
  - !ruby/object:Gem::Version
93
110
  version: '0'
94
111
  description: Fluentd plugin to add or replace fields of a event record
@@ -97,9 +114,9 @@ executables: []
97
114
  extensions: []
98
115
  extra_rdoc_files: []
99
116
  files:
100
- - .gitignore
101
- - .rspec
102
- - .travis.yml
117
+ - ".gitignore"
118
+ - ".rspec"
119
+ - ".travis.yml"
103
120
  - CHANGELOG.md
104
121
  - Gemfile
105
122
  - LICENSE
@@ -107,41 +124,34 @@ files:
107
124
  - Rakefile
108
125
  - fluent-plugin-record-reformer.gemspec
109
126
  - lib/fluent/plugin/out_record_reformer.rb
110
- - spec/out_record_reformer_bench.rb
111
- - spec/out_record_reformer_spec.rb
112
- - spec/spec_helper.rb
127
+ - test/bench_out_record_reformer.rb
128
+ - test/helper.rb
129
+ - test/test_out_record_reformer.rb
113
130
  homepage: https://github.com/sonots/fluent-plugin-record-reformer
114
131
  licenses:
115
132
  - MIT
133
+ metadata: {}
116
134
  post_install_message:
117
135
  rdoc_options: []
118
136
  require_paths:
119
137
  - lib
120
138
  required_ruby_version: !ruby/object:Gem::Requirement
121
- none: false
122
139
  requirements:
123
- - - ! '>='
140
+ - - ">="
124
141
  - !ruby/object:Gem::Version
125
142
  version: '0'
126
- segments:
127
- - 0
128
- hash: -981862289
129
143
  required_rubygems_version: !ruby/object:Gem::Requirement
130
- none: false
131
144
  requirements:
132
- - - ! '>='
145
+ - - ">="
133
146
  - !ruby/object:Gem::Version
134
147
  version: '0'
135
- segments:
136
- - 0
137
- hash: -981862289
138
148
  requirements: []
139
149
  rubyforge_project:
140
- rubygems_version: 1.8.23
150
+ rubygems_version: 2.2.2
141
151
  signing_key:
142
- specification_version: 3
152
+ specification_version: 4
143
153
  summary: Fluentd plugin to add or replace fields of a event record
144
154
  test_files:
145
- - spec/out_record_reformer_bench.rb
146
- - spec/out_record_reformer_spec.rb
147
- - spec/spec_helper.rb
155
+ - test/bench_out_record_reformer.rb
156
+ - test/helper.rb
157
+ - test/test_out_record_reformer.rb
@@ -1,284 +0,0 @@
1
- # encoding: UTF-8
2
- require_relative 'spec_helper'
3
-
4
- describe Fluent::RecordReformerOutput do
5
- before { Fluent::Test.setup }
6
- CONFIG = %[
7
- tag reformed.${tag}
8
-
9
- hostname ${hostname}
10
- input_tag ${tag}
11
- time ${time.strftime('%S')}
12
- message ${hostname} ${tag_parts.last} ${URI.escape(message)}
13
- ]
14
- let(:tag) { 'test.tag' }
15
- let(:tag_parts) { tag.split('.') }
16
- let(:hostname) { Socket.gethostname.chomp }
17
- let(:driver) { Fluent::Test::OutputTestDriver.new(Fluent::RecordReformerOutput, tag).configure(config) }
18
-
19
- describe 'test configure' do
20
- describe 'good configuration' do
21
- subject { driver.instance }
22
-
23
- context "check default" do
24
- let(:config) { CONFIG }
25
- it { expect { subject }.not_to raise_error }
26
- end
27
-
28
- context "tag is not specified" do
29
- let(:config) { %[] }
30
- it { expect { subject }.to raise_error(Fluent::ConfigError) }
31
- end
32
-
33
- context "keep_keys must be specified togerther with renew_record true" do
34
- let(:config) { %[keep_keys a] }
35
- it { expect { subject }.to raise_error(Fluent::ConfigError) }
36
- end
37
- end
38
- end
39
-
40
- describe 'test emit' do
41
- let(:time) { Time.now }
42
- let(:emit) do
43
- driver.run { driver.emit({'foo'=>'bar', 'message' => '1'}, time.to_i) }
44
- end
45
-
46
- context 'typical usage' do
47
- let(:emit) do
48
- driver.run do
49
- driver.emit({'foo'=>'bar', 'message' => '1'}, time.to_i)
50
- driver.emit({'foo'=>'bar', 'message' => '2'}, time.to_i)
51
- end
52
- end
53
- let(:config) { CONFIG }
54
- before do
55
- Fluent::Engine.stub(:now).and_return(time)
56
- Fluent::Engine.should_receive(:emit).with("reformed.#{tag}", time.to_i, {
57
- 'foo' => 'bar',
58
- 'hostname' => hostname,
59
- 'input_tag' => tag,
60
- 'time' => time.strftime('%S'),
61
- 'message' => "#{hostname} #{tag_parts.last} 1",
62
- })
63
- Fluent::Engine.should_receive(:emit).with("reformed.#{tag}", time.to_i, {
64
- 'foo' => 'bar',
65
- 'hostname' => hostname,
66
- 'input_tag' => tag,
67
- 'time' => time.strftime('%S'),
68
- 'message' => "#{hostname} #{tag_parts.last} 2",
69
- })
70
- end
71
- it { emit }
72
- end
73
-
74
- context 'obsolete output_tag' do
75
- let(:config) {%[
76
- output_tag reformed.${tag}
77
- ]}
78
- before do
79
- Fluent::Engine.stub(:now).and_return(time)
80
- Fluent::Engine.should_receive(:emit).with("reformed.#{tag}", time.to_i, {
81
- 'foo' => 'bar',
82
- 'message' => "1",
83
- })
84
- end
85
- it { emit }
86
- end
87
-
88
- context 'record directive' do
89
- let(:config) {%[
90
- tag reformed.${tag}
91
-
92
- <record>
93
- hostname ${hostname}
94
- tag ${tag}
95
- time ${time.strftime('%S')}
96
- message ${hostname} ${tag_parts.last} ${message}
97
- </record>
98
- ]}
99
- before do
100
- Fluent::Engine.stub(:now).and_return(time)
101
- Fluent::Engine.should_receive(:emit).with("reformed.#{tag}", time.to_i, {
102
- 'foo' => 'bar',
103
- 'hostname' => hostname,
104
- 'tag' => tag,
105
- 'time' => time.strftime('%S'),
106
- 'message' => "#{hostname} #{tag_parts.last} 1",
107
- })
108
- end
109
- it { emit }
110
- end
111
-
112
- context 'remove_keys' do
113
- let(:config) { CONFIG + %[remove_keys foo,message] }
114
- before do
115
- Fluent::Engine.stub(:now).and_return(time)
116
- Fluent::Engine.should_receive(:emit).with("reformed.#{tag}", time.to_i, {
117
- 'hostname' => hostname,
118
- 'input_tag' => tag,
119
- 'time' => time.strftime('%S'),
120
- })
121
- end
122
- it { emit }
123
- end
124
-
125
- context 'renew_record true' do
126
- let(:config) { CONFIG + %[renew_record true] }
127
- before do
128
- Fluent::Engine.stub(:now).and_return(time)
129
- Fluent::Engine.should_receive(:emit).with("reformed.#{tag}", time.to_i, {
130
- 'hostname' => hostname,
131
- 'input_tag' => tag,
132
- 'time' => time.strftime('%S'),
133
- 'message' => "#{hostname} #{tag_parts.last} 1",
134
- })
135
- end
136
- it { emit }
137
- end
138
-
139
- context 'keep_keys' do
140
- let(:emit) do
141
- driver.run { driver.emit({'foo'=>'bar', 'message' => 1}, time.to_i) }
142
- end
143
- let(:config) { %[tag reformed.${tag}\nrenew_record true\nkeep_keys foo,message] }
144
- before do
145
- Fluent::Engine.stub(:now).and_return(time)
146
- Fluent::Engine.should_receive(:emit).with("reformed.#{tag}", time.to_i, {
147
- 'foo' => 'bar',
148
- 'message' => 1, # this keep type
149
- })
150
- end
151
- it { emit }
152
- end
153
-
154
- context 'unknown placeholder (enable_ruby no)' do
155
- let(:emit) do
156
- driver.run { driver.emit({}, time.to_i) }
157
- end
158
- let(:config) {%[
159
- tag reformed.${tag}
160
- enable_ruby no
161
- message ${unknown}
162
- ]}
163
- before do
164
- driver.instance.log.should_receive(:warn).with("record_reformer: unknown placeholder `${unknown}` found")
165
- end
166
- it { emit }
167
- end
168
- end
169
-
170
- describe 'test placeholders' do
171
- let(:time) { Time.now }
172
- let(:emit) do
173
- driver.run { driver.emit({}, time.to_i) }
174
- end
175
-
176
- %w[yes no].each do |enable_ruby|
177
- context "hostname with enble_ruby #{enable_ruby}" do
178
- let(:config) {%[
179
- tag tag
180
- enable_ruby #{enable_ruby}
181
- message ${hostname}
182
- ]}
183
- before do
184
- Fluent::Engine.stub(:now).and_return(time)
185
- Fluent::Engine.should_receive(:emit).with("tag", time.to_i, {'message' => hostname})
186
- end
187
- it { emit }
188
- end
189
-
190
- context "tag with enable_ruby #{enable_ruby}" do
191
- let(:config) {%[
192
- tag tag
193
- enable_ruby #{enable_ruby}
194
- message ${tag}
195
- ]}
196
- before do
197
- Fluent::Engine.stub(:now).and_return(time)
198
- Fluent::Engine.should_receive(:emit).with("tag", time.to_i, {'message' => tag})
199
- end
200
- it { emit }
201
- end
202
-
203
- context "tag_parts with enable_ruby #{enable_ruby}" do
204
- let(:config) {%[
205
- tag tag
206
- enable_ruby #{enable_ruby}
207
- message ${tag_parts[0]} ${tag_parts[-1]}
208
- ]}
209
- let(:expected) { "#{tag.split('.').first} #{tag.split('.').last}" }
210
- before do
211
- Fluent::Engine.stub(:now).and_return(time)
212
- Fluent::Engine.should_receive(:emit).with("tag", time.to_i, {'message' => expected})
213
- end
214
- it { emit }
215
- end
216
-
217
- context "support old tags with enable_ruby #{enable_ruby}" do
218
- let(:config) {%[
219
- tag tag
220
- enable_ruby #{enable_ruby}
221
- message ${tags[0]} ${tags[-1]}
222
- ]}
223
- let(:expected) { "#{tag.split('.').first} #{tag.split('.').last}" }
224
- before do
225
- Fluent::Engine.stub(:now).and_return(time)
226
- Fluent::Engine.should_receive(:emit).with("tag", time.to_i, {'message' => expected})
227
- end
228
- it { emit }
229
- end
230
-
231
- context "${tag_prefix[N]} and ${tag_suffix[N]} with enable_ruby #{enable_ruby}" do
232
- let(:config) {%[
233
- tag ${tag_suffix[-2]}
234
- enable_ruby #{enable_ruby}
235
- message ${tag_prefix[1]} ${tag_prefix[-2]} ${tag_suffix[2]} ${tag_suffix[-3]}
236
- ]}
237
- let(:tag) { 'prefix.test.tag.suffix' }
238
- let(:expected) { "prefix.test prefix.test.tag tag.suffix test.tag.suffix" }
239
- before do
240
- Fluent::Engine.stub(:now).and_return(time)
241
- Fluent::Engine.should_receive(:emit).with("tag.suffix", time.to_i, { 'message' => "prefix.test prefix.test.tag tag.suffix test.tag.suffix" })
242
- end
243
- it { emit }
244
- end
245
-
246
- context "time with enable_ruby #{enable_ruby}" do
247
- let(:config) {%[
248
- tag tag
249
- enable_ruby #{enable_ruby}
250
- time ${time}
251
- ]}
252
- before do
253
- Fluent::Engine.stub(:now).and_return(time)
254
- Fluent::Engine.should_receive(:emit).with("tag", time.to_i, {'time' => time.to_s})
255
- end
256
- it { emit }
257
- end
258
-
259
- context "record with enable_ruby #{enable_ruby}" do
260
- let(:emit) do
261
- driver.run do
262
- driver.emit({'message' => '1', 'eventType' => 'foo'}, time.to_i)
263
- driver.emit({'message' => '2', 'eventType' => 'foo'}, time.to_i)
264
- end
265
- end
266
- let(:config) {%[
267
- tag tag
268
- enable_ruby #{enable_ruby}
269
- message bar ${message}
270
- eventtype ${eventType}
271
- remove_keys eventType
272
- ]}
273
- let(:tag) { 'prefix.test.tag.suffix' }
274
- let(:tag_parts) { tag.split('.') }
275
- before do
276
- Fluent::Engine.stub(:now).and_return(time)
277
- Fluent::Engine.should_receive(:emit).with("tag", time.to_i, { 'message' => "bar 1", 'eventtype' => 'foo'})
278
- Fluent::Engine.should_receive(:emit).with("tag", time.to_i, { 'message' => "bar 2", 'eventtype' => 'foo'})
279
- end
280
- it { emit }
281
- end
282
- end
283
- end
284
- end
@@ -1,13 +0,0 @@
1
- # encoding: UTF-8
2
- require 'rubygems'
3
- require 'bundler'
4
- Bundler.setup(:default, :test)
5
- Bundler.require(:default, :test)
6
-
7
- require 'fluent/test'
8
- require 'rspec'
9
- require 'pry'
10
-
11
- $TESTING=true
12
- $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
13
- require 'fluent/plugin/out_record_reformer'