Sha256: 5e73bb3378c41a0bc7533347d2a0d96122cbd6db9e7de2acbcf31f310241f490

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

require 'tracebin/recorder'
require 'tracebin/helpers'

module Tracebin
  ##
  # This is the timer for a top-level transaction. Transactions include
  # request/response cycles, as well as background jobs. Background jobs
  # subclass this class and overwrite the +#transaction_type+ method.
  class Timer
    include ::Tracebin::Helpers

    attr_accessor :transaction_name
    attr_reader :events

    def initialize(transaction_name = nil)
      @transaction_name = transaction_name
      @start_time = nil
      @stop_time = nil
    end

    def start!
      @start_time = timestamp_string
      Recorder.start_recording
    end

    def stop!
      collect_events
      Recorder.stop_recording
      @stop_time = timestamp_string
    end

    def payload
      {
        type: :cycle_transaction,

        data: {
          transaction_type: transaction_type,
          name: @transaction_name,

          start: @start_time,
          stop: @stop_time,
          duration: duration,

          events: @events
        }
      }
    end

    def duration
      milliseconds_between @stop_time, @start_time
    end

    def transaction_type
      'request_response'
    end

    private

    def collect_events
      @events = Recorder.events
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tracebin-0.0.13 lib/tracebin/timer.rb
tracebin-0.0.12 lib/tracebin/timer.rb
tracebin-0.0.11 lib/tracebin/timer.rb