schema.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 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(
  80. max_length=10, default=""
  81. ) # When None, attempt to generate it
  82. message: Optional[str] = None
  83. params: Optional[list[str]] = None
  84. class EventTemplate(Schema):
  85. lineno: int
  86. abs_path: Optional[str] = None
  87. filename: str
  88. context_line: str
  89. pre_context: Optional[list[str]] = None
  90. post_context: Optional[list[str]] = None
  91. Level = Literal["fatal", "error", "warning", "info", "debug"]
  92. class EventBreadcrumb(Schema):
  93. type: Optional[str] = None
  94. category: Optional[str] = None
  95. message: Optional[str] = None
  96. data: Optional[dict[str, Any]] = None
  97. level: Optional[Level] = None
  98. timestamp: Optional[datetime] = None
  99. class ValueEventBreadcrumb(Schema):
  100. values: list[EventBreadcrumb]
  101. class BaseEventIngestSchema(Schema):
  102. timestamp: datetime = Field(default_factory=now)
  103. platform: Optional[str] = None
  104. level: Optional[str] = "error"
  105. logger: Optional[str] = None
  106. transaction: Optional[str] = None
  107. server_name: Optional[str] = None
  108. release: Optional[str] = None
  109. dist: Optional[str] = None
  110. tags: Optional[Union[dict[str, str], list[TagKeyValue]]] = None
  111. environment: Optional[str] = None
  112. modules: Optional[dict[str, str]] = None
  113. extra: Optional[Any] = None
  114. fingerprint: Optional[list[str]] = None
  115. errors: Optional[list[Any]] = None
  116. exception: Optional[Union[list[EventException], ValueEventException]] = None
  117. message: Optional[EventMessage] = None
  118. template: Optional[EventTemplate] = None
  119. breadcrumbs: Optional[Union[list[EventBreadcrumb], ValueEventBreadcrumb]] = None
  120. class EnvelopeEventIngestSchema(BaseEventIngestSchema):
  121. type: str
  122. class EventIngestSchema(BaseEventIngestSchema):
  123. event_id: uuid.UUID
  124. class EnvelopeHeaderSchema(Schema):
  125. event_id: uuid.UUID
  126. dsn: Optional[str] = None
  127. sdk: Optional[Any] = None
  128. sent_at: datetime = Field(default_factory=now)
  129. class ItemHeaderSchema(Schema):
  130. content_type: Optional[str]
  131. type: Literal["transaction", "event"]
  132. length: Optional[int]
  133. EnvelopeSchema = list[
  134. Union[EnvelopeHeaderSchema, ItemHeaderSchema, EnvelopeEventIngestSchema]
  135. ]