test_group_events.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. from datetime import timedelta
  2. from django.utils import timezone
  3. from freezegun import freeze_time
  4. from sentry.issues.grouptype import ProfileBlockedThreadGroupType
  5. from sentry.testutils import APITestCase, SnubaTestCase
  6. from sentry.testutils.cases import PerformanceIssueTestCase
  7. from sentry.testutils.helpers import parse_link_header
  8. from sentry.testutils.helpers.datetime import before_now, iso_format
  9. from sentry.testutils.silo import region_silo_test
  10. from tests.sentry.issues.test_utils import SearchIssueTestMixin
  11. @region_silo_test
  12. class GroupEventsTest(APITestCase, SnubaTestCase, SearchIssueTestMixin, PerformanceIssueTestCase):
  13. def setUp(self):
  14. super().setUp()
  15. self.min_ago = before_now(minutes=1)
  16. self.two_min_ago = before_now(minutes=2)
  17. self.features = {}
  18. def do_request(self, url):
  19. with self.feature(self.features):
  20. return self.client.get(url, format="json")
  21. def _parse_links(self, header):
  22. # links come in {url: {...attrs}}, but we need {rel: {...attrs}}
  23. links = {}
  24. for url, attrs in parse_link_header(header).items():
  25. links[attrs["rel"]] = attrs
  26. attrs["href"] = url
  27. return links
  28. def test_simple(self):
  29. self.login_as(user=self.user)
  30. event_1 = self.store_event(
  31. data={
  32. "event_id": "a" * 32,
  33. "fingerprint": ["1"],
  34. "timestamp": iso_format(self.min_ago),
  35. },
  36. project_id=self.project.id,
  37. )
  38. event_2 = self.store_event(
  39. data={
  40. "event_id": "b" * 32,
  41. "fingerprint": ["1"],
  42. "timestamp": iso_format(self.min_ago),
  43. },
  44. project_id=self.project.id,
  45. )
  46. url = f"/api/0/issues/{event_1.group.id}/events/"
  47. response = self.do_request(url)
  48. assert response.status_code == 200, response.content
  49. assert len(response.data) == 2
  50. assert sorted(map(lambda x: x["eventID"], response.data)) == sorted(
  51. [str(event_1.event_id), str(event_2.event_id)]
  52. )
  53. def test_tags(self):
  54. self.login_as(user=self.user)
  55. event_1 = self.store_event(
  56. data={
  57. "event_id": "a" * 32,
  58. "fingerprint": ["1"],
  59. "tags": {"foo": "baz", "bar": "buz"},
  60. "timestamp": iso_format(self.min_ago),
  61. },
  62. project_id=self.project.id,
  63. )
  64. event_2 = self.store_event(
  65. data={
  66. "event_id": "b" * 32,
  67. "fingerprint": ["1"],
  68. "tags": {"bar": "biz"},
  69. "timestamp": iso_format(before_now(seconds=61)),
  70. },
  71. project_id=self.project.id,
  72. )
  73. url = f"/api/0/issues/{event_1.group.id}/events/"
  74. response = self.do_request(url + "?query=foo:baz")
  75. assert response.status_code == 200, response.content
  76. assert len(response.data) == 1
  77. assert response.data[0]["eventID"] == str(event_1.event_id)
  78. response = self.do_request(url + "?query=!foo:baz")
  79. assert response.status_code == 200, response.content
  80. assert len(response.data) == 1
  81. assert response.data[0]["eventID"] == str(event_2.event_id)
  82. response = self.do_request(url + "?query=bar:biz")
  83. assert response.status_code == 200, response.content
  84. assert len(response.data) == 1
  85. assert response.data[0]["eventID"] == str(event_2.event_id)
  86. response = self.do_request(url + "?query=bar:biz%20foo:baz")
  87. assert response.status_code == 200, response.content
  88. assert len(response.data) == 0
  89. response = self.do_request(url + "?query=bar:buz%20foo:baz")
  90. assert response.status_code == 200, response.content
  91. assert len(response.data) == 1
  92. assert response.data[0]["eventID"] == str(event_1.event_id)
  93. response = self.do_request(url + "?query=bar:baz")
  94. assert response.status_code == 200, response.content
  95. assert len(response.data) == 0
  96. response = self.do_request(url + "?query=a:b")
  97. assert response.status_code == 200, response.content
  98. assert len(response.data) == 0
  99. response = self.do_request(url + "?query=bar:b")
  100. assert response.status_code == 200, response.content
  101. assert len(response.data) == 0
  102. response = self.do_request(url + "?query=bar:baz")
  103. assert response.status_code == 200, response.content
  104. assert len(response.data) == 0
  105. response = self.do_request(url + "?query=!bar:baz")
  106. assert response.status_code == 200, response.content
  107. assert len(response.data) == 2
  108. assert {e["eventID"] for e in response.data} == {event_1.event_id, event_2.event_id}
  109. def test_search_event_by_id(self):
  110. self.login_as(user=self.user)
  111. event_1 = self.store_event(
  112. data={
  113. "event_id": "a" * 32,
  114. "fingerprint": ["group-1"],
  115. "timestamp": iso_format(self.min_ago),
  116. },
  117. project_id=self.project.id,
  118. )
  119. self.store_event(
  120. data={
  121. "event_id": "b" * 32,
  122. "fingerprint": ["group-1"],
  123. "timestamp": iso_format(self.min_ago),
  124. },
  125. project_id=self.project.id,
  126. )
  127. url = f"/api/0/issues/{event_1.group.id}/events/?query={event_1.event_id}"
  128. response = self.do_request(url)
  129. assert response.status_code == 200, response.content
  130. assert len(response.data) == 1
  131. assert response.data[0]["eventID"] == event_1.event_id
  132. def test_search_event_by_message(self):
  133. self.login_as(user=self.user)
  134. event_1 = self.store_event(
  135. data={
  136. "event_id": "a" * 32,
  137. "fingerprint": ["group-1"],
  138. "message": "foo bar hello world",
  139. "timestamp": iso_format(self.min_ago),
  140. },
  141. project_id=self.project.id,
  142. )
  143. group = event_1.group
  144. event_2 = self.store_event(
  145. data={
  146. "event_id": "b" * 32,
  147. "fingerprint": ["group-1"],
  148. "message": "this bar hello world",
  149. "timestamp": iso_format(self.min_ago),
  150. },
  151. project_id=self.project.id,
  152. )
  153. assert group == event_2.group
  154. query_1 = "foo"
  155. query_2 = "hello+world"
  156. # Single Word Query
  157. url = f"/api/0/issues/{group.id}/events/?query={query_1}"
  158. response = self.do_request(url)
  159. assert response.status_code == 200, response.content
  160. assert len(response.data) == 1
  161. assert response.data[0]["eventID"] == event_1.event_id
  162. # Multiple Word Query
  163. url = f"/api/0/issues/{group.id}/events/?query={query_2}"
  164. response = self.do_request(url)
  165. assert response.status_code == 200, response.content
  166. assert len(response.data) == 2
  167. assert sorted(map(lambda x: x["eventID"], response.data)) == sorted(
  168. [str(event_1.event_id), str(event_2.event_id)]
  169. )
  170. def test_search_by_release(self):
  171. self.login_as(user=self.user)
  172. self.create_release(self.project, version="first-release")
  173. event_1 = self.store_event(
  174. data={
  175. "event_id": "a" * 32,
  176. "fingerprint": ["group-1"],
  177. "timestamp": iso_format(self.min_ago),
  178. "release": "first-release",
  179. },
  180. project_id=self.project.id,
  181. )
  182. url = f"/api/0/issues/{event_1.group.id}/events/?query=release:latest"
  183. response = self.do_request(url)
  184. assert response.status_code == 200, response.content
  185. assert len(response.data) == 1
  186. assert response.data[0]["eventID"] == event_1.event_id
  187. def test_environment(self):
  188. self.login_as(user=self.user)
  189. events = {}
  190. for name in ["production", "development"]:
  191. events[name] = self.store_event(
  192. data={
  193. "fingerprint": ["put-me-in-group1"],
  194. "timestamp": iso_format(self.min_ago),
  195. "environment": name,
  196. },
  197. project_id=self.project.id,
  198. )
  199. # Asserts that all are in the same group
  200. (group_id,) = {e.group.id for e in events.values()}
  201. url = f"/api/0/issues/{group_id}/events/"
  202. response = self.do_request(url + "?environment=production")
  203. assert response.status_code == 200, response.content
  204. assert set(map(lambda x: x["eventID"], response.data)) == {
  205. str(events["production"].event_id)
  206. }
  207. response = self.client.get(
  208. url, data={"environment": ["production", "development"]}, format="json"
  209. )
  210. assert response.status_code == 200, response.content
  211. assert set(map(lambda x: x["eventID"], response.data)) == {
  212. str(event.event_id) for event in events.values()
  213. }
  214. response = self.do_request(url + "?environment=invalid")
  215. assert response.status_code == 200, response.content
  216. assert response.data == []
  217. response = self.client.get(
  218. url + "?environment=production&query=environment:development", format="json"
  219. )
  220. assert response.status_code == 200, response.content
  221. assert response.data == []
  222. def test_filters_based_on_retention(self):
  223. self.login_as(user=self.user)
  224. self.store_event(
  225. data={"fingerprint": ["group_1"], "timestamp": iso_format(before_now(days=2))},
  226. project_id=self.project.id,
  227. )
  228. event_2 = self.store_event(
  229. data={"fingerprint": ["group_1"], "timestamp": iso_format(self.min_ago)},
  230. project_id=self.project.id,
  231. )
  232. group = event_2.group
  233. with self.options({"system.event-retention-days": 1}):
  234. response = self.client.get(f"/api/0/issues/{group.id}/events/")
  235. assert response.status_code == 200, response.content
  236. assert len(response.data) == 1
  237. assert sorted(map(lambda x: x["eventID"], response.data)) == sorted([str(event_2.event_id)])
  238. def test_search_event_has_tags(self):
  239. self.login_as(user=self.user)
  240. event = self.store_event(
  241. data={
  242. "timestamp": iso_format(self.min_ago),
  243. "message": "foo",
  244. "tags": {"logger": "python"},
  245. },
  246. project_id=self.project.id,
  247. )
  248. response = self.client.get(f"/api/0/issues/{event.group.id}/events/")
  249. assert response.status_code == 200, response.content
  250. assert len(response.data) == 1
  251. assert {"key": "logger", "value": "python"} in response.data[0]["tags"]
  252. @freeze_time()
  253. def test_date_filters(self):
  254. self.login_as(user=self.user)
  255. event_1 = self.store_event(
  256. data={"timestamp": iso_format(before_now(days=5)), "fingerprint": ["group-1"]},
  257. project_id=self.project.id,
  258. )
  259. event_2 = self.store_event(
  260. data={"timestamp": iso_format(before_now(days=1)), "fingerprint": ["group-1"]},
  261. project_id=self.project.id,
  262. )
  263. group = event_1.group
  264. assert group == event_2.group
  265. response = self.client.get(f"/api/0/issues/{group.id}/events/", data={"statsPeriod": "6d"})
  266. assert response.status_code == 200, response.content
  267. assert len(response.data) == 2
  268. assert sorted(map(lambda x: x["eventID"], response.data)) == sorted(
  269. [str(event_1.event_id), str(event_2.event_id)]
  270. )
  271. response = self.client.get(f"/api/0/issues/{group.id}/events/", data={"statsPeriod": "2d"})
  272. assert response.status_code == 200, response.content
  273. assert len(response.data) == 1
  274. assert response.data[0]["eventID"] == str(event_2.event_id)
  275. def test_invalid_period(self):
  276. self.login_as(user=self.user)
  277. first_seen = timezone.now() - timedelta(days=5)
  278. group = self.create_group(first_seen=first_seen)
  279. response = self.client.get(f"/api/0/issues/{group.id}/events/", data={"statsPeriod": "lol"})
  280. assert response.status_code == 400
  281. def test_invalid_query(self):
  282. self.login_as(user=self.user)
  283. first_seen = timezone.now() - timedelta(days=5)
  284. group = self.create_group(first_seen=first_seen)
  285. response = self.client.get(
  286. f"/api/0/issues/{group.id}/events/",
  287. data={"statsPeriod": "7d", "query": "foo(bar"},
  288. )
  289. assert response.status_code == 400
  290. def test_multiple_group(self):
  291. self.login_as(user=self.user)
  292. event_1 = self.store_event(
  293. data={
  294. "fingerprint": ["group_1"],
  295. "event_id": "a" * 32,
  296. "message": "foo",
  297. "timestamp": iso_format(self.min_ago),
  298. },
  299. project_id=self.project.id,
  300. )
  301. event_2 = self.store_event(
  302. data={
  303. "fingerprint": ["group_2"],
  304. "event_id": "b" * 32,
  305. "message": "group2",
  306. "timestamp": iso_format(self.min_ago),
  307. },
  308. project_id=self.project.id,
  309. )
  310. for event in (event_1, event_2):
  311. url = f"/api/0/issues/{event.group.id}/events/"
  312. response = self.do_request(url)
  313. assert response.status_code == 200, response.content
  314. assert len(response.data) == 1, response.data
  315. assert list(map(lambda x: x["eventID"], response.data)) == [str(event.event_id)]
  316. def test_pagination(self):
  317. self.login_as(user=self.user)
  318. for _ in range(2):
  319. event = self.store_event(
  320. data={
  321. "fingerprint": ["group_1"],
  322. "event_id": "a" * 32,
  323. "message": "foo",
  324. "timestamp": iso_format(self.min_ago),
  325. },
  326. project_id=self.project.id,
  327. )
  328. url = f"/api/0/issues/{event.group.id}/events/?per_page=1"
  329. response = self.do_request(url)
  330. links = self._parse_links(response["Link"])
  331. assert response.status_code == 200, response.content
  332. assert links["previous"]["results"] == "false"
  333. assert links["next"]["results"] == "true"
  334. assert len(response.data) == 1
  335. def test_orderby(self):
  336. self.login_as(user=self.user)
  337. event = self.store_event(
  338. data={
  339. "fingerprint": ["group_1"],
  340. "event_id": "a" * 32,
  341. "message": "foo",
  342. "timestamp": iso_format(self.min_ago),
  343. },
  344. project_id=self.project.id,
  345. )
  346. event = self.store_event(
  347. data={
  348. "fingerprint": ["group_1"],
  349. "event_id": "b" * 32,
  350. "message": "foo",
  351. "timestamp": iso_format(self.two_min_ago),
  352. },
  353. project_id=self.project.id,
  354. )
  355. url = f"/api/0/issues/{event.group.id}/events/"
  356. response = self.do_request(url)
  357. assert len(response.data) == 2
  358. assert response.data[0]["eventID"] == "a" * 32
  359. assert response.data[1]["eventID"] == "b" * 32
  360. def test_perf_issue(self):
  361. event_1 = self.create_performance_issue()
  362. event_2 = self.create_performance_issue()
  363. self.login_as(user=self.user)
  364. url = f"/api/0/issues/{event_1.group.id}/events/"
  365. response = self.do_request(url)
  366. assert response.status_code == 200, response.content
  367. assert sorted(map(lambda x: x["eventID"], response.data)) == sorted(
  368. [str(event_1.event_id), str(event_2.event_id)]
  369. )
  370. def test_generic_issue(self):
  371. event_1, _, group_info = self.store_search_issue(
  372. self.project.id,
  373. self.user.id,
  374. [f"{ProfileBlockedThreadGroupType.type_id}-group1"],
  375. "prod",
  376. before_now(hours=1).replace(tzinfo=timezone.utc),
  377. )
  378. event_2, _, _ = self.store_search_issue(
  379. self.project.id,
  380. self.user.id,
  381. [f"{ProfileBlockedThreadGroupType.type_id}-group1"],
  382. "prod",
  383. before_now(hours=1).replace(tzinfo=timezone.utc),
  384. )
  385. self.login_as(user=self.user)
  386. url = f"/api/0/issues/{group_info.group.id}/events/"
  387. response = self.do_request(url)
  388. assert response.status_code == 200, response.content
  389. assert sorted(map(lambda x: x["eventID"], response.data)) == sorted(
  390. [str(event_1.event_id), str(event_2.event_id)]
  391. )