api.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from asgiref.sync import sync_to_async
  2. from django.conf import settings
  3. from django.http import HttpRequest, HttpResponse
  4. from ninja import Router, Schema
  5. from .authentication import event_auth
  6. from .schema import (
  7. CSPIssueEventSchema,
  8. EnvelopeSchema,
  9. ErrorIssueEventSchema,
  10. EventIngestSchema,
  11. IngestIssueEvent,
  12. InterchangeIssueEvent,
  13. IssueEventSchema,
  14. SecuritySchema,
  15. )
  16. from .tasks import ingest_event
  17. router = Router(auth=event_auth)
  18. class EventIngestOut(Schema):
  19. event_id: str
  20. class EnvelopeIngestOut(Schema):
  21. id: str
  22. async def async_call_celery_task(task, *args):
  23. """
  24. Either dispatch the real celery task or run it with sync_to_async
  25. This can be used for testing or a celery-less operation.
  26. """
  27. if settings.CELERY_TASK_ALWAYS_EAGER:
  28. return await sync_to_async(task.delay)(*args)
  29. else:
  30. return task.delay(*args)
  31. def get_issue_event_class(event: IngestIssueEvent):
  32. return ErrorIssueEventSchema if event.exception else IssueEventSchema
  33. @router.post("/{project_id}/store/", response=EventIngestOut)
  34. async def event_store(
  35. request: HttpRequest,
  36. payload: EventIngestSchema,
  37. project_id: int,
  38. ):
  39. """
  40. Event store is the original event ingest API from OSS Sentry but is used less often
  41. Unlike Envelope, it accepts only one Issue event.
  42. """
  43. issue_event_class = get_issue_event_class(payload)
  44. issue_event = InterchangeIssueEvent(
  45. event_id=payload.event_id,
  46. project_id=project_id,
  47. payload=issue_event_class(**payload.dict()),
  48. )
  49. await async_call_celery_task(ingest_event, issue_event.dict())
  50. return {"event_id": payload.event_id.hex}
  51. @router.post("/{project_id}/envelope/", response=EnvelopeIngestOut)
  52. async def event_envelope(
  53. request: HttpRequest,
  54. payload: EnvelopeSchema,
  55. project_id: int,
  56. ):
  57. """
  58. Envelopes can contain various types of data.
  59. GlitchTip supports issue events and transaction events.
  60. Ignore other data types.
  61. Do support multiple valid events
  62. Make as few io calls as possible. Some language SDKs (PHP) cannot run async code
  63. and will block while waiting for GlitchTip to respond.
  64. """
  65. header = payload._header
  66. for item_header, item in payload._items:
  67. if item_header.type == "event":
  68. issue_event_class = get_issue_event_class(item)
  69. issue_event = InterchangeIssueEvent(
  70. event_id=header.event_id,
  71. project_id=project_id,
  72. payload=issue_event_class(**item.dict()),
  73. )
  74. await async_call_celery_task(ingest_event, issue_event.dict())
  75. elif item_header.type == "transaction":
  76. pass
  77. # ingest_transaction.delay(project_id, {})
  78. return {"id": header.event_id.hex}
  79. @router.post("/{project_id}/security/")
  80. async def event_security(
  81. request: HttpRequest,
  82. payload: SecuritySchema,
  83. project_id: int,
  84. ):
  85. """
  86. Accept Security (and someday other) issue events.
  87. Reformats event to make CSP browser format match more standard
  88. event format.
  89. """
  90. event = CSPIssueEventSchema(csp=payload.csp_report.dict(by_alias=True))
  91. issue_event = InterchangeIssueEvent(
  92. project_id=project_id,
  93. payload=event.dict(by_alias=True),
  94. )
  95. await async_call_celery_task(ingest_event, issue_event.dict(by_alias=True))
  96. return HttpResponse(status=201)