api.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import uuid
  2. import sentry_sdk
  3. from sentry_sdk.utils import logger
  4. from typing import TYPE_CHECKING
  5. if TYPE_CHECKING:
  6. from typing import Optional
  7. from sentry_sdk._types import Event, MonitorConfig
  8. def _create_check_in_event(
  9. monitor_slug=None, # type: Optional[str]
  10. check_in_id=None, # type: Optional[str]
  11. status=None, # type: Optional[str]
  12. duration_s=None, # type: Optional[float]
  13. monitor_config=None, # type: Optional[MonitorConfig]
  14. ):
  15. # type: (...) -> Event
  16. options = sentry_sdk.get_client().options
  17. check_in_id = check_in_id or uuid.uuid4().hex # type: str
  18. check_in = {
  19. "type": "check_in",
  20. "monitor_slug": monitor_slug,
  21. "check_in_id": check_in_id,
  22. "status": status,
  23. "duration": duration_s,
  24. "environment": options.get("environment", None),
  25. "release": options.get("release", None),
  26. } # type: Event
  27. if monitor_config:
  28. check_in["monitor_config"] = monitor_config
  29. return check_in
  30. def capture_checkin(
  31. monitor_slug=None, # type: Optional[str]
  32. check_in_id=None, # type: Optional[str]
  33. status=None, # type: Optional[str]
  34. duration=None, # type: Optional[float]
  35. monitor_config=None, # type: Optional[MonitorConfig]
  36. ):
  37. # type: (...) -> str
  38. check_in_event = _create_check_in_event(
  39. monitor_slug=monitor_slug,
  40. check_in_id=check_in_id,
  41. status=status,
  42. duration_s=duration,
  43. monitor_config=monitor_config,
  44. )
  45. sentry_sdk.capture_event(check_in_event)
  46. logger.debug(
  47. f"[Crons] Captured check-in ({check_in_event.get('check_in_id')}): {check_in_event.get('monitor_slug')} -> {check_in_event.get('status')}"
  48. )
  49. return check_in_event["check_in_id"]