Rake instrumentation

The New Relic Ruby agent has opt-in support for instrumenting Rake tasks (Rake version 10.0.0 or higher). To instrument Rake tasks, specify the tasks by name in your newrelic.yml.

Rake instrumentation requires Ruby agent version 3.13.0 or higher.

Enable Rake support

To instrument your app's Rake tasks, add the names of the target tasks to the rake.tasks element in your newrelic.yml. The Ruby agent matches these names against your active tasks using string regex.

Example: Instrumenting two Rake tasks

For example, to instrument the Rake tasks deploy and deploy:all, add the following to your newrelic.yml file:

rake:
  tasks: ["deploy", "deploy:all"]

Since task name matching is via regex, you can instrument all of your app's Rake tasks by using a wildcard regex like [".+"]. However, this would still not include Rake tasks that are blacklisted by default via the autostart.blacklisted_rake_tasks configuration setting, such as db:migrate.

  • To include any Rake tasks that are blacklisted by default, include them in your customized blacklist.
  • To ensure the tasks are instrumented before they run if you are using Rails but your Rake task does not require the Rails environment, add require 'tasks/newrelic' to the top of the Rake tasks.

Remove newrelic-rake when appropriate

The newrelic-rake third-party gem provides Rake instrumentation support as an add-on to the Ruby agent. If the agent detects newrelic-rake, it won't install the built-in Rake instrumentation and will record a log message like this at startup:

INFO : Not installing New Relic supported Rake instrumentation because the third party newrelic-rake gem is present 

Removing the newrelic-rake gem in favor of the built-in instrumentation will change your transaction names. To preserve your existing transaction names, ignore the log message and do not uninstall the gem.

To switch to New Relic's built-in Rake instrumentation and change your transaction names:

  1. Remove the newrelic-rake gem.
  2. Specify the tasks you want to instrument in your config file.

Capture Rake job arguments

By default Rake job arguments are not captured. To capture Rake job arguments, use:

attributes.include: job.rake.*

For more help

Additional documentation resources include:

Join the discussion about Ruby in the New Relic Online Technical Community! The Technical Community is a public platform to discuss and troubleshoot your New Relic toolset.

If you need additional help, get support at support.newrelic.com.