# HG changeset patch # User RhodeCode Admin # Date 2023-08-07 12:03:39 # Node ID 42e5e37a02714ee7c112b14848966285682fe695 # Parent a44d603fa07c884559dc88f2efcbe741b152c049 exc-tracking: synced with ce diff --git a/vcsserver/lib/exc_tracking.py b/vcsserver/lib/exc_tracking.py --- a/vcsserver/lib/exc_tracking.py +++ b/vcsserver/lib/exc_tracking.py @@ -15,7 +15,7 @@ # along with this program; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - +import io import os import time import sys @@ -34,14 +34,13 @@ exc_store_dir_name = 'rc_exception_store def exc_serialize(exc_id, tb, exc_type, extra_data=None): - data = { - 'version': 'v1', - 'exc_id': exc_id, - 'exc_utc_date': datetime.datetime.utcnow().isoformat(), - 'exc_timestamp': repr(time.time()), - 'exc_message': tb, - 'exc_type': exc_type, + "version": "v1", + "exc_id": exc_id, + "exc_utc_date": datetime.datetime.utcnow().isoformat(), + "exc_timestamp": repr(time.time()), + "exc_message": tb, + "exc_type": exc_type, } if extra_data: data.update(extra_data) @@ -67,33 +66,38 @@ def get_exc_store(): import vcsserver as app - exc_store_dir = app.CONFIG.get('exception_tracker.store_path', '') or tempfile.gettempdir() + exc_store_dir = ( + app.CONFIG.get("exception_tracker.store_path", "") or tempfile.gettempdir() + ) _exc_store_path = os.path.join(exc_store_dir, exc_store_dir_name) _exc_store_path = os.path.abspath(_exc_store_path) if not os.path.isdir(_exc_store_path): os.makedirs(_exc_store_path) - log.debug('Initializing exceptions store at %s', _exc_store_path) + log.debug("Initializing exceptions store at %s", _exc_store_path) _exc_store = _exc_store_path return _exc_store_path def get_detailed_tb(exc_info): - from io import StringIO - try: - from pip._vendor.rich import traceback as rich_tb, scope as rich_scope, console as rich_console + from pip._vendor.rich import ( + traceback as rich_tb, + scope as rich_scope, + console as rich_console, + ) except ImportError: try: - from rich import traceback as rich_tb, scope as rich_scope, console as rich_console + from rich import ( + traceback as rich_tb, + scope as rich_scope, + console as rich_console, + ) except ImportError: return None - console = rich_console.Console( - width=160, - file=StringIO() - ) + console = rich_console.Console(width=160, file=io.StringIO()) exc = rich_tb.Traceback.extract(*exc_info, show_locals=True) @@ -104,11 +108,9 @@ def get_detailed_tb(exc_info): theme=None, word_wrap=False, show_locals=False, - max_frames=100 + max_frames=100, ) - formatted_locals = "" - # last_stack = exc.stacks[-1] # last_frame = last_stack.frames[-1] # if last_frame and last_frame.locals: @@ -127,14 +129,15 @@ def get_request_metadata(request=None) - request_metadata = {} if not request: from pyramid.threadlocal import get_current_request + request = get_current_request() # NOTE(marcink): store request information into exc_data if request: - request_metadata['client_address'] = getattr(request, 'client_addr', '') - request_metadata['user_agent'] = getattr(request, 'user_agent', '') - request_metadata['method'] = getattr(request, 'method', '') - request_metadata['url'] = getattr(request, 'url', '') + request_metadata["client_address"] = getattr(request, "client_addr", "") + request_metadata["user_agent"] = getattr(request, "user_agent", "") + request_metadata["method"] = getattr(request, "method", "") + request_metadata["url"] = getattr(request, "url", "") return request_metadata @@ -180,19 +183,20 @@ def _store_exception(exc_id, exc_info, p exc_type_name = exc_type.__name__ exc_data, org_data = exc_serialize(exc_id, tb, exc_type_name, extra_data=extra_data) - exc_pref_id = '{}_{}_{}'.format(exc_id, prefix, org_data['exc_timestamp']) + exc_pref_id = f"{exc_id}_{prefix}_{org_data['exc_timestamp']}" exc_store_path = get_exc_store() if not os.path.isdir(exc_store_path): os.makedirs(exc_store_path) stored_exc_path = os.path.join(exc_store_path, exc_pref_id) - with open(stored_exc_path, 'wb') as f: + with open(stored_exc_path, "wb") as f: f.write(exc_data) - log.debug('Stored generated exception %s as: %s', exc_id, stored_exc_path) + log.debug("Stored generated exception %s as: %s", exc_id, stored_exc_path) - log.error( - 'error occurred handling this request.\n' - 'Path: `%s`, %s', - request_path, tb) + if request_path: + log.error( + 'error occurred handling this request.\n' + 'Path: `%s`, %s', + request_path, tb) def store_exception(exc_id, exc_info, prefix=global_prefix, request_path=''): @@ -207,11 +211,12 @@ def store_exception(exc_id, exc_info, pr exc_type = exc_info[0] exc_type_name = exc_type.__name__ - _store_exception(exc_id=exc_id, exc_info=exc_info, prefix=prefix, - request_path=request_path) + _store_exception( + exc_id=exc_id, exc_info=exc_info, prefix=prefix, request_path=request_path, + ) return exc_id, exc_type_name except Exception: - log.exception('Failed to store exception `%s` information', exc_id) + log.exception("Failed to store exception `%s` information", exc_id) # there's no way this can fail, it will crash server badly if it does. pass @@ -219,13 +224,13 @@ def store_exception(exc_id, exc_info, pr def _find_exc_file(exc_id, prefix=global_prefix): exc_store_path = get_exc_store() if prefix: - exc_id = f'{exc_id}_{prefix}' + exc_id = f"{exc_id}_{prefix}" else: # search without a prefix - exc_id = f'{exc_id}' + exc_id = f"{exc_id}" found_exc_id = None - matches = glob.glob(os.path.join(exc_store_path, exc_id) + '*') + matches = glob.glob(os.path.join(exc_store_path, exc_id) + "*") if matches: found_exc_id = matches[0] @@ -235,10 +240,10 @@ def _find_exc_file(exc_id, prefix=global def _read_exception(exc_id, prefix): exc_id_file_path = _find_exc_file(exc_id=exc_id, prefix=prefix) if exc_id_file_path: - with open(exc_id_file_path, 'rb') as f: + with open(exc_id_file_path, "rb") as f: return exc_unserialize(f.read()) else: - log.debug('Exception File `%s` not found', exc_id_file_path) + log.debug("Exception File `%s` not found", exc_id_file_path) return None @@ -246,7 +251,7 @@ def read_exception(exc_id, prefix=global try: return _read_exception(exc_id=exc_id, prefix=prefix) except Exception: - log.exception('Failed to read exception `%s` information', exc_id) + log.exception("Failed to read exception `%s` information", exc_id) # there's no way this can fail, it will crash server badly if it does. return None @@ -258,7 +263,7 @@ def delete_exception(exc_id, prefix=glob os.remove(exc_id_file_path) except Exception: - log.exception('Failed to remove exception `%s` information', exc_id) + log.exception("Failed to remove exception `%s` information", exc_id) # there's no way this can fail, it will crash server badly if it does. pass