Commit a5d15c54654f0fc534351de17934b0d6cf0c39d7

Authored by Andrew Kane
1 parent de4a8974

Moved Active Support notifications inline [skip ci]

lib/searchkick.rb
1 1 # dependencies
2 2 require "active_support"
3 3 require "active_support/core_ext/hash/deep_merge"
  4 +require "active_support/core_ext/module/attr_internal"
  5 +require "active_support/notifications"
4 6 require "hashie"
5 7  
6 8 # stdlib
... ... @@ -8,9 +10,11 @@ require "forwardable"
8 10  
9 11 # modules
10 12 require "searchkick/bulk_indexer"
  13 +require "searchkick/controller_runtime"
11 14 require "searchkick/index"
12 15 require "searchkick/indexer"
13 16 require "searchkick/hash_wrapper"
  17 +require "searchkick/log_subscriber"
14 18 require "searchkick/model"
15 19 require "searchkick/multi_search"
16 20 require "searchkick/query"
... ... @@ -23,17 +27,6 @@ require "searchkick/version"
23 27  
24 28 # integrations
25 29 require "searchkick/railtie" if defined?(Rails)
26   -if defined?(ActiveSupport::Notifications)
27   - require "searchkick/logging"
28   - require "searchkick/log_subscriber"
29   - require "searchkick/controller_runtime"
30   -
31   - ActiveSupport.on_load(:action_controller) do
32   - include Searchkick::ControllerRuntime
33   - end
34   -
35   - Searchkick::LogSubscriber.attach_to :searchkick
36   -end
37 30  
38 31 module Searchkick
39 32 # requires faraday
... ... @@ -180,7 +173,13 @@ module Searchkick
180 173  
181 174 def self.multi_search(queries)
182 175 queries = queries.map { |q| q.send(:query) }
183   - Searchkick::MultiSearch.new(queries).perform
  176 + event = {
  177 + name: "Multi Search",
  178 + body: queries.flat_map { |q| [q.params.except(:body).to_json, q.body.to_json] }.map { |v| "#{v}\n" }.join,
  179 + }
  180 + ActiveSupport::Notifications.instrument("multi_search.searchkick", event) do
  181 + Searchkick::MultiSearch.new(queries).perform
  182 + end
184 183 end
185 184  
186 185 # callbacks
... ... @@ -207,7 +206,15 @@ module Searchkick
207 206 begin
208 207 self.callbacks_value = value
209 208 result = yield
210   - indexer.perform if callbacks_value == :bulk
  209 + if callbacks_value == :bulk
  210 + event = {
  211 + name: "Bulk",
  212 + count: indexer.queued_items.size
  213 + }
  214 + ActiveSupport::Notifications.instrument("request.searchkick", event) do
  215 + indexer.perform
  216 + end
  217 + end
211 218 result
212 219 ensure
213 220 self.callbacks_value = previous_value
... ... @@ -322,10 +329,16 @@ module Searchkick
322 329 end
323 330 end
324 331  
  332 +ActiveSupport.on_load(:active_record) do
  333 + extend Searchkick::Model
  334 +end
  335 +
325 336 ActiveSupport.on_load(:mongoid) do
326 337 Mongoid::Document::ClassMethods.include Searchkick::Model
327 338 end
328 339  
329   -ActiveSupport.on_load(:active_record) do
330   - extend Searchkick::Model
  340 +ActiveSupport.on_load(:action_controller) do
  341 + include Searchkick::ControllerRuntime
331 342 end
  343 +
  344 +Searchkick::LogSubscriber.attach_to :searchkick
... ...
lib/searchkick/index.rb
... ... @@ -130,32 +130,47 @@ module Searchkick
130 130 indices
131 131 end
132 132  
133   - # record based
134   - # use helpers for notifications
135   -
136 133 def store(record)
137   - queue_index([record])
  134 + maybe_notify_one(record, "Store") do
  135 + queue_index([record])
  136 + end
138 137 end
139 138  
140 139 def remove(record)
141   - queue_delete([record])
  140 + maybe_notify_one(record, "Remove") do
  141 + queue_delete([record])
  142 + end
142 143 end
143 144  
144 145 def update_record(record, method_name)
145   - queue_update([record], method_name)
  146 + maybe_notify_one(record, "Update") do
  147 + queue_update([record], method_name)
  148 + end
146 149 end
147 150  
148 151 def bulk_delete(records)
149   - queue_delete(records)
  152 + return if records.empty?
  153 +
  154 + maybe_notify_many(records, "Delete") do
  155 + queue_delete(records)
  156 + end
150 157 end
151 158  
152 159 def bulk_index(records)
153   - queue_index(records)
  160 + return if records.empty?
  161 +
  162 + maybe_notify_many(records, "Import") do
  163 + queue_index(records)
  164 + end
154 165 end
155 166 alias_method :import, :bulk_index
156 167  
157 168 def bulk_update(records, method_name)
158   - queue_update(records, method_name)
  169 + return if records.empty?
  170 +
  171 + maybe_notify_many(records, "Update") do
  172 + queue_update(records, method_name)
  173 + end
