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

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

class Job
  include NewRelic::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 NewRelic::Agent::Instrumentation::ControllerInstrumentation#perform_action_with_newrelic_trace for the full list of available options.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/new_relic/agent/instrumentation/controller_instrumentation.rb', line 124

def add_transaction_tracer(method, options={})
  # The metric path:
  options[:name] ||= method.to_s
  # create the argument list:
  options_arg = []
  options.each do |key, value|
    valuestr = case
    when value.is_a?(Symbol)
      value.inspect
    when key == :params
      value.to_s
    else
        %Q["#{value.to_s}"]
    end
    options_arg << %Q[:#{key} => #{valuestr}]
  end
  class_eval <<-EOC
  def #{method.to_s}_with_newrelic_transaction_trace(*args, &block)
    perform_action_with_newrelic_trace(#{options_arg.join(',')}) do
      #{method.to_s}_without_newrelic_transaction_trace(*args, &block)
    end
  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"
  NewRelic::Control.instance.log.debug("Traced transaction: class = #{self.name}, method = #{method.to_s}, options = #{options.inspect}")
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.



43
44
45
# File 'lib/new_relic/agent/instrumentation/controller_instrumentation.rb', line 43

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.



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

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

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

:nodoc:



53
54
55
56
57
58
59
60
61
# File 'lib/new_relic/agent/instrumentation/controller_instrumentation.rb', line 53

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:



68
69
70
# File 'lib/new_relic/agent/instrumentation/controller_instrumentation.rb', line 68

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.



65
66
67
# File 'lib/new_relic/agent/instrumentation/controller_instrumentation.rb', line 65

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