def register_handlers(): from django.core.signals import got_request_exception # HACK: support Sentry's internal communication if 'sentry' in django_settings.INSTALLED_APPS: from django.db import transaction @transaction.commit_on_success def wrap_sentry(request, **kwargs): if transaction.is_dirty(): transaction.rollback() return sentry_exception_handler(request, **kwargs) exception_handler = wrap_sentry else: exception_handler = sentry_exception_handler # Connect to Django's internal signal handler got_request_exception.connect(exception_handler, weak=False) # If Celery is installed, register a signal handler if 'djcelery' in django_settings.INSTALLED_APPS: from raven.contrib.celery import register_signal, register_logger_signal try: register_signal(client) except Exception: logger.exception('Failed installing error handling for Celery') try: register_logger_signal(client) except Exception: logger.exception('Failed installing logging handler for Celery')
def register_handlers(): from django.core.signals import got_request_exception # HACK: support Sentry's internal communication if 'sentry' in django_settings.INSTALLED_APPS: from django.db import transaction @transaction.commit_on_success def wrap_sentry(request, **kwargs): if transaction.is_dirty(): transaction.rollback() return sentry_exception_handler(request, **kwargs) exception_handler = wrap_sentry else: exception_handler = sentry_exception_handler # Connect to Django's internal signal handler got_request_exception.connect(exception_handler) # If Celery is installed, register a signal handler if 'djcelery' in django_settings.INSTALLED_APPS: from raven.contrib.celery import register_signal, register_logger_signal try: register_signal(client) except Exception: logger.exception('Failed installing error handling for Celery') try: register_logger_signal(client) except Exception: logger.exception('Failed installing logging handler for Celery')
def register_handlers(client): from django.core.signals import got_request_exception, request_started, request_finished from elasticapm.contrib.django.handlers import exception_handler # Connect to Django's internal signal handlers got_request_exception.disconnect(dispatch_uid=ERROR_DISPATCH_UID) got_request_exception.connect(partial(exception_handler, client), dispatch_uid=ERROR_DISPATCH_UID, weak=False) request_started.disconnect(dispatch_uid=REQUEST_START_DISPATCH_UID) request_started.connect(partial(_request_started_handler, client), dispatch_uid=REQUEST_START_DISPATCH_UID, weak=False) request_finished.disconnect(dispatch_uid=REQUEST_FINISH_DISPATCH_UID) request_finished.connect( lambda sender, **kwargs: client.end_transaction() if _should_start_transaction(client) else None, dispatch_uid=REQUEST_FINISH_DISPATCH_UID, weak=False, ) # If we can import celery, register ourselves as exception handler try: import celery # noqa F401 from elasticapm.contrib.celery import register_exception_tracking try: register_exception_tracking(client) except Exception as e: client.logger.exception( "Failed installing django-celery hook: %s" % e) except ImportError: client.logger.debug("Not instrumenting Celery, couldn't import")
def main(self, host, port): got_request_exception.connect(exception_printer) self.stdout.write('Serving at {host}:{port}\n'.format(host = host, port = port)) application = WSGIHandler() server = SocketIOServer((host, port), application, namespace = 'socket.io', policy_server = False) server.serve_forever()
def install(self): request_started.connect(self.before_request, weak=False) got_request_exception.connect(self.exception_handler, weak=False) if self.has_celery: try: self.install_celery() except Exception: logger.exception('Failed to install Celery error handler')
def register_handlers(): from django.core.signals import got_request_exception # Connect to Django's internal signal handler got_request_exception.connect(sentry_exception_handler) # If Celery is installed, register a signal handler if 'djcelery' in django_settings.INSTALLED_APPS: from raven.contrib.celery import register_signal register_signal(client)
def main(self, host, port): got_request_exception.connect(exception_printer) self.stdout.write('Serving at {host}:{port}\n'.format(host=host, port=port)) application = WSGIHandler() server = SocketIOServer((host, port), application, namespace='socket.io', policy_server=False) server.serve_forever()
def register_handlers(): from django.core.signals import got_request_exception, request_started def before_request(*args, **kwargs): client.context.activate() request_started.connect(before_request, weak=False) # HACK: support Sentry's internal communication if 'sentry' in settings.INSTALLED_APPS: from django.db import transaction # Django 1.6 if hasattr(transaction, 'atomic'): commit_on_success = transaction.atomic else: commit_on_success = transaction.commit_on_success @commit_on_success def wrap_sentry(request, **kwargs): if transaction.is_dirty(): transaction.rollback() return sentry_exception_handler(request, **kwargs) exception_handler = wrap_sentry else: exception_handler = sentry_exception_handler # Connect to Django's internal signal handler got_request_exception.connect(exception_handler, weak=False) # If Celery is installed, register a signal handler if 'djcelery' in settings.INSTALLED_APPS: try: # Celery < 2.5? is not supported from raven.contrib.celery import (register_signal, register_logger_signal) except ImportError: logger.exception('Failed to install Celery error handler') else: try: register_signal(client) except Exception: logger.exception('Failed to install Celery error handler') try: ga = lambda x, d=None: getattr(settings, 'SENTRY_%s' % x, d) options = getattr(settings, 'RAVEN_CONFIG', {}) loglevel = options.get('celery_loglevel', ga('CELERY_LOGLEVEL', logging.ERROR)) register_logger_signal(client, loglevel=loglevel) except Exception: logger.exception('Failed to install Celery error handler')
def request(self, **request): """ The master request method. Compose the environment dictionary and pass to the handler, return the result of the handler. Assume defaults for the query environment, which can be overridden using the arguments to the request. """ environ = self._base_environ(**request) # Curry a data dictionary into an instance of the template renderer # callback function. data = {} on_template_render = partial(store_rendered_templates, data) signal_uid = "template-render-%s" % id(request) signals.template_rendered.connect(on_template_render, dispatch_uid=signal_uid) # Capture exceptions created by the handler. exception_uid = "request-exception-%s" % id(request) got_request_exception.connect(self.store_exc_info, dispatch_uid=exception_uid) try: response = self.handler(environ) finally: signals.template_rendered.disconnect(dispatch_uid=signal_uid) got_request_exception.disconnect(dispatch_uid=exception_uid) # Look for a signaled exception, clear the current context exception # data, then re-raise the signaled exception. Also clear the signaled # exception from the local cache. response.exc_info = self.exc_info if self.exc_info: _, exc_value, _ = self.exc_info self.exc_info = None if self.raise_request_exception: raise exc_value # Save the client and request that stimulated the response. response.client = self response.request = request # Add any rendered template detail to the response. response.templates = data.get('templates', []) response.context = data.get('context') response.json = partial(self._parse_json, response) # Attach the ResolverMatch instance to the response. response.resolver_match = SimpleLazyObject( lambda: resolve(request['PATH_INFO'])) # Flatten a single context. Not really necessary anymore thanks to the # __getattr__ flattening in ContextList, but has some edge case # backwards compatibility implications. if response.context and len(response.context) == 1: response.context = response.context[0] # Update persistent cookie data. if response.cookies: self.cookies.update(response.cookies) return response
def register_handlers(): from django.core.signals import got_request_exception, request_started def before_request(*args, **kwargs): client.context.activate() request_started.connect(before_request, weak=False) # HACK: support Sentry's internal communication if 'sentry' in settings.INSTALLED_APPS: from django.db import transaction # Django 1.6 if hasattr(transaction, 'atomic'): commit_on_success = transaction.atomic else: commit_on_success = transaction.commit_on_success @commit_on_success def wrap_sentry(request, **kwargs): if transaction.is_dirty(): transaction.rollback() return sentry_exception_handler(request, **kwargs) exception_handler = wrap_sentry else: exception_handler = sentry_exception_handler # Connect to Django's internal signal handler got_request_exception.connect(exception_handler, weak=False) # If Celery is installed, register a signal handler if 'djcelery' in settings.INSTALLED_APPS: try: # Celery < 2.5? is not supported from raven.contrib.celery import ( register_signal, register_logger_signal) except ImportError: logger.exception('Failed to install Celery error handler') else: try: register_signal(client) except Exception: logger.exception('Failed to install Celery error handler') try: ga = lambda x, d=None: getattr(settings, 'SENTRY_%s' % x, d) options = getattr(settings, 'RAVEN_CONFIG', {}) loglevel = options.get('celery_loglevel', ga('CELERY_LOGLEVEL', logging.ERROR)) register_logger_signal(client, loglevel=loglevel) except Exception: logger.exception('Failed to install Celery error handler')
def register_handlers(): from django.core.signals import got_request_exception # Connect to Django's internal signal handler got_request_exception.connect(sentry_exception_handler) # If Celery is installed, register a signal handler if 'djcelery' in django_settings.INSTALLED_APPS: from raven.contrib.celery import register_signal try: register_signal(client) except Exception as e: logger.exception('Failed installing django-celery hook: %s' % e)
def request(self, **request): """ The master request method. Compose the environment dictionary and pass to the handler, return the result of the handler. Assume defaults for the query environment, which can be overridden using the arguments to the request. """ environ = self._base_environ(**request) # Curry a data dictionary into an instance of the template renderer # callback function. data = {} on_template_render = partial(store_rendered_templates, data) signal_uid = "template-render-%s" % id(request) signals.template_rendered.connect(on_template_render, dispatch_uid=signal_uid) # Capture exceptions created by the handler. exception_uid = "request-exception-%s" % id(request) got_request_exception.connect(self.store_exc_info, dispatch_uid=exception_uid) try: response = self.handler(environ) finally: signals.template_rendered.disconnect(dispatch_uid=signal_uid) got_request_exception.disconnect(dispatch_uid=exception_uid) # Look for a signaled exception, clear the current context exception # data, then re-raise the signaled exception. Also clear the signaled # exception from the local cache. response.exc_info = self.exc_info if self.exc_info: _, exc_value, _ = self.exc_info self.exc_info = None if self.raise_request_exception: raise exc_value # Save the client and request that stimulated the response. response.client = self response.request = request # Add any rendered template detail to the response. response.templates = data.get('templates', []) response.context = data.get('context') response.json = partial(self._parse_json, response) # Attach the ResolverMatch instance to the response. response.resolver_match = SimpleLazyObject(lambda: resolve(request['PATH_INFO'])) # Flatten a single context. Not really necessary anymore thanks to the # __getattr__ flattening in ContextList, but has some edge case # backwards compatibility implications. if response.context and len(response.context) == 1: response.context = response.context[0] # Update persistent cookie data. if response.cookies: self.cookies.update(response.cookies) return response
def register_handlers(): from django.core.signals import got_request_exception # Connect to Django's internal signal handler got_request_exception.connect(sentry_exception_handler) # If Celery is installed, register a signal handler if 'djcelery' in django_settings.INSTALLED_APPS: from raven.contrib.celery import register_signal try: register_signal(client) except Exception, e: logger.exception('Failed installing django-celery hook: %s' % e)
async def request(self, **request): """ The master request method. Compose the scope dictionary and pass to the handler, return the result of the handler. Assume defaults for the query environment, which can be overridden using the arguments to the request. """ if "follow" in request: raise NotImplementedError( "AsyncClient request methods do not accept the follow parameter." ) scope = self._base_scope(**request) # Curry a data dictionary into an instance of the template renderer # callback function. data = {} on_template_render = partial(store_rendered_templates, data) signal_uid = "template-render-%s" % id(request) signals.template_rendered.connect(on_template_render, dispatch_uid=signal_uid) # Capture exceptions created by the handler. exception_uid = "request-exception-%s" % id(request) got_request_exception.connect(self.store_exc_info, dispatch_uid=exception_uid) try: response = await self.handler(scope) finally: signals.template_rendered.disconnect(dispatch_uid=signal_uid) got_request_exception.disconnect(dispatch_uid=exception_uid) # Check for signaled exceptions. self.check_exception(response) # Save the client and request that stimulated the response. response.client = self response.request = request # Add any rendered template detail to the response. response.templates = data.get("templates", []) response.context = data.get("context") response.json = partial(self._parse_json, response) # Attach the ResolverMatch instance to the response. urlconf = getattr(response.asgi_request, "urlconf", None) response.resolver_match = SimpleLazyObject( lambda: resolve(request["path"], urlconf=urlconf), ) # Flatten a single context. Not really necessary anymore thanks to the # __getattr__ flattening in ContextList, but has some edge case # backwards compatibility implications. if response.context and len(response.context) == 1: response.context = response.context[0] # Update persistent cookie data. if response.cookies: self.cookies.update(response.cookies) return response
def test_exception_preservation(self): "An exception is preserved and reported correctly, even if sys.exc_clear is called." def on_request_exception(sender, request, **kwargs): sys.exc_clear() got_request_exception.connect(on_request_exception) try: with self.settings(DEBUG=True): handler = WSGIHandler() environ = RequestFactory().get(reverse('view_exception', args=(1,))).environ response = handler(environ, lambda *a, **k: None) self.assertContains(response, '<h2>Traceback ', status_code=500) self.assertContains(response, 'raise BrokenException(', status_code=500) finally: got_request_exception.disconnect(on_request_exception)
def configure(): # default to development if in DEBUG mode if getattr(settings, 'DEBUG'): bugsnag.configure(release_stage='development') request_started.connect(__track_session) got_request_exception.connect(__handle_request_exception) # Import Bugsnag settings from settings.py django_bugsnag_settings = getattr(settings, 'BUGSNAG', {}) bugsnag.configure(**django_bugsnag_settings) middleware = bugsnag.configure().internal_middleware middleware.before_notify(add_django_request_to_notification) bugsnag.configure().runtime_versions['django'] = django.__version__
def register_handlers(): from django.core.signals import got_request_exception # HACK: support Sentry's internal communication if "sentry" in django_settings.INSTALLED_APPS: from django.db import transaction # Django 1.6 if hasattr(transaction, "atomic"): commit_on_success = transaction.atomic else: commit_on_success = transaction.commit_on_success @commit_on_success def wrap_sentry(request, **kwargs): if transaction.is_dirty(): transaction.rollback() return sentry_exception_handler(request, **kwargs) exception_handler = wrap_sentry else: exception_handler = sentry_exception_handler # Connect to Django's internal signal handler got_request_exception.connect(exception_handler, weak=False) # If Celery is installed, register a signal handler if "djcelery" in django_settings.INSTALLED_APPS: try: # Celery < 2.5? is not supported from raven.contrib.celery import register_signal, register_logger_signal except ImportError: logger.exception("Failed to install Celery error handler") else: try: register_signal(client) except Exception: logger.exception("Failed to install Celery error handler") try: ga = lambda x, d=None: getattr(django_settings, "SENTRY_%s" % x, d) options = getattr(django_settings, "RAVEN_CONFIG", {}) loglevel = options.get("celery_loglevel", ga("CELERY_LOGLEVEL", logging.ERROR)) register_logger_signal(client, loglevel=loglevel) except Exception: logger.exception("Failed to install Celery error handler")
def test_signal_sent_with_propagated_exception(self): from django.core.signals import got_request_exception def assertion_handler(sender, request, **kwargs): for key in ['exception', 'exc_data']: assert key in kwargs for key in ['error', 'message', 'traceback']: assert key in kwargs['exc_data'] assert isinstance(kwargs['exception'], Exception) got_request_exception.connect(assertion_handler) @json_view def temp(req): 1 / 0 # sic. temp(rf.get('/'))
def test_signal_sent_with_propagated_exception(self): from django.core.signals import got_request_exception def assertion_handler(sender, request, **kwargs): for key in ['exception', 'exc_data']: assert key in kwargs for key in ['error', 'message', 'traceback']: assert key in kwargs['exc_data'] assert isinstance(kwargs['exception'], Exception) got_request_exception.connect(assertion_handler) @json_view def temp(req): 1/0 # sic. temp(rf.get('/'))
def register_handlers(): from django.core.signals import got_request_exception # Connect to Django's internal signal handler got_request_exception.connect(my_exception_handler, weak=False) log.info('If Celery is installed, register a signal handler') if 'djcelery' in django_settings.INSTALLED_APPS: try: # Celery < 2.5? is not supported from collect_exceptions.contrib.celery import register_signal except ImportError: log.warning('Failed to install Celery error handler') else: try: register_signal() except Exception: log.warning('Failed to install Celery error handler')
def register_handlers(): from django.core.signals import got_request_exception # HACK: support Sentry's internal communication if 'sentry' in settings.INSTALLED_APPS: from django.db import transaction # Django 1.6 if hasattr(transaction, 'atomic'): commit_on_success = transaction.atomic else: commit_on_success = transaction.commit_on_success @commit_on_success def wrap_sentry(request, **kwargs): if transaction.is_dirty(): transaction.rollback() return sentry_exception_handler(request, **kwargs) exception_handler = wrap_sentry else: exception_handler = sentry_exception_handler # Connect to Django's internal signal handler got_request_exception.connect(exception_handler, weak=False) # If Celery is installed, register a signal handler if 'djcelery' in settings.INSTALLED_APPS: try: # Celery < 2.5? is not supported from raven.contrib.celery import ( register_signal, register_logging_filter ) except ImportError: logger.exception('Failed to install Celery error handler') else: try: register_signal(client) except Exception: logger.exception('Failed to install Celery error handler') try: register_logging_filter(client) except Exception: logger.exception('Failed to install Celery error handler')
def ready(self): self.update_scout_config_from_django_settings() setting_changed.connect(self.on_setting_changed) # Finish installing the agent. If the agent isn't installed for any # reason, return without installing instruments installed = scout_apm.core.install() if not installed: return if scout_config.value("errors_enabled"): got_request_exception.connect(self.on_got_request_exception) self.install_middleware() # Setup Instruments ensure_huey_instrumented() ensure_sql_instrumented() ensure_templates_instrumented()
def register_handlers(): from django.core.signals import got_request_exception # HACK: support Sentry's internal communication if 'sentry' in django_settings.INSTALLED_APPS: from django.db import transaction # Django 1.6 if hasattr(transaction, 'atomic'): commit_on_success = transaction.atomic else: commit_on_success = transaction.commit_on_success @commit_on_success def wrap_sentry(request, **kwargs): if transaction.is_dirty(): transaction.rollback() return sentry_exception_handler(request, **kwargs) exception_handler = wrap_sentry else: exception_handler = sentry_exception_handler # Connect to Django's internal signal handler got_request_exception.connect(exception_handler, weak=False) # If Celery is installed, register a signal handler if 'djcelery' in django_settings.INSTALLED_APPS: try: # Celery < 2.5? is not supported from raven.contrib.celery import (register_signal, register_logger_signal) except ImportError: logger.exception('Failed to install Celery error handler') else: try: register_signal(client) except Exception: logger.exception('Failed to install Celery error handler') try: register_logger_signal(client) except Exception: logger.exception('Failed to install Celery error handler')
def register_handlers(client): from django.core.signals import got_request_exception from elasticapm.contrib.django.handlers import exception_handler # Connect to Django's internal signal handler got_request_exception.connect(exception_handler) # If we can import celery, register ourselves as exception handler try: import celery # noqa F401 from elasticapm.contrib.celery import register_exception_tracking try: register_exception_tracking(client) except Exception as e: client.logger.exception( 'Failed installing django-celery hook: %s' % e) except ImportError: client.logger.debug("Not instrumenting Celery, couldn't import")
def register_handlers(): from django.core.signals import got_request_exception # Connect to Django's internal signal handler got_request_exception.connect(opbeat_exception_handler) # If Celery is installed, register a signal handler if 'djcelery' in django_settings.INSTALLED_APPS: from opbeat.contrib.celery import register_signal try: register_signal(client) except Exception as e: logger.exception('Failed installing django-celery hook: %s' % e) # Instrument to get traces skip_env_var = 'SKIP_INSTRUMENT' if skip_env_var in os.environ: logger.debug("Skipping instrumentation. %s is set.", skip_env_var) else: opbeat.instrumentation.control.instrument()
def test_exception_preservation(self): "An exception is preserved and reported correctly, even if sys.exc_clear is called." def on_request_exception(sender, request, **kwargs): sys.exc_clear() got_request_exception.connect(on_request_exception) try: with self.settings(DEBUG=True): handler = WSGIHandler() environ = RequestFactory().get( reverse('view_exception', args=(1, ))).environ response = handler(environ, lambda *a, **k: None) self.assertContains(response, '<h2>Traceback ', status_code=500) self.assertContains(response, 'raise BrokenException(', status_code=500) finally: got_request_exception.disconnect(on_request_exception)
def __init__(self, *args, **kwargs): super(LoudWSGIHandler, self).__init__(*args, **kwargs) got_request_exception.connect(self.store_exc_info)
def setUp(self): self.exceptions = [] got_request_exception.connect(self._on_request_exception) self.client.handler.load_middleware()
_client = (settings.CLIENT, getattr(__import__(module, {}, {}, class_name), class_name)()) return _client[1] client = get_client() @transaction.commit_on_success def sentry_exception_handler(request=None, **kwargs): exc_info = sys.exc_info() try: if settings.DEBUG or getattr(exc_info[0], 'skip_sentry', False): return if transaction.is_dirty(): transaction.rollback() extra = dict( request=request, ) message_id = get_client().create_from_exception(**extra) except Exception, exc: try: logger.exception(u'Unable to process log entry: %s' % (exc,)) except Exception, exc: warnings.warn(u'Unable to process log entry: %s' % (exc,)) finally: del exc_info got_request_exception.connect(sentry_exception_handler)
from django.core.signals import got_request_exception def buggyview(request): raise ValueError("Original error with valuable error message") def handler(signal, sender, **kwargs): # The handler is buggy, too raise KeyError("Django hurt itself in confusion") got_request_exception.connect(handler)
#!/usr/bin/env python from gevent import monkey monkey.patch_all() from gevent.wsgi import WSGIServer import sys import os import traceback from django.core.management import call_command from django.core.signals import got_request_exception from rpg.wsgi import application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") def exception_printer(sender, **kwargs): traceback.print_exc() got_request_exception.connect(exception_printer) print 'Serving on 8088...' WSGIServer(('', 8088), application).serve_forever()
from django.core.signals import got_request_exception from contrib.errors.signals import LoggingExceptionHandler got_request_exception.connect(LoggingExceptionHandler.create_from_exception)
from django.core.signals import got_request_exception from django.db import transaction @transaction.commit_on_success def exception_handler(request=None, **kwargs): logger = logging.getLogger('peavy.signals.exception_handler') if transaction.is_dirty(): transaction.rollback() logger.exception('Exception: {0}'.format(exc_type), exc_info=True) got_request_exception.connect(exception_handler)
height = models.PositiveIntegerField() def get_url(self): return '/images/db/' + self.path def get_path(self, create=False): full_path = os.path.join(settings.CIA_DATA_PATH, 'db', 'images' , self.path) if create: directory = os.path.dirname(full_path) try: os.makedirs(directory) except OSError: pass return full_path def update_image_size(self): self.width, self.height = ImageInstance.objects.magick.get_image_size(self.get_path()) def to_html(self): return '<img src="%s" width="%d" height="%d" />' % ( self.get_url(), self.width, self.height) def remove_deleted_instance(instance, **kwargs): if instance.delete_file: try: os.unlink(instance.get_path()) except OSError: pass got_request_exception.connect(remove_deleted_instance, sender=ImageInstance)
for k in [k for k in sys.modules if k.startswith('django')]: del sys.modules[k] # Force sys.path to have our own directory first, in case we want to import from it. sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) # Must set this env var *before* importing any part of Django os.environ['DJANGO_SETTINGS_MODULE'] = 'errorbucket.settings' import django.core.handlers.wsgi from django.core.signals import got_request_exception import django.db import django.dispatch.dispatcher def log_exception(*args, **kwds): logging.exception('Exception in request:') got_request_exception.connect(log_exception) # Log errors. got_request_exception.disconnect( django.db._rollback_on_exception) # Unregister the rollback event handler. def main(): util.run_wsgi_app(django.core.handlers.wsgi.WSGIHandler()) if __name__ == '__main__': main()
) if settings.USE_LOGGING: logging.getLogger('dblog').critical(exc_value, exc_info=sys.exc_info(), extra=extra) else: Error.objects.create_from_exception(**extra) except Exception, exc: try: logger.exception(u'Unable to process log entry: %s' % (exc,)) except Exception, exc: warnings.warn(u'Unable to process log entry: %s' % (exc,)) class Error(ErrorBase): datetime = models.DateTimeField(auto_now_add=True, editable=False) data = JSONDictField(blank=True, null=True) class Meta: verbose_name = _('message') verbose_name_plural = _('messages') def __unicode__(self): return "%s: %s" % (self.class_name, smart_unicode(self.message)) def save(self, *args, **kwargs): if not self.checksum: self.checksum = construct_checksum(self) super(Error, self).save(*args, **kwargs) got_request_exception.connect(ErrorBatch.handle_exception)
if not app_settings.ENABLED: return exc_type, exc_value, exc_traceback, ident = \ unwrap_exception(*sys.exc_info()) # Ignore some errors if isinstance(exc_type, (Http404, SystemExit)): return try: DjangoError(request, exc_type, exc_value, exc_traceback, ident).send() except Exception: logger.exception("Exception whilst reporting error to keyerror.com") got_request_exception.connect(report_exception) ## Celery #################################################################### if 'djcelery' in settings.INSTALLED_APPS: from celery import signals def report_task_failure(einfo, traceback, **kwargs): if not app_settings.ENABLED: return exc_type, exc_value, exc_traceback, ident = \ unwrap_exception(einfo.type, einfo.exception, traceback) try: QueueError(exc_type, exc_value, exc_traceback, ident).send()
queue.append(t) else: log.debug('Removed duplicate task: %s' % (t,)) class PostRequestTask(Task): """A task whose execution is delayed until after the request finishes. This simply wraps celery's `@task` decorator and stores the task calls until after the request is finished, then fires them off. """ abstract = True def original_apply_async(self, *args, **kwargs): return super(PostRequestTask, self).apply_async(*args, **kwargs) def apply_async(self, *args, **kwargs): _append_task((self, args, kwargs)) # Replacement `@task` decorator. task = partial(base_task, base=PostRequestTask) # Hook the signal handlers up. request_finished.connect(_send_tasks, dispatch_uid='request_finished_tasks') got_request_exception.connect(_discard_tasks, dispatch_uid='request_exception_tasks')
from django.core.signals import got_request_exception from django.conf import settings from erroneous.signals import LoggingExceptionHandler if not settings.DEBUG: got_request_exception.connect( LoggingExceptionHandler.create_from_exception)
#============================================================================== # hook up got_request_exception signal to log uncaught exceptions #============================================================================== if not settings.DEBUG: from django.core.signals import got_request_exception def _log_exc_info(sender, request, **kwargs): try: import sys import traceback exc_type, exc_value, exc_tb = sys.exc_info() exc_tb_stack = traceback.extract_tb(exc_tb) if exc_type and len(exc_tb_stack) > 0: log.error('username=%s clientip=%s url=%s exception=%s' + \ ' filename=%s lineno=%s name=%s', getattr(request.user, 'username', ''), request.META.get('REMOTE_ADDR', ''), request.path, exc_type.__name__, exc_tb_stack[-1][0], exc_tb_stack[-1][1], exc_tb_stack[-1][2], ) except: #ignore any error within this logging function. pass #using dispatch_uid to void the signal being bound multiple time. got_request_exception.connect(_log_exc_info, dispatch_uid="global_exception_logging")
return super(PostRequestTask, self).apply_async(args=args, kwargs=kwargs, **extrakw) def apply_async(self, args=None, kwargs=None, **extrakw): if is_task_queuing_enabled_for_this_thread(): result = _append_task((self, args, kwargs, extrakw)) else: result = self.original_apply_async(args=args, kwargs=kwargs, **extrakw) return result # Replacement `@task` decorator. task = partial(base_task, base=PostRequestTask) # Hook the signal handlers up. # Start queuing the tasks only if we're inside a request-response cycle thread. request_started.connect(_start_queuing_tasks, dispatch_uid='{}.request_started'.format(__name__)) # Send the tasks to celery and stop queuing when the request is finished. request_finished.connect(_send_tasks_and_stop_queuing, dispatch_uid='{}.request_finished'.format(__name__)) # And make sure to discard the task queue when we have an exception in the # request-response cycle. got_request_exception.connect( _discard_tasks, dispatch_uid='{}.got_request_exception'.format(__name__))
context = zmq.Context() client = context.socket(zmq.REQ) client.connect('tcp://%s' % settings.JAZZ_SERVER) else: client = None def ping(*args, **kwargs): client.send('ping') client.recv() def trash(*args, **kwargs): client.send('trash') client.recv() def error(*args, **kwargs): client.send('error') client.recv() if client: client.send('ping') client.recv() from django.core.signals import request_started, got_request_exception from django.db.models.signals import post_delete request_started.connect(ping) post_delete.connect(trash) got_request_exception.connect(error)
_client = (conf.CLIENT, getattr(__import__(module, {}, {}, class_name), class_name)()) return _client[1] client = get_client() def sentry_exception_handler(request=None, **kwargs): try: exc_type, exc_value, exc_traceback = sys.exc_info() if conf.DEBUG or getattr(exc_type, 'skip_sentry', False): return # if transaction.is_dirty(): # transaction.rollback() extra = dict(request=request, ) message_id = get_client().create_from_exception(**extra) except Exception, exc: try: logger.exception(u'Unable to process log entry: %s' % (exc, )) except Exception, exc: warnings.warn(u'Unable to process log entry: %s' % (exc, )) got_request_exception.connect(sentry_exception_handler)
from django.core.signals import got_request_exception from .models import ExceptionRecord # connect request exception signal to a ExceptionRecord method got_request_exception.connect(ExceptionRecord.create_from_exception)
import sys from datetime import datetime from collections import deque from django.core.signals import got_request_exception from core.models import ShareTree import logging logger = logging.getLogger(__name__) def log(*args, **kwargs): logger.exception('Unhandled error.') got_request_exception.connect(log) class Adapter(object): """ Wrapper for all object attributes. """ adaptee = None def __init__(self, model): self.adaptee = model def __getattr__(self, attr): return getattr(self.adaptee, attr)
import sys import logging from django.http import Http404 from django.core.signals import got_request_exception from . import app_settings from .error import DjangoError logger = logging.getLogger(__name__) def report_exception(sender, request, **kwargs): if not app_settings.ENABLED: return exc_type, exc_value, exc_traceback = sys.exc_info() if isinstance(exc_type, (Http404, SystemExit)): return try: DjangoError(request, exc_type, exc_value, exc_traceback).send() except Exception: logger.exception("Exception whilst reporting error to keyerror.com") got_request_exception.connect(report_exception)
exc_info=sys.exc_info(), extra=extra) else: Error.objects.create_from_exception(**extra) except Exception, exc: try: logger.exception(u'Unable to process log entry: %s' % (exc, )) except Exception, exc: warnings.warn(u'Unable to process log entry: %s' % (exc, )) class Error(ErrorBase): datetime = models.DateTimeField(default=datetime.datetime.now, db_index=True) data = JSONDictField(blank=True, null=True) class Meta: verbose_name = _('message') verbose_name_plural = _('messages') def __unicode__(self): return "%s: %s" % (self.class_name, smart_unicode(self.message)) def save(self, *args, **kwargs): if not self.checksum: self.checksum = construct_checksum(self) super(Error, self).save(*args, **kwargs) got_request_exception.connect(ErrorBatch.handle_exception)
queue = _get_task_queue() if t not in queue: queue.append(t) else: log.debug('Removed duplicate task: %s' % (t, )) class PostRequestTask(Task): """A task whose execution is delayed until after the request finishes. This simply wraps celery's `@task` decorator and stores the task calls until after the request is finished, then fires them off. """ abstract = True def original_apply_async(self, *args, **kwargs): return super(PostRequestTask, self).apply_async(*args, **kwargs) def apply_async(self, *args, **kwargs): _append_task((self, args, kwargs)) # Replacement `@task` decorator. task = partial(base_task, base=PostRequestTask) # Hook the signal handlers up. request_finished.connect(_send_tasks, dispatch_uid='request_finished_tasks') got_request_exception.connect(_discard_tasks, dispatch_uid='request_exception_tasks')
import sys import traceback from django.core.signals import got_request_exception def print_exception(f): f.write(''.join(traceback.format_exception(*sys.exc_info())) + '\n\n') def exception_printer(sender, **kwargs): print_exception(sys.stderr) got_request_exception.connect(exception_printer)
def request(self, **request): """ The master request method. Composes the environment dictionary and passes to the handler, returning the result of the handler. Assumes defaults for the query environment, which can be overridden using the arguments to the request. """ environ = self._base_environ(**request) # Curry a data dictionary into an instance of the template renderer # callback function. data = {} on_template_render = curry(store_rendered_templates, data) signal_uid = "template-render-%s" % id(request) signals.template_rendered.connect(on_template_render, dispatch_uid=signal_uid) # Capture exceptions created by the handler. exception_uid = "request-exception-%s" % id(request) got_request_exception.connect(self.store_exc_info, dispatch_uid=exception_uid) try: try: response = self.handler(environ) except TemplateDoesNotExist as e: # If the view raises an exception, Django will attempt to show # the 500.html template. If that template is not available, # we should ignore the error in favor of re-raising the # underlying exception that caused the 500 error. Any other # template found to be missing during view error handling # should be reported as-is. if e.args != ('500.html', ): raise # Look for a signalled exception, clear the current context # exception data, then re-raise the signalled exception. # Also make sure that the signalled exception is cleared from # the local cache! if self.exc_info: exc_info = self.exc_info self.exc_info = None six.reraise(*exc_info) # Save the client and request that stimulated the response. response.client = self response.request = request # Add any rendered template detail to the response. response.templates = data.get("templates", []) response.context = data.get("context") response.json = curry(self._parse_json, response) # Attach the ResolverMatch instance to the response response.resolver_match = SimpleLazyObject( lambda: resolve(request['PATH_INFO'])) # Flatten a single context. Not really necessary anymore thanks to # the __getattr__ flattening in ContextList, but has some edge-case # backwards-compatibility implications. if response.context and len(response.context) == 1: response.context = response.context[0] # Update persistent cookie data. if response.cookies: self.cookies.update(response.cookies) return response finally: signals.template_rendered.disconnect(dispatch_uid=signal_uid) got_request_exception.disconnect(dispatch_uid=exception_uid)
def request(self, **request): """ The master request method. Composes the environment dictionary and passes to the handler, returning the result of the handler. Assumes defaults for the query environment, which can be overridden using the arguments to the request. """ environ = { 'HTTP_COOKIE': self.cookies, 'PATH_INFO': '/', 'QUERY_STRING': '', 'REQUEST_METHOD': 'GET', 'SCRIPT_NAME': '', 'SERVER_NAME': 'testserver', 'SERVER_PORT': '80', 'SERVER_PROTOCOL': 'HTTP/1.1', } environ.update(self.defaults) environ.update(request) # Curry a data dictionary into an instance of the template renderer # callback function. data = {} on_template_render = curry(store_rendered_templates, data) signals.template_rendered.connect(on_template_render) # Capture exceptions created by the handler. got_request_exception.connect(self.store_exc_info) try: response = self.handler(environ) except TemplateDoesNotExist as e: # If the view raises an exception, Django will attempt to show # the 500.html template. If that template is not available, # we should ignore the error in favor of re-raising the # underlying exception that caused the 500 error. Any other # template found to be missing during view error handling # should be reported as-is. if e.args != ('500.html',): raise # Look for a signalled exception, clear the current context # exception data, then re-raise the signalled exception. # Also make sure that the signalled exception is cleared from # the local cache! if self.exc_info: exc_info = self.exc_info self.exc_info = None raise exc_info[1].with_traceback(exc_info[2]) # Save the client and request that stimulated the response. response.client = self response.request = request # Add any rendered template detail to the response. # If there was only one template rendered (the most likely case), # flatten the list to a single element. for detail in ('template', 'context'): if data.get(detail): if len(data[detail]) == 1: setattr(response, detail, data[detail][0]); else: setattr(response, detail, data[detail]) else: setattr(response, detail, None) # Update persistent cookie data. if response.cookies: self.cookies.update(response.cookies) return response
def request(self, **request): """ The master request method. Composes the environment dictionary and passes to the handler, returning the result of the handler. Assumes defaults for the query environment, which can be overridden using the arguments to the request. """ environ = self._base_environ(**request) # Curry a data dictionary into an instance of the template renderer # callback function. data = {} on_template_render = curry(store_rendered_templates, data) signal_uid = "template-render-%s" % id(request) signals.template_rendered.connect(on_template_render, dispatch_uid=signal_uid) # Capture exceptions created by the handler. exception_uid = "request-exception-%s" % id(request) got_request_exception.connect(self.store_exc_info, dispatch_uid=exception_uid) try: try: response = self.handler(environ) except TemplateDoesNotExist as e: # If the view raises an exception, Django will attempt to show # the 500.html template. If that template is not available, # we should ignore the error in favor of re-raising the # underlying exception that caused the 500 error. Any other # template found to be missing during view error handling # should be reported as-is. if e.args != ('500.html',): raise # Look for a signalled exception, clear the current context # exception data, then re-raise the signalled exception. # Also make sure that the signalled exception is cleared from # the local cache! if self.exc_info: exc_info = self.exc_info self.exc_info = None six.reraise(*exc_info) # Save the client and request that stimulated the response. response.client = self response.request = request # Add any rendered template detail to the response. response.templates = data.get("templates", []) response.context = data.get("context") response.json = curry(self._parse_json, response) # Attach the ResolverMatch instance to the response response.resolver_match = SimpleLazyObject(lambda: resolve(request['PATH_INFO'])) # Flatten a single context. Not really necessary anymore thanks to # the __getattr__ flattening in ContextList, but has some edge-case # backwards-compatibility implications. if response.context and len(response.context) == 1: response.context = response.context[0] # Update persistent cookie data. if response.cookies: self.cookies.update(response.cookies) return response finally: signals.template_rendered.disconnect(dispatch_uid=signal_uid) got_request_exception.disconnect(dispatch_uid=exception_uid)
# Force Django to reload its settings. from django.conf import settings settings._target = None # Must set this env var before importing any part of Django os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' import logging import django.core.handlers.wsgi import django.core.signals import django.db import django.dispatch.dispatcher def log_exception(*args, **kwds): logging.exception('Exception in request:') from django.core.signals import got_request_exception # Log errors. got_request_exception.connect(log_exception) # Unregister the rollback event handler. got_request_exception.disconnect(django.db._rollback_on_exception) def main(): # Create a Django application for WSGI. application = django.core.handlers.wsgi.WSGIHandler() # Run the WSGI CGI handler with that application. util.run_wsgi_app(application) if __name__ == '__main__': main()
from django.core.signals import got_request_exception, request_finished from django_rq import thread_queue from .queues import get_commit_mode # If we're not in AUTOCOMMIT mode, wire up request finished/exception signal if not get_commit_mode(): request_finished.connect(thread_queue.commit) got_request_exception.connect(thread_queue.clear)
from __future__ import unicode_literals
else: client = None def ping(*args, **kwargs): client.send('ping') client.recv() def trash(*args, **kwargs): client.send('trash') client.recv() def error(*args, **kwargs): client.send('error') client.recv() if client: client.send('ping') client.recv() from django.core.signals import request_started, got_request_exception from django.db.models.signals import post_delete request_started.connect(ping) post_delete.connect(trash) got_request_exception.connect(error)