Sha256: 5761d5b4e18e68763579695773277fca18d07d243ca932adc99e242afd3a9ae0

Contents?: true

Size: 1.53 KB

Versions: 11

Compression:

Stored size: 1.53 KB

Contents

require 'securerandom'

module ActiveSupport
  module Notifications
    # Instrumentors 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={})
        if block_given?
          @notifier.start(name, @id, payload)
          begin
            yield payload
          rescue Exception => e
            payload[:exception] = [e.class.name, e.message]
            raise e
          ensure
            @notifier.finish(name, @id, payload)
          end
        else
          @notifier.measure(name, @id, payload)
        end
      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       = []
      end

      def 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

11 entries across 11 versions & 1 rubygems

Version Path
skylight-0.0.16 lib/skylight/compat/notifications/instrumenter.rb
skylight-0.0.15 lib/skylight/compat/notifications/instrumenter.rb
skylight-0.0.14 lib/skylight/compat/notifications/instrumenter.rb
skylight-0.0.13 lib/skylight/compat/notifications/instrumenter.rb
skylight-0.0.12 lib/skylight/compat/notifications/instrumenter.rb
skylight-0.0.11 lib/skylight/compat/notifications/instrumenter.rb
skylight-0.0.10 lib/skylight/compat/notifications/instrumenter.rb
skylight-0.0.7 lib/skylight/compat/notifications/instrumenter.rb
skylight-0.0.6 lib/skylight/compat/notifications/instrumenter.rb
skylight-0.0.5 lib/skylight/compat/notifications/instrumenter.rb
skylight-0.0.2 lib/skylight/compat/notifications/instrumenter.rb