Sentry logging

Logging for my Sentry server that tracks the values of the serial number, car fingerprint, user set parameters, and the date and time for when FrogPilot was installed and last updated for debugging and support.
This commit is contained in:
FrogAi
2024-02-27 16:34:47 -07:00
parent bb61ac62a7
commit 6427b1380e
5 changed files with 137 additions and 17 deletions

View File

@@ -1,21 +1,24 @@
"""Install exception handler for process crash."""
import os
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.selfdrive.athena.registration import is_registered_device
from openpilot.system.hardware import HARDWARE, PC
from openpilot.common.swaglog import cloudlog
from openpilot.system.version import get_branch, get_commit, get_origin, get_version, \
is_comma_remote, is_dirty, is_tested_branch
from openpilot.system.version import get_branch, get_commit, get_origin, get_short_branch, get_version, is_tested_branch
CRASHES_DIR = '/data/community/crashes/'
class SentryProject(Enum):
# python project
SELFDRIVE = "https://6f3c7076c1e14b2aa10f5dde6dda0cc4@o33823.ingest.sentry.io/77924"
SELFDRIVE = "https://5ad1714d27324c74a30f9c538bff3b8d@o4505034923769856.ingest.sentry.io/4505034930651136"
# native project
SELFDRIVE_NATIVE = "https://3e4b586ed21a4479ad5d85083b639bc6@o33823.ingest.sentry.io/157615"
SELFDRIVE_NATIVE = "https://5ad1714d27324c74a30f9c538bff3b8d@o4505034923769856.ingest.sentry.io/4505034930651136"
def report_tombstone(fn: str, message: str, contents: str) -> None:
@@ -29,6 +32,7 @@ def report_tombstone(fn: str, message: str, contents: str) -> None:
def capture_exception(*args, **kwargs) -> None:
save_exception(traceback.format_exc())
cloudlog.error("crash", exc_info=kwargs.get('exc_info', 1))
try:
@@ -38,18 +42,57 @@ def capture_exception(*args, **kwargs) -> None:
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)
def bind_user(**kwargs) -> None:
sentry_sdk.set_user(kwargs)
sentry_sdk.flush()
def capture_warning(warning_string, serial_id):
with sentry_sdk.configure_scope() as scope:
scope.fingerprint = [warning_string, serial_id]
bind_user(id=serial_id)
sentry_sdk.capture_message(warning_string, level='info')
sentry_sdk.flush()
def set_tag(key: str, value: str) -> None:
sentry_sdk.set_tag(key, value)
def init(project: SentryProject) -> bool:
# forks like to mess with this, so double check
comma_remote = is_comma_remote() and "commaai" in get_origin()
if not comma_remote or not is_registered_device() or PC:
frogpilot = "frogai" in get_origin().lower()
if not frogpilot or PC:
return False
env = "release" if is_tested_branch() else "master"
dongle_id = Params().get("DongleId", encoding='utf-8')
short_branch = get_short_branch()
if short_branch == "FrogPilot-Development":
env = "Development"
elif short_branch in {"FrogPilot-Staging", "FrogPilot-Testing"}:
env = "Staging"
elif short_branch == "FrogPilot":
env = "Release"
else:
env = short_branch
params = Params()
installed = params.get("InstallDate", encoding='utf-8')
updated = params.get("Updated", encoding='utf-8')
integrations = []
if project == SentryProject.SELFDRIVE:
@@ -61,14 +104,14 @@ def init(project: SentryProject) -> bool:
integrations=integrations,
traces_sample_rate=1.0,
max_value_length=8192,
environment=env)
environment=env,
send_default_pii=True)
sentry_sdk.set_user({"id": dongle_id})
sentry_sdk.set_tag("dirty", is_dirty())
sentry_sdk.set_tag("origin", get_origin())
sentry_sdk.set_user({"id": HARDWARE.get_serial()})
sentry_sdk.set_tag("branch", get_branch())
sentry_sdk.set_tag("commit", get_commit())
sentry_sdk.set_tag("device", HARDWARE.get_device_type())
sentry_sdk.set_tag("updated", updated)
sentry_sdk.set_tag("installed", installed)
if project == SentryProject.SELFDRIVE:
sentry_sdk.Hub.current.start_session()