From bf7c1b85c54a8ec799cf418bd343756cde0c2fc6 Mon Sep 17 00:00:00 2001 From: Daijiro Fukuda Date: Tue, 30 Apr 2024 14:39:10 +0900 Subject: [PATCH] filter_parser: add error event for multiple parsed results It would be not happy if some events are lost without any warnings or errors. Signed-off-by: Daijiro Fukuda --- lib/fluent/plugin/filter_parser.rb | 38 ++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/lib/fluent/plugin/filter_parser.rb b/lib/fluent/plugin/filter_parser.rb index 52909d0171..6998a38286 100644 --- a/lib/fluent/plugin/filter_parser.rb +++ b/lib/fluent/plugin/filter_parser.rb @@ -70,6 +70,13 @@ def filter_with_time(tag, time, record) end end begin + # Note: https://github.com/fluent/fluentd/issues/4100 + # If the parser returns multiple records from one raw_value, + # this returns only the first one record. + # This should be fixed in the future version. + result_time = nil + result_record = nil + @parser.parse(raw_value) do |t, values| if values t = if @reserve_time @@ -79,24 +86,31 @@ def filter_with_time(tag, time, record) end @accessor.delete(record) if @remove_key_name_field r = handle_parsed(tag, record, t, values) - # Note: https://github.com/fluent/fluentd/issues/4100 - # If the parser returns multiple records from one raw_value, - # this returns only the first one record. - # This should be fixed in the future version. - return t, r + + if result_record.nil? + result_time = t + result_record = r + else + if @emit_invalid_record_to_error + router.emit_error_event(tag, t, r, Fluent::Plugin::Parser::ParserError.new( + "Could not emit the event. The parser returned multiple results, but currently filter_parser plugin only returns the first parsed result. Raw data: '#{raw_value}'" + )) + end + end else if @emit_invalid_record_to_error router.emit_error_event(tag, time, record, Fluent::Plugin::Parser::ParserError.new("pattern not matched with data '#{raw_value}'")) end - if @reserve_data - t = time - r = handle_parsed(tag, record, time, {}) - return t, r - else - return FAILED_RESULT - end + + next unless @reserve_data + next unless result_record.nil? + + result_time = time + result_record = handle_parsed(tag, record, time, {}) end end + + return result_time, result_record rescue Fluent::Plugin::Parser::ParserError => e if @emit_invalid_record_to_error raise e