module Mmtrix::Agent::Instrumentation::ControllerInstrumentation::ClassMethods

@api public

Public Instance Methods

add_transaction_tracer(method, options={}) click to toggle source

Add transaction tracing to the given method. This will treat the given method as a main entrypoint for instrumentation, just like controller actions are treated by default. Useful especially for background tasks.

Example for background job:

class Job
  include Mmtrix::Agent::Instrumentation::ControllerInstrumentation
  def run(task)
     ...
  end
  # Instrument run so tasks show up under task.name.  Note single
  # quoting to defer eval to runtime.
  add_transaction_tracer :run, :name => '#{args[0].name}'
end

Here’s an example of a controller that uses a dispatcher action to invoke operations which you want treated as top level actions, so they aren’t all lumped into the invoker action.

MyController < ActionController::Base
  include Mmtrix::Agent::Instrumentation::ControllerInstrumentation
  # dispatch the given op to the method given by the service parameter.
  def invoke_operation
    op = params['operation']
    send op
  end
  # Ignore the invoker to avoid double counting
  mmtrix_ignore :only => 'invoke_operation'
  # Instrument the operations:
  add_transaction_tracer :print
  add_transaction_tracer :show
  add_transaction_tracer :forward
end

Here’s an example of how to pass contextual information into the transaction so it will appear in transaction traces:

class Job
  include Mmtrix::Agent::Instrumentation::ControllerInstrumentation
  def process(account)
     ...
  end
  # Include the account name in the transaction details.  Note the single
  # quotes to defer eval until call time.
  add_transaction_tracer :process, :params => '{ :account_name => args[0].name }'
end

See Mmtrix::Agent::Instrumentation::ControllerInstrumentation#perform_action_with_mmtrix_trace for the full list of available options.

@api public

# File lib/mmtrix/agent/instrumentation/controller_instrumentation.rb, line 159
          def add_transaction_tracer(method, options={})
            # The metric path:
            options[:name] ||= method.to_s

            argument_list = generate_argument_list(options)
            traced_method, punctuation = parse_punctuation(method)
            with_method_name, without_method_name = build_method_names(traced_method, punctuation)

            if already_added_transaction_tracer?(self, with_method_name)
              ::Mmtrix::Agent.logger.warn("Transaction tracer already in place for class = #{self.name}, method = #{method.to_s}, skipping")
              return
            end

            class_eval <<-EOC
              def #{with_method_name}(*args, &block)
                perform_action_with_mmtrix_trace(#{argument_list.join(',')}) do
                  #{without_method_name}(*args, &block)
                 end
              end
            EOC

            visibility = Mmtrix::Helper.instance_method_visibility self, method

            alias_method without_method_name, method.to_s
            alias_method method.to_s, with_method_name
            send visibility, method
            send visibility, with_method_name
            ::Mmtrix::Agent.logger.debug("Traced transaction: class = #{self.name}, method = #{method.to_s}, options = #{options.inspect}")
          end
already_added_transaction_tracer?(target, with_method_name) click to toggle source
# File lib/mmtrix/agent/instrumentation/controller_instrumentation.rb, line 212
def already_added_transaction_tracer?(target, with_method_name)
  if Mmtrix::Helper.instance_methods_include?(target, with_method_name)
    true
  else
    false
  end
end
build_method_names(traced_method, punctuation) click to toggle source
# File lib/mmtrix/agent/instrumentation/controller_instrumentation.rb, line 207
def build_method_names(traced_method, punctuation)
  [ "#{traced_method.to_s}_with_mmtrix_transaction_trace#{punctuation}",
    "#{traced_method.to_s}_without_mmtrix_transaction_trace#{punctuation}" ]
end
generate_argument_list(options) click to toggle source
# File lib/mmtrix/agent/instrumentation/controller_instrumentation.rb, line 193
def generate_argument_list(options)
  options.map do |key, value|
    value = if value.is_a?(Symbol)
      value.inspect
    elsif key == :params
      value.to_s
    else
      %Q["#{value.to_s}"]
    end

    %Q[:#{key} => #{value}]
  end
end
mmtrix_ignore(specifiers={}) click to toggle source

Have Mmtrix ignore actions in this controller. Specify the actions as hash options using :except and :only. If no actions are specified, all actions are ignored.

@api public

# File lib/mmtrix/agent/instrumentation/controller_instrumentation.rb, line 58
def mmtrix_ignore(specifiers={})
  mmtrix_ignore_aspect(NR_DO_NOT_TRACE_KEY, specifiers)
end
mmtrix_ignore_apdex(specifiers={}) click to toggle source

Have Mmtrix omit apdex measurements on the given actions. Typically used for actions that are not user facing or that skew your overall apdex measurement. Accepts :except and :only options, as with mmtrix_ignore.

@api public

# File lib/mmtrix/agent/instrumentation/controller_instrumentation.rb, line 67
def mmtrix_ignore_apdex(specifiers={})
  mmtrix_ignore_aspect(NR_IGNORE_APDEX_KEY, specifiers)
end
mmtrix_ignore_enduser(specifiers={}) click to toggle source

@api public

# File lib/mmtrix/agent/instrumentation/controller_instrumentation.rb, line 72
def mmtrix_ignore_enduser(specifiers={})
  mmtrix_ignore_aspect(NR_IGNORE_ENDUSER_KEY, specifiers)
end
parse_punctuation(method) click to toggle source
# File lib/mmtrix/agent/instrumentation/controller_instrumentation.rb, line 189
def parse_punctuation(method)
  [method.to_s.sub(/([?!=])$/, ''), $1]
end