fluent-plugin-record-reformer 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd21069087496f3c30e1599b22b8847d6429323d
4
- data.tar.gz: 31026f14e6ed73d41ca4ad80ed5f7caca6da72a2
3
+ metadata.gz: 20d0e50cb908d582e7b826037f0e73df95713e44
4
+ data.tar.gz: ffd150c413f42bf034997dc30d96c919061ea2c9
5
5
  SHA512:
6
- metadata.gz: 504c18888e9e06f93fc2eba073399271b558e1a63811de988ecac867429b8e8c06caf9f096210568cbfdfc028d20a1c760301ebaa90f106649776f9f60f9305b
7
- data.tar.gz: a4d047dbc29a96c2eed77e804ec5e08a99d1bef0055f866c1886c7e7facf2ca227058533fb7ec0e87bf26b960d6b19581c7671aa40dd455570d250d8629bca71
6
+ metadata.gz: 6812b8c69cccd058cff3fc41f42d4d058ef4d4fb15a482e71f9263fcb6d9627269f284aaa6a492304fbab0489633b2978a53d89271122e247ea3195fe176f4b9
7
+ data.tar.gz: 9c3e4434096f28fe84393c7fd74cd906ff5772e930168783b10e2f784a055c2dde4c3564c7c022f311b1fc18ffe7c931ebca6b941bf22a6eaab37f130ed45697
@@ -1,3 +1,10 @@
1
+ ## 0.5.0 (2014/03/06)
2
+
3
+ Enhancements:
4
+
5
+ * Support JSON Array/Hash
6
+ * Support placeholders for keys
7
+
1
8
  ## 0.4.0 (2014/10/31)
2
9
 
3
10
  Changes:
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 Naotoshi SEO
1
+ Copyright (c) 2013 - 2015 Naotoshi Seo
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -105,9 +105,9 @@ This results in same, but please note that following option parameters are reser
105
105
 
106
106
  The keys of input json are available as placeholders. In the above example,
107
107
 
108
- * ${foo}
109
- * ${message}
110
108
  * ${remove_me}
109
+ * ${not_remove_me}
110
+ * ${message}
111
111
 
112
112
  shall be available. In addition, following placeholders are reserved:
113
113
 
