fluent-plugin-record-reformer 0.8.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/fluent-plugin-record-reformer.gemspec +1 -1
- data/lib/fluent/plugin/out_record_reformer.rb +35 -36
- data/test/test_out_record_reformer.rb +18 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c12a8d7d13aa7b7cd2774128c13b1ce7ed4473a
|
4
|
+
data.tar.gz: 7bf4dd5a13506bd3d9e89acf739d27322bb737e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54d23d63dca07dad339103db47a0b7374ef6aef190f4d8e64b984a625ac40b1e158a46f2c9b6aa8c735f1701a19fe0fc16418ba39d4e06052307756d1e0f96b3
|
7
|
+
data.tar.gz: fda86acdd87b031272f229b8162a2b2b77caa07788e0db8076fb9e84f8cb74ab865aa6a15be579928aba42e38c904b7ae5eeddabb377596a50dbcf96b9a31e40
|
data/CHANGELOG.md
CHANGED
@@ -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.8.
|
6
|
+
gem.version = "0.8.1"
|
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"
|
@@ -139,30 +139,31 @@ module Fluent
|
|
139
139
|
end
|
140
140
|
|
141
141
|
def reform(tag, record, placeholder_values)
|
142
|
-
@placeholder_expander.prepare_placeholders(placeholder_values)
|
142
|
+
placeholders = @placeholder_expander.prepare_placeholders(placeholder_values)
|
143
143
|
|
144
|
-
new_tag = expand_placeholders(tag)
|
144
|
+
new_tag = expand_placeholders(tag, placeholders)
|
145
145
|
|
146
146
|
new_record = @renew_record ? {} : record.dup
|
147
147
|
@keep_keys.each {|k| new_record[k] = record[k]} if @keep_keys and @renew_record
|
148
|
-
new_record.merge!(expand_placeholders(@map))
|
148
|
+
new_record.merge!(expand_placeholders(@map, placeholders))
|
149
149
|
@remove_keys.each {|k| new_record.delete(k) } if @remove_keys
|
150
150
|
|
151
151
|
[new_tag, new_record]
|
152
152
|
end
|
153
153
|
|
154
|
-
def expand_placeholders(value)
|
154
|
+
def expand_placeholders(value, placeholders)
|
155
155
|
if value.is_a?(String)
|
156
|
-
new_value = @placeholder_expander.expand(value)
|
156
|
+
new_value = @placeholder_expander.expand(value, placeholders)
|
157
157
|
elsif value.is_a?(Hash)
|
158
158
|
new_value = {}
|
159
159
|
value.each_pair do |k, v|
|
160
|
-
|
160
|
+
new_key = @placeholder_expander.expand(k, placeholders, true)
|
161
|
+
new_value[new_key] = expand_placeholders(v, placeholders)
|
161
162
|
end
|
162
163
|
elsif value.is_a?(Array)
|
163
164
|
new_value = []
|
164
165
|
value.each_with_index do |v, i|
|
165
|
-
new_value[i] = expand_placeholders(v)
|
166
|
+
new_value[i] = expand_placeholders(v, placeholders)
|
166
167
|
end
|
167
168
|
else
|
168
169
|
new_value = value
|
@@ -189,6 +190,7 @@ module Fluent
|
|
189
190
|
rev_tag_suffix.reverse!
|
190
191
|
end
|
191
192
|
|
193
|
+
# THIS CLASS MUST BE THREAD-SAFE
|
192
194
|
class PlaceholderExpander
|
193
195
|
attr_reader :placeholders, :log
|
194
196
|
|
@@ -225,36 +227,37 @@ module Fluent
|
|
225
227
|
end
|
226
228
|
end
|
227
229
|
|
228
|
-
|
230
|
+
placeholders
|
229
231
|
end
|
230
232
|
|
231
233
|
# Expand string with placeholders
|
232
234
|
#
|
233
235
|
# @param [String] str
|
234
236
|
# @param [Boolean] force_stringify the value must be string, used for hash key
|
235
|
-
def expand(str, force_stringify = false)
|
237
|
+
def expand(str, placeholders, force_stringify = false)
|
236
238
|
if @auto_typecast and !force_stringify
|
237
239
|
single_placeholder_matched = str.match(/\A(\${[^}]+}|__[A-Z_]+__)\z/)
|
238
240
|
if single_placeholder_matched
|
239
|
-
log_if_unknown_placeholder($1)
|
240
|
-
return
|
241
|
+
log_if_unknown_placeholder($1, placeholders)
|
242
|
+
return placeholders[single_placeholder_matched[1]]
|
241
243
|
end
|
242
244
|
end
|
243
245
|
str.gsub(/(\${[^}]+}|__[A-Z_]+__)/) {
|
244
|
-
log_if_unknown_placeholder($1)
|
245
|
-
|
246
|
+
log_if_unknown_placeholder($1, placeholders)
|
247
|
+
placeholders[$1]
|
246
248
|
}
|
247
249
|
end
|
248
250
|
|
249
251
|
private
|
250
252
|
|
251
|
-
def log_if_unknown_placeholder(placeholder)
|
252
|
-
unless
|
253
|
+
def log_if_unknown_placeholder(placeholder, placeholders)
|
254
|
+
unless placeholders.include?(placeholder)
|
253
255
|
log.warn "record_reformer: unknown placeholder `#{placeholder}` found"
|
254
256
|
end
|
255
257
|
end
|
256
258
|
end
|
257
259
|
|
260
|
+
# THIS CLASS MUST BE THREAD-SAFE
|
258
261
|
class RubyPlaceholderExpander
|
259
262
|
attr_reader :log
|
260
263
|
|
@@ -276,12 +279,13 @@ module Fluent
|
|
276
279
|
new_value = nil
|
277
280
|
if value.is_a?(String)
|
278
281
|
if @auto_typecast and !force_stringify
|
279
|
-
|
280
|
-
|
282
|
+
num_placeholders = value.scan('${').size
|
283
|
+
if num_placeholders == 1 and value.start_with?('${') && value.end_with?('}')
|
284
|
+
new_value = value[2..-2] # ${..} => ..
|
281
285
|
end
|
282
286
|
end
|
283
287
|
unless new_value
|
284
|
-
new_value = %Q
|
288
|
+
new_value = "%Q[#{value.gsub('${', '#{')}]" # xx${..}xx => %Q[xx#{..}xx]
|
285
289
|
end
|
286
290
|
elsif value.is_a?(Hash)
|
287
291
|
new_value = {}
|
@@ -300,28 +304,22 @@ module Fluent
|
|
300
304
|
end
|
301
305
|
|
302
306
|
def prepare_placeholders(placeholder_values)
|
303
|
-
|
304
|
-
@time = placeholder_values['time']
|
305
|
-
@record = placeholder_values['record']
|
306
|
-
@tag_parts = placeholder_values['tag_parts']
|
307
|
-
@tag_prefix = placeholder_values['tag_prefix']
|
308
|
-
@tag_suffix = placeholder_values['tag_suffix']
|
309
|
-
@hostname = placeholder_values['hostname']
|
307
|
+
placeholder_values
|
310
308
|
end
|
311
309
|
|
312
310
|
# Expand string with placeholders
|
313
311
|
#
|
314
312
|
# @param [String] str
|
315
|
-
def expand(str, force_stringify = false)
|
313
|
+
def expand(str, placeholders, force_stringify = false)
|
316
314
|
@cleanroom_expander.expand(
|
317
315
|
str,
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
316
|
+
placeholders['tag'],
|
317
|
+
placeholders['time'],
|
318
|
+
placeholders['record'],
|
319
|
+
placeholders['tag_parts'],
|
320
|
+
placeholders['tag_prefix'],
|
321
|
+
placeholders['tag_suffix'],
|
322
|
+
placeholders['hostname'],
|
325
323
|
)
|
326
324
|
rescue => e
|
327
325
|
log.warn "record_reformer: failed to expand `#{str}`", :error_class => e.class, :error => e.message
|
@@ -332,15 +330,16 @@ module Fluent
|
|
332
330
|
class CleanroomExpander
|
333
331
|
def expand(__str_to_eval__, tag, time, record, tag_parts, tag_prefix, tag_suffix, hostname)
|
334
332
|
tags = tag_parts # for old version compatibility
|
335
|
-
|
333
|
+
Thread.current[:record_reformer_record] = record # for old version compatibility
|
336
334
|
instance_eval(__str_to_eval__)
|
337
335
|
end
|
338
336
|
|
339
337
|
# for old version compatibility
|
340
338
|
def method_missing(name)
|
341
339
|
key = name.to_s
|
342
|
-
|
343
|
-
|
340
|
+
record = Thread.current[:record_reformer_record]
|
341
|
+
if record.has_key?(key)
|
342
|
+
record[key]
|
344
343
|
else
|
345
344
|
raise NameError, "undefined local variable or method `#{key}'"
|
346
345
|
end
|
@@ -554,6 +554,24 @@ EOC
|
|
554
554
|
end
|
555
555
|
end
|
556
556
|
|
557
|
+
# https://github.com/sonots/fluent-plugin-record-reformer/issues/35
|
558
|
+
test 'auto_typecast placeholder containing {} (enable_ruby yes)' do
|
559
|
+
config = %[
|
560
|
+
tag tag
|
561
|
+
enable_ruby yes
|
562
|
+
auto_typecast yes
|
563
|
+
<record>
|
564
|
+
foo ${record.map{|k,v|v}}
|
565
|
+
</record>
|
566
|
+
]
|
567
|
+
d = create_driver(config, use_v1)
|
568
|
+
message = {"@timestamp" => "foo"}
|
569
|
+
d.run { d.emit(message, @time) }
|
570
|
+
d.emits.each do |(tag, time, record)|
|
571
|
+
assert_equal [message["@timestamp"]], record["foo"]
|
572
|
+
end
|
573
|
+
end
|
574
|
+
|
557
575
|
test 'expand fields starting with @ (enable_ruby yes)' do
|
558
576
|
config = %[
|
559
577
|
tag tag
|
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.8.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Naotoshi Seo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fluentd
|