Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 1881996: skip nil keys in json filter #1995

Merged
merged 1 commit into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions fluentd/lib/filter_parse_json_field/lib/filter_parse_json_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def filter_stream(tag, es)
def do_merge_json_log(record)
json_fields.each do |merge_json_log_key|
if record.has_key?(merge_json_log_key)
value = record[merge_json_log_key].strip
if value[0].eql?('{') && value[-1].eql?('}')
value = (record[merge_json_log_key] || "").strip
if value.start_with?('{') && value.end_with?('}')
begin
record = JSON.parse(value).merge(record)
unless @preserve_json_log
Expand All @@ -72,5 +72,22 @@ def do_merge_json_log(record)
record
end

def do_replace_json_log(record)
json_fields.each do |merge_json_log_key|
if record.has_key?(merge_json_log_key)
value = (record[merge_json_log_key] || "").strip
if value.start_with?('{') && value.end_with?('}')
begin
parsed_value = JSON.parse(value)
record[merge_json_log_key] = parsed_value
rescue JSON::ParserError
log.debug "parse_json_field could not parse field [#{merge_json_log_key}] as JSON: value [#{value}]"
end
end
break
end
end
record
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,16 @@ def emit_with_tag(tag, msg={}, conf='')
debugit(@driver, 'in test')
assert_match /\[debug\]: parse_json_field could not parse field \[skip1\] as JSON: value \[\{"bogusvalue\}\]/, @logs[0]
end
test 'fallback if given field is nil' do
# test that - skip1 is skipped because it has a nil key
json_string_val2 = '{"k":{"b":"c"},"l":["e","f"],"m":97,"n":{"i":"j"}}'
orig_a_value = 'orig a value'
rec = emit_with_tag('tag', {'skip1'=>nil, 'a'=>orig_a_value, 'jsonfield'=>json_string_val2},'
merge_json_log true
json_fields skip1,jsonfield
')
assert_equal({'skip1'=>nil, 'a'=>orig_a_value, 'jsonfield'=>json_string_val2}, rec)
debugit(@driver, 'in test')
end
end
end