Class: ActiveRecordProfiler::LogSubscriber

Inherits:
ActiveRecord::LogSubscriber
  • Object
show all
Defined in:
lib/active-record-profiler/log_subscriber.rb

Instance Method Summary collapse

Instance Method Details

#sql(event) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active-record-profiler/log_subscriber.rb', line 5

def sql(event)
  start_time = Time.now.to_f
  payload = event.payload

  duration = event.duration
  sql_string = payload[:sql]

  begin
    collector = ActiveRecordProfiler::Collector.instance
    loc = collector.call_location_name
    collector.record_caller_info(loc, duration, sql_string.strip)

    collector.record_self_info((Time.now.to_f - start_time), 'updating profiler stats') if ActiveRecordProfiler::Collector.profile_self?

    start_time = Time.now.to_f
    if collector.should_flush_stats?
      collector.flush_query_sites_statistics 
      collector.record_self_info((Time.now.to_f - start_time), 'flushing profiler stats') if ActiveRecordProfiler::Collector.profile_self?
    end
  rescue Exception => e
    Rails.logger.error("Caught exception in #{self.class}: #{e} at #{e.backtrace.first}")
  end
end