Sha256: 80849c47456b2b3aea9ae391d264eea00e380670f807f38b9b77ee2eed1d85c0

Contents?: true

Size: 1.38 KB

Versions: 44

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

44 entries across 44 versions & 3 rubygems

Version Path
social_url_stats-0.0.1 vendor/ruby/1.9.1/gems/activesupport-3.0.0/lib/active_support/notifications/instrumenter.rb
activesupport-3.0.20 lib/active_support/notifications/instrumenter.rb
activesupport-3.0.19 lib/active_support/notifications/instrumenter.rb
activesupport-3.0.18 lib/active_support/notifications/instrumenter.rb
activesupport-3.0.17 lib/active_support/notifications/instrumenter.rb
activesupport-3.0.16 lib/active_support/notifications/instrumenter.rb
activesupport-3.0.15 lib/active_support/notifications/instrumenter.rb
activesupport-3.0.14 lib/active_support/notifications/instrumenter.rb
activesupport-3.0.13 lib/active_support/notifications/instrumenter.rb
activesupport-3.0.13.rc1 lib/active_support/notifications/instrumenter.rb
activesupport-3.0.12 lib/active_support/notifications/instrumenter.rb
activesupport-3.0.12.rc1 lib/active_support/notifications/instrumenter.rb
activesupport-3.0.11 lib/active_support/notifications/instrumenter.rb
messagebus_ruby_api-0.4.7 spec/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/notifications/instrumenter.rb
messagebus_ruby_api-0.4.4 spec/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/notifications/instrumenter.rb
activesupport-3.0.10 lib/active_support/notifications/instrumenter.rb
activesupport-3.0.10.rc1 lib/active_support/notifications/instrumenter.rb
activesupport-3.0.9 lib/active_support/notifications/instrumenter.rb
activesupport-3.0.9.rc5 lib/active_support/notifications/instrumenter.rb
activesupport-3.0.9.rc4 lib/active_support/notifications/instrumenter.rb