lib/active_support/notifications/instrumenter.rb in activesupport-3.2.22.5 vs lib/active_support/notifications/instrumenter.rb in activesupport-4.0.0.beta1

- old
+ new

@@ -1,53 +1,72 @@ -require 'active_support/core_ext/module/delegation' +require 'securerandom' module ActiveSupport module Notifications + # Instrumentors are stored in a thread local. class Instrumenter attr_reader :id def initialize(notifier) - @id = unique_id + @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 + # in the passed-in block. def instrument(name, payload={}) - started = Time.now - + start name, payload begin yield rescue Exception => e payload[:exception] = [e.class.name, e.message] raise e ensure - @notifier.publish(name, started, Time.now, @id, payload) + finish name, payload end end + # Send a start notification with +name+ and +payload+. + def start(name, payload) + @notifier.start name, @id, payload + end + + # Send a finish notification with +name+ and +payload+. + def finish(name, payload) + @notifier.finish name, @id, payload + end + private - def unique_id - SecureRandom.hex(10) - end + + def unique_id + SecureRandom.hex(10) + end end class Event - attr_reader :name, :time, :end, :transaction_id, :payload, :duration + attr_reader :name, :time, :transaction_id, :payload, :children + attr_accessor :end 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) + @children = [] end + def duration + 1000.0 * (self.end - time) + end + + def <<(event) + @children << event + end + def parent_of?(event) - start = (time - event.time) * 1000 - start <= 0 && (start + duration >= event.duration) + @children.include? event end end end end