def process_exception(self, request, exception): if (not getattr(self, 'appenlight_client') or not self.appenlight_client.config.get('enabled')): return None environ = request.environ if not self.appenlight_client.config['report_errors'] or environ.get('appenlight.ignore_error'): return None user = getattr(request, 'user', None) end_time = default_timer() if user and user.is_authenticated(): environ['appenlight.username'] = unicode(user.pk) http_status = 500 request._errormator_create_report = True traceback = get_current_traceback(skip=1, show_hidden_frames=True, ignore_system_exceptions=True) appenlight_storage = get_local_storage(local_timing) appenlight_storage.thread_stats['main'] = end_time - request.__start_time__ stats, slow_calls = appenlight_storage.get_thread_stats() self.appenlight_client.save_request_stats(stats, view_name=environ.get('appenlight.view_name','')) self.appenlight_client.py_report(environ, traceback, message=None, http_status=http_status, start_time=datetime.utcfromtimestamp(request.__start_time__), end_time=datetime.utcfromtimestamp(end_time), request_stats=stats, slow_calls=slow_calls) del traceback
def track_exception(environ): if 'appenlight.client' not in environ: return # pass the traceback object to middleware environ['appenlight.__traceback'] = get_current_traceback( skip=1, show_hidden_frames=True, ignore_system_exceptions=True)
def print_traceback(log): traceback = get_current_traceback(skip=1, show_hidden_frames=True, ignore_system_exceptions=True) exception_text = traceback.exception log.error(exception_text) log.error(traceback.plaintext) del traceback
def error_tween(request): try: response = handler(request) except blacklist: raise except: if 'appenlight.client' in request.environ: # pass the traceback object to middleware request.environ[ 'appenlight.__traceback'] = get_current_traceback( skip=1, show_hidden_frames=True, ignore_system_exceptions=True) raise # finally: # appenlight_storage = get_local_storage(local_timing) # print appenlight_storage.view_name return response
def test_py_report_500_traceback(self): self.setUpClient() try: raise Exception('Test Exception') except: traceback = get_current_traceback(skip=1, show_hidden_frames=True, ignore_system_exceptions=True) self.client.py_report(TEST_ENVIRON, traceback=traceback, http_status=500, start_time=REQ_START_TIME, end_time=REQ_END_TIME) self.client.report_queue[0]['traceback'] = \ 'Traceback (most recent call last):' line_no = self.client.report_queue[0]['report_details'][0]['traceback'][0]['line'] assert int(line_no) > 0 # set line number to match as this will change over time PARSED_REPORT_500['report_details'][0]['traceback'][0]['line'] = line_no self.assertDictContainsSubset(PARSED_REPORT_500, self.client.report_queue[0])
def test_frameinfo(self): self.setUpClient(config={'appenlight.report_local_vars': 'true'}) test = 1 b = {1: 'a', '2': 2, 'ccc': 'ddd'} obj = object() e_obj = client.Client({}) unic = 'grzegżółka' a_list = [1, 2, 4, 5, 6, client.Client({}), 'dupa'] long_val = 'imlong' * 100 datetest = datetime.datetime.utcnow() try: raise Exception('Test Exception') except: traceback = get_current_traceback(skip=1, show_hidden_frames=True, ignore_system_exceptions=True) self.client.py_report(TEST_ENVIRON, traceback=traceback, http_status=500) assert len( self.client.report_queue[0]['report_details'][0]['traceback'][0][ 'vars']) == 9
def gather_data(client, environ=None, gather_exception=True, gather_slowness=True, gather_logs=True, clear_storage=True, exc_info=None, start_time=None, end_time=None): """ exc_info is supposed to be (exc_type, exc_value, tb) - what sys.exc_info() returns """ if not client.config['enabled']: return None if environ is None: environ = {} if not environ.get('appenlight.request_id'): environ['appenlight.request_id'] = str(uuid.uuid4()) http_status = 200 traceback = None if gather_exception and not exc_info: traceback = get_current_traceback(skip=1, show_hidden_frames=True, ignore_system_exceptions=True) if traceback: http_status = 500 elif exc_info: traceback = Traceback(*exc_info) http_status = 500 appenlight_storage = get_local_storage(local_timing) stats, slow_calls = appenlight_storage.get_thread_stats() if traceback is not None or (slow_calls and gather_slowness): client.py_report(environ, traceback, http_status=http_status, request_stats=stats, slow_calls=slow_calls, start_time=start_time, end_time=end_time) # dereference del traceback if client.config['logging']: if gather_logs: records = client.log_handler.get_records() client.log_handler.clear_records() client.py_log(environ, records=records, r_uuid=environ['appenlight.request_id']) if clear_storage: appenlight_storage.clear() client.check_if_deliver(client.config['force_send'] or environ.get('appenlight.force_send'))
def __call__(self, environ, start_response): """Run the application and conserve the traceback frames. also determine if we got 404 """ environ['appenlight.request_id'] = str(uuid.uuid4()) appenlight_storage = get_local_storage(local_timing) # clear out thread stats on request start appenlight_storage.clear() app_iter = None detected_data = [] create_report = False traceback = None http_status = 200 start_time = default_timer() def detect_headers(status, headers, *k, **kw): detected_data[:] = status[:3], headers return start_response(status, headers, *k, **kw) # inject client instance reference to environ if 'appenlight.client' not in environ: environ['appenlight.client'] = self.appenlight_client # some bw. compat stubs def local_report(message, include_traceback=True, http_status=200): environ['appenlight.force_send'] = True def local_log(level, message): environ['appenlight.force_send'] = True environ['appenlight.report'] = local_report environ['appenlight.log'] = local_log try: app_iter = self.app(environ, detect_headers) return app_iter except Exception as exc: if hasattr(app_iter, 'close'): app_iter.close() # we need that here traceback = get_current_traceback(skip=1, show_hidden_frames=True, ignore_system_exceptions=True) # by default reraise exceptions for app/FW to handle if self.appenlight_client.config['reraise_exceptions']: raise try: start_response('500 INTERNAL SERVER ERROR', [('Content-Type', 'text/html; charset=utf-8')]) except Exception as exc: environ['wsgi.errors'].write( 'AppenlightWSGIWrapper middleware catched exception ' 'in streamed response at a point where response headers ' 'were already sent.\n') else: return 'Server Error' finally: # report 500's and 404's # report slowness end_time = default_timer() appenlight_storage.thread_stats['main'] = end_time - start_time delta = datetime.timedelta(seconds=(end_time - start_time)) stats, slow_calls = appenlight_storage.get_thread_stats() if 'appenlight.view_name' not in environ: environ['appenlight.view_name'] = getattr(appenlight_storage,'view_name', '') if detected_data and detected_data[0]: http_status = int(detected_data[0]) if self.appenlight_client.config['slow_requests'] and not environ.get('appenlight.ignore_slow'): # do we have slow calls/request ? if (delta >= self.appenlight_client.config['slow_request_time'] or slow_calls): create_report = True if 'appenlight.__traceback' in environ and not environ.get('appenlight.ignore_error'): # get traceback gathered by pyramid tween traceback = environ['appenlight.__traceback'] del environ['appenlight.__traceback'] http_status = 500 create_report = True if traceback and self.appenlight_client.config['report_errors'] and not environ.get('appenlight.ignore_error'): http_status = 500 create_report = True elif (self.appenlight_client.config['report_404'] and http_status == 404): create_report = True if create_report: self.appenlight_client.py_report(environ, traceback, message=None, http_status=http_status, start_time=datetime.datetime.utcfromtimestamp(start_time), end_time=datetime.datetime.utcfromtimestamp(end_time), request_stats=stats, slow_calls=slow_calls) # dereference del traceback # force log fetching traceback = True self.appenlight_client.save_request_stats(stats, view_name=environ.get('appenlight.view_name','')) if self.appenlight_client.config['logging']: records = self.appenlight_client.log_handler.get_records() self.appenlight_client.log_handler.clear_records() self.appenlight_client.py_log(environ, records=records, r_uuid=environ['appenlight.request_id'], created_report=create_report) # send all data we gathered immediately at the end of request self.appenlight_client.check_if_deliver(self.appenlight_client.config['force_send'] or environ.get('appenlight.force_send'))
def get_current_traceback(self): tb = get_current_traceback(skip=1, show_hidden_frames=True, ignore_system_exceptions=True) return tb