Commit 383550e7eeeac4a445e83327476e29eb3384ca89

Authored by Andrew Kane
1 parent 0edde7e2

Added logging for import, store, and remove calls

Showing 2 changed files with 48 additions and 1 deletions   Show diff stats
CHANGELOG.md
... ... @@ -4,6 +4,7 @@
4 4 - Added `wordnet` option
5 5 - Added `edit_distance` option to eventually replace `distance` option
6 6 - Catch misspelling of `misspellings` option
  7 +- Improved logging
7 8  
8 9 ## 0.8.1
9 10  
... ...
lib/searchkick/logging.rb
... ... @@ -11,10 +11,44 @@ module Searchkick
11 11 execute_without_instrumentation
12 12 end
13 13 end
14   -
15 14 alias_method_chain :execute, :instrumentation
16 15 end
17 16  
  17 + class Index
  18 + def store_with_instrumentation(record)
  19 + event = {
  20 + name: "#{record.searchkick_klass.name} Store",
  21 + id: search_id(record)
  22 + }
  23 + ActiveSupport::Notifications.instrument("store.searchkick", event) do
  24 + store_without_instrumentation(record)
  25 + end
  26 + end
  27 + alias_method_chain :store, :instrumentation
  28 +
  29 + def remove_with_instrumentation(record)
  30 + event = {
  31 + name: "#{record.searchkick_klass.name} Remove",
  32 + id: search_id(record)
  33 + }
  34 + ActiveSupport::Notifications.instrument("remove.searchkick", event) do
  35 + remove_without_instrumentation(record)
  36 + end
  37 + end
  38 + alias_method_chain :remove, :instrumentation
  39 +
  40 + def import_with_instrumentation(records)
  41 + event = {
  42 + name: "#{records.first.searchkick_klass.name} Import",
  43 + count: records.size
  44 + }
  45 + ActiveSupport::Notifications.instrument("import.searchkick", event) do
  46 + import_without_instrumentation(records)
  47 + end
  48 + end
  49 + alias_method_chain :import, :instrumentation
  50 + end
  51 +
18 52 # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/log_subscriber.rb
19 53 class LogSubscriber < ActiveSupport::LogSubscriber
20 54 def self.runtime=(value)
... ... @@ -43,6 +77,18 @@ module Searchkick
43 77 host = Searchkick.client.transport.hosts.first
44 78 debug " #{color(name, YELLOW, true)} curl #{host[:protocol]}://#{host[:host]}:#{host[:port]}/#{CGI.escape(index)}#{type ? "/#{type.map{|t| CGI.escape(t) }.join(",")}" : ""}/_search?pretty -d '#{payload[:query][:body].to_json}'"
45 79 end
  80 +
  81 + def store(event)
  82 + self.class.runtime += event.duration
  83 + return unless logger.debug?
  84 +
  85 + payload = event.payload
  86 + name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
  87 +
  88 + debug " #{color(name, YELLOW, true)} #{payload.except(:name).to_json}"
  89 + end
  90 + alias_method :remove, :store
  91 + alias_method :import, :store
46 92 end
47 93  
48 94 # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/controller_runtime.rb
... ...