def add_django_request_to_notification(notification): if not hasattr(notification.request_config, "django_request"): return request = notification.request_config.django_request if notification.context is None: route = resolve(request.path_info) if route: notification.context = route.url_name else: notification.context = "%s %s" % (request.method, request.path_info) if hasattr(request, 'user') and request.user.is_authenticated(): try: name = " ".join([request.user.first_name or '', request.user.last_name or '']) notification.set_user(id=request.user.username, email=request.user.email, name=name) except Exception as e: bugsnag.warn("could not get user data: %s" % e) else: notification.set_user(id=request.META['REMOTE_ADDR']) notification.add_tab("session", dict(request.session)) notification.add_tab("request", { 'path': request.path, 'encoding': request.encoding, 'params': dict(request.REQUEST), 'url': request.build_absolute_uri(), }) notification.add_tab("environment", dict(request.META))
def sanitize_object(obj, **kwargs): filters = kwargs.get("filters", []) if isinstance(obj, dict): clean_dict = {} for k, v in six.iteritems(obj): # Remove values for keys matching filters if any(f in k for f in filters): clean_dict[k] = "[FILTERED]" else: clean_obj = sanitize_object(v, **kwargs) if clean_obj: clean_dict[k] = clean_obj return clean_dict elif any(isinstance(obj, t) for t in (list, set, tuple)): return [sanitize_object(x, **kwargs) for x in obj] else: try: if isinstance(obj, six.string_types): string = obj else: if six.PY2: string = unicode(str(obj), errors='replace') else: string = str(obj) except Exception: exc = traceback.format_exc() bugsnag.warn("Could not add object to metadata: %s" % exc) string = "[BADENCODING]" return string
def deliver(self): """ Deliver the exception notification to Bugsnag. """ url = self.config.get_endpoint() try: if self.config.api_key is None: bugsnag.log("No API key configured, couldn't notify") return # Return early if we shouldn't notify for current release stage if not self.config.should_notify(): return # Return early if we should ignore exceptions of this type if self.config.should_ignore(self.exception): return # Generate the payload and make the request bugsnag.log("Notifying %s of exception" % url) payload = self.__generate_payload().encode('utf-8', errors='replace') req = Request(url, payload, { 'Content-Type': 'application/json' }) threading.Thread(target=request, args=(req,)).start() except Exception: exc = traceback.format_exc() bugsnag.warn("Notification to %s failed:\n%s" % (url, exc))
def sanitize_object(obj, **kwargs): filters = kwargs.get("filters", []) if isinstance(obj, dict): clean_dict = {} for k, v in six.iteritems(obj): # Remove values for keys matching filters if any(f.lower() in k.lower() for f in filters): clean_dict[k] = "[FILTERED]" else: clean_obj = sanitize_object(v, **kwargs) if clean_obj is not None: clean_dict[k] = clean_obj return clean_dict elif any(isinstance(obj, t) for t in (list, set, tuple)): return [sanitize_object(x, **kwargs) for x in obj] elif any(isinstance(obj, t) for t in (bool, float, int)): return obj else: try: if six.PY3 and isinstance(obj, bytes): string = six.text_type(obj, encoding='utf-8', errors='replace') else: string = six.text_type(obj) except Exception: exc = traceback.format_exc() bugsnag.warn("Could not add object to metadata: %s" % exc) string = "[BADENCODING]" return string
def add_django_request_to_notification(notification): if not hasattr(notification.request_config, "django_request"): return request = notification.request_config.django_request if notification.context is None: route = resolve(request.path_info) if route: notification.context = route.url_name else: notification.context = "%s %s" % (request.method, request.path_info) if hasattr(request, 'user') and request.user.is_authenticated(): try: name = request.user.get_full_name() email = getattr(request.user, 'email', None) username = six.text_type(request.user.get_username()) notification.set_user(id=username, email=email, name=name) except Exception as e: bugsnag.warn("could not get user data: %s" % e) else: notification.set_user(id=request.META['REMOTE_ADDR']) notification.add_tab("session", dict(request.session)) notification.add_tab("request", { 'path': request.path, 'encoding': request.encoding, 'GET': dict(request.GET), 'POST': dict(request.POST), 'url': request.build_absolute_uri(), }) notification.add_tab("environment", dict(request.META))
def sanitize_object(obj, **kwargs): filters = kwargs.get("filters", []) if isinstance(obj, dict): clean_dict = {} for k, v in six.iteritems(obj): # Remove values for keys matching filters if any(f.lower() in k.lower() for f in filters): clean_dict[k] = "[FILTERED]" else: clean_obj = sanitize_object(v, **kwargs) if clean_obj is not None: clean_dict[k] = clean_obj return clean_dict elif any(isinstance(obj, t) for t in (list, set, tuple)): return [sanitize_object(x, **kwargs) for x in obj] elif any(isinstance(obj, t) for t in (bool, float, int)): return obj else: try: if isinstance(obj, six.string_types): string = obj else: if six.PY2: string = unicode(str(obj), errors='replace') else: string = str(obj) except Exception: exc = traceback.format_exc() bugsnag.warn("Could not add object to metadata: %s" % exc) string = "[BADENCODING]" return string
def add_django_request_to_notification(notification): if not hasattr(notification.request_config, "django_request"): return request = notification.request_config.django_request if notification.context is None: route = resolve(request.path_info) if route: notification.context = route.url_name else: notification.context = "%s %s" % (request.method, request.path_info) if hasattr(request, 'user') and request.user.is_authenticated(): try: name = " ".join( [request.user.first_name or '', request.user.last_name or '']) notification.set_user(id=request.user.username, email=request.user.email, name=name) except Exception as e: bugsnag.warn("could not get user data: %s" % e) else: notification.set_user(id=request.META['REMOTE_ADDR']) notification.add_tab("session", dict(request.session)) notification.add_tab( "request", { 'path': request.path, 'encoding': request.encoding, 'params': dict(request.REQUEST), 'url': request.build_absolute_uri(), }) notification.add_tab("environment", dict(request.META))
def _generate_stacktrace(self, tb): """ Build the stacktrace """ if tb: trace = traceback.extract_tb(tb) else: trace = traceback.extract_stack() bugsnag_module_path = os.path.dirname(bugsnag.__file__) logging_module_path = os.path.dirname(logging.__file__) exclude_module_paths = [bugsnag_module_path, logging_module_path] user_exclude_modules = self.config.get("traceback_exclude_modules") for exclude_module in user_exclude_modules: try: exclude_module_paths.append(exclude_module.__file__) except: bugsnag.warn("Could not exclude module: %s" % repr(exclude_module)) lib_root = self.config.get("lib_root") if lib_root and lib_root[-1] != os.sep: lib_root += os.sep project_root = self.config.get("project_root") if project_root and project_root[-1] != os.sep: project_root += os.sep stacktrace = [] for line in trace: file_name = os.path.abspath(str(line[0])) in_project = False skip_module = False for module_path in exclude_module_paths: if file_name.startswith(module_path): skip_module = True break if skip_module: continue if lib_root and file_name.startswith(lib_root): file_name = file_name[len(lib_root):] elif project_root and file_name.startswith(project_root): file_name = file_name[len(project_root):] in_project = True stacktrace.append({ "file": file_name, "lineNumber": int(str(line[1])), "method": str(line[2]), "inProject": in_project, "code": self._code_for(file_name, int(str(line[1]))) }) stacktrace.reverse() return stacktrace
def deliver(self): """ Deliver the exception notification to Bugsnag. """ try: # Return early if we shouldn't notify for current release stage if not self.config.should_notify(): return if self.api_key is None: bugsnag.log("No API key configured, couldn't notify") return # Return early if we should ignore exceptions of this type if self.config.should_ignore(self.exception): return self.config.middleware.run(self, self._send_to_bugsnag) except Exception: exc = traceback.format_exc() bugsnag.warn("Notifying Bugsnag failed:\n%s" % (exc))