From 704843bda9997b3da8a7d84feac0258e28f99f05 Mon Sep 17 00:00:00 2001 From: Rick Lan Date: Wed, 23 Sep 2020 15:07:23 +1000 Subject: [PATCH] update sentry --- selfdrive/crash.py | 52 +++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/selfdrive/crash.py b/selfdrive/crash.py index f896a244f..e685c6293 100644 --- a/selfdrive/crash.py +++ b/selfdrive/crash.py @@ -3,20 +3,27 @@ import os import sys import threading import capnp -from common.params import Params +from datetime import datetime +import traceback from selfdrive.version import version, dirty, origin, branch -uniqueID = Params().get('DongleId', None) +from common.params import Params +import requests +from common.dp import is_online +CRASHES_DIR = '/sdcard/crash_logs/' from selfdrive.swaglog import cloudlog from common.android import ANDROID if os.getenv("NOLOG") or os.getenv("NOCRASH") or not ANDROID: - def capture_exception(*exc_info): + def capture_exception(*args, **kwargs): pass + def bind_user(**kwargs): pass + def bind_extra(**kwargs): pass + def install(): pass else: @@ -27,46 +34,61 @@ else: dongle_id = params.get("DongleId").decode('utf8') except AttributeError: dongle_id = "None" - error_tags = {'dirty': dirty, 'username': uniqueID, 'dongle_id': dongle_id, 'branch': branch, 'remote': origin} + try: + ip = requests.get('https://checkip.amazonaws.com/', timeout=3).text.strip() if is_online() else '255.255.255.255' + except: + ip = "255.255.255.255" + error_tags = {'dirty': dirty, 'username': dongle_id, 'dongle_id': dongle_id, 'branch': branch, 'remote': origin} - client = Client('https://980a0cba712a4c3593c33c78a12446e1:fecab286bcaf4dba8b04f7cff0188e2d@sentry.io/1488600', + client = Client('http://7107f046f45b4b4f9b277d0684bc9281@sentry.dragonpilot.cn:9000/2', install_sys_hook=False, transport=HTTPTransport, release=version, tags=error_tags) def capture_exception(*args, **kwargs): + save_exception(traceback.format_exc()) exc_info = sys.exc_info() if not exc_info[0] is capnp.lib.capnp.KjException: client.captureException(*args, **kwargs) cloudlog.error("crash", exc_info=kwargs.get('exc_info', 1)) + # dp - from @ShaneSmiskol, save log into local directory + def save_exception(exc_text): + if not os.path.exists(CRASHES_DIR): + os.mkdir(CRASHES_DIR) + log_file = '{}/{}'.format(CRASHES_DIR, datetime.now().strftime('%Y-%m-%d--%H-%M-%S.%f.log')[:-3]) + with open(log_file, 'w') as f: + f.write(exc_text) + print('Logged current crash to {}'.format(log_file)) + def bind_user(**kwargs): client.user_context(kwargs) def capture_warning(warning_string): - bind_user(id=dongle_id) + bind_user(id=dongle_id, ip_address=ip) client.captureMessage(warning_string, level='warning') def capture_info(info_string): - bind_user(id=dongle_id) + bind_user(id=dongle_id, ip_address=ip) client.captureMessage(info_string, level='info') def bind_extra(**kwargs): client.extra_context(kwargs) def install(): - # installs a sys.excepthook - __excepthook__ = sys.excepthook - def handle_exception(*exc_info): - if exc_info[0] not in (KeyboardInterrupt, SystemExit): - capture_exception() - __excepthook__(*exc_info) - sys.excepthook = handle_exception - """ Workaround for `sys.excepthook` thread bug from: http://bugs.python.org/issue1230540 Call once from the main thread before creating any threads. Source: https://stackoverflow.com/a/31622038 """ + # installs a sys.excepthook + __excepthook__ = sys.excepthook + + def handle_exception(*exc_info): + if exc_info[0] not in (KeyboardInterrupt, SystemExit): + capture_exception() + __excepthook__(*exc_info) + sys.excepthook = handle_exception + init_original = threading.Thread.__init__ def init(self, *args, **kwargs):