Skip to content

Commit

Permalink
Add Sentry integration
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasLM committed Feb 19, 2018
1 parent 2673f15 commit c29a907
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Getting started with spinach:
:maxdepth: 1

user/install
user/integrations
user/signals
user/design
user/faq
Expand Down
25 changes: 25 additions & 0 deletions doc/user/integrations.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.. _integrations:

Sentry
======

With the Sentry integration, failing jobs can be automatically reported to
`Sentry <https://sentry.io>`_ with full traceback, log breadcrumbs and job
information.

The integration requires `Raven <https://pypi.python.org/pypi/raven>`_, the
Sentry client for Python::

pip install raven

The integration just needs to be registered before starting workers::

from raven import Client
from spinach.contrib.sentry import register_sentry

raven_client = Client('https://sentry_dsn/42')
register_sentry(raven_client)

spin = Spinach(MemoryBroker())
spin.start_workers()

Empty file added spinach/contrib/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions spinach/contrib/sentry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Optional

from spinach import signals


def register_sentry(raven_client, namespace: Optional[str]=None):
"""Register the Sentry integration.
Exceptions making jobs fail are sent to Sentry. Note that exceptions
resulting in the job being retried are not sent to Sentry.
:param raven_client: configured Raven client used to sent errors to Sentry
:param namespace: optionally only register the Sentry integration for a
particular Spinach instance.
"""

@signals.job_started.connect_via(namespace)
def job_started(*args, job=None, **kwargs):
raven_client.context.activate()
raven_client.transaction.push(job.task_name)

@signals.job_finished.connect_via(namespace)
def job_finished(*args, job=None, **kwargs):
raven_client.transaction.push(job.task_name)
raven_client.context.clear()

@signals.job_failed.connect_via(namespace)
def job_failed(*args, job=None, **kwargs):
raven_client.captureException(
extra={attr: getattr(job, attr) for attr in job.__slots__}
)

0 comments on commit c29a907

Please sign in to comment.