From a5d15c54654f0fc534351de17934b0d6cf0c39d7 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sat, 19 Feb 2022 20:28:10 -0800 Subject: [PATCH] Moved Active Support notifications inline [skip ci] --- lib/searchkick.rb | 43 ++++++++++++++++++++++++++++--------------- lib/searchkick/index.rb | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- lib/searchkick/logging.rb | 147 --------------------------------------------------------------------------------------------------------------------------------------------------- lib/searchkick/query.rb | 9 ++++++++- 4 files changed, 90 insertions(+), 172 deletions(-) delete mode 100644 lib/searchkick/logging.rb diff --git a/lib/searchkick.rb b/lib/searchkick.rb index 3b740a7..6c21e23 100644 --- a/lib/searchkick.rb +++ b/lib/searchkick.rb @@ -1,6 +1,8 @@ # dependencies require "active_support" require "active_support/core_ext/hash/deep_merge" +require "active_support/core_ext/module/attr_internal" +require "active_support/notifications" require "hashie" # stdlib @@ -8,9 +10,11 @@ require "forwardable" # modules require "searchkick/bulk_indexer" +require "searchkick/controller_runtime" require "searchkick/index" require "searchkick/indexer" require "searchkick/hash_wrapper" +require "searchkick/log_subscriber" require "searchkick/model" require "searchkick/multi_search" require "searchkick/query" @@ -23,17 +27,6 @@ require "searchkick/version" # integrations 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 - - Searchkick::LogSubscriber.attach_to :searchkick -end module Searchkick # requires faraday @@ -180,7 +173,13 @@ module Searchkick def self.multi_search(queries) queries = queries.map { |q| q.send(:query) } - Searchkick::MultiSearch.new(queries).perform + event = { + name: "Multi Search", + body: queries.flat_map { |q| [q.params.except(:body).to_json, q.body.to_json] }.map { |v| "#{v}\n" }.join, + } + ActiveSupport::Notifications.instrument("multi_search.searchkick", event) do + Searchkick::MultiSearch.new(queries).perform + end end # callbacks @@ -207,7 +206,15 @@ module Searchkick begin self.callbacks_value = value result = yield - indexer.perform if callbacks_value == :bulk + if callbacks_value == :bulk + event = { + name: "Bulk", + count: indexer.queued_items.size + } + ActiveSupport::Notifications.instrument("request.searchkick", event) do + indexer.perform + end + end result ensure self.callbacks_value = previous_value @@ -322,10 +329,16 @@ module Searchkick end end +ActiveSupport.on_load(:active_record) do + extend Searchkick::Model +end + ActiveSupport.on_load(:mongoid) do Mongoid::Document::ClassMethods.include Searchkick::Model end -ActiveSupport.on_load(:active_record) do - extend Searchkick::Model +ActiveSupport.on_load(:action_controller) do + include Searchkick::ControllerRuntime end + +Searchkick::LogSubscriber.attach_to :searchkick diff --git a/lib/searchkick/index.rb b/lib/searchkick/index.rb index 9329eac..cc1eed9 100644 --- a/lib/searchkick/index.rb +++ b/lib/searchkick/index.rb @@ -130,32 +130,47 @@ module Searchkick indices end - # record based - # use helpers for notifications - def store(record) - queue_index([record]) + maybe_notify_one(record, "Store") do + queue_index([record]) + end end def remove(record) - queue_delete([record]) + maybe_notify_one(record, "Remove") do + queue_delete([record]) + end end def update_record(record, method_name) - queue_update([record], method_name) + maybe_notify_one(record, "Update") do + queue_update([record], method_name) + end end def bulk_delete(records) - queue_delete(records) + return if records.empty? + + maybe_notify_many(records, "Delete") do + queue_delete(records) + end end def bulk_index(records) - queue_index(records) + return if records.empty? + + maybe_notify_many(records, "Import") do + queue_index(records) + end end alias_method :import, :bulk_index def bulk_update(records, method_name) - queue_update(records, method_name) + return if records.empty? + + maybe_notify_many(records, "Update") do + queue_update(records, method_name) + end end def search_id(record) @@ -382,5 +397,35 @@ module Searchkick raise Searchkick::Error, "Safety check failed - only run one Model.reindex per model at a time" end end + + def maybe_notify_one(record, name) + name = record && record.searchkick_klass ? "#{record.searchkick_klass.name} #{name}" : name + event = { + name: name, + id: search_id(record) + } + if Searchkick.callbacks_value == :bulk + yield + else + ActiveSupport::Notifications.instrument("request.searchkick", event) do + yield + end + end + end + + def maybe_notify_many(records, name) + event = { + name: "#{records.first.searchkick_klass.name} #{name}", + count: records.size + } + event[:id] = search_id(records.first) if records.size == 1 + if Searchkick.callbacks_value == :bulk + yield + else + ActiveSupport::Notifications.instrument("request.searchkick", event) do + yield + end + end + end end end diff --git a/lib/searchkick/logging.rb b/lib/searchkick/logging.rb deleted file mode 100644 index fc799a8..0000000 --- a/lib/searchkick/logging.rb +++ /dev/null @@ -1,147 +0,0 @@ -# based on https://gist.github.com/mnutt/566725 -require "active_support/core_ext/module/attr_internal" - -module Searchkick - module QueryWithInstrumentation - def execute_search - name = searchkick_klass ? "#{searchkick_klass.name} Search" : "Search" - event = { - name: name, - query: params - } - ActiveSupport::Notifications.instrument("search.searchkick", event) do - super - end - end - end - - module IndexWithInstrumentation - def store(record) - event = { - name: "#{record.searchkick_klass.name} Store", - id: search_id(record) - } - if Searchkick.callbacks_value == :bulk - super - else - ActiveSupport::Notifications.instrument("request.searchkick", event) do - super - end - end - end - - def remove(record) - name = record && record.searchkick_klass ? "#{record.searchkick_klass.name} Remove" : "Remove" - event = { - name: name, - id: search_id(record) - } - if Searchkick.callbacks_value == :bulk - super - else - ActiveSupport::Notifications.instrument("request.searchkick", event) do - super - end - end - end - - def update_record(record, method_name) - event = { - name: "#{record.searchkick_klass.name} Update", - id: search_id(record) - } - if Searchkick.callbacks_value == :bulk - super - else - ActiveSupport::Notifications.instrument("request.searchkick", event) do - super - end - end - end - - def bulk_index(records) - if records.any? - event = { - name: "#{records.first.searchkick_klass.name} Import", - count: records.size - } - event[:id] = search_id(records.first) if records.size == 1 - if Searchkick.callbacks_value == :bulk - super - else - ActiveSupport::Notifications.instrument("request.searchkick", event) do - super - end - end - end - end - alias_method :import, :bulk_index - - def bulk_update(records, *args) - if records.any? - event = { - name: "#{records.first.searchkick_klass.name} Update", - count: records.size - } - event[:id] = search_id(records.first) if records.size == 1 - if Searchkick.callbacks_value == :bulk - super - else - ActiveSupport::Notifications.instrument("request.searchkick", event) do - super - end - end - end - end - - def bulk_delete(records) - if records.any? - event = { - name: "#{records.first.searchkick_klass.name} Delete", - count: records.size - } - event[:id] = search_id(records.first) if records.size == 1 - if Searchkick.callbacks_value == :bulk - super - else - ActiveSupport::Notifications.instrument("request.searchkick", event) do - super - end - end - end - end - end - - module IndexerWithInstrumentation - def perform - if Searchkick.callbacks_value == :bulk - event = { - name: "Bulk", - count: queued_items.size - } - ActiveSupport::Notifications.instrument("request.searchkick", event) do - super - end - else - super - end - end - end - - module SearchkickWithInstrumentation - def multi_search(searches) - event = { - name: "Multi Search", - body: searches.flat_map { |q| [q.params.except(:body).to_json, q.body.to_json] }.map { |v| "#{v}\n" }.join, - } - ActiveSupport::Notifications.instrument("multi_search.searchkick", event) do - super - 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) diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index d993078..a2ebf7d 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -233,7 +233,14 @@ module Searchkick end def execute_search - Searchkick.client.search(params) + name = searchkick_klass ? "#{searchkick_klass.name} Search" : "Search" + event = { + name: name, + query: params + } + ActiveSupport::Notifications.instrument("search.searchkick", event) do + Searchkick.client.search(params) + end end def prepare -- libgit2 0.21.0