Search code examples
pythonsentrypython-rq

How to propagate errors in python rq worker tasks to Sentry


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()])

Do I have something wrong, or should I set up a full app confirming this and publish a bug report?


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


Edit 1: when I schedule task my_app.nonexistent_module, the worker correctly raises error, which is caught by sentry.
So I maybe change my question: how to propagate Exceptions in rq worker tasks to Sentry?


Solution

  • So after 7 months, I figured it out.

    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 function, 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), that probably overridden the correct sentry initialization and prevented the tasks from being correctly logged