test_group.py 9.2 KB

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