Skip to content

Commit eb3ff40

Browse files
committed
out_exec_filter: separated {tag_key,time_key,time_format} to {in_,out_}{tag_key,time_key,time_format}
1 parent 264b03a commit eb3ff40

2 files changed

Lines changed: 57 additions & 21 deletions

File tree

lib/fluent/plugin/out_exec_filter.rb

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ def initialize
4444
config_param :in_keys, :default => [] do |val|
4545
val.split(',')
4646
end
47+
config_param :in_tag_key, :default => nil
48+
config_param :in_time_key, :default => nil
49+
config_param :in_time_format, :default => nil
4750

4851
config_param :out_format, :default => :tsv do |val|
4952
f = SUPPORTED_FORMAT[val]
@@ -53,9 +56,11 @@ def initialize
5356
config_param :out_keys, :default => [] do |val| # for tsv format
5457
val.split(',')
5558
end
59+
config_param :out_tag_key, :default => nil
60+
config_param :out_time_key, :default => nil
61+
config_param :out_time_format, :default => nil
5662

5763
config_param :tag, :string, :default => nil
58-
config_param :tag_key, :string, :default => nil
5964

6065
config_param :time_key, :string, :default => nil
6166
config_param :time_format, :string, :default => nil
@@ -66,6 +71,24 @@ def initialize
6671
config_set_default :flush_interval, 1
6772

6873
def configure(conf)
74+
if tag_key = conf['tag_key']
75+
# TODO obsoleted?
76+
@in_tag_key = tag_key
77+
@out_tag_key = tag_key
78+
end
79+
80+
if time_key = conf['time_key']
81+
# TODO obsoleted?
82+
@in_time_key = time_key
83+
@out_time_key = time_key
84+
end
85+
86+
if time_format = conf['time_format']
87+
# TODO obsoleted?
88+
@in_time_format = time_format
89+
@out_time_format = time_format
90+
end
91+
6992
super
7093

7194
if localtime = conf['localtime']
@@ -74,20 +97,29 @@ def configure(conf)
7497
@localtime = false
7598
end
7699

77-
if !@tag && !@tag_key
78-
raise ConfigError, "'tag' or 'tag_key' option is required on exec_filter output"
100+
if !@tag && !@out_tag_key
101+
raise ConfigError, "'tag' or 'out_tag_key' option is required on exec_filter output"
79102
end
80103

81-
if @time_key
82-
if @time_format
83-
f = @time_format
104+
if @in_time_key
105+
if f = @in_time_format
84106
tf = TimeFormatter.new(f, @localtime)
85107
@time_format_proc = tf.method(:format)
86-
@time_parse_proc = Proc.new {|str| Time.strptime(str, f).to_i }
87108
else
88109
@time_format_proc = Proc.new {|time| time.to_s }
110+
end
111+
elsif @in_time_format
112+
$log.warn "in_time_format effects nothing when in_time_key is not specified: #{conf}"
113+
end
114+
115+
if @out_time_key
116+
if f = @out_time_format
117+
@time_parse_proc = Proc.new {|str| Time.strptime(str, f).to_i }
118+
else
89119
@time_parse_proc = Proc.new {|str| str.to_i }
90120
end
121+
elsif @out_time_format
122+
$log.warn "out_time_format effects nothing when out_time_key is not specified: #{conf}"
91123
end
92124

93125
if @remove_prefix
@@ -164,11 +196,11 @@ def format_stream(tag, es)
164196
out = ''
165197

166198
es.each {|time,record|
167-
if @time_key
168-
record[@time_key] = @time_format_proc.call(time)
199+
if @in_time_key
200+
record[@in_time_key] = @time_format_proc.call(time)
169201
end
170-
if @tag_key
171-
record[@tag_key] = tag
202+
if @in_tag_key
203+
record[@in_tag_key] = tag
172204
end
173205
@formatter.call(record, out)
174206
}
@@ -267,13 +299,13 @@ def call(record, out)
267299
end
268300

269301
def on_message(record)
270-
if val = record.delete(@time_key)
302+
if val = record.delete(@out_time_key)
271303
time = @time_parse_proc.call(val)
272304
else
273305
time = Engine.now
274306
end
275307

276-
if val = record.delete(@tag_key)
308+
if val = record.delete(@out_tag_key)
277309
tag = if @add_prefix
278310
@added_prefix_string + val
279311
else

test/plugin/out_exec_filter.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ def setup
88

99
CONFIG = %[
1010
command cat
11-
in_keys time,tag,k1
12-
out_keys time,tag,k2
11+
in_keys time_in,tag,k1
12+
out_keys time_out,tag,k2
1313
tag_key tag
14-
time_key time
14+
in_time_key time_in
15+
out_time_key time_out
1516
time_format %Y-%m-%d %H:%M:%S
1617
localtime
1718
num_children 3
@@ -24,11 +25,14 @@ def create_driver(conf = CONFIG, tag = 'test')
2425
def test_configure
2526
d = create_driver
2627

27-
assert_equal ["time","tag","k1"], d.instance.in_keys
28-
assert_equal ["time","tag","k2"], d.instance.out_keys
29-
assert_equal "tag", d.instance.tag_key
30-
assert_equal "time", d.instance.time_key
31-
assert_equal "%Y-%m-%d %H:%M:%S", d.instance.time_format
28+
assert_equal ["time_in","tag","k1"], d.instance.in_keys
29+
assert_equal ["time_out","tag","k2"], d.instance.out_keys
30+
assert_equal "tag", d.instance.out_tag_key
31+
assert_equal "tag", d.instance.in_tag_key
32+
assert_equal "time_in", d.instance.in_time_key
33+
assert_equal "time_out", d.instance.out_time_key
34+
assert_equal "%Y-%m-%d %H:%M:%S", d.instance.in_time_format
35+
assert_equal "%Y-%m-%d %H:%M:%S", d.instance.out_time_format
3236
assert_equal true, d.instance.localtime
3337
assert_equal 3, d.instance.num_children
3438

0 commit comments

Comments
 (0)