Files
StarPilot/system/sentry.py
firestar5683 3d8af2361e Rename
2026-03-27 18:05:44 -05:00

144 lines
4.4 KiB
Python

import glob
import os
import re
import sentry_sdk
import traceback
from datetime import datetime
from enum import Enum
from sentry_sdk.integrations.threading import ThreadingIntegration
from openpilot.common.params import Params
from openpilot.system.hardware import HARDWARE, PC
from openpilot.common.swaglog import cloudlog
from openpilot.system.hardware.hw import Paths
from openpilot.system.version import get_build_metadata, get_version
from openpilot.starpilot.common.starpilot_variables import ERROR_LOGS_PATH, params
class SentryProject(Enum):
# python project
SELFDRIVE = "https://7305139359a548fcb348ec09497dc389@bugsink.firestar.link/1"
# native project
SELFDRIVE_NATIVE = "https://7305139359a548fcb348ec09497dc389@bugsink.firestar.link/1"
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)
# Attach qlog for debugging context
qlogs = glob.glob(f"{Paths.log_root()}/*/qlog")
if qlogs:
scope.add_attachment(path=max(qlogs, key=os.path.getmtime), filename="qlog")
sentry_sdk.capture_message(message=message)
sentry_sdk.flush()
def capture_block():
with sentry_sdk.push_scope() as scope:
sentry_sdk.capture_message("Blocked user from using the development branch", level='info')
sentry_sdk.flush()
def capture_exception(*args, crash_log=True, **kwargs) -> None:
exc_text = traceback.format_exc()
phrases_to_check = [
"already exists. To overwrite it, set 'overwrite' to True",
"failed after retry",
]
if any(phrase in exc_text for phrase in phrases_to_check):
return
save_exception(exc_text, crash_log)
cloudlog.error("crash", exc_info=kwargs.get('exc_info', 1))
try:
with sentry_sdk.push_scope() as scope:
# Attach qlog for debugging context
qlogs = glob.glob(f"{Paths.log_root()}/*/qlog")
if qlogs:
scope.add_attachment(path=max(qlogs, key=os.path.getmtime), filename="qlog")
sentry_sdk.capture_exception(*args, **kwargs)
sentry_sdk.flush() # https://github.com/getsentry/sentry-python/issues/291
except Exception:
cloudlog.exception("sentry exception")
def capture_report(discord_user, report, starpilot_toggles):
error_file_path = ERROR_LOGS_PATH / "error.txt"
error_content = "No error log found."
if error_file_path.exists():
error_content = error_file_path.read_text()
with sentry_sdk.push_scope() as scope:
scope.set_context("Error Log", {"content": error_content})
scope.set_context("Toggle Values", starpilot_toggles)
sentry_sdk.capture_message(f"{discord_user} submitted report: {report}", level="fatal")
sentry_sdk.flush()
def set_tag(key: str, value: str) -> None:
sentry_sdk.set_tag(key, value)
def save_exception(exc_text: str, crash_log) -> None:
files = [
ERROR_LOGS_PATH / datetime.now().astimezone().strftime("%Y-%m-%d--%H-%M-%S.log"),
ERROR_LOGS_PATH / "error.txt"
]
for file_path in files:
if file_path.name == "error.txt" and crash_log:
lines = exc_text.splitlines()[-10:]
file_path.write_text("\n".join(lines))
else:
file_path.write_text(exc_text)
def init(project: SentryProject) -> bool:
if PC:
return False
build_metadata = get_build_metadata()
short_branch = build_metadata.channel
env = short_branch
if re.search("test", short_branch, re.IGNORECASE):
env = "Testing"
dongle_id = params.get("DongleId", encoding="utf-8")
installed = params.get("InstallDate", encoding="utf-8")
updated = params.get("Updated", encoding="utf-8")
integrations = []
if project == SentryProject.SELFDRIVE:
integrations.append(ThreadingIntegration(propagate_hub=True))
sentry_sdk.init(project.value,
default_integrations=False,
release=get_version(),
integrations=integrations,
traces_sample_rate=1.0,
max_value_length=8192,
environment=env)
sentry_sdk.set_user({"id": dongle_id})
sentry_sdk.set_tag("origin", build_metadata.openpilot.git_origin)
sentry_sdk.set_tag("branch", short_branch)
sentry_sdk.set_tag("commit", build_metadata.openpilot.git_commit)
sentry_sdk.set_tag("updated", updated)
sentry_sdk.set_tag("installed", installed)
if project == SentryProject.SELFDRIVE:
sentry_sdk.Hub.current.start_session()
return True