diff --git a/lib/fluent/plugin/parser_grok.rb b/lib/fluent/plugin/parser_grok.rb index 7e374f4..b8a53da 100644 --- a/lib/fluent/plugin/parser_grok.rb +++ b/lib/fluent/plugin/parser_grok.rb @@ -60,12 +60,16 @@ def configure(conf={}) end @grok.setup + + if (parser = @grok.parsers[:grok_pattern]) + @default_parser.estimate_current_event = parser.estimate_current_event + end end def parse(text) @grok.parsers.each do |name_or_index, parser| time, record = parse_by(parser, text) - if time and record + if record record[@grok_name_key] = name_or_index if @grok_name_key yield time, record return diff --git a/test/test_grok_parser.rb b/test/test_grok_parser.rb index a625116..4acc481 100644 --- a/test/test_grok_parser.rb +++ b/test/test_grok_parser.rb @@ -180,6 +180,36 @@ class GrokParserTest < ::Test::Unit::TestCase end end + sub_test_case "estimate_current_event" do + test "false keeps the matched record instead of falling back" do + config = %[ + estimate_current_event false + grok_failure_key grok_failure + grok_pattern %{IP:ip_address} + ] + d = create_driver(config) + parsed = [] + d.instance.parse("127.0.0.1") do |time, record| + parsed << [time, record] + end + assert_equal([[nil, {"ip_address" => "127.0.0.1"}]], parsed) + end + + test "false leaves the time of an unmatched line nil" do + config = %[ + estimate_current_event false + grok_failure_key grok_failure + grok_pattern %{IP:ip_address} + ] + d = create_driver(config) + parsed = [] + d.instance.parse("not-an-ip") do |time, record| + parsed << [time, record] + end + assert_equal([[nil, {"message" => "not-an-ip", "grok_failure" => "No grok pattern matched"}]], parsed) + end + end + sub_test_case "configure" do test "no grok patterns" do assert_raise Fluent::ConfigError do