Module: NewRelic::Agent::MethodTracer

Extended by:
ClassMethods
Defined in:
lib/new_relic/agent/method_tracer.rb

Overview

This module contains class methods added to support installing custom metric tracers and executing for individual metrics.

Examples

When the agent initializes, it extends Module with these methods. However if you want to use the API in code that might get loaded before the agent is initialized you will need to require this file:

require 'new_relic/agent/method_tracer'
class A
  include NewRelic::Agent::MethodTracer
  def process
    ...
  end
  add_method_tracer :process
end

To instrument a class method:

require 'new_relic/agent/method_tracer'
class An
  def self.process
    ...
  end
  class << self
    include NewRelic::Agent::MethodTracer
    add_method_tracer :process
  end
end

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from ClassMethods

add_method_tracer

Instance Method Details

#trace_execution_scoped(metric_names, options = {}) ⇒ Object

Trace a given block with stats and keep track of the caller. See NewRelic::Agent::MethodTracer::ClassMethods#add_method_tracer for a description of the arguments. metric_names is either a single name or an array of metric names. If more than one metric is passed, the produce_metric option only applies to the first. The others are always recorded. Only the first metric is pushed onto the scope stack.

Generally you pass an array of metric names if you want to record the metric under additional categories, but generally this *should never ever be done*. Most of the time you can aggregate on the server.



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/new_relic/agent/method_tracer.rb', line 268

def trace_execution_scoped(metric_names, options={})
  return yield if trace_disabled?(options)

  metric_names = Array(metric_names)
  first_name = metric_names.shift
  return yield if first_name.nil?

  set_if_nil(options, :metric)
  set_if_nil(options, :deduct_call_time_from_parent)
  additional_metrics_callback = options[:additional_metrics_callback]
  start_time, expected_scope = trace_execution_scoped_header(options)

  begin
    result = yield
    metric_names += Array(additional_metrics_callback.call) if additional_metrics_callback
    result
  ensure
    trace_execution_scoped_footer(start_time, first_name, metric_names, expected_scope, options)
  end
end

#trace_execution_unscoped(metric_names, options = {}) ⇒ Object Also known as: trace_method_execution_no_scope

Trace a given block with stats assigned to the given metric_name. It does not provide scoped measurements, meaning whatever is being traced will not ‘blame the Controller’–that is to say appear in the breakdown chart. This is code is inlined in #add_method_tracer.

  • metric_names is a single name or an array of names of metrics

  • :force => true will force the metric to be captured even when tracing is disabled with NewRelic::Agent#disable_all_tracing



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/new_relic/agent/method_tracer.rb', line 77

def trace_execution_unscoped(metric_names, options={})
  return yield unless NewRelic::Agent.is_execution_traced?
  t0 = Time.now
  begin
    NewRelic::Agent.instance.push_trace_execution_flag(true) if options[:force]
    yield
  ensure
    NewRelic::Agent.instance.pop_trace_execution_flag if options[:force]
    duration = (Time.now - t0).to_f              # for some reason this is 3 usec faster than Time - Time
    stat_engine.record_metrics(metric_names, duration)
  end
end

#trace_method_execution(metric_names, push_scope, produce_metric, deduct_call_time_from_parent, &block) ⇒ Object

Deprecated.

Deprecated: original method preserved for API backward compatibility. Use either #trace_execution_scoped or #trace_execution_unscoped



58
59
60
61
62
63
64
65
# File 'lib/new_relic/agent/method_tracer.rb', line 58

def trace_method_execution(metric_names, push_scope, produce_metric, deduct_call_time_from_parent, &block) #:nodoc:
  if push_scope
    trace_execution_scoped(metric_names, :metric => produce_metric,
                           :deduct_call_time_from_parent => deduct_call_time_from_parent, &block)
  else
    trace_execution_unscoped(metric_names, &block)
  end
end