mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Preserve cached queries name in AS notifications
This commit is contained in:
parent
1996624216
commit
84d35da86c
4 changed files with 18 additions and 11 deletions
|
@ -65,7 +65,7 @@ module ActiveRecord
|
||||||
if @query_cache_enabled && !locked?(arel)
|
if @query_cache_enabled && !locked?(arel)
|
||||||
arel, binds = binds_from_relation arel, binds
|
arel, binds = binds_from_relation arel, binds
|
||||||
sql = to_sql(arel, binds)
|
sql = to_sql(arel, binds)
|
||||||
cache_sql(sql, binds) { super(sql, name, binds, preparable: preparable) }
|
cache_sql(sql, name, binds) { super(sql, name, binds, preparable: preparable) }
|
||||||
else
|
else
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
@ -73,11 +73,17 @@ module ActiveRecord
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def cache_sql(sql, binds)
|
def cache_sql(sql, name, binds)
|
||||||
result =
|
result =
|
||||||
if @query_cache[sql].key?(binds)
|
if @query_cache[sql].key?(binds)
|
||||||
ActiveSupport::Notifications.instrument("sql.active_record",
|
ActiveSupport::Notifications.instrument(
|
||||||
sql: sql, binds: binds, name: "CACHE", connection_id: object_id)
|
"sql.active_record",
|
||||||
|
sql: sql,
|
||||||
|
binds: binds,
|
||||||
|
name: name,
|
||||||
|
connection_id: object_id,
|
||||||
|
cached: true,
|
||||||
|
)
|
||||||
@query_cache[sql][binds]
|
@query_cache[sql][binds]
|
||||||
else
|
else
|
||||||
@query_cache[sql][binds] = yield
|
@query_cache[sql][binds] = yield
|
||||||
|
|
|
@ -18,10 +18,13 @@ module ActiveRecord
|
||||||
#
|
#
|
||||||
# On the other hand, we want to monitor the performance of our real database
|
# 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.
|
# queries, not the performance of the access to the query cache.
|
||||||
IGNORED_PAYLOADS = %w(SCHEMA EXPLAIN CACHE)
|
IGNORED_PAYLOADS = %w(SCHEMA EXPLAIN)
|
||||||
EXPLAINED_SQLS = /\A\s*(with|select|update|delete|insert)\b/i
|
EXPLAINED_SQLS = /\A\s*(with|select|update|delete|insert)\b/i
|
||||||
def ignore_payload?(payload)
|
def ignore_payload?(payload)
|
||||||
payload[:exception] || IGNORED_PAYLOADS.include?(payload[:name]) || payload[:sql] !~ EXPLAINED_SQLS
|
payload[:exception] ||
|
||||||
|
payload[:cached] ||
|
||||||
|
IGNORED_PAYLOADS.include?(payload[:name]) ||
|
||||||
|
payload[:sql] !~ EXPLAINED_SQLS
|
||||||
end
|
end
|
||||||
|
|
||||||
ActiveSupport::Notifications.subscribe("sql.active_record", new)
|
ActiveSupport::Notifications.subscribe("sql.active_record", new)
|
||||||
|
|
|
@ -35,6 +35,7 @@ module ActiveRecord
|
||||||
return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])
|
return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])
|
||||||
|
|
||||||
name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
|
name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
|
||||||
|
name = "CACHE #{name}" if payload[:cached]
|
||||||
sql = payload[:sql]
|
sql = payload[:sql]
|
||||||
binds = nil
|
binds = nil
|
||||||
|
|
||||||
|
|
|
@ -125,12 +125,9 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(name, start, finish, message_id, values)
|
def call(name, start, finish, message_id, values)
|
||||||
|
return if values[:cached]
|
||||||
|
|
||||||
sql = values[:sql]
|
sql = values[:sql]
|
||||||
|
|
||||||
# FIXME: this seems bad. we should probably have a better way to indicate
|
|
||||||
# the query was cached
|
|
||||||
return if "CACHE" == values[:name]
|
|
||||||
|
|
||||||
self.class.log_all << sql
|
self.class.log_all << sql
|
||||||
self.class.log << sql unless ignore.match?(sql)
|
self.class.log << sql unless ignore.match?(sql)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue