test_group_events.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. from datetime import timedelta
  2. from django.utils import timezone
  3. from freezegun import freeze_time
  4. from sentry.testutils import APITestCase, SnubaTestCase
  5. from sentry.testutils.helpers.datetime import before_now, iso_format
  6. from sentry.testutils.silo import region_silo_test
  7. from sentry.types.issues import GroupType
  8. from sentry.utils.samples import load_data
  9. @region_silo_test
  10. class GroupEventsTest(APITestCase, SnubaTestCase):
  11. def setUp(self):
  12. super().setUp()
  13. self.min_ago = before_now(minutes=1)
  14. self.features = {}
  15. def do_request(self, url):
  16. with self.feature(self.features):
  17. return self.client.get(url, format="json")
  18. def test_simple(self):
  19. self.login_as(user=self.user)
  20. event_1 = self.store_event(
  21. data={
  22. "event_id": "a" * 32,
  23. "fingerprint": ["1"],
  24. "timestamp": iso_format(self.min_ago),
  25. },
  26. project_id=self.project.id,
  27. )
  28. event_2 = self.store_event(
  29. data={
  30. "event_id": "b" * 32,
  31. "fingerprint": ["1"],
  32. "timestamp": iso_format(self.min_ago),
  33. },
  34. project_id=self.project.id,
  35. )
  36. url = f"/api/0/issues/{event_1.group.id}/events/"
  37. response = self.do_request(url)
  38. assert response.status_code == 200, response.content
  39. assert len(response.data) == 2
  40. assert sorted(map(lambda x: x["eventID"], response.data)) == sorted(
  41. [str(event_1.event_id), str(event_2.event_id)]
  42. )
  43. def test_tags(self):
  44. self.login_as(user=self.user)
  45. event_1 = self.store_event(
  46. data={
  47. "event_id": "a" * 32,
  48. "fingerprint": ["1"],
  49. "tags": {"foo": "baz", "bar": "buz"},
  50. "timestamp": iso_format(self.min_ago),
  51. },
  52. project_id=self.project.id,
  53. )
  54. event_2 = self.store_event(
  55. data={
  56. "event_id": "b" * 32,
  57. "fingerprint": ["1"],
  58. "tags": {"bar": "biz"},
  59. "timestamp": iso_format(before_now(seconds=61)),
  60. },
  61. project_id=self.project.id,
  62. )
  63. url = f"/api/0/issues/{event_1.group.id}/events/"
  64. response = self.do_request(url + "?query=foo:baz")
  65. assert response.status_code == 200, response.content
  66. assert len(response.data) == 1
  67. assert response.data[0]["eventID"] == str(event_1.event_id)
  68. response = self.do_request(url + "?query=!foo:baz")
  69. assert response.status_code == 200, response.content
  70. assert len(response.data) == 1
  71. assert response.data[0]["eventID"] == str(event_2.event_id)
  72. response = self.do_request(url + "?query=bar:biz")
  73. assert response.status_code == 200, response.content
  74. assert len(response.data) == 1
  75. assert response.data[0]["eventID"] == str(event_2.event_id)
  76. response = self.do_request(url + "?query=bar:biz%20foo:baz")
  77. assert response.status_code == 200, response.content
  78. assert len(response.data) == 0
  79. response = self.do_request(url + "?query=bar:buz%20foo:baz")
  80. assert response.status_code == 200, response.content
  81. assert len(response.data) == 1
  82. assert response.data[0]["eventID"] == str(event_1.event_id)
  83. response = self.do_request(url + "?query=bar:baz")
  84. assert response.status_code == 200, response.content
  85. assert len(response.data) == 0
  86. response = self.do_request(url + "?query=a:b")
  87. assert response.status_code == 200, response.content
  88. assert len(response.data) == 0
  89. response = self.do_request(url + "?query=bar:b")
  90. assert response.status_code == 200, response.content
  91. assert len(response.data) == 0
  92. response = self.do_request(url + "?query=bar:baz")
  93. assert response.status_code == 200, response.content
  94. assert len(response.data) == 0
  95. response = self.do_request(url + "?query=!bar:baz")
  96. assert response.status_code == 200, response.content
  97. assert len(response.data) == 2
  98. assert {e["eventID"] for e in response.data} == {event_1.event_id, event_2.event_id}
  99. def test_search_event_by_id(self):
  100. self.login_as(user=self.user)
  101. event_1 = self.store_event(
  102. data={
  103. "event_id": "a" * 32,
  104. "fingerprint": ["group-1"],
  105. "timestamp": iso_format(self.min_ago),
  106. },
  107. project_id=self.project.id,
  108. )
  109. self.store_event(
  110. data={
  111. "event_id": "b" * 32,
  112. "fingerprint": ["group-1"],
  113. "timestamp": iso_format(self.min_ago),
  114. },
  115. project_id=self.project.id,
  116. )
  117. url = f"/api/0/issues/{event_1.group.id}/events/?query={event_1.event_id}"
  118. response = self.do_request(url)
  119. assert response.status_code == 200, response.content
  120. assert len(response.data) == 1
  121. assert response.data[0]["eventID"] == event_1.event_id
  122. def test_search_event_by_message(self):
  123. self.login_as(user=self.user)
  124. event_1 = self.store_event(
  125. data={
  126. "event_id": "a" * 32,
  127. "fingerprint": ["group-1"],
  128. "message": "foo bar hello world",
  129. "timestamp": iso_format(self.min_ago),
  130. },
  131. project_id=self.project.id,
  132. )
  133. group = event_1.group
  134. event_2 = self.store_event(
  135. data={
  136. "event_id": "b" * 32,
  137. "fingerprint": ["group-1"],
  138. "message": "this bar hello world",
  139. "timestamp": iso_format(self.min_ago),
  140. },
  141. project_id=self.project.id,
  142. )
  143. assert group == event_2.group
  144. query_1 = "foo"
  145. query_2 = "hello+world"
  146. # Single Word Query
  147. url = f"/api/0/issues/{group.id}/events/?query={query_1}"
  148. response = self.do_request(url)
  149. assert response.status_code == 200, response.content
  150. assert len(response.data) == 1
  151. assert response.data[0]["eventID"] == event_1.event_id
  152. # Multiple Word Query
  153. url = f"/api/0/issues/{group.id}/events/?query={query_2}"
  154. response = self.do_request(url)
  155. assert response.status_code == 200, response.content
  156. assert len(response.data) == 2
  157. assert sorted(map(lambda x: x["eventID"], response.data)) == sorted(
  158. [str(event_1.event_id), str(event_2.event_id)]
  159. )
  160. def test_search_by_release(self):
  161. self.login_as(user=self.user)
  162. self.create_release(self.project, version="first-release")
  163. event_1 = self.store_event(
  164. data={
  165. "event_id": "a" * 32,
  166. "fingerprint": ["group-1"],
  167. "timestamp": iso_format(self.min_ago),
  168. "release": "first-release",
  169. },
  170. project_id=self.project.id,
  171. )
  172. url = f"/api/0/issues/{event_1.group.id}/events/?query=release:latest"
  173. response = self.do_request(url)
  174. assert response.status_code == 200, response.content
  175. assert len(response.data) == 1
  176. assert response.data[0]["eventID"] == event_1.event_id
  177. def test_environment(self):
  178. self.login_as(user=self.user)
  179. events = {}
  180. for name in ["production", "development"]:
  181. events[name] = self.store_event(
  182. data={
  183. "fingerprint": ["put-me-in-group1"],
  184. "timestamp": iso_format(self.min_ago),
  185. "environment": name,
  186. },
  187. project_id=self.project.id,
  188. )
  189. # Asserts that all are in the same group
  190. (group_id,) = {e.group.id for e in events.values()}
  191. url = f"/api/0/issues/{group_id}/events/"
  192. response = self.do_request(url + "?environment=production")
  193. assert response.status_code == 200, response.content
  194. assert set(map(lambda x: x["eventID"], response.data)) == {
  195. str(events["production"].event_id)
  196. }
  197. response = self.client.get(
  198. url, data={"environment": ["production", "development"]}, format="json"
  199. )
  200. assert response.status_code == 200, response.content
  201. assert set(map(lambda x: x["eventID"], response.data)) == {
  202. str(event.event_id) for event in events.values()
  203. }
  204. response = self.do_request(url + "?environment=invalid")
  205. assert response.status_code == 200, response.content
  206. assert response.data == []
  207. response = self.client.get(
  208. url + "?environment=production&query=environment:development", format="json"
  209. )
  210. assert response.status_code == 200, response.content
  211. assert response.data == []
  212. def test_filters_based_on_retention(self):
  213. self.login_as(user=self.user)
  214. self.store_event(
  215. data={"fingerprint": ["group_1"], "timestamp": iso_format(before_now(days=2))},
  216. project_id=self.project.id,
  217. )
  218. event_2 = self.store_event(
  219. data={"fingerprint": ["group_1"], "timestamp": iso_format(self.min_ago)},
  220. project_id=self.project.id,
  221. )
  222. group = event_2.group
  223. with self.options({"system.event-retention-days": 1}):
  224. response = self.client.get(f"/api/0/issues/{group.id}/events/")
  225. assert response.status_code == 200, response.content
  226. assert len(response.data) == 1
  227. assert sorted(map(lambda x: x["eventID"], response.data)) == sorted([str(event_2.event_id)])
  228. def test_search_event_has_tags(self):
  229. self.login_as(user=self.user)
  230. event = self.store_event(
  231. data={
  232. "timestamp": iso_format(self.min_ago),
  233. "message": "foo",
  234. "tags": {"logger": "python"},
  235. },
  236. project_id=self.project.id,
  237. )
  238. response = self.client.get(f"/api/0/issues/{event.group.id}/events/")
  239. assert response.status_code == 200, response.content
  240. assert len(response.data) == 1
  241. assert {"key": "logger", "value": "python"} in response.data[0]["tags"]
  242. @freeze_time()
  243. def test_date_filters(self):
  244. self.login_as(user=self.user)
  245. event_1 = self.store_event(
  246. data={"timestamp": iso_format(before_now(days=5)), "fingerprint": ["group-1"]},
  247. project_id=self.project.id,
  248. )
  249. event_2 = self.store_event(
  250. data={"timestamp": iso_format(before_now(days=1)), "fingerprint": ["group-1"]},
  251. project_id=self.project.id,
  252. )
  253. group = event_1.group
  254. assert group == event_2.group
  255. response = self.client.get(f"/api/0/issues/{group.id}/events/", data={"statsPeriod": "6d"})
  256. assert response.status_code == 200, response.content
  257. assert len(response.data) == 2
  258. assert sorted(map(lambda x: x["eventID"], response.data)) == sorted(
  259. [str(event_1.event_id), str(event_2.event_id)]
  260. )
  261. response = self.client.get(f"/api/0/issues/{group.id}/events/", data={"statsPeriod": "2d"})
  262. assert response.status_code == 200, response.content
  263. assert len(response.data) == 1
  264. assert response.data[0]["eventID"] == str(event_2.event_id)
  265. def test_invalid_period(self):
  266. self.login_as(user=self.user)
  267. first_seen = timezone.now() - timedelta(days=5)
  268. group = self.create_group(first_seen=first_seen)
  269. response = self.client.get(f"/api/0/issues/{group.id}/events/", data={"statsPeriod": "lol"})
  270. assert response.status_code == 400
  271. def test_invalid_query(self):
  272. self.login_as(user=self.user)
  273. first_seen = timezone.now() - timedelta(days=5)
  274. group = self.create_group(first_seen=first_seen)
  275. response = self.client.get(
  276. f"/api/0/issues/{group.id}/events/",
  277. data={"statsPeriod": "7d", "query": "foo(bar"},
  278. )
  279. assert response.status_code == 400
  280. def test_multiple_group(self):
  281. self.login_as(user=self.user)
  282. event_1 = self.store_event(
  283. data={
  284. "fingerprint": ["group_1"],
  285. "event_id": "a" * 32,
  286. "message": "foo",
  287. "timestamp": iso_format(self.min_ago),
  288. },
  289. project_id=self.project.id,
  290. )
  291. event_2 = self.store_event(
  292. data={
  293. "fingerprint": ["group_2"],
  294. "event_id": "b" * 32,
  295. "message": "group2",
  296. "timestamp": iso_format(self.min_ago),
  297. },
  298. project_id=self.project.id,
  299. )
  300. for event in (event_1, event_2):
  301. url = f"/api/0/issues/{event.group.id}/events/"
  302. response = self.do_request(url)
  303. assert response.status_code == 200, response.content
  304. assert len(response.data) == 1, response.data
  305. assert list(map(lambda x: x["eventID"], response.data)) == [str(event.event_id)]
  306. def test_perf_issue(self):
  307. event_data = load_data(
  308. "transaction",
  309. fingerprint=[f"{GroupType.PERFORMANCE_N_PLUS_ONE_DB_QUERIES.value}-group1"],
  310. )
  311. event_1 = self.store_event(data=event_data, project_id=self.project.id)
  312. event_2 = self.store_event(data=event_data, project_id=self.project.id)
  313. self.login_as(user=self.user)
  314. url = f"/api/0/issues/{event_1.groups[0].id}/events/"
  315. response = self.do_request(url)
  316. assert response.status_code == 200, response.content
  317. assert sorted(map(lambda x: x["eventID"], response.data)) == sorted(
  318. [str(event_1.event_id), str(event_2.event_id)]
  319. )
  320. class GroupEventsTestWithBuilder(GroupEventsTest):
  321. def setUp(self):
  322. super().setUp()
  323. self.features["organizations:events-use-querybuilder"] = True