4

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?

M. Volf
  • 1,259
  • 11
  • 29
  • How are you redirecting errors to Sentry? Are you just letting function crash or are you catching it? – hardik24 Jun 07 '20 at 23:02
  • I'm not catching any errors in the function the worker runs. But that's exactly what I would expect Sentry to do for me... – M. Volf Jun 08 '20 at 09:18

1 Answers1

0

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

M. Volf
  • 1,259
  • 11
  • 29
  • 1
    We (sentry) generally recommend doing initialization once. There should be no harm in enabling too many integrations. You should be able to do it at module level assuming you really only read from os.environ for configuration. – Markus Unterwaditzer Oct 27 '20 at 19:21
  • @MarkusUnterwaditzer yeah, I realized that now, I didn't realize before that I'm initializing it twice. But i think that this should be mentioned somewhere in the docs, as I think I followed quite common patterns. (The project in question is [on GitHub](https://github.com/mvolfik/homework-fml) in case you or anyone wanted to look at the source code in detail) – M. Volf Oct 27 '20 at 19:27