From 8674fd0f9879b80e458a2241c38a7cfbd60d69e8 Mon Sep 17 00:00:00 2001 From: Shizuo Fujita Date: Tue, 25 Nov 2025 15:35:47 +0900 Subject: [PATCH 1/3] Use msg.to_json for ActiveSupport compatibility Signed-off-by: Shizuo Fujita --- .github/workflows/linux.yml | 7 ++++++- .gitignore | 1 + gemfiles/activesupport.gemfile | 7 +++++++ gemfiles/no-activesupport.gemfile | 4 ++++ lib/fluent/logger/fluent_logger.rb | 6 +++++- spec/fluent_logger_spec.rb | 32 ++++++++++++++++++++++++------ 6 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 gemfiles/activesupport.gemfile create mode 100644 gemfiles/no-activesupport.gemfile diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 37b6bfc..c4ccbde 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -14,7 +14,12 @@ jobs: - '3.4' os: - ubuntu-latest - name: Ruby ${{ matrix.ruby }} unit testing on ${{ matrix.os }} + gemfile: + - activesupport + - no-activesupport + env: + BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile + name: Ruby ${{ matrix.ruby }} unit testing on ${{ matrix.os }} (${{ matrix.gemfile }}) steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 diff --git a/.gitignore b/.gitignore index 3d83501..a3bac6b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .bundle +gemfiles/*.gemfile.lock Gemfile.lock pkg/* coverage/* diff --git a/gemfiles/activesupport.gemfile b/gemfiles/activesupport.gemfile new file mode 100644 index 0000000..13e86aa --- /dev/null +++ b/gemfiles/activesupport.gemfile @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +# Load common dependencies +eval_gemfile("../Gemfile") + +# Add custom dependencies +gem "activesupport" diff --git a/gemfiles/no-activesupport.gemfile b/gemfiles/no-activesupport.gemfile new file mode 100644 index 0000000..d4858f5 --- /dev/null +++ b/gemfiles/no-activesupport.gemfile @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +# Load common dependencies +eval_gemfile("../Gemfile") diff --git a/lib/fluent/logger/fluent_logger.rb b/lib/fluent/logger/fluent_logger.rb index 2aef0a2..299ea21 100644 --- a/lib/fluent/logger/fluent_logger.rb +++ b/lib/fluent/logger/fluent_logger.rb @@ -49,6 +49,10 @@ def self.from_msgpack_ext(data) def to_json(*args) @sec.to_s end + + def as_json(*args) + @sec + end end class FluentLogger < LoggerBase @@ -235,7 +239,7 @@ def to_msgpack(msg) res = begin @packer.pack(msg).to_s rescue NoMethodError - JSON.parse(JSON.generate(msg)).to_msgpack + JSON.parse(msg.to_json).to_msgpack ensure @packer.clear end diff --git a/spec/fluent_logger_spec.rb b/spec/fluent_logger_spec.rb index aa67c8c..3707d66 100644 --- a/spec/fluent_logger_spec.rb +++ b/spec/fluent_logger_spec.rb @@ -8,6 +8,11 @@ require 'fluent/logger/fluent_logger/cui' require 'timeout' +begin + require 'active_support/json' +rescue LoadError +end + describe Fluent::Logger::FluentLogger do let(:fluentd) { DummyFluentd.new @@ -176,7 +181,12 @@ logger.post('tag', data) fluentd.wait_transfer logger_data = fluentd.queue.last.last - expect(logger_data['time']).to eq '2008-09-01 10:05:00 UTC' + if defined?(ActiveSupport) + expect(logger_data['time']).to eq '2008-09-01T10:05:00.000Z' + else + expect(logger_data['time']).to eq '2008-09-01 10:05:00 UTC' + end + expect(logger_data['proc']).to be_truthy expect(logger_data['object']).to be_truthy } @@ -188,7 +198,11 @@ fluentd.wait_transfer logger_data1 = fluentd.queue.first.last - expect(logger_data1['time']).to eq '2008-09-01 10:05:00 UTC' + if defined?(ActiveSupport) + expect(logger_data1['time']).to eq '2008-09-01T10:05:00.000Z' + else + expect(logger_data1['time']).to eq '2008-09-01 10:05:00 UTC' + end logger_data2 = fluentd.queue.last.last expect(logger_data2['time']).to eq '2008-09-01 10:05:00 UTC' @@ -199,13 +213,19 @@ 'time' => Time.utc(2008, 9, 1, 10, 5, 0), 'object' => Object.new, 'proc' => proc { 1 }, - 'NaN' => (0.0/0.0) # JSON don't convert + 'NaN' => (0.0/0.0) # JSON don't convert by default } logger.post('tag', data) fluentd.wait_transfer - expect(fluentd.queue.last).to be_nil - logger_io.rewind - logger_io.read =~ /FluentLogger: Can't convert to msgpack:/ + if defined?(ActiveSupport) + # activesupport converts NaN to nil + # https://github.com/rails/rails/blob/90a1eaa1b30ba1f2d524e197460e549c03cf5698/activesupport/lib/active_support/core_ext/object/json.rb#L117-L118 + expect(fluentd.queue.last).to eq ['logger-test.tag', {"NaN" => nil, "object" => {}, "proc" => {}, "time" => "2008-09-01T10:05:00.000Z"}] + else + expect(fluentd.queue.last).to be_nil + logger_io.rewind + expect(logger_io.read).to match(/FluentLogger: Can't convert to msgpack:/) + end logger.post('tag', { 'a' => 'b' }) fluentd.wait_transfer From 06089ee8f15dae3ccd25ee12e72235dc3b99f2c9 Mon Sep 17 00:00:00 2001 From: Shizuo Fujita Date: Thu, 27 Nov 2025 16:39:33 +0900 Subject: [PATCH 2/3] Move gemfiles into spec Signed-off-by: Shizuo Fujita --- .github/workflows/linux.yml | 2 +- .gitignore | 2 +- {gemfiles => spec/gemfiles}/activesupport.gemfile | 2 +- {gemfiles => spec/gemfiles}/no-activesupport.gemfile | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename {gemfiles => spec/gemfiles}/activesupport.gemfile (77%) rename {gemfiles => spec/gemfiles}/no-activesupport.gemfile (65%) diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index c4ccbde..732dd11 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -18,7 +18,7 @@ jobs: - activesupport - no-activesupport env: - BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile + BUNDLE_GEMFILE: spec/gemfiles/${{ matrix.gemfile }}.gemfile name: Ruby ${{ matrix.ruby }} unit testing on ${{ matrix.os }} (${{ matrix.gemfile }}) steps: - uses: actions/checkout@v4 diff --git a/.gitignore b/.gitignore index a3bac6b..464b6ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ .bundle -gemfiles/*.gemfile.lock +spec/gemfiles/*.gemfile.lock Gemfile.lock pkg/* coverage/* diff --git a/gemfiles/activesupport.gemfile b/spec/gemfiles/activesupport.gemfile similarity index 77% rename from gemfiles/activesupport.gemfile rename to spec/gemfiles/activesupport.gemfile index 13e86aa..7eff369 100644 --- a/gemfiles/activesupport.gemfile +++ b/spec/gemfiles/activesupport.gemfile @@ -1,7 +1,7 @@ # frozen_string_literal: true # Load common dependencies -eval_gemfile("../Gemfile") +eval_gemfile("../../Gemfile") # Add custom dependencies gem "activesupport" diff --git a/gemfiles/no-activesupport.gemfile b/spec/gemfiles/no-activesupport.gemfile similarity index 65% rename from gemfiles/no-activesupport.gemfile rename to spec/gemfiles/no-activesupport.gemfile index d4858f5..4dbba7b 100644 --- a/gemfiles/no-activesupport.gemfile +++ b/spec/gemfiles/no-activesupport.gemfile @@ -1,4 +1,4 @@ # frozen_string_literal: true # Load common dependencies -eval_gemfile("../Gemfile") +eval_gemfile("../../Gemfile") From ec11c6db74b9867b042ee63a39d2d1ef9f589e6e Mon Sep 17 00:00:00 2001 From: Shizuo Fujita Date: Thu, 27 Nov 2025 17:18:39 +0900 Subject: [PATCH 3/3] Add comment Signed-off-by: Shizuo Fujita --- lib/fluent/logger/fluent_logger.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/fluent/logger/fluent_logger.rb b/lib/fluent/logger/fluent_logger.rb index 299ea21..40cc793 100644 --- a/lib/fluent/logger/fluent_logger.rb +++ b/lib/fluent/logger/fluent_logger.rb @@ -51,6 +51,10 @@ def to_json(*args) end def as_json(*args) + # For ActiveSupport. + # By default, the custom classes are represented by their own instance variables. + # https://github.com/rails/rails/blob/87a9fdeef81f293cd972464686a6b4470ec642a2/activesupport/lib/active_support/core_ext/object/json.rb#L63 + # Define it in a way that results in proper data structure when expressed in JSON @sec end end