The Ruby agent has built-in instrumentation for the Delayed::Job
library. No additional instrumentation is required.
Viewing background tasks
As long as the New Relic Ruby agent's gem or plugin is loaded before the Delayed::Job
worker starts, all tasks will be monitored with the same level of detail as controller actions. To view the actions themselves:
- Go to one.newrelic.com > All capabilities > APM & services > (select an app) > Monitor > Transactions.
- From the APM Transactions page, select Other transactions.
Adding Custom Attributes
If you want to add custom attributes to your Delayed::Job
transactions, you can write a Delayed::Job
plugin through a lifecycle hook and add those attributes to the transaction for each executed job.
Create a plugin with the following structure:
module NewRelicInstrumenterclass DelayedJobPlugin < Delayed::Plugincallbacks do |lifecycle|lifecycle.around(:invoke_job) do |job, *args, &block|# Forward the call to the next callback in the callback chain::NewRelic::Agent.add_custom_attributes({# Any custom attributes go here -- from here you can access info# about the job like run_at, created_at, etcmy_custom_attribute: "my_custom_attribute_value"})block.call(job, *args)endendendendAdd the plugin to
Delayed::Job
in the initializer in theconfig/initializers/delayed_job.rb
file:require "new_relic_instrumenter"Delayed::Worker.plugins << NewRelicInstrumenter::DelayedJobPlugin
Troubleshooting
The Ruby agent depends on being able to identify that it is running under Delayed::Job
in order to correctly set up instrumentation. To do this, it examines the script name (the $0
variable in Ruby) to see whether it ends with delayed_job
.
If you have renamed the script you use to start your delayed_job
workers to something else, or if you have a custom script with a different name, you will need to explicitly tell the agent that you are using Delayed::Job
by setting the NEW_RELIC_DISPATCHER
environment variable to delayed_job
when starting your Delayed::Job
workers. For example:
$NEW_RELIC_DISPATCHER=delayed_job bundle exec ./script/my_custom_script
If it appears that jobs are not being monitored, review the newrelic_agent.log file generated when the worker starts up. It should indicate whether the agent detects Delayed
and communicates with the server. If you do not find a log, or if you still cannot determine why the jobs do not appear, get support at support.newrelic.com.