Skip to content

Commit

Permalink
Merge pull request #1433 from fluent/move-remove-keys-to-last
Browse files Browse the repository at this point in the history
record_transformer: remove_keys processing should be last
  • Loading branch information
repeatedly committed Feb 1, 2017
1 parent f57a6aa commit c66ef65
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/fluent/plugin/filter_record_transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,13 @@ def filter_stream(tag, es)
last_record = nil
es.each do |time, record|
last_record = record # for debug log
placeholder_values.merge!({
'time' => @placeholder_expander.time_value(time),
'record' => record,
})
placeholder_values['time'] = @placeholder_expander.time_value(time)
placeholder_values['record'] = record
new_record = reform(record, placeholder_values)
if @renew_time_key && new_record.has_key?(@renew_time_key)
time = new_record[@renew_time_key].to_i
end
@remove_keys.each { |k| new_record.delete(k) } if @remove_keys
new_es.add(time, new_record)
end
new_es
Expand Down Expand Up @@ -131,7 +130,6 @@ def reform(record, placeholder_values)
new_record = @renew_record ? {} : record.dup
@keep_keys.each {|k| new_record[k] = record[k]} if @keep_keys and @renew_record
new_record.merge!(expand_placeholders(@map, placeholders))
@remove_keys.each {|k| new_record.delete(k) } if @remove_keys

new_record
end
Expand Down
19 changes: 19 additions & 0 deletions test/plugin/test_filter_record_transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ def emit(config, msgs = [''])
es = emit(config, msgs)
es.each_with_index do |(time, record), i|
assert_equal(times[i].to_i, time)
assert_true(record.has_key?('message'))
end
end

test 'renew_time_key and remove_keys' do
config = %[
renew_time_key event_time_key
remove_keys event_time_key
auto_typecast true
<record>
event_time_key ${record["message"]}
</record>
]
times = [Time.local(2, 2, 3, 4, 5, 2010, nil, nil, nil, nil), Time.local(3, 2, 3, 4, 5, 2010, nil, nil, nil, nil)]
msgs = times.map { |t| t.to_f.to_s }
filtered = emit(config, msgs)
filtered.each_with_index do |(time, _record), i|
assert_equal(times[i].to_i, time)
assert_false(_record.has_key?('event_time_key'))
end
end

Expand Down

0 comments on commit c66ef65

Please sign in to comment.