From c1e1f1c912ac8d89e4c931c0461617a73fb6b7ab Mon Sep 17 00:00:00 2001 From: Dominik Rosiek <58699848+sumo-drosiek@users.noreply.github.com> Date: Tue, 19 Oct 2021 16:37:49 +0200 Subject: [PATCH] Fix json parsing (#69) * test(json_format): add unit tests for invalid json string * fix(json_format): fix parsing string Signed-off-by: Dominik Rosiek --- lib/fluent/plugin/out_sumologic.rb | 4 +- test/plugin/test_out_sumologic.rb | 84 ++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/lib/fluent/plugin/out_sumologic.rb b/lib/fluent/plugin/out_sumologic.rb index 9de2925..1ee896e 100644 --- a/lib/fluent/plugin/out_sumologic.rb +++ b/lib/fluent/plugin/out_sumologic.rb @@ -1,5 +1,6 @@ require 'fluent/plugin/output' require 'net/https' +require 'json' require 'yajl' require 'httpclient' require 'zlib' @@ -252,8 +253,7 @@ def merge_json(record) def dump_log(log) log.delete('_sumo_metadata') begin - parser = Yajl::Parser.new - hash = parser.parse(log[@log_key]) + hash = JSON.parse(log[@log_key]) log[@log_key] = hash Yajl.dump(log) rescue diff --git a/test/plugin/test_out_sumologic.rb b/test/plugin/test_out_sumologic.rb index b4d85cb..4681ad2 100644 --- a/test/plugin/test_out_sumologic.rb +++ b/test/plugin/test_out_sumologic.rb @@ -713,4 +713,88 @@ def test_emit_text_from_dict times:1 end + def test_emit_fields_string_based + config = %{ + endpoint https://collectors.sumologic.com/v1/receivers/http/1234 + log_format fields + 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, {'message' => '{"foo": "bar", "message": "test"}'}) + 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: /\A{"timestamp":\d+.,"message":{"foo":"bar","message":"test"}}\z/, + times:1 + end + + def test_emit_fields_invalid_json_string_based_1 + config = %{ + endpoint https://collectors.sumologic.com/v1/receivers/http/1234 + log_format fields + 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, {'message' => '{"foo": "bar", "message": "test"'}) + 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: /\A{"timestamp":\d+.,"message":"{\\"foo\\": \\"bar\\", \\"message\\": \\"test\\""}\z/, + times:1 + end + + def test_emit_fields_invalid_json_string_based_2 + config = %{ + endpoint https://collectors.sumologic.com/v1/receivers/http/1234 + log_format fields + 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, {'message' => '{"foo": "bar", "message"'}) + 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: /\A{"timestamp":\d+.,"message":"{\\"foo\\": \\"bar\\", \\"message\\""}\z/, + times:1 + end + + def test_emit_fields_invalid_json_string_based_3 + config = %{ + endpoint https://collectors.sumologic.com/v1/receivers/http/1234 + log_format fields + 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, {'message' => '"foo\": \"bar\", \"mess'}) + 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: /\A{"timestamp":\d+.,"message":"\\"foo\\\\\\": \\\\\\"bar\\\\\\", \\\\\\"mess"}\z/, + times:1 + end + end