Sha256: 437c9c1edcc67b987284702433ef05dabe38f03f1fe3aa57273c32479a06309d

Contents?: true

Size: 1.38 KB

Versions: 53

Compression:

Stored size: 1.38 KB

Contents

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. Notice that events get sent even if an error occurs
      # in the passed-in block
      def instrument(name, payload={})
        started = Time.now

        begin
          yield
        rescue Exception => e
          payload[:exception] = [e.class.name, e.message]
          raise e
        ensure
          @notifier.publish(name, started, 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, :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

Version data entries

53 entries across 53 versions & 2 rubygems

Version Path
activesupport-3.1.12 lib/active_support/notifications/instrumenter.rb
activesupport-3.1.11 lib/active_support/notifications/instrumenter.rb
activesupport-3.1.10 lib/active_support/notifications/instrumenter.rb
activesupport-3.1.9 lib/active_support/notifications/instrumenter.rb
dirty_history-0.7.3 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/notifications/instrumenter.rb
dirty_history-0.7.2 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/notifications/instrumenter.rb
dirty_history-0.7.1 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/notifications/instrumenter.rb
dirty_history-0.7.0 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/notifications/instrumenter.rb
dirty_history-0.6.7 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/notifications/instrumenter.rb
dirty_history-0.6.6 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/notifications/instrumenter.rb
dirty_history-0.6.5 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/notifications/instrumenter.rb
dirty_history-0.6.4 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/notifications/instrumenter.rb
activesupport-3.1.8 lib/active_support/notifications/instrumenter.rb
dirty_history-0.6.3 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/notifications/instrumenter.rb
activesupport-3.1.7 lib/active_support/notifications/instrumenter.rb
dirty_history-0.6.2 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/notifications/instrumenter.rb
dirty_history-0.6.1 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/notifications/instrumenter.rb
dirty_history-0.6.0 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/notifications/instrumenter.rb
activesupport-3.1.6 lib/active_support/notifications/instrumenter.rb
dirty_history-0.5.4 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/notifications/instrumenter.rb