Skip to content

Commit

Permalink
Fix json parsing (#69)
Browse files Browse the repository at this point in the history
* test(json_format): add unit tests for invalid json string
* fix(json_format): fix parsing string

Signed-off-by: Dominik Rosiek <[email protected]>
  • Loading branch information
sumo-drosiek authored Oct 19, 2021
1 parent 7e8f16f commit c1e1f1c
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/fluent/plugin/out_sumologic.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'fluent/plugin/output'
require 'net/https'
require 'json'
require 'yajl'
require 'httpclient'
require 'zlib'
Expand Down Expand Up @@ -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
Expand Down
84 changes: 84 additions & 0 deletions test/plugin/test_out_sumologic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c1e1f1c

Please sign in to comment.