From 62ce767f669e28d6f2139364208910e09740d084 Mon Sep 17 00:00:00 2001 From: Marcos Parreiras Date: Thu, 14 Dec 2023 12:52:01 +0100 Subject: [PATCH 1/2] Use Yajl as the json parser, in order to accept malformed json messages --- fluent-plugin-dynatrace.gemspec | 1 + lib/fluent/plugin/out_dynatrace.rb | 3 ++- test/plugin/out_dynatrace_test.rb | 8 ++++++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/fluent-plugin-dynatrace.gemspec b/fluent-plugin-dynatrace.gemspec index 88ecbb5..d734ab6 100644 --- a/fluent-plugin-dynatrace.gemspec +++ b/fluent-plugin-dynatrace.gemspec @@ -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' diff --git a/lib/fluent/plugin/out_dynatrace.rb b/lib/fluent/plugin/out_dynatrace.rb index 499bd40..e592e27 100644 --- a/lib/fluent/plugin/out_dynatrace.rb +++ b/lib/fluent/plugin/out_dynatrace.rb @@ -16,6 +16,7 @@ require 'fluent/plugin/output' require 'net/http' +require 'yajl' require_relative 'dynatrace_constants' module Fluent @@ -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 diff --git a/test/plugin/out_dynatrace_test.rb b/test/plugin/out_dynatrace_test.rb index d089833..7f2bcdd 100644 --- a/test/plugin/out_dynatrace_test.rb +++ b/test/plugin/out_dynatrace_test.rb @@ -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 From e16b61c596fa01aa88f068ed6f6d45ab82f01c1c Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Wed, 22 May 2024 08:15:02 -0400 Subject: [PATCH 2/2] Bump patch version --- lib/fluent/plugin/dynatrace_constants.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fluent/plugin/dynatrace_constants.rb b/lib/fluent/plugin/dynatrace_constants.rb index ddb3c63..6373d62 100644 --- a/lib/fluent/plugin/dynatrace_constants.rb +++ b/lib/fluent/plugin/dynatrace_constants.rb @@ -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