From 383550e7eeeac4a445e83327476e29eb3384ca89 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sun, 17 Aug 2014 23:44:03 -0700 Subject: [PATCH] Added logging for import, store, and remove calls --- CHANGELOG.md | 1 + lib/searchkick/logging.rb | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 501ee96..04365bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Added `wordnet` option - Added `edit_distance` option to eventually replace `distance` option - Catch misspelling of `misspellings` option +- Improved logging ## 0.8.1 diff --git a/lib/searchkick/logging.rb b/lib/searchkick/logging.rb index a3305f2..9fec0a9 100644 --- a/lib/searchkick/logging.rb +++ b/lib/searchkick/logging.rb @@ -11,10 +11,44 @@ module Searchkick execute_without_instrumentation end end - alias_method_chain :execute, :instrumentation end + class Index + def store_with_instrumentation(record) + event = { + name: "#{record.searchkick_klass.name} Store", + id: search_id(record) + } + ActiveSupport::Notifications.instrument("store.searchkick", event) do + store_without_instrumentation(record) + end + end + alias_method_chain :store, :instrumentation + + def remove_with_instrumentation(record) + event = { + name: "#{record.searchkick_klass.name} Remove", + id: search_id(record) + } + ActiveSupport::Notifications.instrument("remove.searchkick", event) do + remove_without_instrumentation(record) + end + end + alias_method_chain :remove, :instrumentation + + def import_with_instrumentation(records) + event = { + name: "#{records.first.searchkick_klass.name} Import", + count: records.size + } + ActiveSupport::Notifications.instrument("import.searchkick", event) do + import_without_instrumentation(records) + end + end + alias_method_chain :import, :instrumentation + end + # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/log_subscriber.rb class LogSubscriber < ActiveSupport::LogSubscriber def self.runtime=(value) @@ -43,6 +77,18 @@ module Searchkick host = Searchkick.client.transport.hosts.first 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}'" end + + def store(event) + self.class.runtime += event.duration + return unless logger.debug? + + payload = event.payload + name = "#{payload[:name]} (#{event.duration.round(1)}ms)" + + debug " #{color(name, YELLOW, true)} #{payload.except(:name).to_json}" + end + alias_method :remove, :store + alias_method :import, :store end # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/controller_runtime.rb -- libgit2 0.21.0