Skip to content

Commit

Permalink
Use Yajl as the json parser, in order to accept malformed json messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Parreiras committed Dec 14, 2023
1 parent ff955c6 commit 9323420
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions fluent-plugin-dynatrace.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Gem::Specification.new do |gem|
gem.required_ruby_version = '>= 2.7.0'

gem.add_runtime_dependency 'fluentd', ['>= 0.14.22', '< 2']
gem.add_runtime_dependency 'yajl-ruby', ['~> 1.4', '>= 1.4.3']
gem.add_development_dependency 'bundler', ['>= 2', '<3']
gem.add_development_dependency 'rake', '13.1.0'
gem.add_development_dependency 'rubocop', '1.59.0'
Expand Down
3 changes: 2 additions & 1 deletion lib/fluent/plugin/out_dynatrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

require 'fluent/plugin/output'
require 'net/http'
require 'yajl'
require_relative 'dynatrace_constants'

module Fluent
Expand Down Expand Up @@ -155,7 +156,7 @@ def send_records(records)

def serialize(records)
log.on_trace { log.trace('#serialize') }
body = "#{records.to_json.chomp}\n"
body = "#{Yajl.dump(records)}\n"
log.on_trace { log.trace("#serialize body length #{body.length}") }
body
end
Expand Down
8 changes: 6 additions & 2 deletions test/plugin/out_dynatrace_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,22 +212,26 @@ def create_driver(conf = config)
end

sub_test_case 'tests for #write' do
test 'Write all records as a JSON array' do
test 'write all records as a JSON array' do
d = create_driver
t = event_time('2016-06-10 19:46:32 +0900')
malformed_json_message = "This is a malformed json message V\x92\xA1\u000F\xF0ܱ\u0013,\x82"
d.run do
d.feed('tag', t, { 'message' => 'this is a test message', 'amount' => 53 })
d.feed('tag', t, { 'message' => 'this is a second test message', 'amount' => 54 })
d.feed('tag', t, { 'message' => malformed_json_message, 'amount' => 54 })
end

assert_equal 2, d.instance.agent.result.data.length
assert_equal 3, d.instance.agent.result.data.length

content = d.instance.agent.result.data[0]
malformed_json_content = d.instance.agent.result.data[2]

assert_equal "fluent-plugin-dynatrace/#{Fluent::Plugin::DynatraceOutputConstants.version}",
d.instance.agent.result.headers['user-agent']
assert_equal content['message'], 'this is a test message'
assert_equal content['amount'], 53
assert_equal malformed_json_content['message'], malformed_json_message
end

test 'should not export an empty payload' do
Expand Down

0 comments on commit 9323420

Please sign in to comment.