sentry.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """Install exception handler for process crash."""
  2. import sentry_sdk
  3. from enum import Enum
  4. from sentry_sdk.integrations.threading import ThreadingIntegration
  5. from openpilot.common.params import Params
  6. from openpilot.selfdrive.athena.registration import is_registered_device
  7. from openpilot.system.hardware import HARDWARE, PC
  8. from openpilot.common.swaglog import cloudlog
  9. from openpilot.system.version import get_branch, get_commit, get_origin, get_version, \
  10. is_comma_remote, is_dirty, is_tested_branch
  11. class SentryProject(Enum):
  12. # python project
  13. SELFDRIVE = "https://6f3c7076c1e14b2aa10f5dde6dda0cc4@o33823.ingest.sentry.io/77924"
  14. # native project
  15. SELFDRIVE_NATIVE = "https://3e4b586ed21a4479ad5d85083b639bc6@o33823.ingest.sentry.io/157615"
  16. def report_tombstone(fn: str, message: str, contents: str) -> None:
  17. cloudlog.error({'tombstone': message})
  18. with sentry_sdk.configure_scope() as scope:
  19. scope.set_extra("tombstone_fn", fn)
  20. scope.set_extra("tombstone", contents)
  21. sentry_sdk.capture_message(message=message)
  22. sentry_sdk.flush()
  23. def capture_exception(*args, **kwargs) -> None:
  24. cloudlog.error("crash", exc_info=kwargs.get('exc_info', 1))
  25. try:
  26. sentry_sdk.capture_exception(*args, **kwargs)
  27. sentry_sdk.flush() # https://github.com/getsentry/sentry-python/issues/291
  28. except Exception:
  29. cloudlog.exception("sentry exception")
  30. def set_tag(key: str, value: str) -> None:
  31. sentry_sdk.set_tag(key, value)
  32. def init(project: SentryProject) -> bool:
  33. # forks like to mess with this, so double check
  34. comma_remote = is_comma_remote() and "commaai" in get_origin()
  35. if not comma_remote or not is_registered_device() or PC:
  36. return False
  37. env = "release" if is_tested_branch() else "master"
  38. dongle_id = Params().get("DongleId", encoding='utf-8')
  39. integrations = []
  40. if project == SentryProject.SELFDRIVE:
  41. integrations.append(ThreadingIntegration(propagate_hub=True))
  42. sentry_sdk.init(project.value,
  43. default_integrations=False,
  44. release=get_version(),
  45. integrations=integrations,
  46. traces_sample_rate=1.0,
  47. max_value_length=8192,
  48. environment=env)
  49. sentry_sdk.set_user({"id": dongle_id})
  50. sentry_sdk.set_tag("dirty", is_dirty())
  51. sentry_sdk.set_tag("origin", get_origin())
  52. sentry_sdk.set_tag("branch", get_branch())
  53. sentry_sdk.set_tag("commit", get_commit())
  54. sentry_sdk.set_tag("device", HARDWARE.get_device_type())
  55. if project == SentryProject.SELFDRIVE:
  56. sentry_sdk.Hub.current.start_session()
  57. return True