diff --git a/lib/fluent/plugin/out_sumologic.rb b/lib/fluent/plugin/out_sumologic.rb index e4b6ed2..450a181 100644 --- a/lib/fluent/plugin/out_sumologic.rb +++ b/lib/fluent/plugin/out_sumologic.rb @@ -293,6 +293,19 @@ def sumo_timestamp(time) time.to_s.length == 13 ? time : time * 1000 end + # Convert log to string and strip it + def log_to_str(log) + if log.is_a?(Array) or log.is_a?(Hash) + log = Yajl.dump(log) + end + + unless log.nil? + log.strip! + end + + return log + end + # This method is called every flush interval. Write the buffer chunk def write(chunk) messages_list = {} @@ -313,10 +326,7 @@ def write(chunk) when 'logs' case log_format when 'text' - log = record[@log_key] - unless log.nil? - log.strip! - end + log = log_to_str(record[@log_key]) when 'json_merge' if @add_timestamp record = { @timestamp_key => sumo_timestamp(time) }.merge(record) @@ -334,10 +344,7 @@ def write(chunk) log = dump_log(record) end when 'metrics' - log = record[@log_key] - unless log.nil? - log.strip! - end + log = log_to_str(record[@log_key]) end unless log.nil? diff --git a/test/plugin/test_out_sumologic.rb b/test/plugin/test_out_sumologic.rb index c6822a9..b4d85cb 100644 --- a/test/plugin/test_out_sumologic.rb +++ b/test/plugin/test_out_sumologic.rb @@ -671,4 +671,46 @@ def test_emit_json_merge_timestamp_compress_gzip times:1 end -end \ No newline at end of file + def test_emit_text_from_array + config = %{ + endpoint https://collectors.sumologic.com/v1/receivers/http/1234 + log_format text + source_category test + source_host test + source_name test + + } + driver = create_driver(config) + time = event_time + stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') + driver.run do + driver.feed("output.test", time, {'foo' => 'bar', 'message' => ['test', 'test2']}) + end + assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", + headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, + body: '["test","test2"]', + times:1 + end + + def test_emit_text_from_dict + config = %{ + endpoint https://collectors.sumologic.com/v1/receivers/http/1234 + log_format text + source_category test + source_host test + source_name test + + } + driver = create_driver(config) + time = event_time + stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') + driver.run do + driver.feed("output.test", time, {'foo' => 'bar', 'message' => {'test': 'test2', 'test3': 'test4'}}) + end + assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", + headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, + body: '{"test":"test2","test3":"test4"}', + times:1 + end + +end