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
14 changes: 13 additions & 1 deletion lib/fluent/plugin/parser_multiline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ class MultilineParser < Parser

class MultilineRegexpParser < Fluent::Plugin::RegexpParser
def parse(text)
m = @expression.match(text)
m = begin
@expression.match(text)
rescue Encoding::CompatibilityError
utf8_text = RegexpParser.utf8_text_for_retry(@expression, text)
raise unless utf8_text

@expression.match(utf8_text)
end
unless m
yield nil, nil
return m
Expand Down Expand Up @@ -105,6 +112,11 @@ def has_firstline?

def firstline?(text)
@firstline_regex.match?(text)
rescue Encoding::CompatibilityError
utf8_text = RegexpParser.utf8_text_for_retry(@firstline_regex, text)
raise unless utf8_text

@firstline_regex.match?(utf8_text)
end

private
Expand Down
15 changes: 11 additions & 4 deletions lib/fluent/plugin/parser_regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,21 @@ def configure(conf)
end
end

# Returns a UTF-8 copy of text when regexp is UTF-8 and text is a
# binary string with valid UTF-8 bytes. Returns nil otherwise.
def self.utf8_text_for_retry(regexp, text)
return nil unless text.encoding == Encoding::ASCII_8BIT && regexp.encoding == Encoding::UTF_8

utf8_text = text.dup.force_encoding(Encoding::UTF_8)
utf8_text if utf8_text.valid_encoding?
end

def parse(text)
m = begin
@expression.match(text)
rescue Encoding::CompatibilityError
raise unless text.encoding == Encoding::ASCII_8BIT && @expression.encoding == Encoding::UTF_8

utf8_text = text.dup.force_encoding(Encoding::UTF_8)
raise unless utf8_text.valid_encoding?
utf8_text = RegexpParser.utf8_text_for_retry(@expression, text)
raise unless utf8_text

@expression.match(utf8_text)
end
Expand Down
41 changes: 41 additions & 0 deletions test/plugin/test_in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2417,6 +2417,47 @@ def test_multiline_without_firstline(data)
assert_equal({"var1" => "foo 1", "var2" => "bar 1", "var3" => "baz 1"}, events[0][2])
assert_equal({"var1" => "foo 2", "var2" => "bar 2", "var3" => "baz 2"}, events[1][2])
end

data(
'utf8 format_firstline' => [
'/^\\p{Alpha}+:/', '/^(?<key>\\w+):(?<value>.*)/',
# The event with the non-ASCII line does not match format1 and is dropped.
[{ "key" => "a", "value" => "1" }, { "key" => "c", "value" => "3" }, { "key" => "d", "value" => "4" }]
],
'utf8 format1' => [
'/^\\w+:/', '/^(?<key>\\p{Alpha}+):(?<value>.*)/',
[{ "key" => "a", "value" => "1\ncaf\u00e9:2\n continued" }, { "key" => "c", "value" => "3" }, { "key" => "d", "value" => "4" }]
],
'utf8 format_firstline and format1' => [
'/^\\p{Alpha}+:/', '/^(?<key>\\p{Alpha}+):(?<value>.*)/',
[{ "key" => "a", "value" => "1" }, { "key" => "caf\u00e9", "value" => "2\n continued" }, { "key" => "c", "value" => "3" }, { "key" => "d", "value" => "4" }]
])
def test_multiline_utf8_regexp_with_non_ascii_line(data)
format_firstline, format1, expected = data
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "wb") { |f|
f.write "a:1\ncaf\xC3\xA9:2\n continued\nc:3\n".b
}

config = config_element("", "", {
"read_from_head" => "true",
"multiline_flush_interval" => "1s",
}, [
config_element("parse", "", {
"@type" => "multiline",
"format_firstline" => format_firstline,
"format1" => format1,
})
])
d = create_driver(config)
d.run(expect_records: expected.size, timeout: 10) do
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "ab") { |f|
f.write "d:4\n"
}
end

assert_false(d.broken?)
assert_equal(expected, d.events.map { |e| e[2] })
end
end

sub_test_case "path" do
Expand Down
29 changes: 29 additions & 0 deletions test/plugin/test_parser_multiline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,35 @@ def test_parse_with_keep_time_key
}
end

def test_firstline_utf8_regexp_with_binary_text
parser = create_parser('format_firstline' => '/^\p{Alpha}+:/', 'format1' => '/^(?<key>\w+):(?<value>.*)/')
assert(parser.instance.firstline?("caf\u00e9:2".b))
assert(parser.instance.firstline?("a:1".b))
assert_false(parser.instance.firstline?(" continued".b))
end

def test_firstline_utf8_regexp_with_text_not_utf8
parser = create_parser('format_firstline' => '/^\p{Alpha}+:/', 'format1' => '/^(?<key>\w+):(?<value>.*)/')
assert_raise(Encoding::CompatibilityError) do
parser.instance.firstline?("\xff:1".b)
end
end

def test_parse_utf8_regexp_with_binary_text
parser = create_parser('format_firstline' => '/^\w+:/', 'format1' => '/^(?<key>\p{Alpha}+):(?<value>.*)/')
r = []
parser.instance.parse("caf\u00e9:2\n continued".b) { |_, record| r << record }
assert_equal [{ 'key' => "caf\u00e9", 'value' => "2\n continued" }], r
assert_equal Encoding::UTF_8, r[0]['key'].encoding
end

def test_parse_utf8_regexp_with_text_not_utf8
parser = create_parser('format1' => '/^(?<key>\p{Alpha}+):(?<value>.*)/')
assert_raise(Encoding::CompatibilityError) do
parser.instance.parse("\xff:1".b) { |_, _| }
end
end

def test_parse_unmatched_lines
parser = Fluent::Test::Driver::Parser.new(Fluent::Plugin::MultilineParser).configure(
'format1' => '/^message (?<message_id>\d)/',
Expand Down
Loading