test_organization_events_meta.py 17 KB

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