Python agent and Celery

If you are using Celery as a distributed task queuing system, you can use New Relic to record Celery processes as background tasks.

To get the Python agent working with Celery, first follow the agent installation instructions to install, configure, and test the agent. Then use these Celery-specific instructions.

Run Celery

The command you use to run Celery within New Relic depends on your Celery version and your specific setup.

celery

If you use the celery command, include any custom options:

NEW_RELIC_CONFIG_FILE=/some/path/newrelic.ini newrelic-admin run-program celery YOUR_COMMAND_OPTIONS

For example:

NEW_RELIC_CONFIG_FILE=/some/path/newrelic.ini newrelic-admin run-program celery -A tasks worker --loglevel=info
celeryd

The celeryd run command is deprecated in newer Celery releases. If you still use celeryd, include any custom options:

NEW_RELIC_CONFIG_FILE=some/path/newrelic.ini newrelic-admin run-program celeryd YOUR_COMMAND_OPTIONS
Django

If you start Celery with a Django management command, use manage.py. Replace the celery command with your Django command, and include any custom options:

NEW_RELIC_CONFIG_FILE=/some/path/newrelic.ini newrelic-admin run-program python manage.py celery YOUR_COMMAND_OPTIONS
Init scripts

If you are using one of Celery's generic init scripts, edit the script and wrap the celery command that is called when the script is used. This command usually looks like:

CELERY_BIN=${CELERY_BIN:-"celery"}

To add the New Relic agent’s wrapper script, change the CELERY_BIN variable to the following, where some/path/ is your actual path to the file or package:

CELERY_BIN=${CELERY_BIN:-"NEW_RELIC_CONFIG_FILE=/some/path/newrelic.ini /some/path/bin/newrelic-admin run-program /some/path/bin/celery"}

With this change, every time CELERY_BIN is called, it will be called with the New Relic wrapper script around the actual celery command.

Select the application name

The app_name setting in the Python agent configuration file sets the app name displayed in the New Relic user interface.

  • If your New Relic Python agent monitors Celery tasks and you set app_name to the same value used in your application agent's app_name, the data from both sources will be combined in the New Relic UI under that name.
  • If you set different names, the data appears separately in the UI as two different names.

By setting multiple app names in the agent config files, you can monitor both the combined data and the segregated data. Here's a common way to do this, using a Django application as an example:

Using multiple application names

For the agent monitoring the Django app, set the following:

app_name = Your_app_name (Django); Your_app_name (Combined)

For the agent monitoring Celery, set the following:

app_name = Your_app_name (Celery); Your_app_name (Combined)

This results in three displayed names in the New Relic UI:

  • Your_app_name (Combined), which combines data from both Celery and the Django application
  • Your_app_name (Django), which shows the Django app data
  • Your_app_name (Celery), which shows the Celery data

Ignore task retry errors

When using Celery, a task can possibly fail and raise a celery.exceptions:Retry or celery.exceptions:RetryTaskError exception. The exception depends on which version of Celery is used.

To ignore these errors, set this from the New Relic Application settings UI or from the agent config file. UI changes override config file changes per the configuration precedence rules.

To use ignore error settings from the UI:

  1. From rpm.newrelic.com, select APM > (selected app) > Settings > Application.
  2. Select Show advanced settings.
  3. From Error collection, enter the errors you want to ignore, separated by commas.

To ignore these errors using the agent config file, use the ignore_errors setting and a space-separated list of exceptions:

error_collector.ignore_errors = celery.exceptions:Retry celery.exceptions:RetryTaskError

Troubleshooting

When a Celery worker process is killed suddenly, the New Relic agent is not able to complete its normal shutdown process, which means its final data payload is not sent. This results in New Relic reporting fewer Celery transactions than expected or no transactions at all.

This may occur when using the CELERYD_MAX_TASKS_PER_CHILD setting, which defines the maximum number of tasks a pool worker process can execute before it's replaced with a new one. If used, the worker is forcibly shut down when that limit is reached, which means that that data is not recorded by New Relic. For example, if MAX_TASKS_PER_CHILD = 1, this results in no data being reported.

How to troubleshoot this will depend on why you want to use the MAX_TASKS_PER_CHILD limit in your application.

  • To allow the normal shutdown process, return this to the default no-limit setting.
  • To lessen the impact of the problem, raise the MAX_TASKS_PER_CHILD limit.

For more help

Join the discussion about Python 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.