test_organization_events_meta.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 import override_options
  8. from sentry.testutils.helpers.datetime import before_now, iso_format
  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(
  55. self.url, {"query": "is:unresolved priority:[high, medium]"}, format="json"
  56. )
  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_id_or_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_id_or_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),
  96. )
  97. assert group_info is not None
  98. url = reverse(
  99. "sentry-api-0-organization-events-meta",
  100. kwargs={"organization_id_or_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_id_or_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.base.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)
  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. class OrganizationEventsRelatedIssuesEndpoint(APITestCase, SnubaTestCase):
  200. def setUp(self):
  201. super().setUp()
  202. def test_find_related_issue(self):
  203. self.login_as(user=self.user)
  204. project = self.create_project()
  205. event1 = self.store_event(
  206. data={"timestamp": iso_format(before_now(minutes=1)), "transaction": "/beth/sanchez"},
  207. project_id=project.id,
  208. )
  209. url = reverse(
  210. "sentry-api-0-organization-related-issues",
  211. kwargs={"organization_id_or_slug": project.organization.slug},
  212. )
  213. response = self.client.get(url, {"transaction": "/beth/sanchez"}, format="json")
  214. assert response.status_code == 200, response.content
  215. assert len(response.data) == 1
  216. assert response.data[0]["shortId"] == event1.group.qualified_short_id
  217. assert int(response.data[0]["id"]) == event1.group_id
  218. def test_related_issues_no_transaction(self):
  219. self.login_as(user=self.user)
  220. project = self.create_project()
  221. self.store_event(
  222. data={"timestamp": iso_format(before_now(minutes=1)), "transaction": "/beth/sanchez"},
  223. project_id=project.id,
  224. )
  225. url = reverse(
  226. "sentry-api-0-organization-related-issues",
  227. kwargs={"organization_id_or_slug": project.organization.slug},
  228. )
  229. response = self.client.get(url, format="json")
  230. assert response.status_code == 400, response.content
  231. assert (
  232. response.data["detail"]
  233. == "Must provide one of ['transaction'] in order to find related events"
  234. )
  235. def test_related_issues_no_matching_groups(self):
  236. self.login_as(user=self.user)
  237. project = self.create_project()
  238. self.store_event(
  239. data={"timestamp": iso_format(before_now(minutes=1)), "transaction": "/beth/sanchez"},
  240. project_id=project.id,
  241. )
  242. url = reverse(
  243. "sentry-api-0-organization-related-issues",
  244. kwargs={"organization_id_or_slug": project.organization.slug},
  245. )
  246. response = self.client.get(url, {"transaction": "/morty/sanchez"}, format="json")
  247. assert response.status_code == 200, response.content
  248. assert len(response.data) == 0
  249. def test_related_issues_only_issues_in_date(self):
  250. self.login_as(user=self.user)
  251. project = self.create_project()
  252. self.store_event(
  253. data={
  254. "event_id": "a" * 32,
  255. "timestamp": iso_format(before_now(days=2)),
  256. "transaction": "/beth/sanchez",
  257. },
  258. project_id=project.id,
  259. )
  260. event2 = self.store_event(
  261. data={
  262. "event_id": "b" * 32,
  263. "timestamp": iso_format(before_now(minutes=1)),
  264. "transaction": "/beth/sanchez",
  265. },
  266. project_id=project.id,
  267. )
  268. url = reverse(
  269. "sentry-api-0-organization-related-issues",
  270. kwargs={"organization_id_or_slug": project.organization.slug},
  271. )
  272. response = self.client.get(
  273. url, {"transaction": "/beth/sanchez", "statsPeriod": "24h"}, format="json"
  274. )
  275. assert response.status_code == 200, response.content
  276. assert len(response.data) == 1
  277. assert response.data[0]["shortId"] == event2.group.qualified_short_id
  278. assert int(response.data[0]["id"]) == event2.group_id
  279. def test_related_issues_transactions_from_different_projects(self):
  280. self.login_as(user=self.user)
  281. project1 = self.create_project()
  282. project2 = self.create_project()
  283. event1 = self.store_event(
  284. data={
  285. "event_id": "a" * 32,
  286. "timestamp": iso_format(before_now(minutes=1)),
  287. "transaction": "/beth/sanchez",
  288. },
  289. project_id=project1.id,
  290. )
  291. self.store_event(
  292. data={
  293. "event_id": "b" * 32,
  294. "timestamp": iso_format(before_now(minutes=1)),
  295. "transaction": "/beth/sanchez",
  296. },
  297. project_id=project2.id,
  298. )
  299. url = reverse(
  300. "sentry-api-0-organization-related-issues",
  301. kwargs={"organization_id_or_slug": project1.organization.slug},
  302. )
  303. response = self.client.get(
  304. url,
  305. {"transaction": "/beth/sanchez", "project": str(project1.id)},
  306. format="json",
  307. )
  308. assert response.status_code == 200, response.content
  309. assert len(response.data) == 1
  310. assert response.data[0]["shortId"] == event1.group.qualified_short_id
  311. assert int(response.data[0]["id"]) == event1.group_id
  312. def test_related_issues_transactions_with_quotes(self):
  313. self.login_as(user=self.user)
  314. project = self.create_project()
  315. event = self.store_event(
  316. data={
  317. "event_id": "a" * 32,
  318. "timestamp": iso_format(before_now(minutes=1)),
  319. "transaction": '/beth/"sanchez"',
  320. },
  321. project_id=project.id,
  322. )
  323. url = reverse(
  324. "sentry-api-0-organization-related-issues",
  325. kwargs={"organization_id_or_slug": project.organization.slug},
  326. )
  327. response = self.client.get(
  328. url,
  329. {"transaction": '/beth/"sanchez"', "project": str(project.id)},
  330. format="json",
  331. )
  332. assert response.status_code == 200, response.content
  333. assert len(response.data) == 1
  334. assert response.data[0]["shortId"] == event.group.qualified_short_id
  335. assert int(response.data[0]["id"]) == event.group_id
  336. url = reverse(
  337. "sentry-api-0-organization-related-issues",
  338. kwargs={"organization_id_or_slug": project.organization.slug},
  339. )
  340. response = self.client.get(
  341. url,
  342. {"transaction": '/beth/\\"sanchez\\"', "project": str(project.id)},
  343. format="json",
  344. )
  345. assert response.status_code == 200, response.content
  346. assert len(response.data) == 1
  347. assert response.data[0]["shortId"] == event.group.qualified_short_id
  348. assert int(response.data[0]["id"]) == event.group_id
  349. class OrganizationSpansSamplesEndpoint(APITestCase, SnubaTestCase):
  350. url_name = "sentry-api-0-organization-spans-samples"
  351. @mock.patch("sentry.search.events.builder.base.raw_snql_query")
  352. def test_is_segment_properly_converted_in_filter(self, mock_raw_snql_query):
  353. self.login_as(user=self.user)
  354. project = self.create_project()
  355. url = reverse(self.url_name, kwargs={"organization_id_or_slug": project.organization.slug})
  356. response = self.client.get(
  357. url,
  358. {
  359. "query": "span.is_segment:1 transaction:api/0/foo",
  360. "lowerBound": "0",
  361. "firstBound": "10",
  362. "secondBound": "20",
  363. "upperBound": "200",
  364. "column": "span.duration",
  365. },
  366. format="json",
  367. extra={"project": [project.id]},
  368. )
  369. assert response.status_code == 200, response.content
  370. # the SQL should have is_segment converted into an int for all requests
  371. assert all(
  372. "is_segment = 1" in call_args[0][0].serialize()
  373. for call_args in mock_raw_snql_query.call_args_list
  374. )
  375. def test_is_using_sample_rate(self):
  376. self.login_as(user=self.user)
  377. project = self.create_project()
  378. url = reverse(self.url_name, kwargs={"organization_id_or_slug": project.organization.slug})
  379. def request():
  380. return self.client.get(
  381. url,
  382. {
  383. "query": "span.is_segment:1 transaction:api/0/foo",
  384. "lowerBound": "0",
  385. "firstBound": "10",
  386. "secondBound": "20",
  387. "upperBound": "200",
  388. "column": "span.duration",
  389. },
  390. format="json",
  391. extra={"project": [project.id]},
  392. )
  393. response = request()
  394. assert response.status_code == 200, response.content
  395. with mock.patch("sentry.search.events.builder.base.raw_snql_query") as mock_raw_snql_query:
  396. response = request()
  397. assert response.status_code == 200, response.content
  398. assert "MATCH (spans)" in mock_raw_snql_query.call_args_list[0][0][0].serialize()
  399. with (
  400. override_options({"insights.span-samples-query.sample-rate": 100_000_000.0}),
  401. mock.patch("sentry.search.events.builder.base.raw_snql_query") as mock_raw_snql_query,
  402. ):
  403. response = request()
  404. assert response.status_code == 200, response.content
  405. assert (
  406. "MATCH (spans SAMPLE 100000000.0)"
  407. in mock_raw_snql_query.call_args_list[0][0][0].serialize()
  408. )