Module: NewRelic::Agent::MethodTracer::ClassMethods

Included in:
NewRelic::Agent::MethodTracer
Defined in:
lib/new_relic/agent/method_tracer.rb

Overview

Defines methods used at the class level, for adding instrumentation

Instance Method Summary collapse

Instance Method Details

#add_method_tracer(method_name, metric_name_code = nil, options = {}) ⇒ Object

Add a method tracer to the specified method.

Common Options

  • :push_scope => false specifies this method tracer should not keep track of the caller; it will not show up in controller breakdown pie charts.

  • :metric => false specifies that no metric will be recorded. Instead the call will show up in transaction traces as well as traces shown in Developer Mode.

Uncommon Options

  • :scoped_metric_only => true indicates that the unscoped metric should not be recorded. Normally two metrics are potentially created on every invocation: the aggregate method where statistics for all calls of that metric are stored, and the “scoped metric” which records the statistics for invocations in a particular scope–generally a controller action. This option indicates that only the second type should be recorded. The effect is similar to :metric => false but in addition you will also see the invocation in breakdown pie charts.

  • :deduct_call_time_from_parent => false indicates that the method invocation time should never be deducted from the time reported as ‘exclusive’ in the caller. You would want to use this if you are tracing a recursive method or a method that might be called inside another traced method.

  • :code_header and :code_footer specify ruby code that is inserted into the tracer before and after the call.

  • :force = true will ensure the metric is captured even if called inside an untraced execution call. (See NewRelic::Agent#disable_all_tracing)

Overriding the metric name

metric_name_code is a string that is eval’d to get the name of the metric associated with the call, so if you want to use interpolaion evaluated at call time, then single quote the value like this:

add_method_tracer :foo, 'Custom/#{self.class.name}/foo'

This would name the metric according to the class of the runtime intance, as opposed to the class where foo is defined.

If not provided, the metric name will be Custom/ClassName/method_name.

Examples

Instrument foo only for custom views–will not show up in transaction traces or caller breakdown graphs:

add_method_tracer :foo, :push_scope => false

Instrument foo just for transaction traces only:

add_method_tracer :foo, :metric => false

Instrument foo so it shows up in transaction traces and caller breakdown graphs for actions:

add_method_tracer :foo

which is equivalent to:

add_method_tracer :foo, 'Custom/#{self.class.name}/foo', :push_scope => true, :metric => true

Instrument the class method foo with the metric name ‘Custom/People/fetch’:

class << self
  add_method_tracer :foo, 'Custom/People/fetch'
end


533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/new_relic/agent/method_tracer.rb', line 533

def add_method_tracer(method_name, metric_name_code=nil, options = {})
  return unless newrelic_method_exists?(method_name)
  metric_name_code ||= default_metric_name_code(method_name)
  return if traced_method_exists?(method_name, metric_name_code)

  traced_method = code_to_eval(method_name, metric_name_code, options)

  visibility = NewRelic::Helper.instance_method_visibility self, method_name

  class_eval traced_method, __FILE__, __LINE__
  alias_method _untraced_method_name(method_name, metric_name_code), method_name
  alias_method method_name, _traced_method_name(method_name, metric_name_code)
  send visibility, method_name
  send visibility, _traced_method_name(method_name, metric_name_code)
  ::NewRelic::Agent.logger.debug("Traced method: class = #{self.name},"+
            "method = #{method_name}, "+
            "metric = '#{metric_name_code}'")
end