serializers.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import logging
  2. from django.conf import settings
  3. from rest_framework import serializers
  4. from events.serializers import SentrySDKEventSerializer
  5. from glitchtip.serializers import FlexibleDateTimeField
  6. from .models import Span, TransactionEvent, TransactionGroup
  7. logger = logging.getLogger(__name__)
  8. class TransactionGroupSerializer(serializers.ModelSerializer):
  9. avgDuration = serializers.DurationField(source="avg_duration", read_only=True)
  10. transactionCount = serializers.IntegerField(
  11. source="transaction_count", read_only=True
  12. )
  13. class Meta:
  14. model = TransactionGroup
  15. fields = [
  16. "id",
  17. "transaction",
  18. "project",
  19. "op",
  20. "method",
  21. "avgDuration",
  22. "transactionCount",
  23. ]
  24. class SpanSerializer(serializers.ModelSerializer):
  25. spanId = serializers.CharField(source="span_id", read_only=True)
  26. parentSpanId = serializers.CharField(source="parent_span_id", read_only=True)
  27. startTimestamp = serializers.DateTimeField(source="start_timestamp", read_only=True)
  28. start_timestamp = FlexibleDateTimeField(write_only=True)
  29. timestamp = FlexibleDateTimeField(write_only=True)
  30. description = serializers.CharField()
  31. class Meta:
  32. model = Span
  33. fields = [
  34. "spanId",
  35. "span_id",
  36. "parent_span_id",
  37. "parentSpanId",
  38. "op",
  39. "description",
  40. "startTimestamp",
  41. "start_timestamp",
  42. "timestamp",
  43. "tags",
  44. "data",
  45. ]
  46. extra_kwargs = {
  47. "start_timestamp": {"write_only": True},
  48. "span_id": {"write_only": True},
  49. "parent_span_id": {"write_only": True},
  50. }
  51. def to_internal_value(self, data):
  52. # Coerce tags to strings
  53. # Must be done here to avoid failing child CharField validation
  54. if tags := data.get("tags"):
  55. data["tags"] = {key: str(value) for key, value in tags.items()}
  56. return super().to_internal_value(data)
  57. def validate_description(self, value):
  58. # No documented max length here, so we truncate
  59. max_length = self.Meta.model._meta.get_field("description").max_length
  60. if value and len(value) > max_length:
  61. logger.warning("Span description truncation %s", value)
  62. return value[:max_length]
  63. return value
  64. class TransactionEventSerializer(SentrySDKEventSerializer):
  65. type = serializers.CharField(required=False)
  66. contexts = serializers.JSONField()
  67. measurements = serializers.JSONField(required=False)
  68. spans = serializers.ListField(
  69. child=SpanSerializer(), required=False, allow_empty=True
  70. )
  71. start_timestamp = FlexibleDateTimeField()
  72. timestamp = FlexibleDateTimeField()
  73. transaction = serializers.CharField()
  74. def create(self, validated_data):
  75. data = validated_data
  76. contexts = data["contexts"]
  77. project = self.context.get("project")
  78. trace_id = contexts["trace"]["trace_id"]
  79. tags = []
  80. if environment := data.get("environment"):
  81. environment = self.get_environment(data["environment"], project)
  82. tags.append(("environment", environment.name))
  83. if release := data.get("release"):
  84. release = self.get_release(release, project)
  85. tags.append(("release", release.version))
  86. defaults = {}
  87. defaults["tags"] = {tag[0]: [tag[1]] for tag in tags}
  88. group, group_created = TransactionGroup.objects.get_or_create(
  89. project=self.context.get("project"),
  90. transaction=data["transaction"],
  91. op=contexts["trace"]["op"],
  92. method=data.get("request", {}).get("method"),
  93. defaults=defaults,
  94. )
  95. # Merge tags, only save if necessary
  96. update_group = False
  97. if not group_created:
  98. for tag in tags:
  99. if tag[0] not in group.tags:
  100. group.tags[tag[0]] = tag[1]
  101. update_group = True
  102. elif tag[1] not in group.tags[tag[0]]:
  103. group.tags[tag[0]].append(tag[1])
  104. update_group = True
  105. if update_group:
  106. group.save(update_fields=["tags"])
  107. transaction = TransactionEvent.objects.create(
  108. group=group,
  109. data={
  110. "request": data.get("request"),
  111. "sdk": data.get("sdk"),
  112. "platform": data.get("platform"),
  113. },
  114. trace_id=trace_id,
  115. event_id=data["event_id"],
  116. timestamp=data["timestamp"],
  117. start_timestamp=data["start_timestamp"],
  118. duration=data["timestamp"] - data["start_timestamp"],
  119. tags={tag[0]: tag[1] for tag in tags},
  120. )
  121. first_span = SpanSerializer(
  122. data=contexts["trace"]
  123. | {
  124. "start_timestamp": data["start_timestamp"],
  125. "timestamp": data["timestamp"],
  126. }
  127. )
  128. if settings.ENABLE_PERFORMANCE_SPANS:
  129. is_valid = first_span.is_valid()
  130. if is_valid:
  131. spans = data.get("spans", []) + [first_span.validated_data]
  132. else:
  133. spans = data.get("spans")
  134. if spans:
  135. Span.objects.bulk_create(
  136. [
  137. Span(
  138. transaction=transaction,
  139. **span,
  140. )
  141. for span in spans
  142. ]
  143. )
  144. return transaction
  145. class TransactionSerializer(serializers.ModelSerializer):
  146. eventId = serializers.UUIDField(source="pk")
  147. startTimestamp = serializers.DateTimeField(source="start_timestamp")
  148. transaction = serializers.SerializerMethodField()
  149. op = serializers.SerializerMethodField()
  150. method = serializers.SerializerMethodField()
  151. class Meta:
  152. model = TransactionEvent
  153. fields = (
  154. "eventId",
  155. "timestamp",
  156. "startTimestamp",
  157. "transaction",
  158. "op",
  159. "method",
  160. )
  161. def get_transaction(self, obj):
  162. return obj.group.transaction
  163. def get_op(self, obj):
  164. return obj.group.op
  165. def get_method(self, obj):
  166. return obj.group.transaction
  167. class TransactionDetailSerializer(TransactionSerializer):
  168. spans = SpanSerializer(source="span_set", many=True)
  169. class Meta(TransactionSerializer.Meta):
  170. fields = TransactionSerializer.Meta.fields + ("spans",)