test_organization_events_meta.py 16 KB

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