New Relic's Ruby agent automatically instruments several common background job frameworks. Also, it can be customized to trace any background tasks. Data for background jobs appears in the Transactions page in New Relic, complete with transaction traces and errors, by selecting Non-web transactions as the transaction type.
The following background job frameworks are supported by default in recent versions of the Ruby agent:
- Resque instrumentation (Ruby Agent 3.4.0)
- Sidekiq instrumentation (Ruby Agent 3.6.0)
- Delayed::Job instrumentation (Ruby Agent 2.10)
If you are using these frameworks, monitoring background jobs should not require additional configuration.
Monitoring custom background jobs
The Ruby agent can instrument custom background jobs to appear in the Transactions page as Non-web transactions. If you are using an unsupported background job framework, monitoring non-web transactions requires you to add custom instrumentation to your job.
Without additional configuration, the agent captures metrics on the CPU utilization, memory, and database activity (for supported ORMs like ActiveRecord) for your background job.
Note: JRuby users may see issues with CPU metrics. For more information, see New Relic on JRuby.
Using the Ruby agent API, you can designate specific methods to trace like web transactions. This gathers traces for slow running jobs and associates captured errors to transactions.
Example: A background job periodically runs a task called
SalesOrganization#find_new_leads. To monitor the background task, include the
ControllerInstrumentation module and use the
add_transaction_tracer directive below the method definition:
require 'newrelic_rpm' class SalesOrganization include ::NewRelic::Agent::Instrumentation::ControllerInstrumentation def find_new_leads ... end add_transaction_tracer :find_new_leads, :category => :task end
:category => :task option tells the Ruby agent the trace should appear on the Transactions page (selecting Non-web as the transaction type). Without that option, the transaction is reported as a web transaction. A string may also be passed to the
:category, but values will only be captured to the Transactions page if the string begins with
To instrument a class method, use the class singleton:
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 on tracing custom metrics, see Ruby custom metrics.
If your background app is not a 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 the license key.
If your background job runs in the context of an existing web application that is already monitored with New Relic, the 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 from. 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 from, falling back to the
RACK_ENV environment variables in sequence, and finally defaulting to
development if none of these environment variables are set.
Reporting to an alternate application name
If your background jobs run in the context of an existing New Relic web application, but you would like them to appear under a different application name in the New Relic UI, set the
NEW_RELIC_APP_NAME environment variable to the application name you'd like to use for your background jobs when starting your background worker processes.
This environment variable will override the
app_name setting in your
newrelic.yml file. You must set this environment variable before
newrelic_rpm gets required by your worker code.
$ NEW_RELIC_APP_NAME="My Background Jobs" ./bin/my_background_worker.rb
For more information, see Naming your application.
Ensuring the agent starts
The Ruby agent will automatically start in most contexts as soon as you
require 'newrelic_rpm', unless it detects a blacklisted executable name, rake task name, or constant. This is to prevent it from starting during common rake tasks, interactive console sessions, etc. For more information, see the documentation about controlling agent startup.
Note: 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.
If you use the daemons gem to start background tasks, the 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, and the agent attempts to resolve the paths to its configuration file and log file relative to the current working directory of the host process.
Setting environment variables with the locations of the agent configuration file and log file will allow the agent to start in this scenario.
ENV['NRCONFIG'] ||= File.dirname(__FILE__) + '/../../config/newrelic.yml' ENV['NEW_RELIC_LOG'] ||= File.dirname(__FILE__) + '/../../log/newrelic_agent.log'
The above instructions apply when running background jobs in a daemon. If a script executes a single background task and exits, manually shut down the agent when the script finishes. This ensures data is sent to the server.
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.find_new_leads ::NewRelic::Agent.shutdown
For more help
Additional documentation resources include: