Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error reporter #261

Merged
merged 8 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions django_q/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import tasks

from django_q.compat import range
from django_q.conf import Conf, logger, psutil, get_ppid, rollbar
from django_q.conf import Conf, logger, psutil, get_ppid, error_reporter
from django_q.models import Task, Success, Schedule
from django_q.status import Stat, Status
from django_q.brokers import get_broker
Expand Down Expand Up @@ -365,8 +365,8 @@ def worker(task_queue, result_queue, timer, timeout=Conf.TIMEOUT):
f = getattr(m, func)
except (ValueError, ImportError, AttributeError) as e:
result = (e, False)
if rollbar:
rollbar.report_exc_info()
if error_reporter:
error_reporter.report()
# We're still going
if not result:
db.close_old_connections()
Expand All @@ -380,8 +380,8 @@ def worker(task_queue, result_queue, timer, timeout=Conf.TIMEOUT):
result = (res, True)
except Exception as e:
result = ('{}'.format(e), False)
if rollbar:
rollbar.report_exc_info()
if error_reporter:
error_reporter.report()
# Process result
task['result'] = result[0]
task['success'] = result[1]
Expand Down
41 changes: 31 additions & 10 deletions django_q/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

# external
import os
import pkg_resources

# optional
try:
Expand Down Expand Up @@ -141,8 +142,8 @@ class Conf(object):
# The redis stats key
Q_STAT = 'django_q:{}:cluster'.format(PREFIX)

# Optional Rollbar key
ROLLBAR = conf.get('rollbar', {})
# Optional error reporting setup
ERROR_REPORTER = conf.get('error_reporter', {})

# OSX doesn't implement qsize because of missing sem_getvalue()
try:
Expand Down Expand Up @@ -177,17 +178,37 @@ class Conf(object):
handler.setFormatter(formatter)
logger.addHandler(handler)

# rollbar
if Conf.ROLLBAR:
rollbar_conf = deepcopy(Conf.ROLLBAR)

# Error Reporting Interface
class ErrorReporter(object):

# initialize with iterator of reporters (better name, targets?)
def __init__(self, reporters):
self.targets = [target for target in reporters]

# report error to all configured targets
def report(self):
for t in self.targets:
t.report()


# error reporting setup (sentry or rollbar)
if Conf.ERROR_REPORTER:
error_conf = deepcopy(Conf.ERROR_REPORTER)
try:
import rollbar
rollbar.init(rollbar_conf.pop('access_token'), environment=rollbar_conf.pop('environment'), **rollbar_conf)
reporters = []
# iterate through the configured error reporters,
# and instantiate an ErrorReporter using the provided config
for name, conf in error_conf.items():
Reporter = pkg_resources.iter_entry_points(
'djangoq.errorreporters', name).load()
e = Reporter(**conf)
reporters.append(e)
error_reporter = ErrorReporter(reporters)
except ImportError:
rollbar = None

error_reporter = None
else:
rollbar = None
error_reporter = None


# get parent pid compatibility
Expand Down
40 changes: 30 additions & 10 deletions docs/configure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -353,20 +353,40 @@ scheduler
You can disable the scheduler by setting this option to ``False``. This will reduce a little overhead if you're not using schedules, but is most useful if you want to temporarily disable all schedules.
Defaults to ``True``

rollbar
~~~~~~~
You can redirect worker exceptions directly to your `Rollbar <https://rollbar.com/>`__ dashboard by installing the python notifier with ``pip install rollbar`` and adding this configuration dictionary to your config::
.. _error_reporter:

error_reporter
~~~~~~~~~~~~~~
You can redirect worker exceptions directly to various error reportes (for example, `Rollbar <https://rollbar.com/>` or `Sentry <https://docs.sentry.io/>`) by installing Django Q with the necessary `extras <https://setuptools.readthedocs.io/en/latest/setuptools.html#declaring-extras-optional-features-with-their-own-dependencies>`.

# rollbar config
To enable installed error reporters, you must provide the configuration settings required by an error reporter extension::

# error_reporter config--rollbar example
Q_CLUSTER = {
'rollbar': {
'access_token': '32we33a92a5224jiww8982',
'environment': 'Django-Q'
}
'error_reporter': {
'rollbar': {
'access_token': '32we33a92a5224jiww8982',
'environment': 'Django-Q'
}
}
}

Please check the Pyrollbar `configuration reference <https://github.com/rollbar/pyrollbar#configuration-reference>`__ for more options.
Note that you will need a `Rollbar <https://rollbar.com/>`__ account and access token to use this feature.
For more information on error reporters and developing error reporting plugins for Django Q, see :doc:`errors<errors>`.

.. rollbar
.. ~~~~~~~
.. You can redirect worker exceptions directly to your `Rollbar <https://rollbar.com/>`__ dashboard by installing the python notifier with ``pip install rollbar`` and adding this configuration dictionary to your config::

.. # rollbar config
.. Q_CLUSTER = {
.. 'rollbar': {
.. 'access_token': '32we33a92a5224jiww8982',
.. 'environment': 'Django-Q'
.. }
.. }

.. Please check the Pyrollbar `configuration reference <https://github.com/rollbar/pyrollbar#configuration-reference>`__ for more options.
.. Note that you will need a `Rollbar <https://rollbar.com/>`__ account and access token to use this feature.

cpu_affinity
~~~~~~~~~~~~
Expand Down
9 changes: 9 additions & 0 deletions docs/errors.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Errors
------
.. py:currentmodule:: django_q

Django Q uses a pluggable error reporter system based upon python `extras <https://setuptools.readthedocs.io/en/latest/setuptools.html#declaring-extras-optional-features-with-their-own-dependencies>`, allowing anyone to develop plugins for error reporting and monitoring integration. Currently implemented examples include `Rollbar <https://rollbar.com/>` and `Sentry <https://docs.sentry.io/>`).

Error reporting plugins register a class which implements a ``report`` method, which is invoked when a Django Q cluster encounters an error, pasing information to the particular service. Error reporters must be :ref:`configured<error_reporter>` via the ``Q_CLUSTER`` dictionary in your :file:`settings.py`. These settings are passed as kwargs upon initiation of the Error Reporter. Therefore, in order to implement a new plugin, a package must expose a class which will be instantiated with the necessary information via the ``Q_CLUSTER`` settings and implements a single ``report`` method.

For example implementations, see `django-q-rollbar <https://github.com/danielwelch/django-q-rollbar>` and `django-q-sentry <https://github.com/danielwelch/django-q-sentry>`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you provide examples except that they are references to a different repository.
Shouldn't django-q-rollbar be included by default in this repo?

Copy link
Contributor Author

@danielwelch danielwelch Oct 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Eagllus

Any idea's on how you would like to prevent this change from breaking everybody's current setup?

Understandable. To me, the most obvious and probably easiest solution is just to leave the old rollbar support in place along side the error reporter implementation. Then no one currently using rollbar support needs to modify their Q_CLUSTER settings, while new projects can instead use the error_reporter config setting and include the django-q-rollbar extra. Seems to me this would involve simply "putting back" what was removed from django_q/conf.py and django_q/cluster.py. I could modify the documentation changes to re-include the rollbar section, maybe with a note about that config setting being there for compatibility purposes and a link to the error_reporter setting. Maybe a deprecation warning if `Q_CLUSTER['rollbar'] is detected, if you're inclined.

I like that you provide examples except that they are references to a different repository.
Shouldn't django-q-rollbar be included by default in this repo?

In my opinion, different repositories is a strength (and kind of the point). I don't see any reason that django-q-rollbar should be included by default over, let's say, django-q-sentry aside from the backwards compatibility issue you brought up, which I think is valid. So the question seems to be, what is the best way to provide rollbar support for current users without breaking things: via Q_CLUSTER['rollbar']" vs. including django-q-rollbar in the django-q repository itself. To me, it seems cleaner to provide django-q-rollbar in the same way that django-q-sentry and potentially other plugins are provided: via a seperate repository. But I am but a hopeful contributor and those design decisions are probably not mine to make.

1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Contents:
Cluster <cluster>
Monitor <monitor>
Admin <admin>
Errors <errors>
Signals <signals>
Architecture <architecture>
Examples <examples>
Expand Down
12 changes: 11 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,15 @@ def run(self):
'Programming Language :: Python :: 3.6',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Software Development :: Libraries :: Python Modules',
]
],
entry_points={
'djangoq.errorreporters': [
'rollbar = django_q_rollbar.Rollbar',
'sentry = django_q_sentry.Sentry',
]
},
extras_require={
'rollbar': ["django-q-rollbar>=0.1"],
'sentry': ["django-q-sentry>=0.1"],
}
)