@@ -148,6 +148,7 @@ Following plugins look similar:
148
148
  * [fluent-plugin-record-modifier](https://github.com/repeatedly/fluent-plugin-record-modifier)
149
149
  * [fluent-plugin-format](https://github.com/mach/fluent-plugin-format)
150
150
  * [fluent-plugin-add](https://github.com/yu-yamada/fluent-plugin-add)
151
+ * [filter_record_transformer](http://docs.fluentd.org/v0.12/articles/filter_record_transformer) is a Fluentd v0.12 built-in plugin which is based on record-reformer.
151
152
 
152
153
  ## ChangeLog
153
154
 
@@ -163,4 +164,4 @@ See [CHANGELOG.md](CHANGELOG.md) for details.
163
164
 
164
165
  ## Copyright
165
166
 
166
- Copyright (c) 2013 Naotoshi SEO. See [LICENSE](LICENSE) for details.
167
+ Copyright (c) 2013 - 2015 Naotoshi Seo. See [LICENSE](LICENSE) for details.
@@ -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.4.0"
6
+ gem.version = "0.5.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"
@@ -30,13 +30,13 @@ module Fluent
30
30
  conf.each_pair { |k, v|
31
31
  next if BUILTIN_CONFIGURATIONS.include?(k)
32
32
  conf.has_key?(k) # to suppress unread configuration warning
33
- @map[k] = v
33
+ @map[k] = parse_value(v)
34
34
  }
35
35
  # <record></record> directive
36
36
  conf.elements.select { |element| element.name == 'record' }.each { |element|
37
37
  element.each_pair { |k, v|
38
38
  element.has_key?(k) # to suppress unread configuration warning
39
- @map[k] = v
39
+ @map[k] = parse_value(v)
40
40
  }
41
41
  }
42
42
 
@@ -97,18 +97,48 @@ module Fluent
97
97
 
98
98
  private
99
99
 
100
+ def parse_value(value_str)
101
+ if value_str.start_with?('{', '[')
102
+ JSON.parse(value_str)
103
+ else
104
+ value_str
105
+ end
106
+ rescue => e
107
+ log.warn "failed to parse #{value_str} as json. Assuming #{value_str} is a string", :error_class => e.class, :error => e.message
108
+ value_str # emit as string
109
+ end
110
+
100
111
  def reform(tag, time, record, opts)
101
112
  @placeholder_expander.prepare_placeholders(time, record, opts)
102
113
  new_tag = @placeholder_expander.expand(tag)
103
114
 
104
115
  new_record = @renew_record ? {} : record.dup
105
116
  @keep_keys.each {|k| new_record[k] = record[k]} if @keep_keys and @renew_record
106
- @map.each_pair {|k, v| new_record[k] = @placeholder_expander.expand(v) }
117
+ new_record.merge!(expand_placeholders(@map))
107
118
  @remove_keys.each {|k| new_record.delete(k) } if @remove_keys
108
119
 
109
120
  [new_tag, new_record]
110
121
  end
111
122
 
123
+ def expand_placeholders(value)
124
+ if value.is_a?(String)
125
+ new_value = @placeholder_expander.expand(value)
126
+ elsif value.is_a?(Hash)
127
+ new_value = {}
128
+ value.each_pair do |k, v|
129
+ new_value[@placeholder_expander.expand(k)] = expand_placeholders(v)
130
+ end
131
+ elsif value.is_a?(Array)
132
+ new_value = []
133
+ value.each_with_index do |v, i|
134
+ new_value[i] = expand_placeholders(v)
135
+ end
136
+ else
137
+ new_value = value
138
+ end
139
+ new_value
140
+ end
141
+
112
142
  def tag_prefix(tag_parts)
113
143
  return [] if tag_parts.empty?
114
144
  tag_prefix = [tag_parts.first]
@@ -186,13 +216,11 @@ module Fluent
186
216
  # @param [String] str the string to be replaced
187
217
  def expand(str)
188
218
  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
219
+ eval "\"#{interpolated}\"", @placeholders.instance_eval { binding }
220
+ rescue => e
221
+ log.warn "record_reformer: failed to expand `#{str}`", :error_class => e.class, :error => e.message
222
+ log.warn_backtrace
223
+ nil
196
224
  end
197
225
 
198
226
  class UndefOpenStruct < OpenStruct
@@ -7,4 +7,21 @@ unless defined?(Test::Unit::AssertionFailedError)
7
7
  end
8
8
  end
9
9
 
10
- $log = Fluent::Log.new(Fluent::Test::DummyLogDevice.new, Fluent::Log::LEVEL_WARN)
10
+ # Stop non required sleep at
11
+ # https://github.com/fluent/fluentd/blob/018791f6b1b0400b71e37df2fb3ad80e456d2c11/lib/fluent/test/base.rb#L56
12
+ module Fluent
13
+ module Test
14
+ class TestDriver
15
+ def run(&block)
16
+ @instance.start
17
+ begin
18
+ # wait until thread starts
19
+ # 10.times { sleep 0.05 }
20
+ return yield
21
+ ensure
22
+ @instance.shutdown
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -18,14 +18,16 @@ class RecordReformerOutputTest < Test::Unit::TestCase
18
18
  Timecop.return
19
19
  end
20
20
 
21
- def create_driver(conf)
22
- Fluent::Test::OutputTestDriver.new(Fluent::RecordReformerOutput, @tag).configure(conf)
21
+ def create_driver(conf, use_v1)
22
+ Fluent::Test::OutputTestDriver.new(Fluent::RecordReformerOutput, @tag).configure(conf, use_v1)
23
23
  end
24
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) }
25
+ def emit(config, use_v1, msgs = [''])
26
+ d = create_driver(config, use_v1)
27
+ d.run do
28
+ msgs.each do |msg|
29
+ d.emit({'eventType0' => 'bar', 'message' => msg}, @time)
30
+ end
29
31
  end
30
32
 
31
33
  @instance = d.instance
@@ -41,52 +43,53 @@ class RecordReformerOutputTest < Test::Unit::TestCase
41
43
  message ${hostname} ${tag_parts.last} ${URI.escape(message)}
42
44
  ]
43
45
 
44
- sub_test_case 'configure' do
45
- test 'typical usage' do
46
- assert_nothing_raised do
47
- create_driver(CONFIG)
46
+ [true, false].each do |use_v1|
47
+ sub_test_case 'configure' do
48
+ test 'typical usage' do
49
+ assert_nothing_raised do
50
+ create_driver(CONFIG, use_v1)
51
+ end
48
52
  end
49
- end
50
53
 
51
- test "tag is not specified" do
52
- assert_raise(Fluent::ConfigError) do
53
- create_driver('')
54
+ test "tag is not specified" do
55
+ assert_raise(Fluent::ConfigError) do
56
+ create_driver('', use_v1)
57
+ end
54
58
  end
55
- end
56
59
 
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
+ test "keep_keys must be specified together with renew_record true" do
61
+ assert_raise(Fluent::ConfigError) do
62
+ create_driver(%[keep_keys a], use_v1)
63
+ end
60
64
  end
61
65
  end
62
- end
63
66
 
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'])
67
+ sub_test_case "test options" do
68
+ test 'typical usage' do
69
+ msgs = ['1', '2']
70
+ emits = emit(CONFIG, use_v1, msgs)
71
+ assert_equal 2, emits.size
72
+ emits.each_with_index do |(tag, time, record), i|
73
+ assert_equal("reformed.#{@tag}", tag)
74
+ assert_equal('bar', record['eventType0'])
75
+ assert_equal(@hostname, record['hostname'])
76
+ assert_equal(@tag, record['input_tag'])
77
+ assert_equal(@time.to_s, record['time'])
78
+ assert_equal("#{@hostname} #{@tag_parts[-1]} #{msgs[i]}", record['message'])
79
+ end
76
80
  end
77
- end
78
81
 
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)
82
+ test '(obsolete) output_tag' do
83
+ config = %[output_tag reformed.${tag}]
84
+ msgs = ['1']
85
+ emits = emit(config, use_v1, msgs)
86
+ emits.each_with_index do |(tag, time, record), i|
87
+ assert_equal("reformed.#{@tag}", tag)
88
+ end
85
89
  end
