test_organization_events_meta.py 16 KB

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