Monitor Ruby background processes

Our Ruby agent automatically instruments several common background job frameworks. You can also customize it to trace any background tasks. Data from background jobs appears in the Transactions page in New Relic APM as Non-web transactions.

Supported frameworks

The following background job frameworks are supported by default in recent versions of the Ruby agent:

JRuby users may see issues with CPU metrics.

If you are using these frameworks, monitoring background jobs typically doesn't require additional configuration.

Monitor custom background jobs

You can instrument custom background jobs to appear in the New Relic APM Transactions page as Non-web transactions. To monitor Non-web transactions while using an unsupported framework, you must add custom instrumentation.

As an example, a background job periodically runs a task called SalesOrganization#find_new_leads.

  1. Add the ControllerInstrumentation module.
  2. Use the add_transaction_tracer directive below the method definition
  3. Add :category => :task to tell the agent this trace is a Non-web transaction.

    require 'newrelic_rpm' 
    
    class SalesOrganization
      include 
    ::NewRelic::Agent::Instrumentation::ControllerInstrumentation 
      def find_new_leads 
        ... 
      end 
      add_transaction_tracer :find_new_leads, :category => :task 
    end

    You can pass a string to the :category, but values will only appear on the APM Transactions page if the string begins with OtherTransaction/.

Monitor custom background methods

Using the Ruby agent API, you can designate specific methods to trace the Non-web transactions. This gathers traces for slow running jobs and associates captured errors to transactions.

To instrument a class method, use the class singleton.

As an example, a background job periodically runs a task called SalesOrganization#find_new_leads.

  1. Add the ControllerInstrumentation module below the method definition.
  2. Use the add_transaction_tracer directive
  3. Add :category => :task to tell the agent this trace is a Non-web transaction.

    require 'newrelic_rpm' 
    
    class SalesOrganization 
      def self.find_new_leads
        ... 
      end 
      class << self 
          include 
    ::NewRelic::Agent::Instrumentation::ControllerInstrumentation 
        add_transaction_tracer :find_new_leads, :category => :task 
      end 
    end

    For more information, see Ruby custom metrics.

Monitor short-lived processes

Make sure the process isn't running before the agent connects to the back-end servers. To do so, make the Ruby agent synchronously connect to New Relic, rather than the default asynchronous behavior.

Use manual_start and pass in the :sync_startup => true option:

require 'new_relic/agent' 
NewRelic::Agent.manual_start(:sync_startup => true)

Using require 'new_relic/agent' will require the agent's code, and it will make sure the agent doesn't run until you manually start it.

Configure newrelic.yml for background processes

Configuring your newrelic.yml depends on the context of the background application.

Non-Rails background application

If your background app is non-Rails application already running the Ruby agent, copy your newrelic.yml file to the directory where you launch the background job or in the config subdirectory. Make sure it includes your license key.

Background jobs that do not run in a Rails context will examine the NEW_RELIC_ENV environment variable to determine which section of the configuration file to read, falling back to the RUBY_ENV, RAILS_ENV, and RACK_ENV environment variables in sequence, and finally defaulting to development if none of these environment variables are set.

Background job environment monitored by New Relic

If your background job runs in the context of an existing web application that is already monitored with New Relic, the Ruby agent will automatically pick up your existing newrelic.yml file. Background jobs that boot your application's Rails environment will use the RAILS_ENV environment variable in order to determine which section of the newrelic.yml file to read.

Report to an alternate application name

You can make jobs that run in the context of an existing New Relic web application appear under a different application name in the New Relic APM UI.

  1. Begin before newrelic_rpm gets required by your worker code.
  2. Set the NEW_RELIC_APP_NAME environment variable to the application name to use for your background jobs when starting your background worker processes. This will override the app_name setting in your newrelic.yml.
    $ NEW_RELIC_APP_NAME="My Background Jobs" 
    ./bin/my_background_worker.rb

Ensure the agent starts

The Ruby agent will automatically start in most cases as soon as you require 'newrelic_rpm', unless the agent detects a blacklisted executable name, rake task name, or constant. This prevents it from starting during common rake tasks and interactive console sessions.

Non-Rails standalone script

Standalone scripts running without Rails generally will start the agent as soon as they require 'newrelic_rpm'. If you have a script that forks or daemonizes before it begins its main work, you may want to defer this require call until after the initial setup finishes.

Background tasks with daemons gem

If you use the daemons gem to start background tasks, the Ruby agent may fail to start and also not emit any logging. This happens because the daemons gem changes the working directory to / before executing your background code. The agent then attempts to resolve the paths to its configuration file and log file relative to the current working directory of the host process.

To allow the agent to start in this situation, set environment variables with the locations of the agent configuration file and log file; for example:

    ENV['NRCONFIG'] ||= File.dirname(__FILE__) + 
    '/../../config/newrelic.yml' 
    ENV['NEW_RELIC_LOG'] ||= File.dirname(__FILE__) + 
    '/../../log/newrelic_agent.log'

For more information, see the documentation about controlling agent startup

Monitor scripts

The agent startup instructions apply when running background jobs in a daemon. If a script executes a single background task and exits, manually shut down the agent with ::NewRelic::Agents.shutdown when the script finishes. This ensures the New Relic collector receives the data. For example:

require 'newrelic_rpm' 

class SalesOrganization 
  include 
::NewRelic::Agent::Instrumentation::ControllerInstrumentation 
  def find_new_leads 
    ...
  end 
  add_transaction_tracer :find_new_leads, :category => :task 
end 

SalesOrganization.new.find_new_leads 
::NewRelic::Agent.shutdown

For more help

Additional documentation resources include:

Recommendations for learning more: