Module: NewRelic::Agent::MethodTracer

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, TraceExecutionScoped

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TraceExecutionScoped

#agent_instance, #get_stats_scoped, #get_stats_unscoped, #has_parent?, #log_errors, #metrics_for_current_transaction, #metrics_for_parent_transaction, #pop_flag!, #push_flag!, #record_metrics, #set_if_nil, #stat_engine, #trace_disabled?, #trace_execution_scoped, #trace_execution_scoped_footer, #trace_execution_scoped_header, #traced?

Class Method Details

.extended(clazz) ⇒ Object

:nodoc:



46
47
48
# File 'lib/new_relic/agent/method_tracer.rb', line 46

def self.extended clazz #:nodoc:
  clazz.extend ClassMethods
end

.included(clazz) ⇒ Object

:nodoc:



42
43
44
# File 'lib/new_relic/agent/method_tracer.rb', line 42

def self.included clazz #:nodoc:
  clazz.extend ClassMethods
end

Instance Method Details

#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



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/new_relic/agent/method_tracer.rb', line 69

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: original method preserved for API backward compatibility. Use either #trace_execution_scoped or #trace_execution_unscoped



52
53
54
55
56
57
58
59
# File 'lib/new_relic/agent/method_tracer.rb', line 52

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

#trace_method_execution_with_scope(metric_names, produce_metric, deduct_call_time_from_parent, scoped_metric_only = false, &block) ⇒ Object

Deprecated. Use #trace_execution_scoped, a version with an options hash.



83
84
85
86
87
88
# File 'lib/new_relic/agent/method_tracer.rb', line 83

def trace_method_execution_with_scope(metric_names, produce_metric, deduct_call_time_from_parent, scoped_metric_only=false, &block) #:nodoc:
  trace_execution_scoped(metric_names,
                         :metric => produce_metric,
                         :deduct_call_time_from_parent => deduct_call_time_from_parent,
                         :scoped_metric_only => scoped_metric_only, &block)
end