ruby-on-railsnewrelicruby-on-rails-4.1

Why can't New Relic display some metrics for a Ruby on Rails action?


I have a Rails action that has 83% of its run time spent on this:

ApplicationCode in (Nested/Controller/my_controller/my_action

There's an info popup that says this:

Deeper visibility is not available because these classes and methods are not instrumented with the Ruby Agents current configuration. Consult our documentation to find out how to add custom metrics to your app.

I'm not doing anything unusual in this action. Any ideas why it might say this? Is there some internal Rails code that's not being traced?


Solution

  • New Relic agent is not a full profiler. It is able to trace some methods it knows are available in the many frameworks and libraries it supports.

    Usually when you see an entry like that (ApplicationCode in (Nested/Controller/my_controller/my_action) it means that either it is code inside that method that is taking too long (think a long running loop) or some of your methods that the agent wouldn't know.

    class MyController < ApplicationController
      def my_action
        1000.times do
          # something that takes time
        end
      end
    
      def another_action
        Foo.new.do_something
      end
    end
    
    class Foo
      def do_something
        # something that takes time
      end
    end
    

    In my_action you could use the Trace API to trace blocks of code.

    require 'new_relic/agent/method_tracer'
    
    class MyController < ApplicationController
      extend ::NewRelic::Agent::MethodTracer
      def my_action
        self.class.trace_execution_scoped(['Custom/slow_action/beginning_work']) do
          1000.times do
            # something that takes time
          end
        end
      end
    end
    

    Or for the case that you are calling another method (like in another_action), you can use method tracers.

    require 'new_relic/agent/method_tracer'
    
    class Foo
      def do_something
        # something that takes time
      end
    
      class << self
        include ::NewRelic::Agent::MethodTracer
    
        add_method_tracer :do_something, 'Custom/do_something'
      end
    end