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

@@class_list =
nil

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Logging

logger

Constructor Details

- (Probe) initialize(const)

A new instance of Probe



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

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

Class Method Details

+ (Object) all_probes



74
75
76
# File 'lib/insight/instrumentation/probe.rb', line 74

def all_probes
  probes.values
end

+ (Object) class_list



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

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

+ (Object) const_from_name(name)



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

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

+ (Object) get_probe_chain(name)



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

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

+ (Object) probe_for(const)



78
79
80
# File 'lib/insight/instrumentation/probe.rb', line 78

def probe_for(const)
  probes[const]
end

+ (Object) probes



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

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

Instance Method Details

- (Object) all_collectors



94
95
96
# File 'lib/insight/instrumentation/probe.rb', line 94

def all_collectors
  @collectors.values
end

- (Object) build_tracing_wrappers(target, method_name)



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/insight/instrumentation/probe.rb', line 160

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

- (Object) clear_collectors



98
99
100
# File 'lib/insight/instrumentation/probe.rb', line 98

def clear_collectors
  @collectors.clear
end

- (Object) collectors(key)



90
91
92
# File 'lib/insight/instrumentation/probe.rb', line 90

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

- (Object) define_trace_method(target, method)



150
151
152
153
154
155
156
157
158
# File 'lib/insight/instrumentation/probe.rb', line 150

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

- (Object) descendants



111
112
113
114
115
# File 'lib/insight/instrumentation/probe.rb', line 111

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

- (Object) descendants_that_define(method_name)



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

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

- (Object) fulfill_probe_orders



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/insight/instrumentation/probe.rb', line 133

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

- (Object) get_method(klass, method_name)



146
147
148
# File 'lib/insight/instrumentation/probe.rb', line 146

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

- (Object) local_method_defs(klass)



117
118
119
# File 'lib/insight/instrumentation/probe.rb', line 117

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

- (Object) log(&block)



129
130
131
# File 'lib/insight/instrumentation/probe.rb', line 129

def log &block
  logger.debug &block
end

- (Object) probe(collector, *methods)



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

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