module NewRelic::Agent::MethodTracer::ClassMethods

Defines methods used at the class level, for adding instrumentation @api public

Public Instance Methods

add_method_tracer(method_name, metric_name_code=nil, options = {}) click to toggle source

Add a method tracer to the specified method.

By default, this will cause invocations of the traced method to be recorded in transaction traces, and in a metric named after the class and method. It will also make the method show up in transaction-level breakdown charts and tables.

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 interpolation 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.

@param [Symbol] method_name the name of the method to trace @param [String] metric_name_code the metric name to record calls to

the traced method under. This may be either a static string, or Ruby
code to be evaluated at call-time in order to determine the metric
name dynamically.

@param [Hash] options additional options controlling how the method is

traced.

@option options [Boolean] :push_scope (true) If false, the traced method will

not appear in transaction traces or breakdown charts, and it will
only be visible in custom dashboards.

@option options [Boolean] :metric (true) If false, the traced method will

only appear in transaction traces, but no metrics will be recorded
for it.

@option options [String] :code_header ('') Ruby code to be inserted and run

before the tracer begins timing.

@option options [String] :code_footer ('') Ruby code to be inserted and run

after the tracer stops timing.

@example

add_method_tracer :foo

# With a custom metric name
add_method_tracer :foo, 'Custom/#{self.class.name}/foo'

# Instrument foo only for custom dashboards (not in transaction
# traces or breakdown charts)
add_method_tracer :foo, 'Custom/foo', :push_scope => false

# Instrument foo in transaction traces only
add_method_tracer :foo, 'Custom/foo', :metric => false

@api public

# File lib/new_relic/agent/method_tracer.rb, line 333
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

Private Instance Methods

_sanitize_name(name) click to toggle source

makes sure that method names do not contain characters that might break the interpreter, for example ! or ? characters that are not allowed in the middle of method names

# File lib/new_relic/agent/method_tracer.rb, line 382
def _sanitize_name(name)
  name.to_s.tr_s('^a-zA-Z0-9', '_')
end
_traced_method_name(method_name, metric_name) click to toggle source

given a method and a metric, this method returns the traced alias of the method name

# File lib/new_relic/agent/method_tracer.rb, line 375
def _traced_method_name(method_name, metric_name)
  "#{_sanitize_name(method_name)}_with_trace_#{_sanitize_name(metric_name)}"
end
_untraced_method_name(method_name, metric_name) click to toggle source

given a method and a metric, this method returns the untraced alias of the method name

# File lib/new_relic/agent/method_tracer.rb, line 369
def _untraced_method_name(method_name, metric_name)
  "#{_sanitize_name(method_name)}_without_trace_#{_sanitize_name(metric_name)}"
end