2009-11-28 15:49:07 -05:00
|
|
|
require 'active_support/core_ext/module/delegation'
|
|
|
|
|
|
|
|
module ActiveSupport
|
|
|
|
module Notifications
|
|
|
|
class Instrumenter
|
2010-01-04 16:22:21 -05:00
|
|
|
attr_reader :id
|
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
def initialize(notifier)
|
|
|
|
@id = unique_id
|
|
|
|
@notifier = notifier
|
|
|
|
end
|
|
|
|
|
2010-01-13 16:28:18 -05:00
|
|
|
# Instrument the given block by measuring the time taken to execute it
|
2010-05-02 16:40:20 -04:00
|
|
|
# and publish it. Notice that events get sent even if an error occurs
|
|
|
|
# in the passed-in block
|
2010-07-25 14:46:42 -04:00
|
|
|
def instrument(name, payload={})
|
|
|
|
started = Time.now
|
|
|
|
|
2010-04-27 17:13:47 -04:00
|
|
|
begin
|
2010-07-21 19:29:26 -04:00
|
|
|
yield
|
2010-05-02 16:40:20 -04:00
|
|
|
rescue Exception => e
|
|
|
|
payload[:exception] = [e.class.name, e.message]
|
|
|
|
raise e
|
2010-04-27 17:13:47 -04:00
|
|
|
ensure
|
2010-07-25 14:46:42 -04:00
|
|
|
@notifier.publish(name, started, Time.now, @id, payload)
|
2010-04-27 17:13:47 -04:00
|
|
|
end
|
2009-11-28 15:49:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def unique_id
|
|
|
|
SecureRandom.hex(10)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Event
|
2010-07-17 18:53:22 -04:00
|
|
|
attr_reader :name, :time, :end, :transaction_id, :payload, :duration
|
2009-11-28 15:49:07 -05:00
|
|
|
|
2009-12-26 14:28:53 -05:00
|
|
|
def initialize(name, start, ending, transaction_id, payload)
|
2009-11-28 15:49:07 -05:00
|
|
|
@name = name
|
|
|
|
@payload = payload.dup
|
|
|
|
@time = start
|
|
|
|
@transaction_id = transaction_id
|
|
|
|
@end = ending
|
2010-07-17 18:53:22 -04:00
|
|
|
@duration = 1000.0 * (@end - @time)
|
2009-11-28 15:49:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def parent_of?(event)
|
2010-07-17 18:53:22 -04:00
|
|
|
start = (time - event.time) * 1000
|
2009-11-28 15:49:07 -05:00
|
|
|
start <= 0 && (start + duration >= event.duration)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|