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

Defined in:
lib/new_relic/agent/instrumentation/controller_instrumentation.rb

Instance Method Summary collapse

Instance Method Details

#add_transaction_tracer(method, options = {}) ⇒ Object

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 NewRelic::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 NewRelic::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
  newrelic_ignore :only => 'invoke_operation'
  # Instrument the operations:
  add_transaction_tracer :print
  add_transaction_tracer :show
  add_transaction_tracer :forward
end

See NewRelic::Agent::Instrumentation::ControllerInstrumentation#perform_action_with_newrelic_trace for the full list of available options.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/new_relic/agent/instrumentation/controller_instrumentation.rb', line 118

def add_transaction_tracer(method, options={})
  # The metric path:
  options[:name] ||= method.to_s
  options_arg = []
  options.each do |key, value|
    options_arg << %Q[:#{key} => #{value.is_a?(Symbol) ? value.inspect : %Q["#{value.to_s}"]}]
  end
  class_eval <<-EOC
  def #{method.to_s}_with_newrelic_transaction_trace(*args, &block)
    NewRelic::Agent::Instrumentation::DispatcherInstrumentation.newrelic_dispatcher_start
    perform_action_with_newrelic_trace(#{options_arg.join(',')}) do
      #{method.to_s}_without_newrelic_transaction_trace(*args, &block)
    end
  ensure
    NewRelic::Agent::Instrumentation::DispatcherInstrumentation.newrelic_dispatcher_finish
  end
  EOC
  alias_method "#{method.to_s}_without_newrelic_transaction_trace", method.to_s
  alias_method method.to_s, "#{method.to_s}_with_newrelic_transaction_trace"
end

#newrelic_ignore(specifiers = {}) ⇒ Object

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



50
51
52
# File 'lib/new_relic/agent/instrumentation/controller_instrumentation.rb', line 50

def newrelic_ignore(specifiers={})
  newrelic_ignore_aspect('do_not_trace', specifiers)
end

#newrelic_ignore_apdex(specifiers = {}) ⇒ Object

Have NewRelic 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 #newrelic_ignore.



56
57
58
# File 'lib/new_relic/agent/instrumentation/controller_instrumentation.rb', line 56

def newrelic_ignore_apdex(specifiers={})
  newrelic_ignore_aspect('ignore_apdex', specifiers)
end

#newrelic_ignore_aspect(property, specifiers = {}) ⇒ Object

:nodoc:



60
61
62
63
64
65
66
67
68
# File 'lib/new_relic/agent/instrumentation/controller_instrumentation.rb', line 60

def newrelic_ignore_aspect(property, specifiers={}) # :nodoc:
  if specifiers.empty?
    self.newrelic_write_attr property, true
  elsif ! (Hash === specifiers)
    logger.error "newrelic_#{property} takes an optional hash with :only and :except lists of actions (illegal argument type '#{specifiers.class}')"
  else
    self.newrelic_write_attr property, specifiers
  end
end

#newrelic_read_attr(attr_name) ⇒ Object

:nodoc:



75
76
77
# File 'lib/new_relic/agent/instrumentation/controller_instrumentation.rb', line 75

def newrelic_read_attr(attr_name) # :nodoc:
  instance_variable_get "@#{attr_name}"
end

#newrelic_write_attr(attr_name, value) ⇒ Object

Should be monkey patched into the controller class implemented with the inheritable attribute mechanism.



72
73
74
# File 'lib/new_relic/agent/instrumentation/controller_instrumentation.rb', line 72

def newrelic_write_attr(attr_name, value) # :nodoc:
  instance_variable_set "@#{attr_name}", value
end