module TingYun::Instrumentation::Support::ControllerInstrumentation

This module can also be used to capture performance information for background tasks and other non-web transactions, including detailed transaction traces and traced errors.

Constants

NR_DEFAULT_OPTIONS

Below is a controller with an invoke_operation action which dispatches to more specific operation methods based on a parameter (very dangerous, btw!). With this instrumentation, the invoke_operation action is ignored but the operation methods show up in TingYun as if they were first class controller actions

MyController < ActionController::Base
  include TingYun::Instrumentation::Support::ControllerInstrumentation
  # dispatch the given op to the method given by the service parameter.
  def invoke_operation
    op = params['operation']
    perform_action_with_tingyun_trace(:name => op) do
      send op, params['message']
    end
  end
  # Ignore the invoker to avoid double counting
  tingyun_ignore :only => 'invoke_operation'
end

When invoking this method explicitly as in the example above, pass in a block to measure with some combination of options:

  • :category => :controller indicates that this is a controller action and will appear with all the other actions. This is the default.

  • :category => :task indicates that this is a background task and will show up in Ting Yun with other background tasks instead of in the controllers list

  • :category => :middleware if you are instrumenting a rack middleware call. The :name is optional, useful if you have more than one potential transaction in the call.

  • :category => :uri indicates that this is a web transaction whose name is a normalized URI, where 'normalized' means the URI does not have any elements with data in them such as in many REST URIs.

  • :name => action_name is used to specify the action name used as part of the metric name

  • :params => {...} to provide information about the context of the call, used in transaction trace display, for example: :params => { :account => @account.name, :file => file.name } These are treated similarly to request parameters in web transactions.

Seldomly used options:

  • :class_name => aClass.name is used to override the name of the class when used inside the metric name. Default is the current class.

  • :path => metric_path is deprecated in the public API. It allows you to set the entire metric after the category part. Overrides all the other options.

  • :request => Rack::Request#new(env) is used to pass in a request object that may respond to path and referer.

@api public

Public Class Methods

extended(klass) click to toggle source
# File lib/ting_yun/instrumentation/support/controller_instrumentation.rb, line 24
def self.extended klass
  klass.extend ClassMethods
end
included(klass) click to toggle source
# File lib/ting_yun/instrumentation/support/controller_instrumentation.rb, line 20
def self.included klass
  klass.extend ClassMethods
end

Public Instance Methods

perform_action_with_tingyun_trace(*args) { || ... } click to toggle source
# File lib/ting_yun/instrumentation/support/controller_instrumentation.rb, line 212
def perform_action_with_tingyun_trace (*args, &block)

  state = TingYun::Agent::TransactionState.tl_get

  skip_tracing = !state.execution_traced?

  if skip_tracing
    state.current_transaction.ignore! if state.current_transaction
    TingYun::Agent.disable_all_tracing { return yield }
  end
  trace_options = args.last.is_a?(Hash) ? args.last : NR_DEFAULT_OPTIONS
  category = trace_options[:category] || :controller
  txn_options = create_transaction_options(trace_options, category)

  begin
    TingYun::Agent::Transaction.start(state, category, txn_options)
    begin
      yield
    rescue => e
      ::TingYun::Agent.notice_error(e,:type=> :exception)
      raise e
    end
  ensure
    TingYun::Agent::Transaction.stop(state)
  end
end

Private Instance Methods

create_transaction_options(trace_options, category) click to toggle source
# File lib/ting_yun/instrumentation/support/controller_instrumentation.rb, line 241
def create_transaction_options(trace_options, category)
  txn_options = {}

  txn_options[:request] ||= request if respond_to?(:request)
  txn_options[:request] ||= trace_options[:request] if trace_options[:request]
  txn_options[:filtered_params] = trace_options[:params]
  txn_options[:transaction_name] = TingYun::Instrumentation::Support::TransactionNamer.name_for(nil, self, category, trace_options)
  txn_options[:apdex_start_time] = Time.now.to_f

  txn_options
end