test_organization_events_meta.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. from unittest import mock
  2. import pytest
  3. from django.urls import reverse
  4. from pytz import utc
  5. from rest_framework.exceptions import ParseError
  6. from sentry.testutils import APITestCase, MetricsEnhancedPerformanceTestCase, SnubaTestCase
  7. from sentry.testutils.helpers.datetime import before_now, iso_format
  8. pytestmark = pytest.mark.sentry_metrics
  9. class OrganizationEventsMetaEndpoint(APITestCase, SnubaTestCase):
  10. def setUp(self):
  11. super().setUp()
  12. self.min_ago = before_now(minutes=1)
  13. self.login_as(user=self.user)
  14. self.project = self.create_project()
  15. self.url = reverse(
  16. "sentry-api-0-organization-events-meta",
  17. kwargs={"organization_slug": self.project.organization.slug},
  18. )
  19. self.features = {"organizations:discover-basic": True}
  20. def test_simple(self):
  21. self.store_event(data={"timestamp": iso_format(self.min_ago)}, project_id=self.project.id)
  22. with self.feature(self.features):
  23. response = self.client.get(self.url, format="json")
  24. assert response.status_code == 200, response.content
  25. assert response.data["count"] == 1
  26. def test_multiple_projects(self):
  27. project2 = self.create_project()
  28. self.store_event(data={"timestamp": iso_format(self.min_ago)}, project_id=self.project.id)
  29. self.store_event(data={"timestamp": iso_format(self.min_ago)}, project_id=project2.id)
  30. response = self.client.get(self.url, format="json")
  31. assert response.status_code == 400, response.content
  32. self.features["organizations:global-views"] = True
  33. with self.feature(self.features):
  34. response = self.client.get(self.url, format="json")
  35. assert response.status_code == 200, response.content
  36. assert response.data["count"] == 2
  37. def test_search(self):
  38. self.store_event(
  39. data={"timestamp": iso_format(self.min_ago), "message": "how to make fast"},
  40. project_id=self.project.id,
  41. )
  42. self.store_event(
  43. data={"timestamp": iso_format(self.min_ago), "message": "Delete the Data"},
  44. project_id=self.project.id,
  45. )
  46. with self.feature(self.features):
  47. response = self.client.get(self.url, {"query": "delete"}, format="json")
  48. assert response.status_code == 200, response.content
  49. assert response.data["count"] == 1
  50. def test_invalid_query(self):
  51. with self.feature(self.features):
  52. response = self.client.get(self.url, {"query": "is:unresolved"}, format="json")
  53. assert response.status_code == 400, response.content
  54. def test_no_projects(self):
  55. no_project_org = self.create_organization(owner=self.user)
  56. url = reverse(
  57. "sentry-api-0-organization-events-meta",
  58. kwargs={"organization_slug": no_project_org.slug},
  59. )
  60. with self.feature(self.features):
  61. response = self.client.get(url, format="json")
  62. assert response.status_code == 200, response.content
  63. assert response.data["count"] == 0
  64. def test_transaction_event(self):
  65. data = {
  66. "event_id": "a" * 32,
  67. "type": "transaction",
  68. "transaction": "api.issue.delete",
  69. "spans": [],
  70. "contexts": {"trace": {"op": "foobar", "trace_id": "a" * 32, "span_id": "a" * 16}},
  71. "tags": {"important": "yes"},
  72. "timestamp": iso_format(before_now(minutes=1)),
  73. "start_timestamp": iso_format(before_now(minutes=1, seconds=3)),
  74. }
  75. self.store_event(data=data, project_id=self.project.id)
  76. url = reverse(
  77. "sentry-api-0-organization-events-meta",
  78. kwargs={"organization_slug": self.project.organization.slug},
  79. )
  80. with self.feature(self.features):
  81. response = self.client.get(url, {"query": "transaction.duration:>1"}, format="json")
  82. assert response.status_code == 200, response.content
  83. assert response.data["count"] == 1
  84. def test_transaction_event_with_last_seen(self):
  85. data = {
  86. "event_id": "a" * 32,
  87. "type": "transaction",
  88. "transaction": "api.issue.delete",
  89. "spans": [],
  90. "contexts": {"trace": {"op": "foobar", "trace_id": "a" * 32, "span_id": "a" * 16}},
  91. "tags": {"important": "yes"},
  92. "timestamp": iso_format(before_now(minutes=1)),
  93. "start_timestamp": iso_format(before_now(minutes=1, seconds=3)),
  94. }
  95. self.store_event(data=data, project_id=self.project.id)
  96. with self.feature(self.features):
  97. response = self.client.get(
  98. self.url, {"query": "event.type:transaction last_seen():>2012-12-31"}, format="json"
  99. )
  100. assert response.status_code == 200, response.content
  101. assert response.data["count"] == 1
  102. def test_out_of_retention(self):
  103. with self.feature(self.features):
  104. with self.options({"system.event-retention-days": 10}):
  105. response = self.client.get(
  106. self.url,
  107. format="json",
  108. data={
  109. "start": iso_format(before_now(days=20)),
  110. "end": iso_format(before_now(days=15)),
  111. },
  112. )
  113. assert response.status_code == 400
  114. @mock.patch("sentry.search.events.builder.raw_snql_query")
  115. def test_handling_snuba_errors(self, mock_snql_query):
  116. mock_snql_query.side_effect = ParseError("test")
  117. with self.feature(self.features):
  118. response = self.client.get(self.url, format="json")
  119. assert response.status_code == 400, response.content
  120. @mock.patch("sentry.utils.snuba.quantize_time")
  121. def test_quantize_dates(self, mock_quantize):
  122. mock_quantize.return_value = before_now(days=1).replace(tzinfo=utc)
  123. with self.feature(self.features):
  124. # Don't quantize short time periods
  125. self.client.get(
  126. self.url,
  127. format="json",
  128. data={"statsPeriod": "1h", "query": "", "field": ["id", "timestamp"]},
  129. )
  130. # Don't quantize absolute date periods
  131. self.client.get(
  132. self.url,
  133. format="json",
  134. data={
  135. "start": iso_format(before_now(days=20)),
  136. "end": iso_format(before_now(days=15)),
  137. "query": "",
  138. "field": ["id", "timestamp"],
  139. },
  140. )
  141. assert len(mock_quantize.mock_calls) == 0
  142. # Quantize long date periods
  143. self.client.get(
  144. self.url,
  145. format="json",
  146. data={"field": ["id", "timestamp"], "statsPeriod": "90d", "query": ""},
  147. )
  148. assert len(mock_quantize.mock_calls) == 2
  149. class OrganizationEventsRelatedIssuesEndpoint(APITestCase, SnubaTestCase):
  150. def setUp(self):
  151. super().setUp()
  152. def test_find_related_issue(self):
  153. self.login_as(user=self.user)
  154. project = self.create_project()
  155. event1 = self.store_event(
  156. data={"timestamp": iso_format(before_now(minutes=1)), "transaction": "/beth/sanchez"},
  157. project_id=project.id,
  158. )
  159. url = reverse(
  160. "sentry-api-0-organization-related-issues",
  161. kwargs={"organization_slug": project.organization.slug},
  162. )
  163. response = self.client.get(url, {"transaction": "/beth/sanchez"}, format="json")
  164. assert response.status_code == 200, response.content
  165. assert len(response.data) == 1
  166. assert response.data[0]["shortId"] == event1.group.qualified_short_id
  167. assert int(response.data[0]["id"]) == event1.group_id
  168. def test_related_issues_no_transaction(self):
  169. self.login_as(user=self.user)
  170. project = self.create_project()
  171. self.store_event(
  172. data={"timestamp": iso_format(before_now(minutes=1)), "transaction": "/beth/sanchez"},
  173. project_id=project.id,
  174. )
  175. url = reverse(
  176. "sentry-api-0-organization-related-issues",
  177. kwargs={"organization_slug": project.organization.slug},
  178. )
  179. response = self.client.get(url, format="json")
  180. assert response.status_code == 400, response.content
  181. assert (
  182. response.data["detail"]
  183. == "Must provide one of ['transaction'] in order to find related events"
  184. )
  185. def test_related_issues_no_matching_groups(self):
  186. self.login_as(user=self.user)
  187. project = self.create_project()
  188. self.store_event(
  189. data={"timestamp": iso_format(before_now(minutes=1)), "transaction": "/beth/sanchez"},
  190. project_id=project.id,
  191. )
  192. url = reverse(
  193. "sentry-api-0-organization-related-issues",
  194. kwargs={"organization_slug": project.organization.slug},
  195. )
  196. response = self.client.get(url, {"transaction": "/morty/sanchez"}, format="json")
  197. assert response.status_code == 200, response.content
  198. assert len(response.data) == 0
  199. def test_related_issues_only_issues_in_date(self):
  200. self.login_as(user=self.user)
  201. project = self.create_project()
  202. self.store_event(
  203. data={
  204. "event_id": "a" * 32,
  205. "timestamp": iso_format(before_now(days=2)),
  206. "transaction": "/beth/sanchez",
  207. },
  208. project_id=project.id,
  209. )
  210. event2 = self.store_event(
  211. data={
  212. "event_id": "b" * 32,
  213. "timestamp": iso_format(before_now(minutes=1)),
  214. "transaction": "/beth/sanchez",
  215. },
  216. project_id=project.id,
  217. )
  218. url = reverse(
  219. "sentry-api-0-organization-related-issues",
  220. kwargs={"organization_slug": project.organization.slug},
  221. )
  222. response = self.client.get(
  223. url, {"transaction": "/beth/sanchez", "statsPeriod": "24h"}, format="json"
  224. )
  225. assert response.status_code == 200, response.content
  226. assert len(response.data) == 1
  227. assert response.data[0]["shortId"] == event2.group.qualified_short_id
  228. assert int(response.data[0]["id"]) == event2.group_id
  229. def test_related_issues_transactions_from_different_projects(self):
  230. self.login_as(user=self.user)
  231. project1 = self.create_project()
  232. project2 = self.create_project()
  233. event1 = self.store_event(
  234. data={
  235. "event_id": "a" * 32,
  236. "timestamp": iso_format(before_now(minutes=1)),
  237. "transaction": "/beth/sanchez",
  238. },
  239. project_id=project1.id,
  240. )
  241. self.store_event(
  242. data={
  243. "event_id": "b" * 32,
  244. "timestamp": iso_format(before_now(minutes=1)),
  245. "transaction": "/beth/sanchez",
  246. },
  247. project_id=project2.id,
  248. )
  249. url = reverse(
  250. "sentry-api-0-organization-related-issues",
  251. kwargs={"organization_slug": project1.organization.slug},
  252. )
  253. response = self.client.get(
  254. url,
  255. {"transaction": "/beth/sanchez", "project": project1.id},
  256. format="json",
  257. )
  258. assert response.status_code == 200, response.content
  259. assert len(response.data) == 1
  260. assert response.data[0]["shortId"] == event1.group.qualified_short_id
  261. assert int(response.data[0]["id"]) == event1.group_id
  262. def test_related_issues_transactions_with_quotes(self):
  263. self.login_as(user=self.user)
  264. project = self.create_project()
  265. event = self.store_event(
  266. data={
  267. "event_id": "a" * 32,
  268. "timestamp": iso_format(before_now(minutes=1)),
  269. "transaction": '/beth/"sanchez"',
  270. },
  271. project_id=project.id,
  272. )
  273. url = reverse(
  274. "sentry-api-0-organization-related-issues",
  275. kwargs={"organization_slug": project.organization.slug},
  276. )
  277. response = self.client.get(
  278. url,
  279. {"transaction": '/beth/"sanchez"', "project": project.id},
  280. format="json",
  281. )
  282. assert response.status_code == 200, response.content
  283. assert len(response.data) == 1
  284. assert response.data[0]["shortId"] == event.group.qualified_short_id
  285. assert int(response.data[0]["id"]) == event.group_id
  286. url = reverse(
  287. "sentry-api-0-organization-related-issues",
  288. kwargs={"organization_slug": project.organization.slug},
  289. )
  290. response = self.client.get(
  291. url,
  292. {"transaction": '/beth/\\"sanchez\\"', "project": project.id},
  293. format="json",
  294. )
  295. assert response.status_code == 200, response.content
  296. assert len(response.data) == 1
  297. assert response.data[0]["shortId"] == event.group.qualified_short_id
  298. assert int(response.data[0]["id"]) == event.group_id
  299. class OrganizationEventsMetricsCompatiblity(MetricsEnhancedPerformanceTestCase):
  300. def setUp(self):
  301. super().setUp()
  302. self.min_ago = before_now(minutes=1)
  303. self.two_min_ago = before_now(minutes=2)
  304. self.features = {
  305. "organizations:performance-use-metrics": True,
  306. }
  307. self.login_as(user=self.user)
  308. self.project.update_option("sentry:dynamic_sampling", "something-it-doesn't-matter")
  309. # Don't create any txn on this, don't set its DS rules, it shouldn't show up anywhere
  310. self.create_project()
  311. def test_unparameterized_transactions(self):
  312. # Make current project incompatible
  313. self.store_transaction_metric(
  314. 1, tags={"transaction": "<< unparameterized >>"}, timestamp=self.min_ago
  315. )
  316. url = reverse(
  317. "sentry-api-0-organization-events-metrics-compatibility",
  318. kwargs={"organization_slug": self.project.organization.slug},
  319. )
  320. response = self.client.get(url, format="json")
  321. assert response.status_code == 200, response.content
  322. assert response.data["compatible_projects"] == []
  323. assert response.data["dynamic_sampling_projects"] == [self.project.id]
  324. assert response.data["sum"]["metrics"] == 1
  325. assert response.data["sum"]["metrics_unparam"] == 1
  326. assert response.data["sum"]["metrics_null"] == 0
  327. def test_null_transaction(self):
  328. # Make current project incompatible
  329. self.store_transaction_metric(1, tags={}, timestamp=self.min_ago)
  330. url = reverse(
  331. "sentry-api-0-organization-events-metrics-compatibility",
  332. kwargs={"organization_slug": self.project.organization.slug},
  333. )
  334. response = self.client.get(url, format="json")
  335. assert response.status_code == 200, response.content
  336. assert response.data["compatible_projects"] == []
  337. assert response.data["dynamic_sampling_projects"] == [self.project.id]
  338. assert response.data["sum"]["metrics"] == 1
  339. assert response.data["sum"]["metrics_unparam"] == 0
  340. assert response.data["sum"]["metrics_null"] == 1
  341. def test_no_transaction(self):
  342. # Make current project incompatible by having nothing
  343. url = reverse(
  344. "sentry-api-0-organization-events-metrics-compatibility",
  345. kwargs={"organization_slug": self.project.organization.slug},
  346. )
  347. response = self.client.get(url, format="json")
  348. assert response.status_code == 200, response.content
  349. assert response.data["compatible_projects"] == []
  350. assert response.data["dynamic_sampling_projects"] == [self.project.id]
  351. assert response.data["sum"]["metrics"] == 0
  352. assert response.data["sum"]["metrics_unparam"] == 0
  353. assert response.data["sum"]["metrics_null"] == 0
  354. def test_has_transaction(self):
  355. self.store_transaction_metric(
  356. 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago
  357. )
  358. url = reverse(
  359. "sentry-api-0-organization-events-metrics-compatibility",
  360. kwargs={"organization_slug": self.project.organization.slug},
  361. )
  362. response = self.client.get(url, format="json")
  363. assert response.status_code == 200, response.content
  364. assert response.data["compatible_projects"] == [self.project.id]
  365. assert response.data["dynamic_sampling_projects"] == [self.project.id]
  366. assert response.data["sum"]["metrics"] == 1
  367. assert response.data["sum"]["metrics_unparam"] == 0
  368. assert response.data["sum"]["metrics_null"] == 0
  369. def test_multiple_projects(self):
  370. project2 = self.create_project()
  371. project2.update_option("sentry:dynamic_sampling", "something-it-doesn't-matter")
  372. project3 = self.create_project()
  373. project3.update_option("sentry:dynamic_sampling", "something-it-doesn't-matter")
  374. # Not setting DS, it shouldn't show up
  375. project4 = self.create_project()
  376. self.store_transaction_metric(
  377. 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago
  378. )
  379. self.store_transaction_metric(
  380. 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago, project=project4.id
  381. )
  382. self.store_transaction_metric(
  383. 1,
  384. tags={"transaction": "<< unparameterized >>"},
  385. timestamp=self.min_ago,
  386. project=project2.id,
  387. )
  388. self.store_transaction_metric(
  389. 1,
  390. tags={},
  391. timestamp=self.min_ago,
  392. project=project3.id,
  393. )
  394. self.store_event(
  395. data={"timestamp": iso_format(self.min_ago), "transaction": "foo_transaction"},
  396. project_id=self.project.id,
  397. )
  398. url = reverse(
  399. "sentry-api-0-organization-events-metrics-compatibility",
  400. kwargs={"organization_slug": self.project.organization.slug},
  401. )
  402. response = self.client.get(url, format="json")
  403. assert response.status_code == 200, response.content
  404. assert response.data["compatible_projects"] == [self.project.id]
  405. assert response.data["dynamic_sampling_projects"] == [
  406. self.project.id,
  407. project2.id,
  408. project3.id,
  409. ]
  410. # project 4 shouldn't show up in these sums
  411. assert response.data["sum"]["metrics"] == 3
  412. assert response.data["sum"]["metrics_unparam"] == 1
  413. assert response.data["sum"]["metrics_null"] == 1