Class: Insight::Instrumentation::Probe

Inherits:
Object
  • Object
show all
Extended by:
Logging
Includes:
Logging
Defined in:
lib/insight/instrumentation/probe.rb

Direct Known Subclasses

ClassProbe, InstanceProbe

Defined Under Namespace

Modules: Interpose, ProbeRunner

Constant Summary collapse

@@class_list =
nil

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

logger

Constructor Details

#initialize(const) ⇒ Probe

Returns a new instance of Probe.



85
86
87
88
89
90
# File 'lib/insight/instrumentation/probe.rb', line 85

def initialize(const)
  @const = const
  @probed = {}
  @collectors = Hash.new{|h,k| h[k] = []}
  @probe_orders = []
end

Class Method Details

.all_probesObject



76
77
78
# File 'lib/insight/instrumentation/probe.rb', line 76

def all_probes
  probes.values
end

.class_listObject



38
39
40
41
42
43
44
45
46
# File 'lib/insight/instrumentation/probe.rb', line 38

def class_list
  @@class_list ||= begin
                     classes = []
                     ObjectSpace.each_object(Class) do |klass|
                       classes << klass
                     end
                     classes
                   end
end

.const_from_name(name) ⇒ Object



59
60
61
62
63
64
# File 'lib/insight/instrumentation/probe.rb', line 59

def const_from_name(name)
  parts = name.split("::")
  const = parts.inject(Kernel) do |namespace, part|
    namespace.const_get(part)
  end
end

.get_probe_chain(name) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/insight/instrumentation/probe.rb', line 48

def get_probe_chain(name)
  const = const_from_name(name)
  chain = []
  const.ancestors.each do |mod|
    if probes.has_key?(mod.name)
      chain << probes[mod.name]
    end
  end
  chain
end

.probe_for(const) ⇒ Object



80
81
82
# File 'lib/insight/instrumentation/probe.rb', line 80

def probe_for(const)
  probes[const]
end

.probesObject



66
67
68
69
70
71
72
73
74
# File 'lib/insight/instrumentation/probe.rb', line 66

def probes
  @probes ||= Hash.new do |h,k|
    begin
      h[k] = self.new(const_from_name(k))
    rescue NameError
      logger.info{ "Cannot find constant: #{k}" }
    end
  end
end

Instance Method Details

#all_collectorsObject



96
97
98
# File 'lib/insight/instrumentation/probe.rb', line 96

def all_collectors
  @collectors.values
end

#build_tracing_wrappers(target, method_name) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/insight/instrumentation/probe.rb', line 163

def build_tracing_wrappers(target, method_name)
  return if @probed.has_key?([target,method_name])
  @probed[[target,method_name]] = true

  meth = get_method(target, method_name)

  log{ {:tracing => meth } }
  define_trace_method(target, meth)
rescue NameError => ne
  log{ {:not_tracing => NameError } }
end

#clear_collectorsObject



100
101
102
# File 'lib/insight/instrumentation/probe.rb', line 100

def clear_collectors
  @collectors.clear
end

#collectors(key) ⇒ Object



92
93
94
# File 'lib/insight/instrumentation/probe.rb', line 92

def collectors(key)
  @collectors[key.to_sym]
end

#define_trace_method(target, method) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/insight/instrumentation/probe.rb', line 153

def define_trace_method(target, method)
  target.class_exec() do
    define_method(method.name) do |*args, &block|
      ProbeRunner::probe_run(self, method.owner.name, :instance, args, caller(0)[0], method.name) do
        method.bind(self).call(*args, &block)
      end
    end
  end
end

#descendantsObject



113
114
115
116
117
# File 'lib/insight/instrumentation/probe.rb', line 113

def descendants
  @descendants ||= self.class.class_list.find_all do |klass|
    klass.ancestors.include?(@const)
  end
end

#descendants_that_define(method_name) ⇒ Object



123
124
125
126
127
128
# File 'lib/insight/instrumentation/probe.rb', line 123

def descendants_that_define(method_name)
  log{{ :descendants => descendants }}
  descendants.find_all do |klass|
    (@const != klass and local_method_defs(klass).include?(method_name))
  end
end

#fulfill_probe_ordersObject



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/insight/instrumentation/probe.rb', line 136

def fulfill_probe_orders
  log{{:probes_for => @const.name, :type => self.class}}
  @probe_orders.each do |method_name|
    log{{ :method => method_name }}
    build_tracing_wrappers(@const, method_name)
    descendants_that_define(method_name).each do |klass|
      log{{ :subclass => klass.name }}
      build_tracing_wrappers(klass, method_name)
    end
  end
  @probe_orders.clear
end

#get_method(klass, method_name) ⇒ Object



149
150
151
# File 'lib/insight/instrumentation/probe.rb', line 149

def get_method(klass, method_name)
  klass.instance_method(method_name)
end

#local_method_defs(klass) ⇒ Object



119
120
121
# File 'lib/insight/instrumentation/probe.rb', line 119

def local_method_defs(klass)
  klass.instance_methods(false)
end

#log(&block) ⇒ Object



131
132
133
134
# File 'lib/insight/instrumentation/probe.rb', line 131

def log &block
  logger.debug &block
  #$stderr.puts block.call.inspect
end

#probe(collector, *methods) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/insight/instrumentation/probe.rb', line 104

def probe(collector, *methods)
  methods.each do |name|
    unless @collectors[name.to_sym].include?(collector)
      @collectors[name.to_sym] << collector
    end
    @probe_orders << name
  end
end