Skip to content

Instantly share code, notes, and snippets.

@fredv
Created July 12, 2012 14:30
Show Gist options
  • Save fredv/3098462 to your computer and use it in GitHub Desktop.
Save fredv/3098462 to your computer and use it in GitHub Desktop.
Solr instrumentation for Supernova after Michael Nutt
module SolrInstrumentation
class LogSubscriber < ActiveSupport::LogSubscriber
def query(event)
self.class.runtime += event.duration
return unless logger.debug?
name = '%s (%.1fms)' % ["SOLR Query", event.duration]
# produces: 'query: "foo" OR "bar", rows: 3, ...'
query = event.payload[:query].map{ |k, v| "#{k}: #{color(v, BOLD, true)}" }.join(', ')
debug " #{color(name, YELLOW, true)} [ #{query} ]"
end
def self.runtime
Thread.current['solr_query_runtime'] ||= 0
end
def self.runtime=(value)
Thread.current['solr_query_runtime'] = value
end
end
module ControllerRuntime
extend ActiveSupport::Concern
protected
def append_info_to_payload(payload)
super
payload[:solr_runtime] = SolrInstrumentation::LogSubscriber.runtime
end
module ClassMethods
def log_process_action(payload)
messages, solr_runtime = super, payload[:solr_runtime]
messages << ("Solr: %.1fms" % solr_runtime.to_f) if solr_runtime
messages
end
end
end
end
SolrInstrumentation::LogSubscriber.attach_to :solr
ActiveSupport.on_load(:action_controller) do
include SolrInstrumentation::ControllerRuntime
end
# Instrument query calls in code:
#
# ActiveSupport::Notifications.instrument("query.solr", query: solr_search_params) do | 28
# @solr_result ||= Search::SolrHandler.select(solr_search_params) | 29 def append_info_to_payload(payload)
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment