Class: Instrument

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/bug/panels/speedtracer_panel/instrumentation.rb

Defined Under Namespace

Modules: TraceRunner

Instance Method Summary collapse

Constructor Details

#initialize(const) ⇒ Instrument

Returns a new instance of Instrument.



38
39
40
41
42
43
44
# File 'lib/rack/bug/panels/speedtracer_panel/instrumentation.rb', line 38

def initialize(const)
  @const = const
  @traced = {}
  @unsafe_names = (@const.public_instance_methods(true) + 
                  @const.protected_instance_methods(true) + 
                  @const.private_instance_methods(true)).sort.uniq
end

Instance Method Details

#build_tracing_wrappers(target, context, *methods) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rack/bug/panels/speedtracer_panel/instrumentation.rb', line 83

def build_tracing_wrappers(target, context, *methods)
  safe_method_names(methods).each do |method_name, old_method|
    next if @traced.has_key?(method_name)
    @traced[method_name] = true


    #TODO: nicer chaining
    target.class_eval <<-EOC, __FILE__, __LINE__
    alias #{old_method} #{method_name}
    include TraceRunner
    def #{method_name}(*args, &block)

      trace_run("#{context}", caller(0)[0], args) do
        #{old_method}(*args, &block)
      end
    end
    EOC
  end
end

#safe_method_names(method_names) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rack/bug/panels/speedtracer_panel/instrumentation.rb', line 68

def safe_method_names(method_names)
  method_names.map do |name|
    name = name.to_s
    prefix = "0"
    hidden_name = ["_", prefix, name].join("_")
    while @unsafe_names.include?(hidden_name)
      prefix = prefix.next
      hidden_name = ["_", prefix, name].join("_")
    end

    @unsafe_names << hidden_name
    [name, hidden_name]
  end
end

#trace_class_methods(*methods_names) ⇒ Object



46
47
48
# File 'lib/rack/bug/panels/speedtracer_panel/instrumentation.rb', line 46

def trace_class_methods(*methods_names)
  build_tracing_wrappers((class << @const; self; end), '#{self.name}::', *methods_names)
end

#trace_methods(*methods) ⇒ Object



50
51
52
# File 'lib/rack/bug/panels/speedtracer_panel/instrumentation.rb', line 50

def trace_methods(*methods)
  build_tracing_wrappers(@const, '#{self.class.name}#', *methods)
end