Module: Mmtrix::Agent::MethodTracer

Extended by:
ClassMethods
Defined in:
lib/mmtrix/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 'mmtrix/agent/method_tracer'
class A
  include Mmtrix::Agent::MethodTracer
  def process
    ...
  end
  add_method_tracer :process
end

To instrument a class method:

require 'mmtrix/agent/method_tracer'
class An
  def self.process
    ...
  end
  class << self
    include Mmtrix::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

#get_stats_scoped(first_name, scoped_metric_only) ⇒ Object

Deprecated.

This method is deprecated and exists only for backwards-compatibility reasons. Usages should be replaced with calls to Mmtrix::Agent.record_metric.



132
133
134
# File 'lib/mmtrix/agent/method_tracer.rb', line 132

def get_stats_scoped(first_name, scoped_metric_only)
  Mmtrix::Agent.instance.stats_engine.get_stats(first_name, true, scoped_metric_only)
end

#get_stats_unscoped(name) ⇒ Object

Deprecated.

This method is deprecated and exists only for backwards-compatibility reasons. Usages should be replaced with calls to Mmtrix::Agent.record_metric.



143
144
145
# File 'lib/mmtrix/agent/method_tracer.rb', line 143

def get_stats_unscoped(name)
  Mmtrix::Agent.instance.stats_engine.get_stats_no_scope(name)
end

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

Trace a given block with stats and keep track of the caller. See Mmtrix::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
# File 'lib/mmtrix/agent/method_tracer.rb', line 70

def trace_execution_scoped(metric_names, options={}) #THREAD_LOCAL_ACCESS
  Mmtrix::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 = {}) ⇒ 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



85
86
87
88
89
90
91
92
93
94
# File 'lib/mmtrix/agent/method_tracer.rb', line 85

def trace_execution_unscoped(metric_names, options={}) #THREAD_LOCAL_ACCESS
  return yield unless Mmtrix::Agent.tl_is_execution_traced?
  t0 = Time.now
  begin
    yield
  ensure
    duration = (Time.now - t0).to_f              # for some reason this is 3 usec faster than Time - Time
    Mmtrix::Agent.instance.stats_engine.tl_record_unscoped_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



102
103
104
105
106
107
108
109
# File 'lib/mmtrix/agent/method_tracer.rb', line 102

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