diff --git a/lib/searchkick.rb b/lib/searchkick.rb index e4f5d79..8df65bb 100644 --- a/lib/searchkick.rb +++ b/lib/searchkick.rb @@ -26,6 +26,11 @@ require "searchkick/railtie" if defined?(Rails) if defined?(ActiveSupport::Notifications) require "searchkick/logging" require "searchkick/log_subscriber" + require "searchkick/controller_runtime" + + ActiveSupport.on_load(:action_controller) do + include Searchkick::ControllerRuntime + end end module Searchkick diff --git a/lib/searchkick/controller_runtime.rb b/lib/searchkick/controller_runtime.rb new file mode 100644 index 0000000..dff8f33 --- /dev/null +++ b/lib/searchkick/controller_runtime.rb @@ -0,0 +1,40 @@ +module Searchkick + # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/controller_runtime.rb + module ControllerRuntime + extend ActiveSupport::Concern + + protected + + attr_internal :searchkick_runtime + + def process_action(action, *args) + # We also need to reset the runtime before each action + # because of queries in middleware or in cases we are streaming + # and it won't be cleaned up by the method below. + Searchkick::LogSubscriber.reset_runtime + super + end + + def cleanup_view_runtime + searchkick_rt_before_render = Searchkick::LogSubscriber.reset_runtime + runtime = super + searchkick_rt_after_render = Searchkick::LogSubscriber.reset_runtime + self.searchkick_runtime = searchkick_rt_before_render + searchkick_rt_after_render + runtime - searchkick_rt_after_render + end + + def append_info_to_payload(payload) + super + payload[:searchkick_runtime] = (searchkick_runtime || 0) + Searchkick::LogSubscriber.reset_runtime + end + + module ClassMethods + def log_process_action(payload) + messages = super + runtime = payload[:searchkick_runtime] + messages << ("Searchkick: %.1fms" % runtime.to_f) if runtime.to_f > 0 + messages + end + end + end +end diff --git a/lib/searchkick/logging.rb b/lib/searchkick/logging.rb index ee3a725..fc799a8 100644 --- a/lib/searchkick/logging.rb +++ b/lib/searchkick/logging.rb @@ -139,51 +139,9 @@ module Searchkick end end end - - # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/controller_runtime.rb - module ControllerRuntime - extend ActiveSupport::Concern - - protected - - attr_internal :searchkick_runtime - - def process_action(action, *args) - # We also need to reset the runtime before each action - # because of queries in middleware or in cases we are streaming - # and it won't be cleaned up by the method below. - Searchkick::LogSubscriber.reset_runtime - super - end - - def cleanup_view_runtime - searchkick_rt_before_render = Searchkick::LogSubscriber.reset_runtime - runtime = super - searchkick_rt_after_render = Searchkick::LogSubscriber.reset_runtime - self.searchkick_runtime = searchkick_rt_before_render + searchkick_rt_after_render - runtime - searchkick_rt_after_render - end - - def append_info_to_payload(payload) - super - payload[:searchkick_runtime] = (searchkick_runtime || 0) + Searchkick::LogSubscriber.reset_runtime - end - - module ClassMethods - def log_process_action(payload) - messages = super - runtime = payload[:searchkick_runtime] - messages << ("Searchkick: %.1fms" % runtime.to_f) if runtime.to_f > 0 - messages - end - end - end end Searchkick::Query.prepend(Searchkick::QueryWithInstrumentation) Searchkick::Index.prepend(Searchkick::IndexWithInstrumentation) Searchkick::Indexer.prepend(Searchkick::IndexerWithInstrumentation) Searchkick.singleton_class.prepend(Searchkick::SearchkickWithInstrumentation) -ActiveSupport.on_load(:action_controller) do - include Searchkick::ControllerRuntime -end -- libgit2 0.21.0