Skip to content
Merged
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
7 changes: 6 additions & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: spec/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
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.bundle
spec/gemfiles/*.gemfile.lock
Gemfile.lock
pkg/*
coverage/*
Expand Down
10 changes: 9 additions & 1 deletion lib/fluent/logger/fluent_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ def self.from_msgpack_ext(data)
def to_json(*args)
@sec.to_s
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

class FluentLogger < LoggerBase
Expand Down Expand Up @@ -235,7 +243,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
Expand Down
32 changes: 26 additions & 6 deletions spec/fluent_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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'
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/gemfiles/activesupport.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

# Load common dependencies
eval_gemfile("../../Gemfile")

# Add custom dependencies
gem "activesupport"
4 changes: 4 additions & 0 deletions spec/gemfiles/no-activesupport.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

# Load common dependencies
eval_gemfile("../../Gemfile")