159 174 end
160 175  
161 176 def search_id(record)
... ... @@ -382,5 +397,35 @@ module Searchkick
382 397 raise Searchkick::Error, "Safety check failed - only run one Model.reindex per model at a time"
383 398 end
384 399 end
  400 +
  401 + def maybe_notify_one(record, name)
  402 + name = record && record.searchkick_klass ? "#{record.searchkick_klass.name} #{name}" : name
  403 + event = {
  404 + name: name,
  405 + id: search_id(record)
  406 + }
  407 + if Searchkick.callbacks_value == :bulk
  408 + yield
  409 + else
  410 + ActiveSupport::Notifications.instrument("request.searchkick", event) do
  411 + yield
  412 + end
  413 + end
  414 + end
  415 +
  416 + def maybe_notify_many(records, name)
  417 + event = {
  418 + name: "#{records.first.searchkick_klass.name} #{name}",
  419 + count: records.size
  420 + }
  421 + event[:id] = search_id(records.first) if records.size == 1
  422 + if Searchkick.callbacks_value == :bulk
  423 + yield
  424 + else
  425 + ActiveSupport::Notifications.instrument("request.searchkick", event) do
  426 + yield
  427 + end
  428 + end
  429 + end
385 430 end
386 431 end
... ...
lib/searchkick/logging.rb
... ... @@ -1,147 +0,0 @@
1   -# based on https://gist.github.com/mnutt/566725
2   -require "active_support/core_ext/module/attr_internal"
3   -
4   -module Searchkick
5   - module QueryWithInstrumentation
6   - def execute_search
7   - name = searchkick_klass ? "#{searchkick_klass.name} Search" : "Search"
8   - event = {
9   - name: name,
10   - query: params
11   - }
12   - ActiveSupport::Notifications.instrument("search.searchkick", event) do
13   - super
14   - end
15   - end
16   - end
17   -
18   - module IndexWithInstrumentation
19   - def store(record)
20   - event = {
21   - name: "#{record.searchkick_klass.name} Store",
22   - id: search_id(record)
23   - }
24   - if Searchkick.callbacks_value == :bulk
25   - super
26   - else
27   - ActiveSupport::Notifications.instrument("request.searchkick", event) do
28   - super
29   - end
30   - end
31   - end
32   -
33   - def remove(record)
34   - name = record && record.searchkick_klass ? "#{record.searchkick_klass.name} Remove" : "Remove"
35   - event = {
36   - name: name,
37   - id: search_id(record)
38   - }
39   - if Searchkick.callbacks_value == :bulk
40   - super
41   - else
42   - ActiveSupport::Notifications.instrument("request.searchkick", event) do
43   - super
44   - end
45   - end
46   - end
47   -
48   - def update_record(record, method_name)
49   - event = {
50   - name: "#{record.searchkick_klass.name} Update",
51   - id: search_id(record)
52   - }
53   - if Searchkick.callbacks_value == :bulk
54   - super
55   - else
56   - ActiveSupport::Notifications.instrument("request.searchkick", event) do
57   - super
58   - end
59   - end
60   - end
61   -
62   - def bulk_index(records)
63   - if records.any?
64   - event = {
65   - name: "#{records.first.searchkick_klass.name} Import",
66   - count: records.size
67   - }
68   - event[:id] = search_id(records.first) if records.size == 1
69   - if Searchkick.callbacks_value == :bulk
70   - super
71   - else
72   - ActiveSupport::Notifications.instrument("request.searchkick", event) do
73   - super
74   - end
75   - end
76   - end
77   - end
78   - alias_method :import, :bulk_index
79   -
80   - def bulk_update(records, *args)
81   - if records.any?
82   - event = {
83   - name: "#{records.first.searchkick_klass.name} Update",
84   - count: records.size
85   - }
86   - event[:id] = search_id(records.first) if records.size == 1
87   - if Searchkick.callbacks_value == :bulk
88   - super
89   - else
90   - ActiveSupport::Notifications.instrument("request.searchkick", event) do
91   - super
92   - end
93   - end
94   - end
95   - end
96   -
97   - def bulk_delete(records)
98   - if records.any?
99   - event = {
100   - name: "#{records.first.searchkick_klass.name} Delete",
101   - count: records.size
102   - }
103   - event[:id] = search_id(records.first) if records.size == 1
104   - if Searchkick.callbacks_value == :bulk
105   - super
106   - else
107   - ActiveSupport::Notifications.instrument("request.searchkick", event) do
108   - super
109   - end
110   - end
111   - end
112   - end
113   - end
114   -
115   - module IndexerWithInstrumentation
116   - def perform
117   - if Searchkick.callbacks_value == :bulk
118   - event = {
119   - name: "Bulk",
120   - count: queued_items.size
121   - }
122   - ActiveSupport::Notifications.instrument("request.searchkick", event) do
123   - super
124   - end
125   - else
126   - super
127   - end
128   - end
129   - end
130   -
131   - module SearchkickWithInstrumentation
132   - def multi_search(searches)
133   - event = {
134   - name: "Multi Search",
135   - body: searches.flat_map { |q| [q.params.except(:body).to_json, q.body.to_json] }.map { |v| "#{v}\n" }.join,
136   - }
137   - ActiveSupport::Notifications.instrument("multi_search.searchkick", event) do
138   - super
139   - end
140   - end
141   - end
142   -end
143   -
144   -Searchkick::Query.prepend(Searchkick::QueryWithInstrumentation)
145   -Searchkick::Index.prepend(Searchkick::IndexWithInstrumentation)
146   -Searchkick::Indexer.prepend(Searchkick::IndexerWithInstrumentation)
147   -Searchkick.singleton_class.prepend(Searchkick::SearchkickWithInstrumentation)
lib/searchkick/query.rb
... ... @@ -233,7 +233,14 @@ module Searchkick
233 233 end
234 234  
235 235 def execute_search
236   - Searchkick.client.search(params)
  236 + name = searchkick_klass ? "#{searchkick_klass.name} Search" : "Search"
  237 + event = {
  238 + name: name,
  239 + query: params
  240 + }
  241 + ActiveSupport::Notifications.instrument("search.searchkick", event) do
  242 + Searchkick.client.search(params)
  243 + end
237 244 end
238 245  
239 246 def prepare
... ...