test_organization_events_meta.py 16 KB

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