Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Fix celery error logging #1267

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tilboerner
Copy link

@tilboerner tilboerner commented Jun 29, 2018

Error logging from Celery tasks is broken out of the box. While uncaught exception reporting works, error-level log messages from inside Celery worker do not get reported to Sentry.

For the user, it's not obvious that this is happening, or what can be done to fix it.The setup instructions for Django and Celery are not sufficient to get it working. There is documentation on raven.readthedocs.io suggesting that the Django handler "captures the errors" from workers. Consequently, there is #922, suggesting a workaround.

The problem is that when setting up logging for worker processes, Celery configures the task logger to not propagate to the root logger:

https://github.com/celery/celery/blob/47ca2b462f22a8d48ed8d80c2f9bf8b9dc4a4de6/celery/app/log.py#L132

https://github.com/celery/celery/blob/47ca2b462f22a8d48ed8d80c2f9bf8b9dc4a4de6/celery/app/log.py#L176

This prevents log events from reaching the Celery handler in the root logger. (Note that this setup does not happen in the Django shell, for example. There, things work fine.)

The workaround in #922 disables this logging setup completely. I'd argue that it's cleaner to work with whatever Celery decides to do and not mess with the logging system any further, beyond adding sentry handlers.

So, this PR adds a signal handler for the task logger, to give it the sentry handler if needed. It also adds code so that the DjangoCeleryHandler sets this up properly out of the box, if any of the CELERY_LOGLEVEL settings are defined. (This way it's opt-in; it could also be enabled by default.)

People should not have to discover the hard way that their errors do not properly get logged, so I think Raven should really fix this and work out of the box. If any manual action remains to be required, this should be noted in the documentation.

If you're willing to consider this PR, I'd be happy to add tests and documentation. In that case, it would be great if you could point me towards the proper places to put them.

@tilboerner tilboerner force-pushed the fix-celery-error-logging branch 2 times, most recently from 99e3a8e to e577bcf Compare July 2, 2018 15:47
@ashwoods ashwoods self-assigned this Jul 11, 2018
@ashwoods ashwoods self-requested a review July 11, 2018 16:27
@ashwoods
Copy link
Contributor

@tilboerner thx! Mind rebasing with master to get the tests passing.

@tilboerner
Copy link
Author

tilboerner commented Jul 13, 2018

@ashwoods Rebased it. Could you restart that one failed travis job? Looks like some pip download timed out.

@blodone
Copy link

blodone commented Jul 19, 2018

its really really painful to enable logging celery to sentry... +1

@huangsam
Copy link

Is there anything blocking this from getting released? This PR would be incredibly useful for folks who need to track Celery errors via the Raven client.

@untitaker
Copy link
Member

To all, I think the proper fix is to upgrade to the new Python SDK which bypasses those problems entirely.

I'm a bit vary of fixing this in raven as it is a breaking change for something that is effectively in maintenance mode.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants