Files
sunnypilot/selfdrive/sentry.py
T
Jason Wen 9219b7966c Merge branch 'master' into dev-priv/master-test
# Conflicts:
#	panda
#	release/build_release.sh
#	selfdrive/car/car_helpers.py
#	selfdrive/car/chrysler/carcontroller.py
#	selfdrive/car/chrysler/carstate.py
#	selfdrive/car/chrysler/interface.py
#	selfdrive/car/ford/carstate.py
#	selfdrive/car/ford/interface.py
#	selfdrive/car/gm/carcontroller.py
#	selfdrive/car/gm/carstate.py
#	selfdrive/car/gm/interface.py
#	selfdrive/car/honda/carcontroller.py
#	selfdrive/car/honda/interface.py
#	selfdrive/car/hyundai/carcontroller.py
#	selfdrive/car/hyundai/carstate.py
#	selfdrive/car/hyundai/hyundaican.py
#	selfdrive/car/hyundai/interface.py
#	selfdrive/car/interfaces.py
#	selfdrive/car/mazda/carcontroller.py
#	selfdrive/car/mazda/carstate.py
#	selfdrive/car/mazda/interface.py
#	selfdrive/car/nissan/carcontroller.py
#	selfdrive/car/nissan/interface.py
#	selfdrive/car/subaru/carstate.py
#	selfdrive/car/subaru/interface.py
#	selfdrive/car/subaru/subarucan.py
#	selfdrive/car/toyota/carcontroller.py
#	selfdrive/car/toyota/carstate.py
#	selfdrive/car/toyota/interface.py
#	selfdrive/car/toyota/toyotacan.py
#	selfdrive/car/volkswagen/carcontroller.py
#	selfdrive/car/volkswagen/carstate.py
#	selfdrive/car/volkswagen/interface.py
#	selfdrive/controls/controlsd.py
#	selfdrive/controls/lib/latcontrol_torque.py
#	selfdrive/controls/lib/lateral_planner.py
#	selfdrive/controls/lib/longitudinal_mpc_lib/long_mpc.py
#	selfdrive/controls/lib/longitudinal_planner.py
#	selfdrive/locationd/paramsd.py
#	selfdrive/manager/manager.py
#	selfdrive/manager/process.py
#	selfdrive/manager/process_config.py
#	selfdrive/sentry.py
#	selfdrive/ui/SConscript
#	selfdrive/ui/qt/home.cc
#	selfdrive/ui/qt/maps/map_settings.cc
#	selfdrive/ui/qt/maps/map_settings.h
#	selfdrive/ui/qt/offroad/settings.cc
#	selfdrive/ui/qt/onroad.cc
#	selfdrive/ui/qt/onroad.h
#	selfdrive/ui/qt/util.cc
#	selfdrive/ui/qt/util.h
#	selfdrive/ui/soundd/sound.h
#	selfdrive/ui/ui.h
#	system/version.py
2023-09-05 20:17:19 -04:00

147 lines
4.3 KiB
Python

