Skip to content
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
4 changes: 4 additions & 0 deletions lib/fluent/plugin_helper/record_accessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def call(r)
# See https://gist.github.com/repeatedly/ab553ed260cd080bd01ec71da9427312
def call_dig(r)
r.dig(*@keys)
rescue TypeError
# Treat TypeError from dig as a missing key, e.g. when looking up
# a field on a string or using a string as an array index.
nil
end

def delete(r)
Expand Down
39 changes: 39 additions & 0 deletions test/plugin/test_filter_grep.rb
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,45 @@ def filter(config, msgs)
}
end
end

# A value on the way to the nested key can be a string, a number, or an array
def messages_with_non_hash_parent
messages + ["plain text", 42, ["WARN"]]
end

test 'regexps drops records whose parent of the nested key is not a hash' do
d = create_driver(%[
<regexp>
key $.message.nest1.nest2
pattern WARN
</regexp>
])
d.run {
messages_with_non_hash_parent.each { |msg|
d.feed("filter.test", @time, {'foo' => 'bar', 'message' => msg})
}
}
assert_equal([{"nest1" => {"nest2" => "WARN"}}] * 2,
d.filtered_records.map { |r| r['message'] })
assert_equal([], d.logs.select { |log| log.include?("failed to grep events") })
end

test 'excludes keeps records whose parent of the nested key is not a hash' do
d = create_driver(%[
<exclude>
key $.message.nest1.nest2
pattern WARN
</exclude>
])
d.run {
messages_with_non_hash_parent.each { |msg|
d.feed("filter.test", @time, {'foo' => 'bar', 'message' => msg})
}
}
assert_equal([{"nest1" => {"nest2" => "INFO"}}, "plain text", 42, ["WARN"]],
d.filtered_records.map { |r| r['message'] })
assert_equal([], d.logs.select { |log| log.include?("failed to grep events") })
end
end

sub_test_case 'grep non-string jsonable values' do
Expand Down
10 changes: 10 additions & 0 deletions test/plugin_helper/test_record_accessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ class Dummy < Fluent::Plugin::TestBase
assert_equal 'value', accessor.call(r)
end

data('string' => {'key1' => {'key2' => 'value'}},
'integer' => {'key1' => {'key2' => 1}},
'array' => {'key1' => {'key2' => ['value']}})
test "don't raise an error when a parent of the nested key is not a hash" do |r|
accessor = @d.record_accessor_create('$.key1.key2.key3')
assert_nothing_raised do
assert_nil accessor.call(r)
end
end

data("missing ']'" => "$['key1'",
"missing array index with dot" => "$.hello[]",
"missing array index with bracket" => "$['hello'][]",
Expand Down
Loading