Skip to content

Commit

Permalink
feat: Workaround tagged_logging bug in Rails 7.0.3
Browse files Browse the repository at this point in the history
Signed-off-by: Ferenc Géczi <[email protected]>
  • Loading branch information
Ferenc- committed Apr 5, 2024
1 parent 900457d commit e738c97
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gemfiles/rails_70.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ gem "puma"
gem "rack-test"
gem "simplecov", "~> 0.21.2"
gem "mail", ">= 2.8.1"
gem "rails", "7.0.8"
gem "rails", "7.0.3"
gem "mysql2", "0.5.5"
gem "pg"
gem "sqlite3", "~> 1.4"
Expand Down
22 changes: 22 additions & 0 deletions lib/instana/frameworks/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@ class Railtie < ::Rails::Railtie
if ::Instana.config[:logging][:enabled] && !ENV.key?('INSTANA_TEST')
logger = ::Instana::InstrumentedLogger.new('/dev/null')
if ::Rails::VERSION::STRING < "7.1"
# Rails 7.0.3 `tagged_logging.rb` has a number of bugs.
# What pertains to our instrumentation here is that in
# https://github.com/rails/rails/blob/v7.0.3/activesupport/lib/active_support/tagged_logging.rb#L100
# sets the `other_logger.formatter` to `formatter` if `other_logger.formatter` is `nil`
# but then
# https://github.com/rails/rails/blob/v7.0.3/activesupport/lib/active_support/tagged_logging.rb#L102
# redefines `formatter.current_tags` method to a proc calling `formatter.current_tags`, itself.
# which is an infinite recursion.
#
# This bug starts with 7.0.3:
# https://github.com/rails/rails/compare/v7.0.2...v7.0.3#diff-68f90ba4998610be2f7da027e006e8ddeeb06dece66f41579c94070e12f23011
# And is already reverted/fixed in 7.0.4
# https://github.com/rails/rails/compare/v7.0.3...v7.0.4#diff-68f90ba4998610be2f7da027e006e8ddeeb06dece66f41579c94070e12f23011
# The commit which fixes it is:
# https://github.com/rails/rails/commit/ff277583e22ddfbcfbd2131789a7cb7c2f868d68
#
# Since instana does trigger this bug here we workaround it, by defining the `formatter`,
# to break the infinite loop. The `formatter` used here is the same as the default used in
# https://github.com/rails/rails/blob/v7.0.3/activesupport/lib/active_support/tagged_logging.rb#L89C9-L89C70
if ::Rails::VERSION::STRING == "7.0.3"
logger.formatter = ActiveSupport::Logger::SimpleFormatter.new
end
Rails.logger.extend(ActiveSupport::Logger.broadcast(logger))
else
Rails.logger.broadcast_to(logger)
Expand Down

0 comments on commit e738c97

Please sign in to comment.