class SourceRoute::GenerateResult

How it work

  1. Config collect route options

  2. Proxy generate TracePoint Filter

  3. Proxy generate TracePoint Monitor Block

  4. Generator collect Wanted TracePoint

  5. Parse and Generate Useful data from wanted TracePoint

  6. Output data with correct format

Constants

DEFAULT_ATTRS

see event description in TracePoint API Doc

Attributes

collected_data[R]
tp_self_caches[R]
trace_chain[R]

Public Class Methods

clear_wanted_attributes() click to toggle source
# File lib/source_route/generate_result.rb, line 50
def self.clear_wanted_attributes
  @wanted_attributes = {}
end
new(proxy) click to toggle source
# File lib/source_route/generate_result.rb, line 33
def initialize(proxy)
  @proxy = proxy
  @trace_chain = TraceChain.new
  @tp_self_caches = []
end
wanted_attributes(eve) click to toggle source

it cached and only calculate once for one trace point block round

# File lib/source_route/generate_result.rb, line 40
def self.wanted_attributes(eve)
  event = eve.to_sym
  @wanted_attributes.fetch event do
    attrs = DEFAULT_ATTRS[event] + Array(SourceRoute.proxy.config.show_additional_attrs)
    attrs.push(:event)
    @wanted_attributes[event] = attrs.uniq
    @wanted_attributes[event]
  end
end

Public Instance Methods

assign_tp_self_caches(tp_ins) click to toggle source

include? will evaluate @tp.self, if @tp.self is AR::Relation, it could cause problems So that's why I use object_id as replace

# File lib/source_route/generate_result.rb, line 84
def assign_tp_self_caches(tp_ins)
  unless tp_self_caches.find { |tp_cache| tp_cache.object_id.equal? tp_ins.self.object_id }
    tp_self_caches.push tp_ins.self
  end
end
jsonify_events() click to toggle source
# File lib/source_route/generate_result.rb, line 90
def jsonify_events
  JSON.dump(@proxy.config.event.map(&:to_s))
end
jsonify_tp_self_caches() click to toggle source
# File lib/source_route/generate_result.rb, line 104
def jsonify_tp_self_caches
  JSON.dump(tp_self_caches.clone
             .map(&:to_s))
end
jsonify_trace_chain() click to toggle source
# File lib/source_route/generate_result.rb, line 94
def jsonify_trace_chain
  value = trace_chain.chain.map(&:to_hash)
  JSON.dump(value)

  # not worked
  # trace_chain.to_json
  # json_array = trace_chain.map { |result| JSON.dump(result) }
  # '[ ' + json_array.join(',') + ' ]'
end
output(tp_ins) click to toggle source
# File lib/source_route/generate_result.rb, line 54
def output(tp_ins)
  format = @proxy.config.output_format

  assign_tp_self_caches(tp_ins)
  # we cant call method on tp_ins outside of track block,
  # so we have to run it immediately
  @collected_data = TpResult.new(tp_ins)
  case format
  when :console
    console_put(tp_ins)
  when :html
    # we cant generate html right now becase the tp callback is still in process
    # so we gather data into array
    @trace_chain.push(TpResult.new(tp_ins))
  when :silence, :none
  # do nothing at now
  when :test
    @trace_chain.push(TpResult.new(tp_ins))
  when :stack_overflow
    console_stack_overflow
  when Proc
    format.call(tp_ins)
  else
    klass = "SourceRoute::Formats::#{format.to_s.capitalize}"
    ::SourceRoute.const_get(klass).render(self, tp_ins, @collected_data)
  end
end

Private Instance Methods

console_put(tp) click to toggle source
# File lib/source_route/generate_result.rb, line 111
def console_put(tp)
  ret = []
  ret << "#{collected_data.defined_class.inspect}##{collected_data.method_id}"
  left_attrs = self.class.wanted_attributes(tp.event) - [:defined_class, :method_id]
  left_values = left_attrs.inject({}) do |memo, key|
    memo[key] = collected_data.send(key)
    memo
  end
  unless left_values == {}
    ret << left_values
  end
  ap ret
end
console_stack_overflow() click to toggle source
# File lib/source_route/generate_result.rb, line 125
def console_stack_overflow
  ap "#{collected_data.defined_class.inspect}##{collected_data.method_id}"
end