Propagate Python RQ tasks exceptions

I have a Flask app with Sentry error tracking. Now I created some tasks with rq , but their errors do not show up in Sentry Issues stream. I can tell the issues aren’t filtered out, because the number of filtered issues doesn’t increase. The errors show up in heroku logs --tail .

I run the worker with rq worker homework-fetcher -c my_app.rq_sentry

my_app/rq_sentry.py :

import os

import sentry_sdk
from sentry_sdk.integrations.rq import RqIntegration

dsn = os.environ["SENTRY_DSN"]
print(dsn) # I confirmed this appears in logs, so it is initialized

sentry_sdk.init(dsn=dsn, integrations=[RqIntegration()])

I noticed that when I enqueue task my_app.nonexistent_module , the worker correctly raises error, which is caught by sentry. However when I enqueue an existing task which raises Exception, nothing appears in Sentry.


Also, I have a (a bit side-) question:

Should I include RqIntegration and RedisIntegration in sentry settings of the app itself? What is the benefit of these?

Thanks a lot for any help

I also posted this to StackOveflow.

Hello, so I tracked down the issue. The Flask app uses the app factory pattern. Then in the worker, I need to access the database the same way the app does, and for that, I need the app context. So I from app_factory import create_app, and then create_app().app_context().push(). And that’s the issue - in the factory pattern, I also init Sentry for the app itself, so I end up with Sentry initialized twice - once for the app, and once for the worker. Combined with the fact that I called the app initialization in the worker tasks file (not the config one), that probably overridden the correct sentry initialization and prevented the tasks from being correctly logged

Damn sorry, this issue slipped through the cracks. I just saw your Stackoverflow post independently. Glad you figured it out. I will respond on SO.