Module: ProcessMetrics::ClassMethods

Defined in:
lib/process_metrics/timer.rb

Constant Summary collapse

@@replacing =
false

Instance Method Summary collapse

Instance Method Details

#measure(*methods_names) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/process_metrics/timer.rb', line 5

def measure(*methods_names)
  options = extract_options(methods_names)

  methods_names.each do |method_name|
    measure_method_name = :"measure_#{method_name}"
    raw_method_name     = :"raw_#{method_name}"
    self.send(:define_method, measure_method_name) do |*args, &block|
      parent_uuid  = parent_uuid(options)
      process_name = "#{self.class.name}##{method_name}"
      parent       = ProcessMetrics::Base.new(process_name)
      parent.uuid  = parent_uuid

      ProcessMetrics.measure(process_name, parent) do |metric|
        ProcessMetrics.logger.debug "About to send #{raw_method_name} with args #{args.inspect} and block #{block.inspect} to #{self}"
        metric.data = {args: args, block: block}

        send(raw_method_name, *args, &block)
      end
    end
  end
end

#method_added(method_name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/process_metrics/timer.rb', line 27

def method_added(method_name)
  return if @@replacing || method_name =~ /^measure_/ || method_name =~ /^raw_/
  ProcessMetrics.logger.debug "Adding method #{method_name} in #{self}"

  measure_method_name = :"measure_#{method_name}"
  raw_method_name     = :"raw_#{method_name}"

  if self.instance_methods.include? measure_method_name
    ProcessMetrics.logger.debug "#{self}##{measure_method_name} exists. Replacing..."
    @@replacing = true
    alias_method raw_method_name, method_name
    alias_method method_name, measure_method_name
    @@replacing = false
  else
    ProcessMetrics.logger.debug "#{self}##{measure_method_name} does not exist. Exiting..."
  end
end