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.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/new_relic/agent/instrumentation/controller_instrumentation.rb', line 130

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
  traced_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
  visibility = NewRelic::Helper.instance_method_visibility self, method

  class_eval <<-EOC
    def #{traced_method.to_s}_with_newrelic_transaction_trace#{punctuation}(*args, &block)
      perform_action_with_newrelic_trace(#{options_arg.join(',')}) do
        #{traced_method.to_s}_without_newrelic_transaction_trace#{punctuation}(*args, &block)
       end
    end
  EOC
  without_method_name = "#{traced_method.to_s}_without_newrelic_transaction_trace#{punctuation}"
  with_method_name = "#{traced_method.to_s}_with_newrelic_transaction_trace#{punctuation}"
  alias_method without_method_name, method.to_s
  alias_method method.to_s, with_method_name
  send visibility, method
  send visibility, with_method_name
  ::NewRelic::Agent.logger.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.



45
46
47
# File 'lib/new_relic/agent/instrumentation/controller_instrumentation.rb', line 45

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.



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

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

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

:nodoc:



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

def newrelic_ignore_aspect(property, specifiers={}) # :nodoc:
  if specifiers.empty?
    self.newrelic_write_attr property, true
  elsif ! (Hash === specifiers)
    ::NewRelic::Agent.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_ignore_enduser(specifiers = {}) ⇒ Object



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

def newrelic_ignore_enduser(specifiers={})
  newrelic_ignore_aspect('ignore_enduser', specifiers)
end

#newrelic_read_attr(attr_name) ⇒ Object

:nodoc:



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

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.



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

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