diff --git a/guides/source/active_support_instrumentation.md b/guides/source/active_support_instrumentation.md index 969596f470..6c77a40d42 100644 --- a/guides/source/active_support_instrumentation.md +++ b/guides/source/active_support_instrumentation.md @@ -396,6 +396,15 @@ INFO. Cache stores my add their own keys } ``` +Railties +-------- + +### load_config_initializer.railties + +| Key | Value | +| -------------- | ----------------------------------------------------- | +| `:initializer` | Path to loaded initializer from `config/initializers` | + Rails ----- diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index bf09066cf1..3822486412 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,33 @@ +* Instrument an `load_config_initializer.railties` event on each load of configuration initializer + from `config/initializers`. Subscribers should be attached before `load_config_initializers` + initializer completed. + + Registering subscriber examples: + + # config/application.rb + module RailsApp + class Application < Rails::Application + ActiveSupport::Notifications.subscribe('load_config_initializer.railties') do |*args| + event = ActiveSupport::Notifications::Event.new(*args) + puts "Loaded initializer #{event.payload[:initializer]} (#{event.duration}ms)" + end + end + end + + # my_engine/lib/my_engine/engine.rb + module MyEngine + class Engine < ::Rails::Engine + config.before_initialize do + ActiveSupport::Notifications.subscribe('load_config_initializer.railties') do |*args| + event = ActiveSupport::Notifications::Event.new(*args) + puts "Loaded initializer #{event.payload[:initializer]} (#{event.duration}ms)" + end + end + end + end + + *Paul Nikitochkin* + * Support for Pathnames in eager load paths. *Mike Pack* diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 1296c0a843..3f109debdc 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -611,7 +611,7 @@ module Rails initializer :load_config_initializers do config.paths["config/initializers"].existent.sort.each do |initializer| - load(initializer) + load_config_initializer(initializer) end end @@ -645,6 +645,12 @@ module Rails protected + def load_config_initializer(initializer) + ActiveSupport::Notifications.instrument('load_config_initializer.railties', initializer: initializer) do + load(initializer) + end + end + def run_tasks_blocks(*) #:nodoc: super paths["lib/tasks"].existent.sort.each { |ext| load(ext) } diff --git a/railties/test/application/initializers/notifications_test.rb b/railties/test/application/initializers/notifications_test.rb index baae6fd928..0c10726b6f 100644 --- a/railties/test/application/initializers/notifications_test.rb +++ b/railties/test/application/initializers/notifications_test.rb @@ -39,5 +39,18 @@ module ApplicationTests assert_equal 1, logger.logged(:debug).size assert_match(/SHOW tables/, logger.logged(:debug).last) end + + test 'rails load_config_initializer event is instrumented' do + app_file 'config/initializers/foo.rb', '' + + events = [] + callback = -> (*_) { events << _ } + ActiveSupport::Notifications.subscribed(callback, 'load_config_initializer.railties') do + app + end + + assert_equal %w[load_config_initializer.railties], events.map(&:first) + assert_includes events.first.last[:initializer], 'config/initializers/foo.rb' + end end end