Browse Source

Added Celery client thanks to Andy McCurdy

David Cramer 14 years ago
parent
commit
834abf3db0

+ 26 - 2
README.rst

@@ -106,6 +106,31 @@ Other configuration options
 
 Several options exist to configure django-sentry via your ``settings.py``:
 
+#############
+SENTRY_CLIENT
+#############
+
+In some situations you may wish for a slightly different behavior to how Sentry communicates with your server. For
+this, Sentry allows you to specify a custom client::
+
+	SENTRY_CLIENT = 'sentry.client.base.SentryClient'
+
+In addition to the default client (which will handle multi-db and REMOTE_URL for you) we also include two additional options:
+
+-------------------------------------
+sentry.client.log.LoggingSentryClient
+-------------------------------------
+
+Pipes all Sentry errors to a named logger: ``sentry``. If you wish to use Sentry in a strictly client based logging mode
+this would be the way to do it.
+
+---------------------------------------
+sentry.client.celery.CelerySentryClient
+---------------------------------------
+
+Integrates with the Celery message queue (http://celeryproject.org/). To use this you will also need to add ``sentry.client.celery`` to ``INSTALLED_APPS`` for ``tasks.py`` auto discovery. You may also specify ``SENTRY_CELERY_ROUTING_KEY`` to change the task queue
+name (defaults to ``sentry``).
+
 #############
 SENTRY_ADMINS
 #############
@@ -252,5 +277,4 @@ Notes
 =====
 
 * sentry-client will automatically integrate with django-idmapper.
-* sentry-client supports South migrations.
-* The fact that the admin shows large quantities of results, even if there aren't, is not a bug. This is an efficiency hack on top of Django.
+* sentry-client supports South migrations.

+ 12 - 0
runtests.py

@@ -24,8 +24,12 @@ if not settings.configured:
 
             'sentry',
             'sentry.client',
+            'sentry.client.celery',
             'paging',
             'indexer',
+            
+            # celery client
+            'djcelery',
 
             # No fucking idea why I have to do this
             'sentry.tests',
@@ -33,7 +37,15 @@ if not settings.configured:
         ROOT_URLCONF='',
         DEBUG=False,
         SITE_ID=1,
+        BROKER_HOST="localhost",
+        BROKER_PORT=5672,
+        BROKER_USER="guest",
+        BROKER_PASSWORD="guest",
+        BROKER_VHOST="/",
+        CELERY_ALWAYS_EAGER=True,
     )
+    import djcelery
+    djcelery.setup_loader()
 
 from django.test.simple import run_tests
 

+ 1 - 0
sentry/client/celery/__init__.py

@@ -0,0 +1 @@
+from client import CelerySentryClient

+ 8 - 0
sentry/client/celery/client.py

@@ -0,0 +1,8 @@
+from django.contrib.sites.models import Site
+from sentry.client.base import SentryClient
+from sentry.client.celery import tasks
+
+class CelerySentryClient(SentryClient):
+    def send(self, **kwargs):
+        "Errors through celery"
+        tasks.send.delay(kwargs)

+ 5 - 0
sentry/client/celery/models.py

@@ -0,0 +1,5 @@
+from django.conf import settings
+
+if 'djcelery' not in settings.INSTALLED_APPS:
+    raise ImproperlyConfigured("Put 'djcelery' in your "
+        "INSTALLED_APPS setting in order to use the sentry celery client.")

+ 3 - 0
sentry/client/celery/settings.py

@@ -0,0 +1,3 @@
+from django.conf import settings
+
+CELERY_ROUTING_KEY = getattr(settings, 'SENTRY_CELERY_ROUTING_KEY', 'sentry')

+ 7 - 0
sentry/client/celery/tasks.py

@@ -0,0 +1,7 @@
+from celery.decorators import task
+from sentry.client.celery import settings
+
+@task(routing_key=settings.CELERY_ROUTING_KEY)
+def send(data):
+    from sentry.models import GroupedMessage
+    return GroupedMessage.objects.from_kwargs(**data)

+ 19 - 0
sentry/tests/tests.py

@@ -835,6 +835,25 @@ class SentryClientTest(TestCase):
         self.assertEquals(_foo[''].levelno, client.default_level)
         self.assertEquals(_foo[''].class_name, 'Exception')
 
+    def test_celery_client(self):
+        from sentry.client.base import SentryClient
+        from sentry.client.celery import CelerySentryClient
+        self.assertEquals(get_client().__class__, SentryClient)
+        self.assertEquals(get_client(), get_client())
+
+        settings.CLIENT = 'sentry.client.celery.CelerySentryClient'
+
+        self.assertEquals(get_client().__class__, CelerySentryClient)
+        self.assertEquals(get_client(), get_client())
+
+        self.assertRaises(Exception, self.client.get, reverse('sentry-raise-exc'))
+
+        message = GroupedMessage.objects.get()
+        self.assertEqual(message.class_name, 'Exception')
+        self.assertEqual(message.message, 'view exception')
+
+        settings.CLIENT = 'sentry.client.base.SentryClient'
+        
 class SentryManageTest(TestCase):
     fixtures = ['sentry/tests/fixtures/cleanup.json']