serializers.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.IntegerField(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(required=False)
  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. release = self.set_release(data.get("release"), project)
  81. if project.release_id:
  82. tags.append(("release", release))
  83. environment = self.set_environment(data.get("environment"), project)
  84. if project.environment_id:
  85. tags.append(("environment", environment))
  86. if data.get("tags"):
  87. tags += [(k, v) for k, v in data["tags"].items()]
  88. defaults = {}
  89. defaults["tags"] = {tag[0]: [tag[1]] for tag in tags}
  90. group, group_created = TransactionGroup.objects.get_or_create(
  91. project=self.context.get("project"),
  92. transaction=data["transaction"],
  93. op=contexts["trace"].get("op", ""),
  94. method=data.get("request", {}).get("method"),
  95. defaults=defaults,
  96. )
  97. # Merge tags, only save if necessary
  98. update_group = False
  99. if not group_created:
  100. for tag in tags:
  101. if tag[0] not in group.tags:
  102. new_tag_value = tag[1]
  103. # Coerce to List[str]
  104. if isinstance(new_tag_value, str):
  105. new_tag_value = [new_tag_value]
  106. group.tags[tag[0]] = new_tag_value
  107. update_group = True
  108. elif tag[1] not in group.tags[tag[0]]:
  109. group.tags[tag[0]].append(tag[1])
  110. update_group = True
  111. if update_group:
  112. group.save(update_fields=["tags"])
  113. transaction = TransactionEvent.objects.create(
  114. group=group,
  115. data={
  116. "request": data.get("request"),
  117. "sdk": data.get("sdk"),
  118. "platform": data.get("platform"),
  119. },
  120. trace_id=trace_id,
  121. event_id=data["event_id"],
  122. timestamp=data["timestamp"],
  123. start_timestamp=data["start_timestamp"],
  124. duration=(data["timestamp"] - data["start_timestamp"]).microseconds / 1000,
  125. tags={tag[0]: tag[1] for tag in tags},
  126. )
  127. first_span = SpanSerializer(
  128. data=contexts["trace"]
  129. | {
  130. "start_timestamp": data["start_timestamp"],
  131. "timestamp": data["timestamp"],
  132. }
  133. )
  134. if settings.ENABLE_PERFORMANCE_SPANS:
  135. is_valid = first_span.is_valid()
  136. if is_valid:
  137. spans = data.get("spans", []) + [first_span.validated_data]
  138. else:
  139. spans = data.get("spans")
  140. if spans:
  141. Span.objects.bulk_create(
  142. [
  143. Span(
  144. transaction=transaction,
  145. **span,
  146. )
  147. for span in spans
  148. ]
  149. )
  150. return transaction
  151. class TransactionSerializer(serializers.ModelSerializer):
  152. eventId = serializers.UUIDField(source="pk")
  153. startTimestamp = serializers.DateTimeField(source="start_timestamp")
  154. transaction = serializers.SerializerMethodField()
  155. op = serializers.SerializerMethodField()
  156. method = serializers.SerializerMethodField()
  157. class Meta:
  158. model = TransactionEvent
  159. fields = (
  160. "eventId",
  161. "timestamp",
  162. "startTimestamp",
  163. "transaction",
  164. "op",
  165. "method",
  166. )
  167. def get_transaction(self, obj):
  168. return obj.group.transaction
  169. def get_op(self, obj):
  170. return obj.group.op
  171. def get_method(self, obj):
  172. return obj.group.transaction
  173. class TransactionDetailSerializer(TransactionSerializer):
  174. spans = SpanSerializer(source="span_set", many=True)
  175. class Meta(TransactionSerializer.Meta):
  176. fields = TransactionSerializer.Meta.fields + ("spans",)