lib/searchkick/logging.rb in searchkick-1.2.0 vs lib/searchkick/logging.rb in searchkick-1.2.1
- old
+ new
@@ -1,12 +1,14 @@
# 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: "#{searchkick_klass.name} Search",
+ name: name,
query: params
}
ActiveSupport::Notifications.instrument("search.searchkick", event) do
super
end
@@ -17,23 +19,31 @@
def store(record)
event = {
name: "#{record.searchkick_klass.name} Store",
id: search_id(record)
}
- ActiveSupport::Notifications.instrument("request.searchkick", event) do
- super(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: "#{record.searchkick_klass.name} Remove",
+ name: name,
id: search_id(record)
}
- ActiveSupport::Notifications.instrument("request.searchkick", event) do
- super(record)
+ if Searchkick.callbacks_value == :bulk
+ super
+ else
+ ActiveSupport::Notifications.instrument("request.searchkick", event) do
+ super
+ end
end
end
def import(records)
if records.any?
@@ -46,10 +56,36 @@
end
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
+
+ def perform_items(items)
+ if callbacks_value == :bulk
+ event = {
+ name: "Bulk",
+ count: items.size
+ }
+ ActiveSupport::Notifications.instrument("request.searchkick", event) do
+ super
+ end
+ else
+ super
+ end
+ end
+ end
+
# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/log_subscriber.rb
class LogSubscriber < ActiveSupport::LogSubscriber
def self.runtime=(value)
Thread.current[:searchkick_runtime] = value
end
@@ -85,10 +121,22 @@
payload = event.payload
name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
debug " #{color(name, YELLOW, true)} #{payload.except(:name).to_json}"
end
+
+ def multi_search(event)
+ self.class.runtime += event.duration
+ return unless logger.debug?
+
+ payload = event.payload
+ name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
+
+ # no easy way to tell which host the client will use
+ host = Searchkick.client.transport.hosts.first
+ debug " #{color(name, YELLOW, true)} curl #{host[:protocol]}://#{host[:host]}:#{host[:port]}/_msearch?pretty -d '#{payload[:body]}'"
+ end
end
# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/controller_runtime.rb
module ControllerRuntime
extend ActiveSupport::Concern
@@ -128,9 +176,10 @@
end
end
end
Searchkick::Query.send(:prepend, Searchkick::QueryWithInstrumentation)
Searchkick::Index.send(:prepend, Searchkick::IndexWithInstrumentation)
+Searchkick.singleton_class.send(:prepend, Searchkick::SearchkickWithInstrumentation)
Searchkick::LogSubscriber.attach_to :searchkick
ActiveSupport.on_load(:action_controller) do
include Searchkick::ControllerRuntime
end