Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

instrument elastic searchable queries

  • Loading branch information...
commit 2b3376c5f4508770569df652167e015747b5caee 1 parent 0b338b3
Ryan Sonnek authored
37 lib/elastic_searchable/log_instrumentation.rb
... ... @@ -0,0 +1,37 @@
  1 +module ElasticSearchable
  2 + class LogSubscriber < ActiveSupport::LogSubscriber
  3 + def query(event)
  4 + return unless logger.debug?
  5 +
  6 + name = '%s (%.1fms)' % ["ElasticSearchable Query", event.duration]
  7 +
  8 + params = event.payload[:query].merge event.payload[:options]
  9 + # produces: 'query: "foo" OR "bar", rows: 3, ...'
  10 + options = params.map{ |k, v| "#{k}: #{color(v, BOLD, true)}" }.join(', ')
  11 +
  12 + debug " #{color(name, YELLOW, true)} [ #{query} ]"
  13 + end
  14 + end
  15 +
  16 + module ControllerRuntime
  17 + extend ActiveSupport::Concern
  18 +
  19 + protected
  20 + def append_info_to_payload(payload)
  21 + super
  22 + payload[:elastic_searchable_runtime] = ElasticSearchable::LogSubscriber.runtime
  23 + end
  24 +
  25 + module ClassMethods
  26 + def log_process_action(payload)
  27 + messages, elastic_searchable_runtime = super, payload[:elastic_searchable_runtime]
  28 + messages << ("ElasticSearchable: %.1fms" % elastic_searchable_runtime.to_f) if elastic_searchable_runtime
  29 + messages
  30 + end
  31 + end
  32 + end
  33 +end
  34 +ElasticSearchable::LogSubscriber.attach_to :elastic_searchable
  35 +ActiveSupport.on_load(:action_controller) do
  36 + include ElasticSearchable::ControllerRuntime
  37 +end
18 lib/elastic_searchable/queries.rb
@@ -35,16 +35,18 @@ def search(query, options = {})
35 35 query[:sort] = sort
36 36 end
37 37
38   - response = ElasticSearchable.request :get, index_mapping_path('_search'), :query => query, :json_body => options
39   - hits = response['hits']
40   - ids = hits['hits'].collect {|h| h['_id'].to_i }
41   - results = self.find(ids).sort_by {|result| ids.index(result.id) }
  38 + ActiveSupport::Notifications.instrument("query.elastic_searchable", :query => query, :options => options) do
  39 + response = ElasticSearchable.request :get, index_mapping_path('_search'), :query => query, :json_body => options
  40 + hits = response['hits']
  41 + ids = hits['hits'].collect {|h| h['_id'].to_i }
  42 + results = self.find(ids).sort_by {|result| ids.index(result.id) }
42 43
43   - results.each do |result|
44   - result.instance_variable_set '@hit', hits['hits'][ids.index(result.id)]
45   - end
  44 + results.each do |result|
  45 + result.instance_variable_set '@hit', hits['hits'][ids.index(result.id)]
  46 + end
46 47
47   - ElasticSearchable::Paginator.handler.new(results, page, options[:size], hits['total'])
  48 + return ElasticSearchable::Paginator.handler.new(results, page, options[:size], hits['total'])
  49 + end
48 50 end
49 51
50 52 private

0 comments on commit 2b3376c

Please sign in to comment.
Something went wrong with that request. Please try again.