data_models.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import uuid
  2. from collections.abc import Callable
  3. from datetime import datetime
  4. from typing import Annotated, Any, Literal, Optional, Union
  5. from django.utils.timezone import now
  6. from ninja import Field, Schema
  7. from pydantic import Json, ValidationError, WrapValidator
  8. def invalid_to_none(v: Any, handler: Callable[[Any], Any]) -> Any:
  9. try:
  10. return handler(v)
  11. except ValidationError:
  12. return None
  13. class TagKeyValue(Schema):
  14. key: str
  15. value: str
  16. class Signal(Schema):
  17. number: int
  18. code: Optional[int]
  19. name: Optional[str]
  20. code_name: Optional[str]
  21. class MachException(Schema):
  22. number: int
  23. code: int
  24. subcode: int
  25. name: Optional[str]
  26. class NSError(Schema):
  27. code: int
  28. domain: str
  29. class Errno(Schema):
  30. number: int
  31. name: Optional[str]
  32. class MechanismMeta(Schema):
  33. signal: Optional[Signal] = None
  34. match_exception: Optional[MachException] = None
  35. ns_error: Optional[NSError] = None
  36. errno: Optional[Errno] = None
  37. class ExceptionMechanism(Schema):
  38. type: str
  39. description: Optional[str] = None
  40. help_link: Optional[str] = None
  41. handled: Optional[bool] = None
  42. synthetic: Optional[bool] = None
  43. meta: Optional[dict] = None
  44. data: Optional[dict] = None
  45. class StackTraceFrame(Schema):
  46. filename: Optional[str] = None
  47. function: Optional[str] = None
  48. raw_function: Optional[str] = None
  49. module: Optional[str] = None
  50. lineno: Optional[int] = None
  51. colno: Optional[int] = None
  52. abs_path: Optional[str] = None
  53. context_line: Optional[str] = None
  54. pre_context: Optional[list[str]] = None
  55. post_context: Optional[list[str]] = None
  56. source_link: Optional[str] = None
  57. in_app: Optional[bool] = None
  58. stack_start: Optional[bool] = None
  59. vars: Optional[dict[str, str]] = None
  60. instruction_addr: Optional[str] = None
  61. addr_mode: Optional[str] = None
  62. symbol_addr: Optional[str] = None
  63. image_addr: Optional[str] = None
  64. package: Optional[str] = None
  65. platform: Optional[str] = None
  66. class StackTrace(Schema):
  67. frames: list[StackTraceFrame]
  68. registers: Optional[dict[str, str]] = None
  69. class EventException(Schema):
  70. type: str
  71. value: Annotated[Optional[str], WrapValidator(invalid_to_none)]
  72. module: Optional[str] = None
  73. thread_id: Optional[str] = None
  74. mechanism: Optional[ExceptionMechanism] = None
  75. stacktrace: Optional[StackTrace] = None
  76. class ValueEventException(Schema):
  77. values: list[EventException]
  78. class EventMessage(Schema):
  79. formatted: str = Field(max_length=10) # When None, attempt to generate it
  80. message: Optional[str] = None
  81. params: Optional[list[str]] = None
  82. class EventTemplate(Schema):
  83. lineno: int
  84. abs_path: Optional[str] = None
  85. filename: str
  86. context_line: str
  87. pre_context: Optional[list[str]] = None
  88. post_context: Optional[list[str]] = None
  89. Level = Literal["fatal", "error", "warning", "info", "debug"]
  90. class EventBreadcrumb(Schema):
  91. type: Optional[str] = None
  92. category: Optional[str] = None
  93. message: Optional[str] = None
  94. data: Optional[dict[str, Any]] = None
  95. level: Optional[Level] = None
  96. timestamp: Optional[datetime] = None
  97. class ValueEventBreadcrumb(Schema):
  98. values: list[EventBreadcrumb]
  99. class EventIngestSchema(Schema):
  100. event_id: uuid.UUID
  101. timestamp: datetime = Field(default_factory=now)
  102. platform: Optional[str] = None
  103. level: Optional[str] = "error"
  104. logger: Optional[str] = None
  105. transaction: Optional[str] = None
  106. server_name: Optional[str] = None
  107. release: Optional[str] = None
  108. dist: Optional[str] = None
  109. tags: Optional[Union[dict[str, str], list[TagKeyValue]]] = None
  110. environment: Optional[str] = None
  111. modules: Optional[dict[str, str]] = None
  112. extra: Optional[Json] = None
  113. fingerprint: Optional[list[str]] = None
  114. errors: Optional[list[Json]] = None
  115. exception: Optional[Union[list[EventException], ValueEventException]] = None
  116. message: Optional[EventMessage] = None
  117. template: Optional[EventTemplate] = None
  118. breadcrumbs: Optional[Union[list[EventBreadcrumb], ValueEventBreadcrumb]] = None
  119. class EnvelopeHeaderSchema(Schema):
  120. event_id: str
  121. dsn: Optional[str]
  122. sdk: Optional[Any]
  123. class ItemHeaderSchema(Schema):
  124. content_type: Optional[str]
  125. type: str
  126. length: Optional[int]
  127. # EnvelopeSchema = list[Any]
  128. EnvelopeSchema = list[Union[EnvelopeHeaderSchema, ItemHeaderSchema, EventIngestSchema]]