test_organization_events_meta.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. from unittest import mock
  2. import pytest
  3. from django.urls import reverse
  4. from rest_framework.exceptions import ParseError
  5. from sentry.issues.grouptype import ProfileFileIOGroupType
  6. from sentry.testutils.cases import APITestCase, SnubaTestCase
  7. from sentry.testutils.helpers.datetime import before_now, iso_format
  8. from sentry.testutils.helpers.features import with_feature
  9. from tests.sentry.issues.test_utils import SearchIssueTestMixin
  10. pytestmark = pytest.mark.sentry_metrics
  11. class OrganizationEventsMetaEndpoint(APITestCase, SnubaTestCase, SearchIssueTestMixin):
  12. def setUp(self):
  13. super().setUp()
  14. self.min_ago = before_now(minutes=1)
  15. self.login_as(user=self.user)
  16. self.project = self.create_project()
  17. self.url = reverse(
  18. "sentry-api-0-organization-events-meta",
  19. kwargs={"organization_id_or_slug": self.project.organization.slug},
  20. )
  21. self.features = {"organizations:discover-basic": True}
  22. def test_simple(self):
  23. self.store_event(data={"timestamp": iso_format(self.min_ago)}, project_id=self.project.id)
  24. with self.feature(self.features):
  25. response = self.client.get(self.url, format="json")
  26. assert response.status_code == 200, response.content
  27. assert response.data["count"] == 1
  28. def test_multiple_projects(self):
  29. project2 = self.create_project()
  30. self.store_event(data={"timestamp": iso_format(self.min_ago)}, project_id=self.project.id)
  31. self.store_event(data={"timestamp": iso_format(self.min_ago)}, project_id=project2.id)
  32. response = self.client.get(self.url, format="json")
  33. assert response.status_code == 400, response.content
  34. self.features["organizations:global-views"] = True
  35. with self.feature(self.features):
  36. response = self.client.get(self.url, format="json")
  37. assert response.status_code == 200, response.content
  38. assert response.data["count"] == 2
  39. def test_search(self):
  40. self.store_event(
  41. data={"timestamp": iso_format(self.min_ago), "message": "how to make fast"},
  42. project_id=self.project.id,
  43. )
  44. self.store_event(
  45. data={"timestamp": iso_format(self.min_ago), "message": "Delete the Data"},
  46. project_id=self.project.id,
  47. )
  48. with self.feature(self.features):
  49. response = self.client.get(self.url, {"query": "delete"}, format="json")
  50. assert response.status_code == 200, response.content
  51. assert response.data["count"] == 1
  52. def test_invalid_query(self):
  53. with self.feature(self.features):
  54. response = self.client.get(self.url, {"query": "is:unresolved"}, format="json")
  55. assert response.status_code == 400, response.content
  56. @with_feature("organizations:issue-priority-ui")
  57. def test_invalid_query_priority(self):
  58. with self.feature(self.features):
  59. response = self.client.get(
  60. self.url, {"query": "is:unresolved priority:[high, medium]"}, format="json"
  61. )
  62. assert response.status_code == 400, response.content
  63. def test_no_projects(self):
  64. no_project_org = self.create_organization(owner=self.user)
  65. url = reverse(
  66. "sentry-api-0-organization-events-meta",
  67. kwargs={"organization_id_or_slug": no_project_org.slug},
  68. )
  69. with self.feature(self.features):
  70. response = self.client.get(url, format="json")
  71. assert response.status_code == 200, response.content
  72. assert response.data["count"] == 0
  73. def test_transaction_event(self):
  74. data = {
  75. "event_id": "a" * 32,
  76. "type": "transaction",
  77. "transaction": "api.issue.delete",
  78. "spans": [],
  79. "contexts": {"trace": {"op": "foobar", "trace_id": "a" * 32, "span_id": "a" * 16}},
  80. "tags": {"important": "yes"},
  81. "timestamp": iso_format(before_now(minutes=1)),
  82. "start_timestamp": iso_format(before_now(minutes=1, seconds=3)),
  83. }
  84. self.store_event(data=data, project_id=self.project.id)
  85. url = reverse(
  86. "sentry-api-0-organization-events-meta",
  87. kwargs={"organization_id_or_slug": self.project.organization.slug},
  88. )
  89. with self.feature(self.features):
  90. response = self.client.get(url, {"query": "transaction.duration:>1"}, format="json")
  91. assert response.status_code == 200, response.content
  92. assert response.data["count"] == 1
  93. def test_generic_event(self):
  94. """Test that the issuePlatform dataset returns data for a generic issue's short ID"""
  95. _, _, group_info = self.store_search_issue(
  96. self.project.id,
  97. self.user.id,
  98. [f"{ProfileFileIOGroupType.type_id}-group1"],
  99. "prod",
  100. before_now(hours=1),
  101. )
  102. assert group_info is not None
  103. url = reverse(
  104. "sentry-api-0-organization-events-meta",
  105. kwargs={"organization_id_or_slug": self.project.organization.slug},
  106. )
  107. with self.feature(self.features):
  108. response = self.client.get(
  109. url,
  110. {
  111. "query": f"issue:{group_info.group.qualified_short_id}",
  112. "dataset": "issuePlatform",
  113. },
  114. format="json",
  115. )
  116. assert response.status_code == 200, response.content
  117. assert response.data["count"] == 1
  118. def test_errors_dataset_event(self):
  119. """Test that the errors dataset returns data for an issue's short ID"""
  120. with self.options({"issues.group_attributes.send_kafka": True}):
  121. group_1 = self.store_event(
  122. data={"timestamp": iso_format(self.min_ago)}, project_id=self.project.id
  123. ).group
  124. url = reverse(
  125. "sentry-api-0-organization-events-meta",
  126. kwargs={"organization_id_or_slug": self.project.organization.slug},
  127. )
  128. with self.feature(self.features):
  129. response = self.client.get(
  130. url,
  131. {
  132. "query": f"issue:{group_1.qualified_short_id} is:unresolved",
  133. "dataset": "errors",
  134. },
  135. format="json",
  136. )
  137. assert response.status_code == 200, response.content
  138. assert response.data["count"] == 1
  139. def test_transaction_event_with_last_seen(self):
  140. data = {
  141. "event_id": "a" * 32,
  142. "type": "transaction",
  143. "transaction": "api.issue.delete",
  144. "spans": [],
  145. "contexts": {"trace": {"op": "foobar", "trace_id": "a" * 32, "span_id": "a" * 16}},
  146. "tags": {"important": "yes"},
  147. "timestamp": iso_format(before_now(minutes=1)),
  148. "start_timestamp": iso_format(before_now(minutes=1, seconds=3)),
  149. }
  150. self.store_event(data=data, project_id=self.project.id)
  151. with self.feature(self.features):
  152. response = self.client.get(
  153. self.url, {"query": "event.type:transaction last_seen():>2012-12-31"}, format="json"
  154. )
  155. assert response.status_code == 200, response.content
  156. assert response.data["count"] == 1
  157. def test_out_of_retention(self):
  158. with self.feature(self.features):
  159. with self.options({"system.event-retention-days": 10}):
  160. response = self.client.get(
  161. self.url,
  162. format="json",
  163. data={
  164. "start": iso_format(before_now(days=20)),
  165. "end": iso_format(before_now(days=15)),
  166. },
  167. )
  168. assert response.status_code == 400
  169. @mock.patch("sentry.search.events.builder.discover.raw_snql_query")
  170. def test_handling_snuba_errors(self, mock_snql_query):
  171. mock_snql_query.side_effect = ParseError("test")
  172. with self.feature(self.features):
  173. response = self.client.get(self.url, format="json")
  174. assert response.status_code == 400, response.content
  175. @mock.patch("sentry.utils.snuba.quantize_time")
  176. def test_quantize_dates(self, mock_quantize):
  177. mock_quantize.return_value = before_now(days=1)
  178. with self.feature(self.features):
  179. # Don't quantize short time periods
  180. self.client.get(
  181. self.url,
  182. format="json",
  183. data={"statsPeriod": "1h", "query": "", "field": ["id", "timestamp"]},
  184. )
  185. # Don't quantize absolute date periods
  186. self.client.get(
  187. self.url,
  188. format="json",
  189. data={
  190. "start": iso_format(before_now(days=20)),
  191. "end": iso_format(before_now(days=15)),
  192. "query": "",
  193. "field": ["id", "timestamp"],
  194. },
  195. )
  196. assert len(mock_quantize.mock_calls) == 0
  197. # Quantize long date periods
  198. self.client.get(
  199. self.url,
  200. format="json",
  201. data={"field": ["id", "timestamp"], "statsPeriod": "90d", "query": ""},
  202. )
  203. assert len(mock_quantize.mock_calls) == 2
  204. class OrganizationEventsRelatedIssuesEndpoint(APITestCase, SnubaTestCase):
  205. def setUp(self):
  206. super().setUp()
  207. def test_find_related_issue(self):
  208. self.login_as(user=self.user)
  209. project = self.create_project()
  210. event1 = self.store_event(
  211. data={"timestamp": iso_format(before_now(minutes=1)), "transaction": "/beth/sanchez"},
  212. project_id=project.id,
  213. )
  214. url = reverse(
  215. "sentry-api-0-organization-related-issues",
  216. kwargs={"organization_id_or_slug": project.organization.slug},
  217. )
  218. response = self.client.get(url, {"transaction": "/beth/sanchez"}, format="json")
  219. assert response.status_code == 200, response.content
  220. assert len(response.data) == 1
  221. assert response.data[0]["shortId"] == event1.group.qualified_short_id
  222. assert int(response.data[0]["id"]) == event1.group_id
  223. def test_related_issues_no_transaction(self):
  224. self.login_as(user=self.user)
  225. project = self.create_project()
  226. self.store_event(
  227. data={"timestamp": iso_format(before_now(minutes=1)), "transaction": "/beth/sanchez"},
  228. project_id=project.id,
  229. )
  230. url = reverse(
  231. "sentry-api-0-organization-related-issues",
  232. kwargs={"organization_id_or_slug": project.organization.slug},
  233. )
  234. response = self.client.get(url, format="json")
  235. assert response.status_code == 400, response.content
  236. assert (
  237. response.data["detail"]
  238. == "Must provide one of ['transaction'] in order to find related events"
  239. )
  240. def test_related_issues_no_matching_groups(self):
  241. self.login_as(user=self.user)
  242. project = self.create_project()
  243. self.store_event(
  244. data={"timestamp": iso_format(before_now(minutes=1)), "transaction": "/beth/sanchez"},
  245. project_id=project.id,
  246. )
  247. url = reverse(
  248. "sentry-api-0-organization-related-issues",
  249. kwargs={"organization_id_or_slug": project.organization.slug},
  250. )
  251. response = self.client.get(url, {"transaction": "/morty/sanchez"}, format="json")
  252. assert response.status_code == 200, response.content
  253. assert len(response.data) == 0
  254. def test_related_issues_only_issues_in_date(self):
  255. self.login_as(user=self.user)
  256. project = self.create_project()
  257. self.store_event(
  258. data={
  259. "event_id": "a" * 32,
  260. "timestamp": iso_format(before_now(days=2)),
  261. "transaction": "/beth/sanchez",
  262. },
  263. project_id=project.id,
  264. )
  265. event2 = self.store_event(
  266. data={
  267. "event_id": "b" * 32,
  268. "timestamp": iso_format(before_now(minutes=1)),
  269. "transaction": "/beth/sanchez",
  270. },
  271. project_id=project.id,
  272. )
  273. url = reverse(
  274. "sentry-api-0-organization-related-issues",
  275. kwargs={"organization_id_or_slug": project.organization.slug},
  276. )
  277. response = self.client.get(
  278. url, {"transaction": "/beth/sanchez", "statsPeriod": "24h"}, format="json"
  279. )
  280. assert response.status_code == 200, response.content
  281. assert len(response.data) == 1
  282. assert response.data[0]["shortId"] == event2.group.qualified_short_id
  283. assert int(response.data[0]["id"]) == event2.group_id
  284. def test_related_issues_transactions_from_different_projects(self):
  285. self.login_as(user=self.user)
  286. project1 = self.create_project()
  287. project2 = self.create_project()
  288. event1 = self.store_event(
  289. data={
  290. "event_id": "a" * 32,
  291. "timestamp": iso_format(before_now(minutes=1)),
  292. "transaction": "/beth/sanchez",
  293. },
  294. project_id=project1.id,
  295. )
  296. self.store_event(
  297. data={
  298. "event_id": "b" * 32,
  299. "timestamp": iso_format(before_now(minutes=1)),
  300. "transaction": "/beth/sanchez",
  301. },
  302. project_id=project2.id,
  303. )
  304. url = reverse(
  305. "sentry-api-0-organization-related-issues",
  306. kwargs={"organization_id_or_slug": project1.organization.slug},
  307. )
  308. response = self.client.get(
  309. url,
  310. {"transaction": "/beth/sanchez", "project": project1.id},
  311. format="json",
  312. )
  313. assert response.status_code == 200, response.content
  314. assert len(response.data) == 1
  315. assert response.data[0]["shortId"] == event1.group.qualified_short_id
  316. assert int(response.data[0]["id"]) == event1.group_id
  317. def test_related_issues_transactions_with_quotes(self):
  318. self.login_as(user=self.user)
  319. project = self.create_project()
  320. event = self.store_event(
  321. data={
  322. "event_id": "a" * 32,
  323. "timestamp": iso_format(before_now(minutes=1)),
  324. "transaction": '/beth/"sanchez"',
  325. },
  326. project_id=project.id,
  327. )
  328. url = reverse(
  329. "sentry-api-0-organization-related-issues",
  330. kwargs={"organization_id_or_slug": project.organization.slug},
  331. )
  332. response = self.client.get(
  333. url,
  334. {"transaction": '/beth/"sanchez"', "project": project.id},
  335. format="json",
  336. )
  337. assert response.status_code == 200, response.content
  338. assert len(response.data) == 1
  339. assert response.data[0]["shortId"] == event.group.qualified_short_id
  340. assert int(response.data[0]["id"]) == event.group_id
  341. url = reverse(
  342. "sentry-api-0-organization-related-issues",
  343. kwargs={"organization_id_or_slug": project.organization.slug},
  344. )
  345. response = self.client.get(
  346. url,
  347. {"transaction": '/beth/\\"sanchez\\"', "project": project.id},
  348. format="json",
  349. )
  350. assert response.status_code == 200, response.content
  351. assert len(response.data) == 1
  352. assert response.data[0]["shortId"] == event.group.qualified_short_id
  353. assert int(response.data[0]["id"]) == event.group_id
  354. class OrganizationSpansSamplesEndpoint(APITestCase, SnubaTestCase):
  355. url_name = "sentry-api-0-organization-spans-samples"
  356. @mock.patch("sentry.search.events.builder.discover.raw_snql_query")
  357. def test_is_segment_properly_converted_in_filter(self, mock_raw_snql_query):
  358. self.login_as(user=self.user)
  359. project = self.create_project()
  360. url = reverse(self.url_name, kwargs={"organization_id_or_slug": project.organization.slug})
  361. response = self.client.get(
  362. url,
  363. {
  364. "query": "span.is_segment:1 transaction:api/0/foo",
  365. "lowerBound": "0",
  366. "firstBound": "10",
  367. "secondBound": "20",
  368. "upperBound": "200",
  369. "column": "span.duration",
  370. },
  371. format="json",
  372. extra={"project": [project.id]},
  373. )
  374. assert response.status_code == 200, response.content
  375. # the SQL should have is_segment converted into an int for all requests
  376. assert all(
  377. "is_segment = 1" in call_args[0][0].serialize()
  378. for call_args in mock_raw_snql_query.call_args_list
  379. )