RQ (Redis Queue)

Learn about using Sentry with RQ.

The RQ integration adds support for the RQ Job Queue System.

Install sentry-sdk from PyPI with the rq extra:

Copied
pip install --upgrade 'sentry-sdk[rq]'

If you have the rq package in your dependencies, the RQ integration will be enabled automatically when you initialize the Sentry SDK.

Create a file called mysettings.py with the following content:

mysettings.py
Copied
# mysettings.py
import sentry_sdk

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    enable_tracing=True,
)

Start your worker with:

Copied
rq worker \
    -c mysettings \  # module name of mysettings.py
    --sentry-dsn="https://examplePublicKey@o0.ingest.sentry.io/0"  # only necessary for RQ < 1.0

The integration will automatically report errors from all RQ jobs.

Generally, make sure that the call to init is loaded on worker startup, and not only in the module where your jobs are defined. Otherwise, the initialization happens too late and events might end up not being reported.

In addition, make sure that init is called only once in your app. For example, if you have a Flask app and a worker that depends on the app, we recommend only initializing Sentry once. Note that because the Flask integration is enabled automatically, you don't need to change the configuration shown above.

app.py
Copied
# app.py
import sentry_sdk

sentry_sdk.init(
    dsn=https://examplePublicKey@o0.ingest.sentry.io/0,
    enable_tracing=True,
)

The worker configuration mysettings.py then becomes:

mysettings.py
Copied
# mysettings.py
# This import causes the Sentry SDK to be initialized
import app

To verify, create a main.py script that enqueues a function in RQ, then start an RQ worker to run the function:

jobs.py
Copied
# jobs.py
def hello(name):
    1/0  # raises an error
    return "Hello %s!" % name

mysettings.py
Copied
# mysettings.py
import sentry_sdk

# Sentry configuration for RQ worker processes
sentry_sdk.init(
    dsn=https://examplePublicKey@o0.ingest.sentry.io/0,
    enable_tracing=True,
)

main.py
Copied
# main.py
from redis import Redis
from rq import Queue

from jobs import hello

import sentry_sdk

# Sentry configuration for main.py process
sentry_sdk.init(
    dsn=https://examplePublicKey@o0.ingest.sentry.io/0,
    enable_tracing=True,
)

q = Queue(connection=Redis())
with sentry_sdk.start_transaction(name="testing_sentry"):
    result = q.enqueue(hello, "World")

When you run python main.py a transaction named testing_sentry will be created in the Performance section of sentry.io and spans for the enqueueing will be created.

If you run the RQ worker with rq worker -c mysettings, a transaction for the execution of hello() will be created. Additionally, an error event will be sent to sentry.io and will be connected to the transaction.

It takes a couple of moments for the data to appear in sentry.io.

Passing --sentry-dsn="" to RQ forcibly disables RQ's shortcut for using Sentry. For RQ versions before 1.0 this is necessary to avoid conflicts, because back then RQ would attempt to use the raven package instead of this SDK. Since RQ 1.0 it's possible to use this CLI option and the associated RQ settings for initializing the SDK.

We still recommend against using those shortcuts because it would be harder to provide options to the SDK at a later point. See the GitHub issue about RQ's Sentry integration for discussion.

  • RQ: 0.6+
  • Python: 3.6+

The versions above apply for Sentry Python SDK version 2.0+, which drops support for some legacy Python and framework versions. If you're looking to use Sentry with older Python or framework versions, consider using an SDK version from the 1.x major line of releases.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").