test_organization_events_meta.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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.search.events.builder.raw_snql_query")
  113. def test_handling_snuba_errors(self, mock_snql_query):
  114. mock_snql_query.side_effect = ParseError("test")
  115. with self.feature(self.features):
  116. response = self.client.get(self.url, format="json")
  117. assert response.status_code == 400, response.content
  118. @mock.patch("sentry.utils.snuba.quantize_time")
  119. def test_quantize_dates(self, mock_quantize):
  120. mock_quantize.return_value = before_now(days=1).replace(tzinfo=utc)
  121. with self.feature(self.features):
  122. # Don't quantize short time periods
  123. self.client.get(
  124. self.url,
  125. format="json",
  126. data={"statsPeriod": "1h", "query": "", "field": ["id", "timestamp"]},
  127. )
  128. # Don't quantize absolute date periods
  129. self.client.get(
  130. self.url,
  131. format="json",
  132. data={
  133. "start": iso_format(before_now(days=20)),
  134. "end": iso_format(before_now(days=15)),
  135. "query": "",
  136. "field": ["id", "timestamp"],
  137. },
  138. )
  139. assert len(mock_quantize.mock_calls) == 0
  140. # Quantize long date periods
  141. self.client.get(
  142. self.url,
  143. format="json",
  144. data={"field": ["id", "timestamp"], "statsPeriod": "90d", "query": ""},
  145. )
  146. assert len(mock_quantize.mock_calls) == 2
  147. class OrganizationEventsRelatedIssuesEndpoint(APITestCase, SnubaTestCase):
  148. def setUp(self):
  149. super().setUp()
  150. def test_find_related_issue(self):
  151. self.login_as(user=self.user)
  152. project = self.create_project()
  153. event1 = self.store_event(
  154. data={"timestamp": iso_format(before_now(minutes=1)), "transaction": "/beth/sanchez"},
  155. project_id=project.id,
  156. )
  157. url = reverse(
  158. "sentry-api-0-organization-related-issues",
  159. kwargs={"organization_slug": project.organization.slug},
  160. )
  161. response = self.client.get(url, {"transaction": "/beth/sanchez"}, format="json")
  162. assert response.status_code == 200, response.content
  163. assert len(response.data) == 1
  164. assert response.data[0]["shortId"] == event1.group.qualified_short_id
  165. assert int(response.data[0]["id"]) == event1.group_id
  166. def test_related_issues_no_transaction(self):
  167. self.login_as(user=self.user)
  168. project = self.create_project()
  169. self.store_event(
  170. data={"timestamp": iso_format(before_now(minutes=1)), "transaction": "/beth/sanchez"},
  171. project_id=project.id,
  172. )
  173. url = reverse(
  174. "sentry-api-0-organization-related-issues",
  175. kwargs={"organization_slug": project.organization.slug},
  176. )
  177. response = self.client.get(url, format="json")
  178. assert response.status_code == 400, response.content
  179. assert (
  180. response.data["detail"]
  181. == "Must provide one of ['transaction'] in order to find related events"
  182. )
  183. def test_related_issues_no_matching_groups(self):
  184. self.login_as(user=self.user)
  185. project = self.create_project()
  186. self.store_event(
  187. data={"timestamp": iso_format(before_now(minutes=1)), "transaction": "/beth/sanchez"},
  188. project_id=project.id,
  189. )
  190. url = reverse(
  191. "sentry-api-0-organization-related-issues",
  192. kwargs={"organization_slug": project.organization.slug},
  193. )
  194. response = self.client.get(url, {"transaction": "/morty/sanchez"}, format="json")
  195. assert response.status_code == 200, response.content
  196. assert len(response.data) == 0
  197. def test_related_issues_only_issues_in_date(self):
  198. self.login_as(user=self.user)
  199. project = self.create_project()
  200. self.store_event(
  201. data={
  202. "event_id": "a" * 32,
  203. "timestamp": iso_format(before_now(days=2)),
  204. "transaction": "/beth/sanchez",
  205. },
  206. project_id=project.id,
  207. )
  208. event2 = self.store_event(
  209. data={
  210. "event_id": "b" * 32,
  211. "timestamp": iso_format(before_now(minutes=1)),
  212. "transaction": "/beth/sanchez",
  213. },
  214. project_id=project.id,
  215. )
  216. url = reverse(
  217. "sentry-api-0-organization-related-issues",
  218. kwargs={"organization_slug": project.organization.slug},
  219. )
  220. response = self.client.get(
  221. url, {"transaction": "/beth/sanchez", "statsPeriod": "24h"}, format="json"
  222. )
  223. assert response.status_code == 200, response.content
  224. assert len(response.data) == 1
  225. assert response.data[0]["shortId"] == event2.group.qualified_short_id
  226. assert int(response.data[0]["id"]) == event2.group_id
  227. def test_related_issues_transactions_from_different_projects(self):
  228. self.login_as(user=self.user)
  229. project1 = self.create_project()
  230. project2 = self.create_project()
  231. event1 = self.store_event(
  232. data={
  233. "event_id": "a" * 32,
  234. "timestamp": iso_format(before_now(minutes=1)),
  235. "transaction": "/beth/sanchez",
  236. },
  237. project_id=project1.id,
  238. )
  239. self.store_event(
  240. data={
  241. "event_id": "b" * 32,
  242. "timestamp": iso_format(before_now(minutes=1)),
  243. "transaction": "/beth/sanchez",
  244. },
  245. project_id=project2.id,
  246. )
  247. url = reverse(
  248. "sentry-api-0-organization-related-issues",
  249. kwargs={"organization_slug": project1.organization.slug},
  250. )
  251. response = self.client.get(
  252. url,
  253. {"transaction": "/beth/sanchez", "project": project1.id},
  254. format="json",
  255. )
  256. assert response.status_code == 200, response.content
  257. assert len(response.data) == 1
  258. assert response.data[0]["shortId"] == event1.group.qualified_short_id
  259. assert int(response.data[0]["id"]) == event1.group_id
  260. def test_related_issues_transactions_with_quotes(self):
  261. self.login_as(user=self.user)
  262. project = self.create_project()
  263. event = self.store_event(
  264. data={
  265. "event_id": "a" * 32,
  266. "timestamp": iso_format(before_now(minutes=1)),
  267. "transaction": '/beth/"sanchez"',
  268. },
  269. project_id=project.id,
  270. )
  271. url = reverse(
  272. "sentry-api-0-organization-related-issues",
  273. kwargs={"organization_slug": project.organization.slug},
  274. )
  275. response = self.client.get(
  276. url,
  277. {"transaction": '/beth/"sanchez"', "project": project.id},
  278. format="json",
  279. )
  280. assert response.status_code == 200, response.content
  281. assert len(response.data) == 1
  282. assert response.data[0]["shortId"] == event.group.qualified_short_id
  283. assert int(response.data[0]["id"]) == event.group_id
  284. url = reverse(
  285. "sentry-api-0-organization-related-issues",
  286. kwargs={"organization_slug": project.organization.slug},
  287. )
  288. response = self.client.get(
  289. url,
  290. {"transaction": '/beth/\\"sanchez\\"', "project": project.id},
  291. format="json",
  292. )
  293. assert response.status_code == 200, response.content
  294. assert len(response.data) == 1
  295. assert response.data[0]["shortId"] == event.group.qualified_short_id
  296. assert int(response.data[0]["id"]) == event.group_id