mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
109d3ee38d
This is important when trying to keep track of many layers of interrelated calls i.e.: ActiveRecord::Base.transaction do MyModel.find(1) #ActiveRecord::NotFound end # should capture the full time until the error propagation Signed-off-by: José Valim <jose.valim@gmail.com>
54 lines
1.4 KiB
Ruby
54 lines
1.4 KiB
Ruby
require 'active_support/secure_random'
|
|
require 'active_support/core_ext/module/delegation'
|
|
|
|
module ActiveSupport
|
|
module Notifications
|
|
class Instrumenter
|
|
attr_reader :id
|
|
|
|
def initialize(notifier)
|
|
@id = unique_id
|
|
@notifier = notifier
|
|
end
|
|
|
|
# Instrument the given block by measuring the time taken to execute it
|
|
# and publish it.
|
|
def instrument(name, payload={})
|
|
time = Time.now
|
|
begin
|
|
yield(payload) if block_given?
|
|
ensure
|
|
# Notify in an ensure block so that we can be certain end
|
|
# events get sent even if an error occurs in the passed-in block
|
|
@notifier.publish(name, time, Time.now, @id, payload)
|
|
end
|
|
end
|
|
|
|
private
|
|
def unique_id
|
|
SecureRandom.hex(10)
|
|
end
|
|
end
|
|
|
|
class Event
|
|
attr_reader :name, :time, :end, :transaction_id, :payload
|
|
|
|
def initialize(name, start, ending, transaction_id, payload)
|
|
@name = name
|
|
@payload = payload.dup
|
|
@time = start
|
|
@transaction_id = transaction_id
|
|
@end = ending
|
|
end
|
|
|
|
def duration
|
|
@duration ||= 1000.0 * (@end - @time)
|
|
end
|
|
|
|
def parent_of?(event)
|
|
start = (self.time - event.time) * 1000
|
|
start <= 0 && (start + duration >= event.duration)
|
|
end
|
|
end
|
|
end
|
|
end
|