mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Revert the previous three commits.
* AS::Notifications#instrument should not measure anything, it is not its responsibility; * Adding another argument to AS::Notifications#instrument API needs to be properly discussed;
This commit is contained in:
parent
0d0e793983
commit
ff0d842454
7 changed files with 30 additions and 49 deletions
|
@ -36,15 +36,11 @@ module ActiveRecord
|
||||||
|
|
||||||
define_callbacks :checkout, :checkin
|
define_callbacks :checkout, :checkin
|
||||||
|
|
||||||
@@row_even = true
|
|
||||||
|
|
||||||
def initialize(connection, logger = nil) #:nodoc:
|
def initialize(connection, logger = nil) #:nodoc:
|
||||||
@active = nil
|
@active = nil
|
||||||
@connection, @logger = connection, logger
|
@connection, @logger = connection, logger
|
||||||
@runtime = 0
|
|
||||||
@query_cache_enabled = false
|
@query_cache_enabled = false
|
||||||
@query_cache = {}
|
@query_cache = {}
|
||||||
@instrumenter = ActiveSupport::Notifications.instrumenter
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the human-readable name of the adapter. Use mixed case - one
|
# Returns the human-readable name of the adapter. Use mixed case - one
|
||||||
|
@ -93,11 +89,6 @@ module ActiveRecord
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
def reset_runtime #:nodoc:
|
|
||||||
rt, @runtime = @runtime, 0
|
|
||||||
rt
|
|
||||||
end
|
|
||||||
|
|
||||||
# QUOTING ==================================================
|
# QUOTING ==================================================
|
||||||
|
|
||||||
# Override to return the quoted table name. Defaults to column quoting.
|
# Override to return the quoted table name. Defaults to column quoting.
|
||||||
|
@ -200,15 +191,10 @@ module ActiveRecord
|
||||||
|
|
||||||
def log(sql, name)
|
def log(sql, name)
|
||||||
name ||= "SQL"
|
name ||= "SQL"
|
||||||
info = {}
|
ActiveSupport::Notifications.instrument("sql.active_record",
|
||||||
|
:sql => sql, :name => name, :connection_id => object_id) do
|
||||||
result = @instrumenter.instrument("sql.active_record",
|
|
||||||
{:sql => sql, :name => name, :connection_id => object_id}, info) do
|
|
||||||
yield
|
yield
|
||||||
end
|
end
|
||||||
@runtime += info[:elapsed]
|
|
||||||
|
|
||||||
result
|
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
message = "#{e.class.name}: #{e.message}: #{sql}"
|
message = "#{e.class.name}: #{e.message}: #{sql}"
|
||||||
@logger.debug message if @logger
|
@logger.debug message if @logger
|
||||||
|
|
|
@ -1,11 +1,25 @@
|
||||||
module ActiveRecord
|
module ActiveRecord
|
||||||
class LogSubscriber < ActiveSupport::LogSubscriber
|
class LogSubscriber < ActiveSupport::LogSubscriber
|
||||||
|
def self.runtime=(value)
|
||||||
|
Thread.current["active_record_sql_runtime"] = value
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.runtime
|
||||||
|
Thread.current["active_record_sql_runtime"] ||= 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.reset_runtime
|
||||||
|
rt, self.runtime = runtime, 0
|
||||||
|
rt
|
||||||
|
end
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
super
|
super
|
||||||
@odd_or_even = false
|
@odd_or_even = false
|
||||||
end
|
end
|
||||||
|
|
||||||
def sql(event)
|
def sql(event)
|
||||||
|
self.class.runtime += event.duration
|
||||||
return unless logger.debug?
|
return unless logger.debug?
|
||||||
|
|
||||||
name = '%s (%.1fms)' % [event.payload[:name], event.duration]
|
name = '%s (%.1fms)' % [event.payload[:name], event.duration]
|
||||||
|
@ -31,4 +45,4 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
ActiveRecord::LogSubscriber.attach_to :active_record
|
ActiveRecord::LogSubscriber.attach_to :active_record
|
|
@ -11,10 +11,9 @@ module ActiveRecord
|
||||||
|
|
||||||
def cleanup_view_runtime
|
def cleanup_view_runtime
|
||||||
if ActiveRecord::Base.connected?
|
if ActiveRecord::Base.connected?
|
||||||
connection = ActiveRecord::Base.connection
|
db_rt_before_render = ActiveRecord::LogSubscriber.reset_runtime
|
||||||
db_rt_before_render = connection.reset_runtime
|
|
||||||
runtime = super
|
runtime = super
|
||||||
db_rt_after_render = connection.reset_runtime
|
db_rt_after_render = ActiveRecord::LogSubscriber.reset_runtime
|
||||||
self.db_runtime = db_rt_before_render + db_rt_after_render
|
self.db_runtime = db_rt_before_render + db_rt_after_render
|
||||||
runtime - db_rt_after_render
|
runtime - db_rt_after_render
|
||||||
else
|
else
|
||||||
|
|
|
@ -57,4 +57,8 @@ class LogSubscriberTest < ActiveRecord::TestCase
|
||||||
wait
|
wait
|
||||||
assert_equal 0, @logger.logged(:debug).size
|
assert_equal 0, @logger.logged(:debug).size
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_initializes_runtime
|
||||||
|
Thread.new { assert_equal 0, ActiveRecord::LogSubscriber.runtime }.join
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -47,21 +47,11 @@ module ActiveSupport
|
||||||
attr_writer :notifier
|
attr_writer :notifier
|
||||||
delegate :publish, :unsubscribe, :to => :notifier
|
delegate :publish, :unsubscribe, :to => :notifier
|
||||||
|
|
||||||
def instrument(name, payload = {}, info = nil)
|
def instrument(name, payload = {})
|
||||||
if @instrumenters[name]
|
if @instrumenters[name]
|
||||||
instrumenter.instrument(name, payload, info) {
|
instrumenter.instrument(name, payload) { yield payload if block_given? }
|
||||||
yield payload if block_given?
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
value = nil
|
yield payload if block_given?
|
||||||
if block_given?
|
|
||||||
if info
|
|
||||||
info[:elapsed] = Benchmark.ms { value = yield payload }
|
|
||||||
else
|
|
||||||
value = yield payload
|
|
||||||
end
|
|
||||||
end
|
|
||||||
value
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -14,19 +14,16 @@ module ActiveSupport
|
||||||
# Instrument the given block by measuring the time taken to execute it
|
# Instrument the given block by measuring the time taken to execute it
|
||||||
# and publish it. Notice that events get sent even if an error occurs
|
# and publish it. Notice that events get sent even if an error occurs
|
||||||
# in the passed-in block
|
# in the passed-in block
|
||||||
def instrument(name, payload={}, info = nil)
|
def instrument(name, payload={})
|
||||||
|
started = Time.now
|
||||||
|
|
||||||
begin
|
begin
|
||||||
started = Time.now
|
|
||||||
yield
|
yield
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
payload[:exception] = [e.class.name, e.message]
|
payload[:exception] = [e.class.name, e.message]
|
||||||
raise e
|
raise e
|
||||||
ensure
|
ensure
|
||||||
finished = Time.now
|
@notifier.publish(name, started, Time.now, @id, payload)
|
||||||
if info
|
|
||||||
info[:elapsed] = 1000.0 * (finished.to_f - started.to_f)
|
|
||||||
end
|
|
||||||
@notifier.publish(name, started, finished, @id, payload)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -172,15 +172,6 @@ module Notifications
|
||||||
:exception => ["RuntimeError", "FAIL"]], @events.last.payload
|
:exception => ["RuntimeError", "FAIL"]], @events.last.payload
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_elapsed
|
|
||||||
instrument(:something) do
|
|
||||||
sleep(0.001)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Elapsed returns duration in ms
|
|
||||||
assert_in_delta 1, ActiveSupport::Notifications.instrumenter.elapsed, 100
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_event_is_pushed_even_without_block
|
def test_event_is_pushed_even_without_block
|
||||||
instrument(:awesome, :payload => "notifications")
|
instrument(:awesome, :payload => "notifications")
|
||||||
assert_equal 1, @events.size
|
assert_equal 1, @events.size
|
||||||
|
|
Loading…
Reference in a new issue