"""Install exception handler for process crash."""
import sentry_sdk
from enum import Enum
from sentry_sdk.integrations.threading import ThreadingIntegration
from openpilot.common.params import Params
#from openpilot.selfdrive.athena.registration import is_registered_device
from openpilot.system.hardware import HARDWARE, PC
from openpilot.system.swaglog import cloudlog
from openpilot.system.version import get_branch, get_commit, get_origin, get_version, \
is_comma_remote, is_dirty, is_tested_branch, get_branch_type
import os
import traceback
from cereal import car
from datetime import datetime
class SentryProject(Enum):
# python project
SELFDRIVE = "https://7e3be9bfcfe04c9abe58bd25fe290d1a@o1138119.ingest.sentry.io/6191481"
# native project
SELFDRIVE_NATIVE = "https://7e3be9bfcfe04c9abe58bd25fe290d1a@o1138119.ingest.sentry.io/6191481"
CRASHES_DIR = '/data/community/crashes/'
ret = car.CarParams.new_message()
candidate = ret.carFingerprint
params = Params()
#uniqueID = op_params.get('uniqueID')
try:
dongle_id = params.get("DongleId").decode('utf8')
except AttributeError:
dongle_id = "None"
try:
gitname = params.get("GithubUsername", encoding='utf-8')
except Exception:
gitname = ""
error_tags = {
'dirty': is_dirty(),
'dongle_id': dongle_id,
'branch': get_branch(),
'remote': get_origin(),
'fingerprintedAs': candidate,
'gitname': gitname
}
ip = "{{auto}}"
def report_tombstone(fn: str, message: str, contents: str) -> None:
cloudlog.error({'tombstone': message})
with sentry_sdk.configure_scope() as scope:
scope.set_extra("tombstone_fn", fn)
scope.set_extra("tombstone", contents)
sentry_sdk.capture_message(message=message)
sentry_sdk.flush()
def capture_exception(*args, **kwargs) -> None:
save_exception(traceback.format_exc())
cloudlog.error("crash", exc_info=kwargs.get('exc_info', 1))
bind_user(id=dongle_id, ip_address=ip, username=gitname)
try:
sentry_sdk.capture_exception(*args, **kwargs)
sentry_sdk.flush() # https://github.com/getsentry/sentry-python/issues/291
except Exception:
cloudlog.exception("sentry exception")
def save_exception(exc_text):
if not os.path.exists(CRASHES_DIR):
os.makedirs(CRASHES_DIR)
files = [
os.path.join(CRASHES_DIR, datetime.now().strftime('%Y-%m-%d--%H-%M-%S.log')),
os.path.join(CRASHES_DIR, 'error.txt')
]
for file in files:
with open(file, 'w') as f:
f.write(exc_text)
print('Logged current crash to {}'.format(files))
def bind_user(**kwargs) -> None:
sentry_sdk.set_user(kwargs)
sentry_sdk.flush()
def capture_warning(warning_string):
bind_user(id=dongle_id, ip_address=ip, name=gitname)
sentry_sdk.capture_message(warning_string, level='warning')
sentry_sdk.flush()
def capture_info(info_string):
bind_user(id=dongle_id, ip_address=ip, name=gitname)
sentry_sdk.capture_message(info_string, level='info')
sentry_sdk.flush()
def set_tag(key: str, value: str) -> None:
sentry_sdk.set_tag(key, value)
sentry_sdk.flush()
def init(project: SentryProject) -> None:
# forks like to mess with this, so double check
#comma_remote = is_comma_remote() and "commaai" in get_origin(default="")
#if not comma_remote or not is_registered_device() or PC:
# return
#env = "release" if is_tested_branch() else "master"
env = get_branch_type()
dongle_id = Params().get("DongleId", encoding='utf-8')
ip = "{{auto}}"
gitname = Params().get("GithubUsername", encoding='utf-8')
integrations = []
if project == SentryProject.SELFDRIVE:
integrations.append(ThreadingIntegration(propagate_hub=True))
else:
sentry_sdk.utils.MAX_STRING_LENGTH = 8192
sentry_sdk.init(project.value,
default_integrations=False,
release=get_version(),
integrations=integrations,
traces_sample_rate=1.0,
environment=env,
send_default_pii=True)
sentry_sdk.set_user({"id": dongle_id})
sentry_sdk.set_user({"ip_address": ip})
sentry_sdk.set_user({"username": gitname})
sentry_sdk.set_tag("dirty", is_dirty())
sentry_sdk.set_tag("origin", get_origin())
sentry_sdk.set_tag("branch", get_branch())
sentry_sdk.set_tag("commit", get_commit())
sentry_sdk.set_tag("device", HARDWARE.get_device_type())
if project == SentryProject.SELFDRIVE:
sentry_sdk.Hub.current.start_session()