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

Includes:
AddMethodTracer
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

Defined Under Namespace

Modules: AddMethodTracer

Constant Summary

Constants included from AddMethodTracer

AddMethodTracer::ALLOWED_KEYS, AddMethodTracer::DEFAULT_SETTINGS

Instance Method Summary collapse

Methods included from AddMethodTracer

#_nr_clear_traced_methods!, #_nr_default_metric_name, #_nr_derived_class_name, #_nr_traced_method_module, #_nr_validate_method_tracer_options, #method_traced?, #newrelic_method_exists?

Instance Method Details

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

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 is a String or Proc. If a Proc is given, it is bound to the object that called the traced method. For example:

add_method_tracer :foo, -> { "Custom/#{self.class.name}/foo" }

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

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

Examples:

add_method_tracer :foo

# With a custom metric name
add_method_tracer :foo, "Custom/MyClass/foo"
add_method_tracer :bar, -> { "Custom/#{self.class.name}/bar" }

# 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

Parameters:

  • method_name (Symbol)

    the name of the method to trace

  • metric_name (String, Proc, Array) (defaults to: nil)

    the metric name to record calls to the traced method under. This may be either a String, or a Proc to be evaluated at call-time in order to determine the metric name dynamically. This method also accepts an array of Strings/Procs, in which case the first metric given will be scoped, while the remaining metrics will be recorded as though passed with :push_scope => false. If an Array of metric names is given with :push_scope => false, all metrics will be unscoped.

  • options (Hash) (defaults to: {})

    additional options controlling how the method is traced.

Options Hash (options):

  • :push_scope (Boolean) — default: true

    If false, the traced method will not appear in transaction traces or breakdown charts, and it will only be visible in custom dashboards.

  • :metric (Boolean) — default: true

    If false, the traced method will only appear in transaction traces, but no metrics will be recorded for it.

  • :code_header (Proc) — default: ''

    Ruby code to be inserted and run before the tracer begins timing.

  • :code_footer (Proc) — default: ''

    Ruby code to be inserted and run after the tracer stops timing.



246
247
248
# File 'lib/new_relic/agent/method_tracer.rb', line 246

def add_method_tracer(method_name, metric_name = nil, options = {})
  ::NewRelic::Agent.add_or_defer_method_tracer(self, method_name, metric_name, options)
end

#remove_method_tracer(method_name) ⇒ Object

For tests only because tracers must be removed in reverse-order from when they were added, or else other tracers that were added to the same method may get removed as well.



253
254
255
256
257
258
259
260
261
262
# File 'lib/new_relic/agent/method_tracer.rb', line 253

def remove_method_tracer(method_name) # :nodoc:
  return unless Agent.config[:agent_enabled]

  if _nr_traced_method_module.method_defined?(method_name)
    _nr_traced_method_module.send(:remove_method, method_name)
    ::NewRelic::Agent.logger.debug("removed method tracer #{method_name}\n")
  else
    raise "No tracer on method '#{method_name}'"
  end
end