Module: NewRelic::Agent::Instrumentation::ControllerInstrumentation

Included in:
Rack, Sinatra
Defined in:
lib/new_relic/agent/instrumentation/controller_instrumentation.rb

Overview

NewRelic instrumentation for controller actions and tasks

This instrumentation is applied to the action controller to collect metrics for every web request.

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

For details on how to instrument background tasks see ClassMethods#add_transaction_tracer and #perform_action_with_newrelic_trace

Defined Under Namespace

Modules: ClassMethods, ClassMethodsShim, Shim

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(clazz) ⇒ Object

:nodoc:



21
22
23
# File 'lib/new_relic/agent/instrumentation/controller_instrumentation.rb', line 21

def self.included(clazz) # :nodoc:
  clazz.extend(ClassMethods)
end

Instance Method Details

#newrelic_metric_path(action_name_override = nil) ⇒ Object

Must be implemented in the controller class: Determine the path that is used in the metric name for the called controller action. Of the form controller_path/action_name



170
171
172
# File 'lib/new_relic/agent/instrumentation/controller_instrumentation.rb', line 170

def newrelic_metric_path(action_name_override = nil) # :nodoc:
  raise "Not implemented!"
end

#perform_action_with_newrelic_trace(*args, &block) ⇒ Object

Yield to the given block with NewRelic tracing. Used by default instrumentation on controller actions in Rails and Merb. But it can also be used in custom instrumentation of controller methods and background tasks.

This is the method invoked by instrumentation added by the ClassMethods#add_transaction_tracer.

Here’s a more verbose version of the example shown in ClassMethods#add_transaction_tracer using this method instead of #add_transaction_tracer.

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 New Relic as if they were first class controller actions

MyController < ActionController::Base
  include NewRelic::Agent::Instrumentation::ControllerInstrumentation
  # dispatch the given op to the method given by the service parameter.
  def invoke_operation
    op = params['operation']
    perform_action_with_newrelic_trace(:name => op) do
      send op, params['message']
    end
  end
  # Ignore the invoker to avoid double counting
  newrelic_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 New Relic with other background tasks instead of in the controllers list

  • :category => :rack 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:

  • :force => true indicates you should capture all metrics even if the #newrelic_ignore directive was specified

  • :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 uri and referer.

If a single argument is passed in, it is treated as a metric path. This form is deprecated.



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/new_relic/agent/instrumentation/controller_instrumentation.rb', line 245

def perform_action_with_newrelic_trace(*args, &block)
  request = newrelic_request(args)
  NewRelic::Agent::TransactionInfo.reset(request)

  # Skip instrumentation based on the value of 'do_not_trace' and if
  # we aren't calling directly with a block.
  if !block_given? && do_not_trace?
    # Also ignore all instrumentation in the call sequence
    NewRelic::Agent.disable_all_tracing do
      return perform_action_without_newrelic_trace(*args)
    end
  end

  control = NewRelic::Control.instance
  return perform_action_with_newrelic_profile(args, &block) if control.profiling?

  frame_data = _push_metric_frame(block_given? ? args : [])
  begin
    NewRelic::Agent.trace_execution_scoped frame_data.recorded_metrics, :force => frame_data.force_flag do
      frame_data.start_transaction
      begin
        NewRelic::Agent::BusyCalculator.dispatcher_start frame_data.start
        result = if block_given?
          yield
        else
          perform_action_without_newrelic_trace(*args)
        end
        if defined?(request) && request && defined?(response) && response
          if !Agent.config[:disable_mobile_headers]
            NewRelic::Agent::BrowserMonitoring.insert_mobile_response_header(request, response)
          end
        end
        result
      rescue => e
        frame_data.notice_error(e)
        raise
      end
    end
  ensure
    NewRelic::Agent::BusyCalculator.dispatcher_finish
    # Look for a metric frame in the thread local and process it.
    # Clear the thread local when finished to ensure it only gets called once.
    frame_data.record_apdex unless ignore_apdex?
    frame_data.pop

    NewRelic::Agent::TransactionInfo.get.ignore_end_user = true if ignore_enduser?
  end
end