serializers.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. from rest_framework import serializers
  2. from events.serializers import SentrySDKEventSerializer
  3. from glitchtip.serializers import FlexibleDateTimeField
  4. from .models import TransactionEvent, TransactionGroup, Span
  5. class TransactionGroupSerializer(serializers.ModelSerializer):
  6. avgDuration = serializers.DurationField(source="avg_duration", read_only=True)
  7. transactionCount = serializers.IntegerField(
  8. source="transaction_count", read_only=True
  9. )
  10. class Meta:
  11. model = TransactionGroup
  12. fields = [
  13. "transaction",
  14. "project",
  15. "op",
  16. "method",
  17. "avgDuration",
  18. "transactionCount",
  19. ]
  20. class SpanSerializer(serializers.ModelSerializer):
  21. spanId = serializers.CharField(source="span_id", read_only=True)
  22. parentSpanId = serializers.CharField(source="parent_span_id", read_only=True)
  23. startTimestamp = serializers.DateTimeField(source="start_timestamp", read_only=True)
  24. class Meta:
  25. model = Span
  26. fields = [
  27. "spanId",
  28. "span_id",
  29. "parent_span_id",
  30. "parentSpanId",
  31. "op",
  32. "description",
  33. "startTimestamp",
  34. "start_timestamp",
  35. "timestamp",
  36. "tags",
  37. "data",
  38. ]
  39. extra_kwargs = {
  40. "start_timestamp": {"write_only": True},
  41. "span_id": {"write_only": True},
  42. "parent_span_id": {"write_only": True},
  43. }
  44. class TransactionEventSerializer(SentrySDKEventSerializer):
  45. type = serializers.CharField()
  46. contexts = serializers.JSONField()
  47. measurements = serializers.JSONField(required=False)
  48. spans = serializers.ListField(
  49. child=SpanSerializer(), required=False, allow_empty=True
  50. )
  51. start_timestamp = FlexibleDateTimeField()
  52. timestamp = FlexibleDateTimeField()
  53. transaction = serializers.CharField()
  54. def create(self, data):
  55. trace_id = data["contexts"]["trace"]["trace_id"]
  56. group, _ = TransactionGroup.objects.get_or_create(
  57. project=self.context.get("project"),
  58. transaction=data["transaction"],
  59. op=data["contexts"]["trace"]["op"],
  60. method=data.get("request", {}).get("method"),
  61. )
  62. transaction = TransactionEvent.objects.create(
  63. group=group,
  64. data={
  65. "request": data.get("request"),
  66. "sdk": data.get("sdk"),
  67. "platform": data.get("platform"),
  68. },
  69. trace_id=trace_id,
  70. event_id=data["event_id"],
  71. timestamp=data["timestamp"],
  72. start_timestamp=data["start_timestamp"],
  73. )
  74. first_span = SpanSerializer(
  75. data=data["contexts"]["trace"]
  76. | {
  77. "start_timestamp": data["start_timestamp"],
  78. "timestamp": data["timestamp"],
  79. }
  80. )
  81. first_span.is_valid()
  82. spans = data.get("spans", []) + [first_span.validated_data]
  83. Span.objects.bulk_create(
  84. [
  85. Span(
  86. transaction=transaction,
  87. **span,
  88. )
  89. for span in spans
  90. ]
  91. )
  92. return transaction
  93. class TransactionSerializer(serializers.ModelSerializer):
  94. eventId = serializers.UUIDField(source="pk")
  95. startTimestamp = serializers.DateTimeField(source="start_timestamp")
  96. transaction = serializers.SerializerMethodField()
  97. op = serializers.SerializerMethodField()
  98. method = serializers.SerializerMethodField()
  99. class Meta:
  100. model = TransactionEvent
  101. fields = (
  102. "eventId",
  103. "timestamp",
  104. "startTimestamp",
  105. "transaction",
  106. "op",
  107. "method",
  108. )
  109. def get_transaction(self, obj):
  110. return obj.group.transaction
  111. def get_op(self, obj):
  112. return obj.group.op
  113. def get_method(self, obj):
  114. return obj.group.transaction
  115. class TransactionDetailSerializer(TransactionSerializer):
  116. spans = SpanSerializer(source="span_set", many=True)
  117. class Meta(TransactionSerializer.Meta):
  118. fields = TransactionSerializer.Meta.fields + ("spans",)