Sha256: 06e70b6b23043002b1732512c3ce6c511c528a57e2f8790ecc8966f5c742007b

Contents?: true

Size: 1.41 KB

Versions: 8

Compression:

Stored size: 1.41 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={})
        time = Time.now
        begin
          yield(payload) if block_given?
        rescue Exception => e
          payload[:exception] = [e.class.name, e.message]
          raise e
        ensure
          @notifier.publish(name, time, 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

      def initialize(name, start, ending, transaction_id, payload)
        @name           = name
        @payload        = payload.dup
        @time           = start
        @transaction_id = transaction_id
        @end            = ending
      end

      def duration
        @duration ||= 1000.0 * (@end - @time)
      end

      def parent_of?(event)
        start = (self.time - event.time) * 1000
        start <= 0 && (start + duration >= event.duration)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
csd-0.1.5 lib/active_support/notifications/instrumenter.rb
csd-0.1.4 lib/active_support/notifications/instrumenter.rb
csd-0.1.3 lib/active_support/notifications/instrumenter.rb
csd-0.1.2 lib/active_support/notifications/instrumenter.rb
csd-0.1.1 lib/active_support/notifications/instrumenter.rb
csd-0.1.0 lib/active_support/notifications/instrumenter.rb
csd-0.0.16 lib/active_support/notifications/instrumenter.rb
activesupport-3.0.0.beta4 lib/active_support/notifications/instrumenter.rb