From d33a53d1cec38132b40b1476147bf882d3ea4b91 Mon Sep 17 00:00:00 2001 From: Kenji Okimoto Date: Tue, 12 Feb 2019 10:33:57 +0900 Subject: [PATCH 1/3] Use String#sub instead of String#sub! Because String#sub! is a destructive method. Built-in in_tail plugin will configure the parse section twice. The in_tail plugin will use raw parse section at the 1st time and it will use parse section with expanded grok pattern without types. We can avoid this behavior when we stop using a destructive method to expand grok patterns. Signed-off-by: Kenji Okimoto --- lib/fluent/plugin/grok.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fluent/plugin/grok.rb b/lib/fluent/plugin/grok.rb index 5bbb78a..f493503 100644 --- a/lib/fluent/plugin/grok.rb +++ b/lib/fluent/plugin/grok.rb @@ -131,7 +131,7 @@ def expand_pattern(pattern) else replacement_pattern = "(?:#{curr_pattern})" end - pattern.sub!(m[0]) do |s| + pattern = pattern.sub(m[0]) do |s| replacement_pattern end end From b2007214c3a409ce0529d6108254e93a94e7f1fb Mon Sep 17 00:00:00 2001 From: Kenji Okimoto Date: Tue, 12 Feb 2019 10:54:36 +0900 Subject: [PATCH 2/3] test: Add group configure Signed-off-by: Kenji Okimoto --- test/test_grok_parser.rb | 50 +++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/test/test_grok_parser.rb b/test/test_grok_parser.rb index 13cdaa7..ecad553 100644 --- a/test/test_grok_parser.rb +++ b/test/test_grok_parser.rb @@ -158,39 +158,41 @@ class GrokParserTest < ::Test::Unit::TestCase end end - test "no grok patterns" do - assert_raise Fluent::ConfigError do - create_driver('') + sub_test_case "configure" do + test "no grok patterns" do + assert_raise Fluent::ConfigError do + create_driver('') + end + end + + test "invalid config value type" do + assert_raise Fluent::ConfigError do + create_driver(%[ + + pattern %{PATH:path:foo} + + ]) + end end - end - test "invalid config value type" do - assert_raise Fluent::ConfigError do - create_driver(%[ + test "invalid config value type and normal grok pattern" do + d = create_driver(%[ pattern %{PATH:path:foo} + + pattern %{IP:ip_address} + ]) + assert_equal(1, d.instance.instance_variable_get(:@grok).parsers.size) + logs = $log.instance_variable_get(:@logger).instance_variable_get(:@logdev).logs + error_logs = logs.grep(/error_class/) + assert_equal(1, error_logs.size) + error_message = error_logs.first[/error="(.+)"/, 1] + assert_equal("unknown value conversion for key:'path', type:'foo'", error_message) end end - test "invalid config value type and normal grok pattern" do - d = create_driver(%[ - - pattern %{PATH:path:foo} - - - pattern %{IP:ip_address} - - ]) - assert_equal(1, d.instance.instance_variable_get(:@grok).parsers.size) - logs = $log.instance_variable_get(:@logger).instance_variable_get(:@logdev).logs - error_logs = logs.grep(/error_class/) - assert_equal(1, error_logs.size) - error_message = error_logs.first[/error="(.+)"/, 1] - assert_equal("unknown value conversion for key:'path', type:'foo'", error_message) - end - sub_test_case "grok_name_key" do test "one grok section with name" do d = create_driver(%[ From ea1b7bcbaba8cd1d381248a648752cebf52b685b Mon Sep 17 00:00:00 2001 From: Kenji Okimoto Date: Tue, 12 Feb 2019 10:55:10 +0900 Subject: [PATCH 3/3] test: Add test about #63 Signed-off-by: Kenji Okimoto --- test/test_grok_parser.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/test_grok_parser.rb b/test/test_grok_parser.rb index ecad553..a9abf46 100644 --- a/test/test_grok_parser.rb +++ b/test/test_grok_parser.rb @@ -191,6 +191,16 @@ class GrokParserTest < ::Test::Unit::TestCase error_message = error_logs.first[/error="(.+)"/, 1] assert_equal("unknown value conversion for key:'path', type:'foo'", error_message) end + + test "keep original configuration" do + config = %[ + + pattern %{INT:user_id:integer} paid %{NUMBER:paid_amount:float} + + ] + d = create_driver(config) + assert_equal("%{INT:user_id:integer} paid %{NUMBER:paid_amount:float}", d.instance.config.elements("grok").first["pattern"]) + end end sub_test_case "grok_name_key" do