Class: RubyMethodTracer::EnhancedTracer

Inherits:
SimpleTracer show all
Defined in:
lib/ruby_method_tracer/enhanced_tracer.rb

Overview

EnhancedTracer extends SimpleTracer with hierarchical call tracking

In addition to the basic tracing functionality, this tracer maintains a call tree that captures parent-child relationships between method calls, enabling visualization of complex call hierarchies.

Options:

  • All options from SimpleTracer

  • :track_hierarchy (Boolean): Enable call tree tracking; defaults to true

Usage:

tracer = RubyMethodTracer::EnhancedTracer.new(MyClass, threshold: 0.005)
tracer.trace_method(:expensive_call)
tracer.print_tree

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SimpleTracer

#fetch_results, #record_call

Constructor Details

#initialize(target_class, **options) ⇒ EnhancedTracer

Returns a new instance of EnhancedTracer.



25
26
27
28
29
30
# File 'lib/ruby_method_tracer/enhanced_tracer.rb', line 25

def initialize(target_class, **options)
  super
  @call_tree = CallTree.new
  @track_hierarchy = @options.fetch(:track_hierarchy, true)
  @formatter = Formatters::TreeFormatter.new
end

Instance Attribute Details

#call_treeObject (readonly)

Returns the value of attribute call_tree.



23
24
25
# File 'lib/ruby_method_tracer/enhanced_tracer.rb', line 23

def call_tree
  @call_tree
end

Instance Method Details

#clear_resultsObject

Clear both simple tracer results and call tree



79
80
81
82
# File 'lib/ruby_method_tracer/enhanced_tracer.rb', line 79

def clear_results
  super
  @call_tree.clear
end

#fetch_enhanced_resultsHash

Get enhanced results including both flat list and hierarchy

Returns:

  • (Hash)

    Results with call tree and statistics



70
71
72
73
74
75
76
# File 'lib/ruby_method_tracer/enhanced_tracer.rb', line 70

def fetch_enhanced_results
  {
    flat_calls: fetch_results,
    call_hierarchy: @call_tree.call_hierarchy,
    statistics: @call_tree.statistics
  }
end

#format_tree(options = {}) ⇒ String

Get call tree as string without printing

Parameters:

  • options (Hash) (defaults to: {})

    Formatting options

Returns:

  • (String)

    Formatted call tree



63
64
65
# File 'lib/ruby_method_tracer/enhanced_tracer.rb', line 63

def format_tree(options = {})
  @formatter.format(@call_tree, options)
end

Print the call tree visualization

Parameters:

  • options (Hash) (defaults to: {})

    Formatting options

Options Hash (options):

  • :show_errors (Boolean) — default: true

    Include error information

  • :colorize (Boolean) — default: true

    Apply colors to output



55
56
57
# File 'lib/ruby_method_tracer/enhanced_tracer.rb', line 55

def print_tree(options = {})
  puts @formatter.format(@call_tree, options)
end

#trace_method(name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_method_tracer/enhanced_tracer.rb', line 32

def trace_method(name)
  method_name = name.to_sym
  visibility = method_visibility(method_name)
  return unless visibility
  return unless mark_wrapped?(method_name)

  aliased = alias_for(method_name)
  @target_class.send(:alias_method, aliased, method_name)

  tracer = self
  key = @tracer_key # unique per tracer instance; prevents cross-tracer interference

  # Build wrapper that tracks hierarchy
  @target_class.define_method(method_name, &build_enhanced_wrapper(aliased, method_name, key, tracer))

  @target_class.send(visibility, method_name)
end