module Krane::StatsD::MeasureMethods

Public Instance Methods

measure_method(method_name, metric = nil) click to toggle source
Calls superclass method
# File lib/krane/statsd.rb, line 27
def measure_method(method_name, metric = nil)
  unless method_defined?(method_name) || private_method_defined?(method_name)
    raise NotImplementedError, "Cannot instrument undefined method #{method_name}"
  end

  unless const_defined?("InstrumentationProxy")
    const_set("InstrumentationProxy", Module.new)
    should_prepend = true
  end

  metric ||= "#{method_name}.duration"
  self::InstrumentationProxy.send(:define_method, method_name) do |*args, &block|
    begin
      start_time = Time.now.utc
      super(*args, &block)
    rescue
      error = true
      raise
    ensure
      dynamic_tags = send(:statsd_tags) if respond_to?(:statsd_tags, true)
      dynamic_tags ||= {}
      if error
        dynamic_tags[:error] = error if dynamic_tags.is_a?(Hash)
        dynamic_tags << "error:#{error}" if dynamic_tags.is_a?(Array)
      end

      Krane::StatsD.client.distribution(
        metric,
        Krane::StatsD.duration(start_time),
        tags: dynamic_tags
      )
    end
  end

  prepend(self::InstrumentationProxy) if should_prepend
end