Module: Mmtrix::Agent::MethodTracer::ClassMethods

Included in:
Mmtrix::Agent::MethodTracer
Defined in:
lib/mmtrix/agent/method_tracer.rb

Overview

Defines methods used at the class level, for adding instrumentation

Instance Method Summary collapse

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

Parameters:

  • method_name (Symbol)

    the name of the method to trace

  • metric_name_code (String) (defaults to: nil)

    the metric name to record calls to the traced method under. This may be either a static string, or Ruby code to be evaluated at call-time in order to determine the metric name dynamically.

  • options (Hash) (defaults to: {})

    additional options controlling how the method is traced.

Options Hash (options):

  • :push_scope (Boolean) — default: true

    If false, the traced method will not appear in transaction traces or breakdown charts, and it will only be visible in custom dashboards.

  • :metric (Boolean) — default: true

    If false, the traced method will only appear in transaction traces, but no metrics will be recorded for it.

  • :code_header (String) — default: ''

    Ruby code to be inserted and run before the tracer begins timing.

  • :code_footer (String) — default: ''

    Ruby code to be inserted and run after the tracer stops timing.



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/mmtrix/agent/method_tracer.rb', line 334

def add_method_tracer(method_name, metric_name_code=nil, options = {})
  return unless mmtrix_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 = Mmtrix::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)
  ::Mmtrix::Agent.logger.debug("Traced method: class = #{self.name},"+
            "method = #{method_name}, "+
            "metric = '#{metric_name_code}'")
end