Class: ActiveRecord::ExplainRegistry::Subscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/explain_registry.rb

Overview

:nodoc:

Constant Summary collapse

MUTEX =
Mutex.new
IGNORED_PAYLOADS =

SCHEMA queries cannot be EXPLAINed, also we do not want to run EXPLAIN on our own EXPLAINs no matter how loopingly beautiful that would be.

On the other hand, we want to monitor the performance of our real database queries, not the performance of the access to the query cache.

%w(SCHEMA EXPLAIN)
EXPLAINED_SQLS =
/\A\s*(\/\*.*\*\/)?\s*(with|select|update|delete|insert)\b/i

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ensure_subscribedObject



16
17
18
19
20
21
22
23
24
# File 'lib/active_record/explain_registry.rb', line 16

def ensure_subscribed
  return if @subscribed
  MUTEX.synchronize do
    return if @subscribed

    ActiveSupport::Notifications.subscribe("sql.active_record", new)
    @subscribed = true
  end
end

Instance Method Details

#finish(name, id, payload) ⇒ Object



31
32
33
34
35
# File 'lib/active_record/explain_registry.rb', line 31

def finish(name, id, payload)
  if ExplainRegistry.collect? && !ignore_payload?(payload)
    ExplainRegistry.queries << payload.values_at(:sql, :binds)
  end
end

#ignore_payload?(payload) ⇒ Boolean



48
49
50
51
52
53
# File 'lib/active_record/explain_registry.rb', line 48

def ignore_payload?(payload)
  payload[:exception] ||
    payload[:cached] ||
    IGNORED_PAYLOADS.include?(payload[:name]) ||
    !payload[:sql].match?(EXPLAINED_SQLS)
end

#silenced?(_name) ⇒ Boolean



37
38
39
# File 'lib/active_record/explain_registry.rb', line 37

def silenced?(_name)
  !ExplainRegistry.collect?
end

#start(name, id, payload) ⇒ Object



27
28
29
# File 'lib/active_record/explain_registry.rb', line 27

def start(name, id, payload)
  # unused
end