Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to enable writing to new lines for tsv formatter #1691

Merged
merged 6 commits into from
Sep 13, 2017
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
6 changes: 5 additions & 1 deletion lib/fluent/plugin/formatter_tsv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ class TSVFormatter < Formatter
config_param :keys, :array, value_type: :string
desc 'The delimiter character (or string) of TSV values'
config_param :delimiter, :string, default: "\t"
desc 'The parameter to enable writing to new lines'
config_param :add_newline, :bool, default: true

def format(tag, time, record)
@keys.map{|k| record[k].to_s }.join(@delimiter)
formatted = @keys.map{|k| record[k].to_s }.join(@delimiter)
formatted << "\n".freeze if @add_newline
formatted
end
end
end
Expand Down
68 changes: 68 additions & 0 deletions test/plugin/test_formatter_tsv.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require_relative '../helper'
require 'fluent/test/driver/formatter'
require 'fluent/plugin/formatter_tsv'

class TSVFormatterTest < ::Test::Unit::TestCase
def setup
@time = event_time
end

def create_driver(conf = "")
Fluent::Test::Driver::Formatter.new(Fluent::Plugin::TSVFormatter).configure(conf)
end

def tag
"tag"
end

def record
{'message' => 'awesome', 'greeting' => 'hello'}
end

def test_config_params
d = create_driver(
'keys' => 'message,greeting',
)
assert_equal ["message", "greeting"], d.instance.keys
assert_equal "\t", d.instance.delimiter
assert_equal true, d.instance.add_newline

d = create_driver(
'keys' => 'message,greeting',
'delimiter' => ',',
'add_newline' => false,
)
assert_equal ["message", "greeting"], d.instance.keys
assert_equal ",", d.instance.delimiter
assert_equal false, d.instance.add_newline
end

def test_format
d = create_driver(
'keys' => 'message,greeting',
)
formatted = d.instance.format(tag, @time, record)

assert_equal("awesome\thello\n", formatted)
end

def test_format_without_newline
d = create_driver(
'keys' => 'message,greeting',
'add_newline' => false,
)
formatted = d.instance.format(tag, @time, record)

assert_equal("awesome\thello", formatted)
end

def test_format_with_customized_delimiters
d = create_driver(
'keys' => 'message,greeting',
'delimiter' => ',',
)
formatted = d.instance.format(tag, @time, record)

assert_equal("awesome,hello\n", formatted)
end
end