module ProcessMetrics::ClassMethods

Public Instance Methods

measure(*methods_names) click to toggle source
# File lib/process_metrics/timer.rb, line 5
def measure(*methods_names)
  methods_names.flatten!
  options = extract_options(methods_names)

  methods_names.each do |method_name|
    define_wrapper_method(method_name, options)
    wrap_method(method_name) if method_exists?(method_name)
  end
end
method_added(method_name) click to toggle source
# File lib/process_metrics/timer.rb, line 15
def method_added(method_name)
  return if method_name =~ /^measure_/ || method_name =~ /^raw_/
  ProcessMetrics.config.logger.debug "Adding method #{method_name} in #{self}"

  measure_method_name = :"measure_#{method_name}"

  if self.instance_methods.include? measure_method_name
    ProcessMetrics.config.logger.debug "#{self}##{measure_method_name} exists. Wrapping..."
    wrap_method(method_name)
  else
    ProcessMetrics.config.logger.debug "#{self}##{measure_method_name} does not exist. Exiting..."
  end
end

Private Instance Methods

define_wrapper_method(method_name, options) click to toggle source
# File lib/process_metrics/timer.rb, line 34
def define_wrapper_method(method_name, options)
  measure_method_name = :"measure_#{method_name}"
  raw_method_name     = :"raw_#{method_name}"

  self.send(:define_method, measure_method_name) do |*args, &block|
    parent       = nil
    uuid         = extract_option(options, :uuid)
    parent_uuid  = extract_option(options, :parent_uuid)
    process_name = extract_option(options, :name) || "#{self.class.name}##{method_name}"

    if parent_uuid
      parent       = ProcessMetrics::Base.new(process_name)
      parent.uuid  = parent_uuid
    end

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

      send(raw_method_name, *args, &block)
    end
  end
end
extract_options(array) click to toggle source
# File lib/process_metrics/timer.rb, line 30
def extract_options(array)
  array.last && array.last.is_a?(Hash) ? array.pop : nil
end
method_exists?(method_name) click to toggle source
# File lib/process_metrics/timer.rb, line 59
def method_exists?(method_name)
  self.instance_methods.include? method_name
end
wrap_method(method_name) click to toggle source
# File lib/process_metrics/timer.rb, line 63
def wrap_method(method_name)
  return if @@replacing

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

  @@replacing = true
  alias_method raw_method_name, method_name
  alias_method method_name, measure_method_name
  @@replacing = false
  nil
end