test_group.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import uuid
  2. from sentry.issues.grouptype import PerformanceNPlusOneGroupType, ProfileFileIOGroupType
  3. from sentry.issues.occurrence_consumer import process_event_and_issue_occurrence
  4. from sentry.models import Group
  5. from sentry.testutils.cases import PerformanceIssueTestCase, SnubaTestCase, TestCase
  6. from sentry.testutils.helpers.datetime import before_now, iso_format
  7. from sentry.testutils.silo import region_silo_test
  8. from sentry.utils.samples import load_data
  9. from tests.sentry.issues.test_utils import OccurrenceTestMixin
  10. @region_silo_test
  11. class GroupTestSnuba(TestCase, SnubaTestCase, PerformanceIssueTestCase, OccurrenceTestMixin):
  12. def test_get_oldest_latest_for_environments(self):
  13. project = self.create_project()
  14. min_ago = iso_format(before_now(minutes=1))
  15. self.store_event(
  16. data={
  17. "event_id": "a" * 32,
  18. "environment": "production",
  19. "timestamp": min_ago,
  20. "fingerprint": ["group-1"],
  21. },
  22. project_id=project.id,
  23. )
  24. self.store_event(
  25. data={
  26. "event_id": "b" * 32,
  27. "environment": "production",
  28. "timestamp": min_ago,
  29. "fingerprint": ["group-1"],
  30. },
  31. project_id=project.id,
  32. )
  33. self.store_event(
  34. data={"event_id": "c" * 32, "timestamp": min_ago, "fingerprint": ["group-1"]},
  35. project_id=project.id,
  36. )
  37. group = Group.objects.first()
  38. assert group.get_latest_event_for_environments().event_id == "c" * 32
  39. assert group.get_latest_event_for_environments(["staging"]) is None
  40. assert group.get_latest_event_for_environments(["production"]).event_id == "b" * 32
  41. assert group.get_oldest_event_for_environments().event_id == "a" * 32
  42. assert (
  43. group.get_oldest_event_for_environments(["staging", "production"]).event_id == "a" * 32
  44. )
  45. assert group.get_oldest_event_for_environments(["staging"]) is None
  46. def test_perf_issue(self):
  47. group_fingerprint = f"{PerformanceNPlusOneGroupType.type_id}-group1"
  48. event_data_1 = load_data("transaction-n-plus-one", fingerprint=[group_fingerprint])
  49. event_data_1["timestamp"] = iso_format(before_now(seconds=10))
  50. event_data_1["start_timestamp"] = iso_format(before_now(seconds=11))
  51. event_data_1["event_id"] = "d" * 32
  52. event_data_2 = load_data("transaction-n-plus-one", fingerprint=[group_fingerprint])
  53. event_data_2["timestamp"] = iso_format(before_now(seconds=20))
  54. event_data_2["start_timestamp"] = iso_format(before_now(seconds=21))
  55. event_data_2["event_id"] = "f" * 32
  56. event_data_3 = load_data("transaction-n-plus-one", fingerprint=[group_fingerprint])
  57. event_data_3["timestamp"] = iso_format(before_now(seconds=30))
  58. event_data_3["start_timestamp"] = iso_format(before_now(seconds=31))
  59. event_data_3["event_id"] = "f" * 32
  60. transaction_event_1 = self.create_performance_issue(
  61. event_data=event_data_1, fingerprint=group_fingerprint
  62. )
  63. self.create_performance_issue(event_data=event_data_2, fingerprint=group_fingerprint)
  64. self.create_performance_issue(event_data=event_data_3, fingerprint=group_fingerprint)
  65. perf_group = transaction_event_1.group
  66. assert perf_group.get_latest_event_for_environments().event_id == "d" * 32
  67. assert perf_group.get_oldest_event_for_environments().event_id == "f" * 32
  68. def test_error_issue_get_helpful_for_environments(self):
  69. project = self.create_project()
  70. min_ago = iso_format(before_now(minutes=1))
  71. replay_id = uuid.uuid4().hex
  72. event_all_helpful_params = self.store_event(
  73. data={
  74. "event_id": "a" * 32,
  75. "timestamp": min_ago,
  76. "fingerprint": ["group-1"],
  77. "contexts": {
  78. "replay": {"replay_id": replay_id},
  79. "trace": {
  80. "sampled": True,
  81. "span_id": "babaae0d4b7512d9",
  82. "trace_id": "a7d67cf796774551a95be6543cacd459",
  83. },
  84. },
  85. "errors": [],
  86. },
  87. project_id=project.id,
  88. assert_no_errors=False,
  89. )
  90. self.store_event(
  91. data={
  92. "event_id": "b" * 32,
  93. "timestamp": min_ago,
  94. "fingerprint": ["group-1"],
  95. "contexts": {
  96. "replay": {"replay_id": replay_id},
  97. },
  98. "errors": [{"type": "one"}, {"type": "two"}],
  99. },
  100. project_id=project.id,
  101. assert_no_errors=False,
  102. )
  103. event_none_helpful_params = self.store_event(
  104. data={"event_id": "c" * 32, "timestamp": min_ago, "fingerprint": ["group-1"]},
  105. project_id=project.id,
  106. )
  107. group = Group.objects.first()
  108. assert (
  109. group.get_helpful_event_for_environments().event_id == event_all_helpful_params.event_id
  110. )
  111. assert (
  112. group.get_latest_event_for_environments().event_id == event_none_helpful_params.event_id
  113. )
  114. assert (
  115. group.get_oldest_event_for_environments().event_id == event_all_helpful_params.event_id
  116. )
  117. def test_perf_issue_helpful(self):
  118. group_fingerprint = f"{PerformanceNPlusOneGroupType.type_id}-group1"
  119. transaction_event_data_with_all_helpful = load_data(
  120. "transaction-n-plus-one", fingerprint=[group_fingerprint]
  121. )
  122. transaction_event_data_with_all_helpful["timestamp"] = iso_format(before_now(seconds=10))
  123. transaction_event_data_with_all_helpful["start_timestamp"] = iso_format(
  124. before_now(seconds=11)
  125. )
  126. transaction_event_data_with_all_helpful["event_id"] = "d" * 32
  127. transaction_event_data_with_all_helpful["contexts"].update(
  128. {"profile": {"profile_id": uuid.uuid4().hex}}
  129. )
  130. transaction_event_data_with_all_helpful["contexts"].update(
  131. {"replay": {"replay_id": uuid.uuid4().hex}}
  132. )
  133. transaction_event_data_with_all_helpful["contexts"]["trace"]["sampled"] = True
  134. transaction_event_data_with_all_helpful["errors"] = []
  135. transaction_event_data_with_none_helpful = load_data(
  136. "transaction-n-plus-one", fingerprint=[group_fingerprint]
  137. )
  138. transaction_event_data_with_none_helpful["timestamp"] = iso_format(before_now(seconds=20))
  139. transaction_event_data_with_none_helpful["start_timestamp"] = iso_format(
  140. before_now(seconds=21)
  141. )
  142. transaction_event_data_with_none_helpful["event_id"] = "f" * 32
  143. transaction_event_1 = self.create_performance_issue(
  144. event_data=transaction_event_data_with_all_helpful, fingerprint=group_fingerprint
  145. )
  146. transaction_event_2 = self.create_performance_issue(
  147. event_data=transaction_event_data_with_none_helpful, fingerprint=group_fingerprint
  148. )
  149. perf_group = transaction_event_1.group
  150. assert (
  151. perf_group.get_helpful_event_for_environments().event_id == transaction_event_1.event_id
  152. )
  153. assert (
  154. perf_group.get_latest_event_for_environments().event_id == transaction_event_1.event_id
  155. )
  156. assert (
  157. perf_group.get_oldest_event_for_environments().event_id == transaction_event_2.event_id
  158. )
  159. def test_profile_issue_helpful(self):
  160. event_id_1 = uuid.uuid4().hex
  161. occurrence = process_event_and_issue_occurrence(
  162. self.build_occurrence_data(event_id=event_id_1, project_id=self.project.id),
  163. {
  164. "event_id": event_id_1,
  165. "fingerprint": ["group-1"],
  166. "project_id": self.project.id,
  167. "timestamp": before_now(minutes=2).isoformat(),
  168. "contexts": {
  169. "profile": {"profile_id": uuid.uuid4().hex},
  170. "replay": {"replay_id": uuid.uuid4().hex},
  171. "trace": {
  172. "sampled": True,
  173. "span_id": "babaae0d4b7512d9",
  174. "trace_id": "a7d67cf796774551a95be6543cacd459",
  175. },
  176. },
  177. "errors": [],
  178. },
  179. )[0]
  180. event_id_2 = uuid.uuid4().hex
  181. occurrence_2 = process_event_and_issue_occurrence(
  182. self.build_occurrence_data(event_id=event_id_2, project_id=self.project.id),
  183. {
  184. "event_id": event_id_2,
  185. "fingerprint": ["group-1"],
  186. "project_id": self.project.id,
  187. "timestamp": before_now(minutes=1).isoformat(),
  188. },
  189. )[0]
  190. group = Group.objects.first()
  191. group.update(type=ProfileFileIOGroupType.type_id)
  192. group_event = group.get_helpful_event_for_environments()
  193. assert group_event.event_id == occurrence.event_id
  194. self.assert_occurrences_identical(group_event.occurrence, occurrence)
  195. assert group.get_latest_event_for_environments().event_id == occurrence_2.event_id
  196. assert group.get_oldest_event_for_environments().event_id == occurrence.event_id