mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
51 lines
1.3 KiB
Ruby
51 lines
1.3 KiB
Ruby
module ActiveSupport
|
|
module Notifications
|
|
# Instrumentors are stored in a thread local.
|
|
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. Notice that events get sent even if an error occurs
|
|
# in the passed-in block
|
|
def instrument(name, payload={})
|
|
@notifier.start(name, @id, payload)
|
|
begin
|
|
yield
|
|
rescue Exception => e
|
|
payload[:exception] = [e.class.name, e.message]
|
|
raise e
|
|
ensure
|
|
@notifier.finish(name, @id, payload)
|
|
end
|
|
end
|
|
|
|
private
|
|
def unique_id
|
|
SecureRandom.hex(10)
|
|
end
|
|
end
|
|
|
|
class Event
|
|
attr_reader :name, :time, :end, :transaction_id, :payload, :duration
|
|
|
|
def initialize(name, start, ending, transaction_id, payload)
|
|
@name = name
|
|
@payload = payload.dup
|
|
@time = start
|
|
@transaction_id = transaction_id
|
|
@end = ending
|
|
@duration = 1000.0 * (@end - @time)
|
|
end
|
|
|
|
def parent_of?(event)
|
|
start = (time - event.time) * 1000
|
|
start <= 0 && (start + duration >= event.duration)
|
|
end
|
|
end
|
|
end
|
|
end
|