Module: OneApm::Support::MethodTracer::ClassMethods

Includes:
AddMethodTracer
Included in:
OneApm::Support::MethodTracer
Defined in:
lib/one_apm/support/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::DEPRECATED_KEYS, AddMethodTracer::OA_DEFAULT_SETTINGS

Instance Method Summary collapse

Methods included from AddMethodTracer

#assemble_code_header, #check_for_illegal_keys!, #check_for_push_scope_and_metric, #code_to_eval, #default_metric_name_code, #method_with_push_scope, #method_without_push_scope, #oneapm_method_exists?, #traced_method_exists?, #validate_options

Instance Method Details

#add_method_tracer(method_name, metric_name_code = 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_code is a string that is eval’d to get the name of the metric associated with the call, so if you want to use interpolation evaluated at call time, then single quote the value like this:

add_method_tracer :foo, 'Custom/#{self.class.name}/foo'

This would name the metric according to the class of the runtime intance, 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/#{self.class.name}/foo'

# 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


285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/one_apm/support/method_tracer.rb', line 285

def add_method_tracer(method_name, metric_name_code = nil, options = {})
  return unless oneapm_method_exists?(method_name)
  metric_name_code ||= default_metric_name_code(method_name)
  return if traced_method_exists?(method_name, metric_name_code)

  traced_method = code_to_eval(method_name, metric_name_code, options)

  visibility = OneApm::Helper.instance_method_visibility self, method_name

  class_eval traced_method, __FILE__, __LINE__
  alias_method _untraced_method_name(method_name, metric_name_code), method_name
  alias_method method_name, _traced_method_name(method_name, metric_name_code)
  send visibility, method_name
  send visibility, _traced_method_name(method_name, metric_name_code)
  OneApm::Manager.logger.debug("Traced method: class = #{self.name},"+
            "method = #{method_name}, "+
            "metric = '#{metric_name_code}'")
end

#remove_method_tracer(method_name, metric_name_code) ⇒ 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.



307
308
309
310
311
312
313
314
315
316
# File 'lib/one_apm/support/method_tracer.rb', line 307

def remove_method_tracer(method_name, metric_name_code) # :nodoc:
  return unless OneApm::Manager.config[:agent_enabled]
  if method_defined? "#{_traced_method_name(method_name, metric_name_code)}"
    alias_method method_name, "#{_untraced_method_name(method_name, metric_name_code)}"
    undef_method "#{_traced_method_name(method_name, metric_name_code)}"
    OneApm::Manager.logger.debug("removed method tracer #{method_name} #{metric_name_code}\n")
  else
    raise "No tracer for '#{metric_name_code}' on method '#{method_name}'"
  end
end