Skip to content

Commit

Permalink
fix: Use Active Support Lazy Load Hooks to avoid prematurely initiali…
Browse files Browse the repository at this point in the history
…zing ActiveRecord::Base and ActiveJob::Base (#1104)

Co-authored-by: Kayla Reopelle <87386821+kaylareopelle@users.noreply.github.com>
  • Loading branch information
bensheldon and kaylareopelle authored Aug 12, 2024
1 parent f944d78 commit a9e6e1a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,17 @@ def gem_version
end

def require_dependencies
require 'active_support/lazy_load_hooks'
require_relative 'patches/base'
require_relative 'handlers'
end

def patch_activejob
::ActiveJob::Base.prepend(Patches::Base) unless ::ActiveJob::Base <= Patches::Base

Handlers.subscribe

ActiveSupport.on_load(:active_job) do
::ActiveJob::Base.prepend(Patches::Base) unless ::ActiveJob::Base <= Patches::Base
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base

install do |_config|
require_dependencies
patch
patch_activerecord
end

present do
Expand All @@ -30,27 +30,8 @@ def gem_version
::ActiveRecord.version
end

def patch
# The original approach taken here was to patch each individual module of interest.
# However the patches are applied too late in some applications and as a result the
# Active Record models will not have the instrumentation patches applied.
# Prepending the ActiveRecord::Base class is more consistent in applying
# the patches regardless of initialization order.
#
# Modules to prepend to ActiveRecord::Base are still grouped by the source
# module that they are defined in.
# Example: Patches::PersistenceClassMethods refers to https://github.com/rails/rails/blob/v6.1.0/activerecord/lib/active_record/persistence.rb#L10
::ActiveRecord::Base.prepend(Patches::Querying)
::ActiveRecord::Base.prepend(Patches::Persistence)
::ActiveRecord::Base.prepend(Patches::PersistenceClassMethods)
::ActiveRecord::Base.prepend(Patches::PersistenceInsertClassMethods)
::ActiveRecord::Base.prepend(Patches::TransactionsClassMethods)
::ActiveRecord::Base.prepend(Patches::Validations)

::ActiveRecord::Relation.prepend(Patches::RelationPersistence)
end

def require_dependencies
require 'active_support/lazy_load_hooks'
require_relative 'patches/querying'
require_relative 'patches/persistence'
require_relative 'patches/persistence_class_methods'
Expand All @@ -59,6 +40,23 @@ def require_dependencies
require_relative 'patches/validations'
require_relative 'patches/relation_persistence'
end

def patch_activerecord
ActiveSupport.on_load(:active_record) do
# Modules to prepend to ActiveRecord::Base are grouped by the source
# module that they are defined in as they are included into ActiveRecord::Base
# Example: Patches::PersistenceClassMethods refers to https://github.com/rails/rails/blob/v6.1.0/activerecord/lib/active_record/persistence.rb#L10
# which is included into ActiveRecord::Base in https://github.com/rails/rails/blob/914caca2d31bd753f47f9168f2a375921d9e91cc/activerecord/lib/active_record/base.rb#L283
::ActiveRecord::Base.prepend(Patches::Querying)
::ActiveRecord::Base.prepend(Patches::Persistence)
::ActiveRecord::Base.prepend(Patches::PersistenceClassMethods)
::ActiveRecord::Base.prepend(Patches::PersistenceInsertClassMethods)
::ActiveRecord::Base.prepend(Patches::TransactionsClassMethods)
::ActiveRecord::Base.prepend(Patches::Validations)

::ActiveRecord::Relation.prepend(Patches::RelationPersistence)
end
end
end
end
end
Expand Down

0 comments on commit a9e6e1a

Please sign in to comment.