Sha256: 062f04695936cb88bb095298e834c19e77afd13fc02f44b81763c781264e17ac

Contents?: true

Size: 1.72 KB

Versions: 306

Compression:

Stored size: 1.72 KB

Contents

require 'securerandom'

module ActiveSupport
  module Notifications
    # Instrumenters are stored in a thread local.
    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={})
        start name, payload
        begin
          yield payload
        rescue Exception => e
          payload[:exception] = [e.class.name, e.message]
          raise e
        ensure
          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
    end

    class Event
      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
        @children       = []
        @duration       = nil
      end

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

      def <<(event)
        @children << event
      end

      def parent_of?(event)
        @children.include? event
      end
    end
  end
end

Version data entries

306 entries across 301 versions & 13 rubygems

Version Path
activesupport-4.2.11.3 lib/active_support/notifications/instrumenter.rb
activesupport-4.2.11.2 lib/active_support/notifications/instrumenter.rb
cocoapods-dependency-html-0.0.2 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/notifications/instrumenter.rb
cocoapods-dependency-html-0.0.1 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/notifications/instrumenter.rb
activesupport-4.2.11.1 lib/active_support/notifications/instrumenter.rb
activesupport-4.2.11 lib/active_support/notifications/instrumenter.rb
activesupport-4.2.10 lib/active_support/notifications/instrumenter.rb
activesupport-4.2.10.rc1 lib/active_support/notifications/instrumenter.rb
activesupport-4.2.9 lib/active_support/notifications/instrumenter.rb
activesupport-4.2.9.rc2 lib/active_support/notifications/instrumenter.rb
activesupport-4.2.9.rc1 lib/active_support/notifications/instrumenter.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activesupport-4.2.8/lib/active_support/notifications/instrumenter.rb
activesupport-4.2.8 lib/active_support/notifications/instrumenter.rb
activesupport-4.2.8.rc1 lib/active_support/notifications/instrumenter.rb
appsignal-1.3.6 lib/vendor/active_support/notifications/instrumenter.rb
appsignal-1.3.6.beta.1 lib/vendor/active_support/notifications/instrumenter.rb
appsignal-1.3.5 lib/vendor/active_support/notifications/instrumenter.rb
appsignal-1.3.5.beta.1 lib/vendor/active_support/notifications/instrumenter.rb
appsignal-1.3.4 lib/vendor/active_support/notifications/instrumenter.rb
appsignal-1.3.3 lib/vendor/active_support/notifications/instrumenter.rb