class AirbrakeLogObserver(object): def __init__(self, settings): if not settings.get('AIRBRAKE', False) or \ settings['AIRBRAKE'].get('DISABLE', False): return self.enabled = True project_id = settings['AIRBRAKE'].get('PROJECT_ID', None) api_key = settings['AIRBRAKE'].get('API_KEY', None) host = settings['AIRBRAKE'].get('HOST', None) timeout = settings['AIRBRAKE'].get('TIMEOUT', None) environment = settings['AIRBRAKE'].get('ENVIRONMENT', None) self.ab = Airbrake(project_id, api_key, host, timeout, environment=environment, send_uncaught_exc=False) def __call__(self, event): if not event["log_failure"] or not self.ab: return error = build_error(event["log_failure"].value, message=event["log_failure"].getErrorMessage()) notice = self.ab.build_notice(error) self.ab.notify(notice)
class AirbrakeNotifierMiddleware(MiddlewareMixin): """Send an error to airbrake for all exceptions""" def __init__(self, *args, **kwargs): super(AirbrakeNotifierMiddleware, self).__init__(*args, **kwargs) self.enabled = False if hasattr(settings, 'AIRBRAKE') \ and not settings.AIRBRAKE.get('DISABLE', False): self.enabled = True project_id = settings.AIRBRAKE.get('PROJECT_ID', None) api_key = settings.AIRBRAKE.get('API_KEY', None) host = settings.AIRBRAKE.get('HOST', None) timeout = settings.AIRBRAKE.get('TIMEOUT', None) environment = settings.AIRBRAKE.get('ENVIRONMENT', None) self.ab = Airbrake(project_id, api_key, host, timeout, environment=environment) def process_exception(self, request, exception): if self.enabled: self.ab.notify(exception)
class AirbrakeApp(object): def __init__(self, app=None): self.app = app if app is not None: self.init_app(app) def init_app(self, app=None): project_id = app.config.get('AIRBRAKE_PROJECT_ID', None) api_key = app.config.get('AIRBRAKE_API_KEY', None) host = app.config.get('AIRBRAKE_HOST', None) environment = app.config.get('AIRBRAKE_ENVIRONMENT', None) if project_id and api_key: self.ab = Airbrake(project_id, api_key, host, environment=environment) got_request_exception.connect(self.process_exception, sender=app) def process_exception(self, *args, **kwargs): if not self.ab: return self.ab.notify(kwargs["exception"])