Module: NewRelic::Agent::Instrumentation::ControllerInstrumentation
- Included in:
- Rack
- 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
Instance Method Summary collapse
-
#perform_action_with_newrelic_trace(*args, &block) ⇒ Object
Yield to the given block with NewRelic tracing.
Instance Method Details
#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.
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/new_relic/agent/instrumentation/controller_instrumentation.rb', line 312 def perform_action_with_newrelic_trace(*args, &block) NewRelic::Agent::TransactionState.request = newrelic_request(args) # 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 txn = _start_transaction(block_given? ? args : []) begin = { :force => txn.force_flag, :transaction => true } return yield if !(NewRelic::Agent.is_execution_traced? || [:force]) [:metric] = true if [:metric].nil? [:deduct_call_time_from_parent] = true if [:deduct_call_time_from_parent].nil? _, expected_scope = NewRelic::Agent::MethodTracer::TraceExecutionScoped.trace_execution_scoped_header(, txn.start_time.to_f) begin NewRelic::Agent::BusyCalculator.dispatcher_start txn.start_time if block_given? yield else perform_action_without_newrelic_trace(*args) end rescue => e txn.notice_error(e) raise end ensure end_time = Time.now txn.freeze_name metric_names = Array(recorded_metrics(txn)) txn_name = metric_names.shift NewRelic::Agent::MethodTracer::TraceExecutionScoped.(txn.start_time.to_f, txn_name, metric_names, expected_scope, , end_time.to_f) NewRelic::Agent::BusyCalculator.dispatcher_finish(end_time) txn.record_apdex(end_time) unless ignore_apdex? txn = Transaction.stop(txn_name, end_time) NewRelic::Agent::TransactionState.get.request_ignore_enduser = true if ignore_enduser? end end |