86
- end
87
90
 
88
- test 'record directive' do
89
- config = %[
91
+ test 'record directive' do
92
+ config = %[
90
93
  tag reformed.${tag}
91
94
 
92
95
  <record>
@@ -95,226 +98,293 @@ class RecordReformerOutputTest < Test::Unit::TestCase
95
98
  time ${time.to_s}
96
99
  message ${hostname} ${tag_parts.last} ${message}
97
100
  </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')
101
+ ]
102
+ msgs = ['1', '2']
103
+ emits = emit(config, use_v1, msgs)
104
+ emits.each_with_index do |(tag, time, record), i|
105
+ assert_equal("reformed.#{@tag}", tag)
106
+ assert_equal('bar', record['eventType0'])
107
+ assert_equal(@hostname, record['hostname'])
108
+ assert_equal(@tag, record['tag'])
109
+ assert_equal(@time.to_s, record['time'])
110
+ assert_equal("#{@hostname} #{@tag_parts[-1]} #{msgs[i]}", record['message'])
111
+ end
121
112
  end
122
- end
123
113
 
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'])
114
+ test 'remove_keys' do
115
+ config = CONFIG + %[remove_keys eventType0,message]
116
+ emits = emit(config, use_v1)
117
+ emits.each_with_index do |(tag, time, record), i|
118
+ assert_equal("reformed.#{@tag}", tag)
119
+ assert_not_include(record, 'eventType0')
120
+ assert_equal(@hostname, record['hostname'])
121
+ assert_equal(@tag, record['input_tag'])
122
+ assert_equal(@time.to_s, record['time'])
123
+ assert_not_include(record, 'message')
124
+ end
135
125
  end
136
- end
137
126
 
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'])
127
+ test 'renew_record' do
128
+ config = CONFIG + %[renew_record true]
129
+ msgs = ['1', '2']
130
+ emits = emit(config, use_v1, msgs)
131
+ emits.each_with_index do |(tag, time, record), i|
132
+ assert_equal("reformed.#{@tag}", tag)
133
+ assert_not_include(record, 'eventType0')
134
+ assert_equal(@hostname, record['hostname'])
135
+ assert_equal(@tag, record['input_tag'])
136
+ assert_equal(@time.to_s, record['time'])
137
+ assert_equal("#{@hostname} #{@tag_parts[-1]} #{msgs[i]}", record['message'])
138
+ end
146
139
  end
147
- end
148
140
 
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'])
141
+ test 'keep_keys' do
142
+ config = %[tag reformed.${tag}\nrenew_record true\nkeep_keys eventType0,message]
143
+ msgs = ['1', '2']
144
+ emits = emit(config, use_v1, msgs)
145
+ emits.each_with_index do |(tag, time, record), i|
146
+ assert_equal("reformed.#{@tag}", tag)
147
+ assert_equal('bar', record['eventType0'])
148
+ assert_equal(msgs[i], record['message'])
149
+ end
162
150
  end
163
- end
164
- end
165
151
 
166
- sub_test_case 'test placeholders' do
167
- %w[yes no].each do |enable_ruby|
168
- test "hostname with enble_ruby #{enable_ruby}" do
152
+ test 'enable_ruby no' do
169
153
  config = %[
170
- tag tag
171
- enable_ruby #{enable_ruby}
154
+ tag reformed.${tag}
155
+ enable_ruby no
172
156
  <record>
173
- message ${hostname}
157
+ message ${hostname} ${tag_parts.last} ${URI.encode(message)}
174
158
  </record>
175
159
  ]
176
- emits = emit(config)
177
- emits.each do |(tag, time, record)|
178
- assert_equal(@hostname, record['message'])
160
+ msgs = ['1', '2']
161
+ emits = emit(config, use_v1, msgs)
162
+ emits.each_with_index do |(tag, time, record), i|
163
+ assert_equal("reformed.#{@tag}", tag)
164
+ assert_equal("#{@hostname} ${tag_parts.last} ${URI.encode(message)}", record['message'])
179
165
  end
180
166
  end
167
+ end
181
168
 
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'])
169
+ sub_test_case 'test placeholders' do
170
+ %w[yes no].each do |enable_ruby|
171
+
172
+ test "hostname with enble_ruby #{enable_ruby}" do
173
+ config = %[
174
+ tag tag
175
+ enable_ruby #{enable_ruby}
176
+ <record>
177
+ message ${hostname}
178
+ </record>
179
+ ]
180
+ emits = emit(config, use_v1)
181
+ emits.each do |(tag, time, record)|
182
+ assert_equal(@hostname, record['message'])
183
+ end
193
184
  end
194
- end
195
185
 
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'])
186
+ test "tag with enable_ruby #{enable_ruby}" do
187
+ config = %[
188
+ tag tag
189
+ enable_ruby #{enable_ruby}
190
+ <record>
191
+ message ${tag}
192
+ </record>
193
+ ]
194
+ emits = emit(config, use_v1)
195
+ emits.each do |(tag, time, record)|
196
+ assert_equal(@tag, record['message'])
197
+ end
208
198
  end
209
- end
210
199
 
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'])
200
+ test "tag_parts with enable_ruby #{enable_ruby}" do
201
+ config = %[
202
+ tag tag
203
+ enable_ruby #{enable_ruby}
204
+ <record>
205
+ message ${tag_parts[0]} ${tag_parts[-1]}
206
+ </record>
207
+ ]
208
+ expected = "#{@tag.split('.').first} #{@tag.split('.').last}"
209
+ emits = emit(config, use_v1)
210
+ emits.each do |(tag, time, record)|
211
+ assert_equal(expected, record['message'])
212
+ end
223
213
  end
224
- end
225
214
 
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'])
215
+ test "(obsolete) tags with enable_ruby #{enable_ruby}" do
216
+ config = %[
217
+ tag tag
218
+ enable_ruby #{enable_ruby}
219
+ <record>
220
+ message ${tags[0]} ${tags[-1]}
221
+ </record>
222
+ ]
223
+ expected = "#{@tag.split('.').first} #{@tag.split('.').last}"
224
+ emits = emit(config, use_v1)
225
+ emits.each do |(tag, time, record)|
226
+ assert_equal(expected, record['message'])
227
+ end
228
+ end
229
+
230
+ test "${tag_prefix[N]} and ${tag_suffix[N]} with enable_ruby #{enable_ruby}" do
231
+ config = %[
232
+ tag tag
233
+ enable_ruby #{enable_ruby}
234
+ <record>
235
+ message ${tag_prefix[1]} ${tag_prefix[-2]} ${tag_suffix[2]} ${tag_suffix[-3]}
236
+ </record>
237
+ ]
238
+ @tag = 'prefix.test.tag.suffix'
239
+ expected = "prefix.test prefix.test.tag tag.suffix test.tag.suffix"
240
+ emits = emit(config, use_v1)
241
+ emits.each do |(tag, time, record)|
242
+ assert_equal(expected, record['message'])
243
+ end
244
+ end
245
+
246
+ test "time with enable_ruby #{enable_ruby}" do
247
+ config = %[
248
+ tag tag
249
+ enable_ruby #{enable_ruby}
250
+ <record>
251
+ message ${time}
252
+ </record>
253
+ ]
254
+ emits = emit(config, use_v1)
255
+ emits.each do |(tag, time, record)|
256
+ assert_equal(@time.to_s, record['message'])
257
+ end
258
+ end
259
+
260
+ test "record keys with enable_ruby #{enable_ruby}" do
261
+ config = %[
262
+ tag tag
263
+ enable_ruby #{enable_ruby}
264
+ remove_keys eventType0
265
+ <record>
266
+ message bar ${message}
267
+ eventtype ${eventType0}
268
+ </record>
269
+ ]
270
+ msgs = ['1', '2']
271
+ emits = emit(config, use_v1, msgs)
272
+ emits.each_with_index do |(tag, time, record), i|
273
+ assert_not_include(record, 'eventType0')
274
+ assert_equal("bar", record['eventtype'])
275
+ assert_equal("bar #{msgs[i]}", record['message'])
276
+ end
277
+ end
278
+
279
+ test "hash values with placeholders with enable_ruby #{enable_ruby}" do
280
+ config = %[
281
+ tag tag
282
+ enable_ruby #{enable_ruby}
283
+ <record>
284
+ hash_field {"hostname":"${hostname}", "tag":"${tag}", "${tag}":100}
285
+ </record>
286
+ ]
287
+ msgs = ['1', '2']
288
+ es = emit(config, use_v1, msgs)
289
+ es.each_with_index do |(tag, time, record), i|
290
+ assert_equal({"hostname" => @hostname, "tag" => @tag, "#{@tag}" => 100}, record['hash_field'])
291
+ end
292
+ end
293
+
294
+ test "array values with placeholders with enable_ruby #{enable_ruby}" do
295
+ config = %[
296
+ tag tag
297
+ enable_ruby #{enable_ruby}
298
+ <record>
299
+ array_field ["${hostname}", "${tag}"]
300
+ </record>
301
+ ]
302
+ msgs = ['1', '2']
303
+ es = emit(config, use_v1, msgs)
304
+ es.each_with_index do |(tag, time, record), i|
305
+ assert_equal([@hostname, @tag], record['array_field'])
306
+ end
307
+ end
308
+
309
+ test "array and hash values with placeholders with enable_ruby #{enable_ruby}" do
310
+ config = %[
311
+ tag tag
312
+ enable_ruby #{enable_ruby}
313
+ <record>
314
+ mixed_field [{"tag":"${tag}"}]
315
+ </record>
316
+ ]
317
+ msgs = ['1', '2']
318
+ es = emit(config, use_v1, msgs)
319
+ es.each_with_index do |(tag, time, record), i|
320
+ assert_equal([{"tag" => @tag}], record['mixed_field'])
321
+ end
322
+ end
323
+
324
+ if use_v1 == true
325
+ # works with only v1 config
326
+ test "keys with placeholders with enable_ruby #{enable_ruby}" do
327
+ config = %[
328
+ tag tag
329
+ enable_ruby #{enable_ruby}
330
+ renew_record true
331
+ <record>
332
+ ${hostname} hostname
333
+ foo.${tag} tag
334
+ </record>
335
+ ]
336
+ msgs = ['1', '2']
337
+ es = emit(config, use_v1, msgs)
338
+ es.each_with_index do |(tag, time, record), i|
339
+ assert_equal({@hostname=>'hostname',"foo.#{@tag}"=>'tag'}, record)
340
+ end
341
+ end
239
342
  end
240
343
  end
241
344
 
242
- test "time with enable_ruby #{enable_ruby}" do
345
+ test 'unknown placeholder (enable_ruby no)' do
243
346
  config = %[
244
347
  tag tag
245
- enable_ruby #{enable_ruby}
348
+ enable_ruby no
246
349
  <record>
247
- message ${time}
350
+ message ${unknown}
248
351
  </record>
249
352
  ]
250
- emits = emit(config)
251
- emits.each do |(tag, time, record)|
252
- assert_equal(@time.to_s, record['message'])
253
- end
353
+ d = create_driver(config, use_v1)
354
+ mock(d.instance.log).warn("record_reformer: unknown placeholder `${unknown}` found")
355
+ d.run { d.emit({}, @time) }
356
+ assert_equal 1, d.emits.size
254
357
  end
255
358
 
256
- test "record keys with enable_ruby #{enable_ruby}" do
359
+ test 'failed to expand record field (enable_ruby yes)' do
257
360
  config = %[
258
361
  tag tag
259
- enable_ruby #{enable_ruby}
260
- remove_keys eventType0
362
+ enable_ruby yes
261
363
  <record>
262
- message bar ${message}
263
- eventtype ${eventType0}
364
+ message ${unknown['bar']}
264
365
  </record>
265
366
  ]
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'])
367
+ d = create_driver(config, use_v1)
368
+ mock(d.instance.log).warn("record_reformer: failed to expand `${unknown['bar']}`", anything)
369
+ d.run { d.emit({}, @time) }
370
+ # emit, but nil value
371
+ assert_equal 1, d.emits.size
372
+ d.emits.each do |(tag, time, record)|
373
+ assert_nil(record['message'])
272
374
  end
273
375
  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
376
 
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'])
377
+ test 'failed to expand tag (enable_ruby yes)' do
378
+ config = %[
379
+ tag ${unknown['bar']}
380
+ enable_ruby yes
381
+ ]
382
+ d = create_driver(config, use_v1)
383
+ mock(d.instance.log).warn("record_reformer: failed to expand `${unknown['bar']}`", anything)
384
+ d.run { d.emit({}, @time) }
385
+ # nil tag message should not be emitted
386
+ assert_equal 0, d.emits.size
305
387
  end
306
388
  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
389
  end
320
390
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-record-reformer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-31 00:00:00.000000000 Z
11
+ date: 2015-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fluentd
@@ -147,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
147
  version: '0'
148
148
  requirements: []
149
149
  rubyforge_project:
150
- rubygems_version: 2.2.2
150
+ rubygems_version: 2.4.5
151
151
  signing_key:
152
152
  specification_version: 4
153
153
  summary: Fluentd plugin to add or replace fields of a event record