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 = NewRelic::EMPTY_HASH) ⇒ 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.



70
71
72
73
74
75
76
# File 'lib/new_relic/agent/method_tracer.rb', line 70

def trace_execution_scoped(metric_names, options = NewRelic::EMPTY_HASH) # THREAD_LOCAL_ACCESS
  NewRelic::Agent.record_api_supportability_metric(:trace_execution_scoped) unless options[:internal]
  NewRelic::Agent::MethodTracerHelpers.trace_execution_scoped(metric_names, options) do
    # Using an implicit block avoids object allocation for a &block param
    yield
  end
end

#trace_execution_unscoped(metric_names, options = NewRelic::EMPTY_HASH) ⇒ Object

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.

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



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/new_relic/agent/method_tracer.rb', line 86

def trace_execution_unscoped(metric_names, options = NewRelic::EMPTY_HASH) # THREAD_LOCAL_ACCESS
  NewRelic::Agent.record_api_supportability_metric(:trace_execution_unscoped) unless options[:internal]
  return yield unless NewRelic::Agent.tl_is_execution_traced?

  t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  begin
    yield
  ensure
    duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0
    NewRelic::Agent.instance.stats_engine.tl_record_unscoped_metrics(metric_names, duration)
  end
end