• /
  • EnglishEspañol日本語한국어Português
  • Log inStart now

Delayed::Job instrumentation

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:

  1. Go to one.newrelic.com > All capabilities > APM & services > (select an app) > Monitor > Transactions.
  2. 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.

  1. Create a plugin with the following structure:

    module NewRelicInstrumenter
    class DelayedJobPlugin < Delayed::Plugin
    callbacks 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, etc
    my_custom_attribute: "my_custom_attribute_value"
    }
    )
    block.call(job, *args)
    end
    end
    end
    end
  2. Add the plugin to Delayed::Job in the initializer in the config/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:

bash
$
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.

Copyright © 2024 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.