Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Yajl as the json parser, in order to accept malformed json messages #46

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion lib/fluent/plugin/dynatrace_constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Plugin
class DynatraceOutputConstants
# The version of the Dynatrace output plugin
def self.version
'0.2.1'
'0.2.2'
end
end
end
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
Loading