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 APM as Non-web transactions.
Supported frameworks
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)
重要
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 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
.
Add the
ControllerInstrumentation
module.Use the
add_transaction_tracer
directive below the method definitionAdd
:category => :task
to tell the agent this trace is a Non-web transaction.require 'newrelic_rpm'class SalesOrganizationinclude::NewRelic::Agent::Instrumentation::ControllerInstrumentationdef find_new_leads...endadd_transaction_tracer :find_new_leads, :category => :taskendYou can pass a string to the
:category
, but values will only appear on the APM Transactions page if the string begins withOtherTransaction/
.
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
.
Add the
ControllerInstrumentation
module below the method definition.Use the
add_transaction_tracer
directiveAdd
:category => :task
to tell the agent this trace is a Non-web transaction.require 'newrelic_rpm'class SalesOrganizationdef self.find_new_leads...endclass << selfinclude::NewRelic::Agent::Instrumentation::ControllerInstrumentationadd_transaction_tracer :find_new_leads, :category => :taskendendFor more information, see Ruby custom metrics.
Monitor short-lived processes
Make sure the process isn't running before the agent connects to the backend servers. To do so, make the Ruby agent synchronously connect to New Relic, rather than the default asynchronous behavior.
First, in your Gemfile, add require: false
to the end of your newrelic_rpm
gem installation:
gem 'newrelic_rpm', require: false
Then, call manual_start
and pass in the :sync_startup => true
option:
require 'new_relic/agent'NewRelic::Agent.manual_start(:sync_startup => true)
Note: Most configuration options can be passed to manual start.
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.
If the process is shorter than the agent harvest cycle, you need to manually shut down the agent with ::NewRelic::Agent.shutdown
to ensure all queued data is sent.
Configure newrelic.yml for background processes
Configuring your newrelic.yml depends on the context of the background application.
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 APM UI.
Begin before
newrelic_rpm
gets required by your worker code.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 theapp_name
setting in yournewrelic.yml
.bash$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.
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::Agent.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 => :taskend
SalesOrganization.new.find_new_leads::NewRelic::Agent.shutdown