test_organization_events_meta.py 19 KB

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