From e7fd4c13d54bf2e53289563fc34d6d75ffeea89a Mon Sep 17 00:00:00 2001 From: Mike Playle Date: Tue, 8 Dec 2020 19:17:46 +0000 Subject: [PATCH 1/2] formatter_ltsv: suppress delimiters in output Replace tabs with spaces before writing them to the output, to prevent creating malformed LTSV files. Also add test cases to verify this behaviour. Signed-off-by: Mike Playle --- lib/fluent/plugin/formatter_ltsv.rb | 3 ++- test/test_formatter.rb | 30 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/fluent/plugin/formatter_ltsv.rb b/lib/fluent/plugin/formatter_ltsv.rb index cbcab8b5e6..093572376c 100644 --- a/lib/fluent/plugin/formatter_ltsv.rb +++ b/lib/fluent/plugin/formatter_ltsv.rb @@ -27,6 +27,7 @@ class LabeledTSVFormatter < Formatter config_param :delimiter, :string, default: "\t".freeze config_param :label_delimiter, :string, default: ":".freeze + config_param :replacement, :string, default: " ".freeze config_param :add_newline, :bool, default: true # TODO: escaping for \t in values @@ -34,7 +35,7 @@ def format(tag, time, record) formatted = "" record.each do |label, value| formatted << @delimiter if formatted.length.nonzero? - formatted << "#{label}#{@label_delimiter}#{value}" + formatted << "#{label}#{@label_delimiter}#{value.to_s.gsub(@delimiter, @replacement)}" end formatted << @newline if @add_newline formatted diff --git a/test/test_formatter.rb b/test/test_formatter.rb index c2d1172ff8..ca3f7ef488 100644 --- a/test/test_formatter.rb +++ b/test/test_formatter.rb @@ -184,6 +184,36 @@ def test_format_with_customized_delimiters assert_equal("message=awesome,greeting=hello#{@newline}", formatted) end + + def record_with_tab + {'message' => "awe\tsome", 'greeting' => "hello\t"} + end + + def test_format_suppresses_tab + @formatter.configure({}) + formatted = @formatter.format(tag, @time, record_with_tab) + + assert_equal("message:awe some\tgreeting:hello \n", formatted) + end + + def test_format_suppresses_tab_custom_replacement + @formatter.configure( + 'replacement' => 'X', + ) + formatted = @formatter.format(tag, @time, record_with_tab) + + assert_equal("message:aweXsome\tgreeting:helloX\n", formatted) + end + + def test_format_suppresses_custom_delimiter + @formatter.configure( + 'delimiter' => 'w', + 'label_delimiter' => '=', + ) + formatted = @formatter.format(tag, @time, record) + + assert_equal("message=a esomewgreeting=hello\n", formatted) + end end class CsvFormatterTest < ::Test::Unit::TestCase From 7d94ae0c5ac39e16993485bf27d2e87f7cd1dc68 Mon Sep 17 00:00:00 2001 From: Takuro Ashie Date: Mon, 8 Mar 2021 18:22:30 +0900 Subject: [PATCH 2/2] Remove an obsolete comment Signed-off-by: Takuro Ashie --- lib/fluent/plugin/formatter_ltsv.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/fluent/plugin/formatter_ltsv.rb b/lib/fluent/plugin/formatter_ltsv.rb index 093572376c..76100bb91b 100644 --- a/lib/fluent/plugin/formatter_ltsv.rb +++ b/lib/fluent/plugin/formatter_ltsv.rb @@ -30,7 +30,6 @@ class LabeledTSVFormatter < Formatter config_param :replacement, :string, default: " ".freeze config_param :add_newline, :bool, default: true - # TODO: escaping for \t in values def format(tag, time, record) formatted = "" record.each do |label, value|