Skip to content

Commit

Permalink
Convert Array and Hash to string (JSON) before strip (#66)
Browse files Browse the repository at this point in the history
Convert Array and Hash to string (JSON) before strip (#66)
  • Loading branch information
sumo-drosiek authored Nov 23, 2020
1 parent 7b25023 commit f320356
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
23 changes: 15 additions & 8 deletions lib/fluent/plugin/out_sumologic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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)
Expand All @@ -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?
Expand Down
44 changes: 43 additions & 1 deletion test/plugin/test_out_sumologic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -671,4 +671,46 @@ def test_emit_json_merge_timestamp_compress_gzip
times:1
end

end
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

0 comments on commit f320356

Please sign in to comment.