serializers.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from rest_framework import serializers
  2. from events.serializers import SentrySDKEventSerializer
  3. from glitchtip.serializers import FlexibleDateTimeField
  4. from .models import TransactionEvent
  5. class SpanSerializer(serializers.Serializer):
  6. data = serializers.JSONField(required=False)
  7. description = serializers.CharField(required=False)
  8. op = serializers.CharField(required=False)
  9. parent_span_id = serializers.CharField(required=False)
  10. span_id = serializers.CharField(required=False)
  11. start_timestamp = FlexibleDateTimeField()
  12. status = serializers.CharField(required=False)
  13. tags = serializers.JSONField(required=False)
  14. timestamp = FlexibleDateTimeField()
  15. trace_id = serializers.UUIDField()
  16. same_process_as_parent = serializers.BooleanField(required=False)
  17. class TransactionEventSerializer(SentrySDKEventSerializer):
  18. type = serializers.CharField()
  19. contexts = serializers.JSONField()
  20. measurements = serializers.JSONField(required=False)
  21. spans = serializers.ListField(
  22. child=SpanSerializer(), required=False, allow_empty=True
  23. )
  24. start_timestamp = FlexibleDateTimeField()
  25. timestamp = FlexibleDateTimeField()
  26. transaction = serializers.CharField()
  27. def create(self, data):
  28. project = self.context.get("project")
  29. return TransactionEvent.objects.create(
  30. data={},
  31. transaction=data["transaction"],
  32. event_id=data["event_id"],
  33. timestamp=data["timestamp"],
  34. start_timestamp=data["start_timestamp"],
  35. project=project,
  36. )
  37. class TransactionSerializer(serializers.ModelSerializer):
  38. eventId = serializers.UUIDField(source="pk")
  39. startTimestamp = serializers.DateTimeField(source="start_timestamp")
  40. class Meta:
  41. model = TransactionEvent
  42. fields = ("eventId", "transaction", "timestamp", "startTimestamp")