---
title: Delayed::Job instrumentation
source: https://docs.newrelic.com/docs/apm/agents/ruby-agent/background-jobs/delayedjob-instrumentation
---

The Ruby agent has built-in instrumentation for the `Delayed::Job` library. No additional instrumentation is required.

## Viewing background tasks [#viewing]

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](https://one.newrelic.com/all-capabilities) > APM & services > (select an app) > Monitor > Transactions**.
2.  From the [APM **Transactions** page](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/transactions-page), select **Other transactions**.

## Adding Custom Attributes [#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:

    ```ruby
    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:

    ```ruby
    require "new_relic_instrumenter"

    Delayed::Worker.plugins << NewRelicInstrumenter::DelayedJobPlugin
    ```

## Troubleshooting [#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](https://support.newrelic.com "Link opens in a new window").
