lib/shiba/query_watcher.rb in shiba-0.2.0 vs lib/shiba/query_watcher.rb in shiba-0.2.2
- old
+ new
@@ -1,39 +1,31 @@
require 'shiba/query'
require 'shiba/backtrace'
-require 'json'
-require 'rails'
module Shiba
+ # Logs ActiveRecord SELECT queries that originate from application code.
class QueryWatcher
- def self.watch(file)
- new(file).tap { |w| w.watch }
- end
-
attr_reader :queries
def initialize(file)
@file = file
# fixme mem growth on this is kinda nasty
@queries = {}
end
- # Logs ActiveRecord SELECT queries that originate from application code.
- def watch
- ActiveSupport::Notifications.subscribe('sql.active_record') do |name, start, finish, id, payload|
- sql = payload[:sql]
+ def call(name, start, finish, id, payload)
+ sql = payload[:sql]
+ return if !sql.start_with?("SELECT")
- if sql.start_with?("SELECT")
- fingerprint = Query.get_fingerprint(sql)
- if !@queries[fingerprint]
- if lines = Backtrace.from_app
- @file.puts("#{sql} /*shiba#{lines}*/")
- end
- end
- @queries[fingerprint] = true
- end
- end
+ fingerprint = Query.get_fingerprint(sql)
+ return if @queries[fingerprint]
+
+ lines = Backtrace.from_app
+ return if !lines
+
+ @file.puts("#{sql} /*shiba#{lines}*/")
+ @queries[fingerprint] = true
end
end
-end
+end
\ No newline at end of file