test_organization_events_meta.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. from unittest import mock
  2. from django.urls import reverse
  3. from pytz import utc
  4. from rest_framework.exceptions import ParseError
  5. from sentry.testutils import APITestCase, SnubaTestCase
  6. from sentry.testutils.helpers.datetime import before_now, iso_format
  7. class OrganizationEventsMetaEndpoint(APITestCase, SnubaTestCase):
  8. def setUp(self):
  9. super().setUp()
  10. self.min_ago = before_now(minutes=1)
  11. self.login_as(user=self.user)
  12. self.project = self.create_project()
  13. self.url = reverse(
  14. "sentry-api-0-organization-events-meta",
  15. kwargs={"organization_slug": self.project.organization.slug},
  16. )
  17. self.features = {"organizations:discover-basic": True}
  18. def test_simple(self):
  19. self.store_event(data={"timestamp": iso_format(self.min_ago)}, project_id=self.project.id)
  20. with self.feature(self.features):
  21. response = self.client.get(self.url, format="json")
  22. assert response.status_code == 200, response.content
  23. assert response.data["count"] == 1
  24. def test_multiple_projects(self):
  25. project2 = self.create_project()
  26. self.store_event(data={"timestamp": iso_format(self.min_ago)}, project_id=self.project.id)
  27. self.store_event(data={"timestamp": iso_format(self.min_ago)}, project_id=project2.id)
  28. response = self.client.get(self.url, format="json")
  29. assert response.status_code == 400, response.content
  30. self.features["organizations:global-views"] = True
  31. with self.feature(self.features):
  32. response = self.client.get(self.url, format="json")
  33. assert response.status_code == 200, response.content
  34. assert response.data["count"] == 2
  35. def test_search(self):
  36. self.store_event(
  37. data={"timestamp": iso_format(self.min_ago), "message": "how to make fast"},
  38. project_id=self.project.id,
  39. )
  40. self.store_event(
  41. data={"timestamp": iso_format(self.min_ago), "message": "Delete the Data"},
  42. project_id=self.project.id,
  43. )
  44. with self.feature(self.features):
  45. response = self.client.get(self.url, {"query": "delete"}, format="json")
  46. assert response.status_code == 200, response.content
  47. assert response.data["count"] == 1
  48. def test_invalid_query(self):
  49. with self.feature(self.features):
  50. response = self.client.get(self.url, {"query": "is:unresolved"}, format="json")
  51. assert response.status_code == 400, response.content
  52. def test_no_projects(self):
  53. no_project_org = self.create_organization(owner=self.user)
  54. url = reverse(
  55. "sentry-api-0-organization-events-meta",
  56. kwargs={"organization_slug": no_project_org.slug},
  57. )
  58. with self.feature(self.features):
  59. response = self.client.get(url, format="json")
  60. assert response.status_code == 200, response.content
  61. assert response.data["count"] == 0
  62. def test_transaction_event(self):
  63. data = {
  64. "event_id": "a" * 32,
  65. "type": "transaction",
  66. "transaction": "api.issue.delete",
  67. "spans": [],
  68. "contexts": {"trace": {"op": "foobar", "trace_id": "a" * 32, "span_id": "a" * 16}},
  69. "tags": {"important": "yes"},
  70. "timestamp": iso_format(before_now(minutes=1)),
  71. "start_timestamp": iso_format(before_now(minutes=1, seconds=3)),
  72. }
  73. self.store_event(data=data, project_id=self.project.id)
  74. url = reverse(
  75. "sentry-api-0-organization-events-meta",
  76. kwargs={"organization_slug": self.project.organization.slug},
  77. )
  78. with self.feature(self.features):
  79. response = self.client.get(url, {"query": "transaction.duration:>1"}, format="json")
  80. assert response.status_code == 200, response.content
  81. assert response.data["count"] == 1
  82. def test_transaction_event_with_last_seen(self):
  83. data = {
  84. "event_id": "a" * 32,
  85. "type": "transaction",
  86. "transaction": "api.issue.delete",
  87. "spans": [],
  88. "contexts": {"trace": {"op": "foobar", "trace_id": "a" * 32, "span_id": "a" * 16}},
  89. "tags": {"important": "yes"},
  90. "timestamp": iso_format(before_now(minutes=1)),
  91. "start_timestamp": iso_format(before_now(minutes=1, seconds=3)),
  92. }
  93. self.store_event(data=data, project_id=self.project.id)
  94. with self.feature(self.features):
  95. response = self.client.get(
  96. self.url, {"query": "event.type:transaction last_seen():>2012-12-31"}, format="json"
  97. )
  98. assert response.status_code == 200, response.content
  99. assert response.data["count"] == 1
  100. def test_out_of_retention(self):
  101. with self.feature(self.features):
  102. with self.options({"system.event-retention-days": 10}):
  103. response = self.client.get(
  104. self.url,
  105. format="json",
  106. data={
  107. "start": iso_format(before_now(days=20)),
  108. "end": iso_format(before_now(days=15)),
  109. },
  110. )
  111. assert response.status_code == 400
  112. @mock.patch("sentry.snuba.discover.raw_query")
  113. @mock.patch("sentry.search.events.builder.raw_snql_query")
  114. def test_handling_snuba_errors(self, mock_query, mock_snql_query):
  115. mock_query.side_effect = ParseError("test")
  116. mock_snql_query.side_effect = ParseError("test")
  117. with self.feature(self.features):
  118. response = self.client.get(self.url, format="json")
  119. assert response.status_code == 400, response.content
  120. @mock.patch("sentry.utils.snuba.quantize_time")
  121. def test_quantize_dates(self, mock_quantize):
  122. mock_quantize.return_value = before_now(days=1).replace(tzinfo=utc)
  123. with self.feature(self.features):
  124. # Don't quantize short time periods
  125. self.client.get(
  126. self.url,
  127. format="json",
  128. data={"statsPeriod": "1h", "query": "", "field": ["id", "timestamp"]},
  129. )
  130. # Don't quantize absolute date periods
  131. self.client.get(
  132. self.url,
  133. format="json",
  134. data={
  135. "start": iso_format(before_now(days=20)),
  136. "end": iso_format(before_now(days=15)),
  137. "query": "",
  138. "field": ["id", "timestamp"],
  139. },
  140. )
  141. assert len(mock_quantize.mock_calls) == 0
  142. # Quantize long date periods
  143. self.client.get(
  144. self.url,
  145. format="json",
  146. data={"field": ["id", "timestamp"], "statsPeriod": "90d", "query": ""},
  147. )
  148. assert len(mock_quantize.mock_calls) == 2
  149. class OrganizationEventsMetaEndpointWithSnql(OrganizationEventsMetaEndpoint):
  150. def setUp(self):
  151. super().setUp()
  152. self.features["organizations:discover-use-snql"] = True
  153. class OrganizationEventsRelatedIssuesEndpoint(APITestCase, SnubaTestCase):
  154. def setUp(self):
  155. super().setUp()
  156. def test_find_related_issue(self):
  157. self.login_as(user=self.user)
  158. project = self.create_project()
  159. event1 = self.store_event(
  160. data={"timestamp": iso_format(before_now(minutes=1)), "transaction": "/beth/sanchez"},
  161. project_id=project.id,
  162. )
  163. url = reverse(
  164. "sentry-api-0-organization-related-issues",
  165. kwargs={"organization_slug": project.organization.slug},
  166. )
  167. response = self.client.get(url, {"transaction": "/beth/sanchez"}, format="json")
  168. assert response.status_code == 200, response.content
  169. assert len(response.data) == 1
  170. assert response.data[0]["shortId"] == event1.group.qualified_short_id
  171. assert int(response.data[0]["id"]) == event1.group_id
  172. def test_related_issues_no_transaction(self):
  173. self.login_as(user=self.user)
  174. project = self.create_project()
  175. self.store_event(
  176. data={"timestamp": iso_format(before_now(minutes=1)), "transaction": "/beth/sanchez"},
  177. project_id=project.id,
  178. )
  179. url = reverse(
  180. "sentry-api-0-organization-related-issues",
  181. kwargs={"organization_slug": project.organization.slug},
  182. )
  183. response = self.client.get(url, format="json")
  184. assert response.status_code == 400, response.content
  185. assert (
  186. response.data["detail"]
  187. == "Must provide one of ['transaction'] in order to find related events"
  188. )
  189. def test_related_issues_no_matching_groups(self):
  190. self.login_as(user=self.user)
  191. project = self.create_project()
  192. self.store_event(
  193. data={"timestamp": iso_format(before_now(minutes=1)), "transaction": "/beth/sanchez"},
  194. project_id=project.id,
  195. )
  196. url = reverse(
  197. "sentry-api-0-organization-related-issues",
  198. kwargs={"organization_slug": project.organization.slug},
  199. )
  200. response = self.client.get(url, {"transaction": "/morty/sanchez"}, format="json")
  201. assert response.status_code == 200, response.content
  202. assert len(response.data) == 0
  203. def test_related_issues_only_issues_in_date(self):
  204. self.login_as(user=self.user)
  205. project = self.create_project()
  206. self.store_event(
  207. data={
  208. "event_id": "a" * 32,
  209. "timestamp": iso_format(before_now(days=2)),
  210. "transaction": "/beth/sanchez",
  211. },
  212. project_id=project.id,
  213. )
  214. event2 = self.store_event(
  215. data={
  216. "event_id": "b" * 32,
  217. "timestamp": iso_format(before_now(minutes=1)),
  218. "transaction": "/beth/sanchez",
  219. },
  220. project_id=project.id,
  221. )
  222. url = reverse(
  223. "sentry-api-0-organization-related-issues",
  224. kwargs={"organization_slug": project.organization.slug},
  225. )
  226. response = self.client.get(
  227. url, {"transaction": "/beth/sanchez", "statsPeriod": "24h"}, format="json"
  228. )
  229. assert response.status_code == 200, response.content
  230. assert len(response.data) == 1
  231. assert response.data[0]["shortId"] == event2.group.qualified_short_id
  232. assert int(response.data[0]["id"]) == event2.group_id
  233. def test_related_issues_transactions_from_different_projects(self):
  234. self.login_as(user=self.user)
  235. project1 = self.create_project()
  236. project2 = self.create_project()
  237. event1 = self.store_event(
  238. data={
  239. "event_id": "a" * 32,
  240. "timestamp": iso_format(before_now(minutes=1)),
  241. "transaction": "/beth/sanchez",
  242. },
  243. project_id=project1.id,
  244. )
  245. self.store_event(
  246. data={
  247. "event_id": "b" * 32,
  248. "timestamp": iso_format(before_now(minutes=1)),
  249. "transaction": "/beth/sanchez",
  250. },
  251. project_id=project2.id,
  252. )
  253. url = reverse(
  254. "sentry-api-0-organization-related-issues",
  255. kwargs={"organization_slug": project1.organization.slug},
  256. )
  257. response = self.client.get(
  258. url,
  259. {"transaction": "/beth/sanchez", "project": project1.id},
  260. format="json",
  261. )
  262. assert response.status_code == 200, response.content
  263. assert len(response.data) == 1
  264. assert response.data[0]["shortId"] == event1.group.qualified_short_id
  265. assert int(response.data[0]["id"]) == event1.group_id
  266. def test_related_issues_transactions_with_quotes(self):
  267. self.login_as(user=self.user)
  268. project = self.create_project()
  269. event = self.store_event(
  270. data={
  271. "event_id": "a" * 32,
  272. "timestamp": iso_format(before_now(minutes=1)),
  273. "transaction": '/beth/"sanchez"',
  274. },
  275. project_id=project.id,
  276. )
  277. url = reverse(
  278. "sentry-api-0-organization-related-issues",
  279. kwargs={"organization_slug": project.organization.slug},
  280. )
  281. response = self.client.get(
  282. url,
  283. {"transaction": '/beth/"sanchez"', "project": project.id},
  284. format="json",
  285. )
  286. assert response.status_code == 200, response.content
  287. assert len(response.data) == 1
  288. assert response.data[0]["shortId"] == event.group.qualified_short_id
  289. assert int(response.data[0]["id"]) == event.group_id
  290. url = reverse(
  291. "sentry-api-0-organization-related-issues",
  292. kwargs={"organization_slug": project.organization.slug},
  293. )
  294. response = self.client.get(
  295. url,
  296. {"transaction": '/beth/\\"sanchez\\"', "project": project.id},
  297. format="json",
  298. )
  299. assert response.status_code == 200, response.content
  300. assert len(response.data) == 1
  301. assert response.data[0]["shortId"] == event.group.qualified_short_id
  302. assert int(response.data[0]["id"]) == event.group_id