##// END OF EJS Templates
exc-tracking: synced with ce
super-admin -
r1146:42e5e37a default
parent child Browse files
Show More
@@ -15,7 +15,7 b''
15 # along with this program; if not, write to the Free Software Foundation,
15 # along with this program; if not, write to the Free Software Foundation,
16 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
18
18 import io
19 import os
19 import os
20 import time
20 import time
21 import sys
21 import sys
@@ -34,14 +34,13 b" exc_store_dir_name = 'rc_exception_store"
34
34
35
35
36 def exc_serialize(exc_id, tb, exc_type, extra_data=None):
36 def exc_serialize(exc_id, tb, exc_type, extra_data=None):
37
38 data = {
37 data = {
39 'version': 'v1',
38 "version": "v1",
40 'exc_id': exc_id,
39 "exc_id": exc_id,
41 'exc_utc_date': datetime.datetime.utcnow().isoformat(),
40 "exc_utc_date": datetime.datetime.utcnow().isoformat(),
42 'exc_timestamp': repr(time.time()),
41 "exc_timestamp": repr(time.time()),
43 'exc_message': tb,
42 "exc_message": tb,
44 'exc_type': exc_type,
43 "exc_type": exc_type,
45 }
44 }
46 if extra_data:
45 if extra_data:
47 data.update(extra_data)
46 data.update(extra_data)
@@ -67,33 +66,38 b' def get_exc_store():'
67
66
68 import vcsserver as app
67 import vcsserver as app
69
68
70 exc_store_dir = app.CONFIG.get('exception_tracker.store_path', '') or tempfile.gettempdir()
69 exc_store_dir = (
70 app.CONFIG.get("exception_tracker.store_path", "") or tempfile.gettempdir()
71 )
71 _exc_store_path = os.path.join(exc_store_dir, exc_store_dir_name)
72 _exc_store_path = os.path.join(exc_store_dir, exc_store_dir_name)
72
73
73 _exc_store_path = os.path.abspath(_exc_store_path)
74 _exc_store_path = os.path.abspath(_exc_store_path)
74 if not os.path.isdir(_exc_store_path):
75 if not os.path.isdir(_exc_store_path):
75 os.makedirs(_exc_store_path)
76 os.makedirs(_exc_store_path)
76 log.debug('Initializing exceptions store at %s', _exc_store_path)
77 log.debug("Initializing exceptions store at %s", _exc_store_path)
77 _exc_store = _exc_store_path
78 _exc_store = _exc_store_path
78
79
79 return _exc_store_path
80 return _exc_store_path
80
81
81
82
82 def get_detailed_tb(exc_info):
83 def get_detailed_tb(exc_info):
83 from io import StringIO
84
85 try:
84 try:
86 from pip._vendor.rich import traceback as rich_tb, scope as rich_scope, console as rich_console
85 from pip._vendor.rich import (
86 traceback as rich_tb,
87 scope as rich_scope,
88 console as rich_console,
89 )
87 except ImportError:
90 except ImportError:
88 try:
91 try:
89 from rich import traceback as rich_tb, scope as rich_scope, console as rich_console
92 from rich import (
93 traceback as rich_tb,
94 scope as rich_scope,
95 console as rich_console,
96 )
90 except ImportError:
97 except ImportError:
91 return None
98 return None
92
99
93 console = rich_console.Console(
100 console = rich_console.Console(width=160, file=io.StringIO())
94 width=160,
95 file=StringIO()
96 )
97
101
98 exc = rich_tb.Traceback.extract(*exc_info, show_locals=True)
102 exc = rich_tb.Traceback.extract(*exc_info, show_locals=True)
99
103
@@ -104,11 +108,9 b' def get_detailed_tb(exc_info):'
104 theme=None,
108 theme=None,
105 word_wrap=False,
109 word_wrap=False,
106 show_locals=False,
110 show_locals=False,
107 max_frames=100
111 max_frames=100,
108 )
112 )
109
113
110 formatted_locals = ""
111
112 # last_stack = exc.stacks[-1]
114 # last_stack = exc.stacks[-1]
113 # last_frame = last_stack.frames[-1]
115 # last_frame = last_stack.frames[-1]
114 # if last_frame and last_frame.locals:
116 # if last_frame and last_frame.locals:
@@ -127,14 +129,15 b' def get_request_metadata(request=None) -'
127 request_metadata = {}
129 request_metadata = {}
128 if not request:
130 if not request:
129 from pyramid.threadlocal import get_current_request
131 from pyramid.threadlocal import get_current_request
132
130 request = get_current_request()
133 request = get_current_request()
131
134
132 # NOTE(marcink): store request information into exc_data
135 # NOTE(marcink): store request information into exc_data
133 if request:
136 if request:
134 request_metadata['client_address'] = getattr(request, 'client_addr', '')
137 request_metadata["client_address"] = getattr(request, "client_addr", "")
135 request_metadata['user_agent'] = getattr(request, 'user_agent', '')
138 request_metadata["user_agent"] = getattr(request, "user_agent", "")
136 request_metadata['method'] = getattr(request, 'method', '')
139 request_metadata["method"] = getattr(request, "method", "")
137 request_metadata['url'] = getattr(request, 'url', '')
140 request_metadata["url"] = getattr(request, "url", "")
138 return request_metadata
141 return request_metadata
139
142
140
143
@@ -180,19 +183,20 b' def _store_exception(exc_id, exc_info, p'
180 exc_type_name = exc_type.__name__
183 exc_type_name = exc_type.__name__
181 exc_data, org_data = exc_serialize(exc_id, tb, exc_type_name, extra_data=extra_data)
184 exc_data, org_data = exc_serialize(exc_id, tb, exc_type_name, extra_data=extra_data)
182
185
183 exc_pref_id = '{}_{}_{}'.format(exc_id, prefix, org_data['exc_timestamp'])
186 exc_pref_id = f"{exc_id}_{prefix}_{org_data['exc_timestamp']}"
184 exc_store_path = get_exc_store()
187 exc_store_path = get_exc_store()
185 if not os.path.isdir(exc_store_path):
188 if not os.path.isdir(exc_store_path):
186 os.makedirs(exc_store_path)
189 os.makedirs(exc_store_path)
187 stored_exc_path = os.path.join(exc_store_path, exc_pref_id)
190 stored_exc_path = os.path.join(exc_store_path, exc_pref_id)
188 with open(stored_exc_path, 'wb') as f:
191 with open(stored_exc_path, "wb") as f:
189 f.write(exc_data)
192 f.write(exc_data)
190 log.debug('Stored generated exception %s as: %s', exc_id, stored_exc_path)
193 log.debug("Stored generated exception %s as: %s", exc_id, stored_exc_path)
191
194
192 log.error(
195 if request_path:
193 'error occurred handling this request.\n'
196 log.error(
194 'Path: `%s`, %s',
197 'error occurred handling this request.\n'
195 request_path, tb)
198 'Path: `%s`, %s',
199 request_path, tb)
196
200
197
201
198 def store_exception(exc_id, exc_info, prefix=global_prefix, request_path=''):
202 def store_exception(exc_id, exc_info, prefix=global_prefix, request_path=''):
@@ -207,11 +211,12 b' def store_exception(exc_id, exc_info, pr'
207 exc_type = exc_info[0]
211 exc_type = exc_info[0]
208 exc_type_name = exc_type.__name__
212 exc_type_name = exc_type.__name__
209
213
210 _store_exception(exc_id=exc_id, exc_info=exc_info, prefix=prefix,
214 _store_exception(
211 request_path=request_path)
215 exc_id=exc_id, exc_info=exc_info, prefix=prefix, request_path=request_path,
216 )
212 return exc_id, exc_type_name
217 return exc_id, exc_type_name
213 except Exception:
218 except Exception:
214 log.exception('Failed to store exception `%s` information', exc_id)
219 log.exception("Failed to store exception `%s` information", exc_id)
215 # there's no way this can fail, it will crash server badly if it does.
220 # there's no way this can fail, it will crash server badly if it does.
216 pass
221 pass
217
222
@@ -219,13 +224,13 b' def store_exception(exc_id, exc_info, pr'
219 def _find_exc_file(exc_id, prefix=global_prefix):
224 def _find_exc_file(exc_id, prefix=global_prefix):
220 exc_store_path = get_exc_store()
225 exc_store_path = get_exc_store()
221 if prefix:
226 if prefix:
222 exc_id = f'{exc_id}_{prefix}'
227 exc_id = f"{exc_id}_{prefix}"
223 else:
228 else:
224 # search without a prefix
229 # search without a prefix
225 exc_id = f'{exc_id}'
230 exc_id = f"{exc_id}"
226
231
227 found_exc_id = None
232 found_exc_id = None
228 matches = glob.glob(os.path.join(exc_store_path, exc_id) + '*')
233 matches = glob.glob(os.path.join(exc_store_path, exc_id) + "*")
229 if matches:
234 if matches:
230 found_exc_id = matches[0]
235 found_exc_id = matches[0]
231
236
@@ -235,10 +240,10 b' def _find_exc_file(exc_id, prefix=global'
235 def _read_exception(exc_id, prefix):
240 def _read_exception(exc_id, prefix):
236 exc_id_file_path = _find_exc_file(exc_id=exc_id, prefix=prefix)
241 exc_id_file_path = _find_exc_file(exc_id=exc_id, prefix=prefix)
237 if exc_id_file_path:
242 if exc_id_file_path:
238 with open(exc_id_file_path, 'rb') as f:
243 with open(exc_id_file_path, "rb") as f:
239 return exc_unserialize(f.read())
244 return exc_unserialize(f.read())
240 else:
245 else:
241 log.debug('Exception File `%s` not found', exc_id_file_path)
246 log.debug("Exception File `%s` not found", exc_id_file_path)
242 return None
247 return None
243
248
244
249
@@ -246,7 +251,7 b' def read_exception(exc_id, prefix=global'
246 try:
251 try:
247 return _read_exception(exc_id=exc_id, prefix=prefix)
252 return _read_exception(exc_id=exc_id, prefix=prefix)
248 except Exception:
253 except Exception:
249 log.exception('Failed to read exception `%s` information', exc_id)
254 log.exception("Failed to read exception `%s` information", exc_id)
250 # there's no way this can fail, it will crash server badly if it does.
255 # there's no way this can fail, it will crash server badly if it does.
251 return None
256 return None
252
257
@@ -258,7 +263,7 b' def delete_exception(exc_id, prefix=glob'
258 os.remove(exc_id_file_path)
263 os.remove(exc_id_file_path)
259
264
260 except Exception:
265 except Exception:
261 log.exception('Failed to remove exception `%s` information', exc_id)
266 log.exception("Failed to remove exception `%s` information", exc_id)
262 # there's no way this can fail, it will crash server badly if it does.
267 # there's no way this can fail, it will crash server badly if it does.
263 pass
268 pass
264
269
General Comments 0
You need to be logged in to leave comments. Login now