test_organization_events_meta.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. from __future__ import absolute_import
  2. import mock
  3. from pytz import utc
  4. from rest_framework.exceptions import ParseError
  5. from django.core.urlresolvers import reverse
  6. from sentry.testutils import APITestCase, SnubaTestCase
  7. from sentry.testutils.helpers.datetime import before_now, iso_format
  8. class OrganizationEventsMetaEndpoint(APITestCase, SnubaTestCase):
  9. def setUp(self):
  10. super(OrganizationEventsMetaEndpoint, self).setUp()
  11. self.min_ago = before_now(minutes=1)
  12. self.login_as(user=self.user)
  13. self.project = self.create_project()
  14. self.url = reverse(
  15. "sentry-api-0-organization-events-meta",
  16. kwargs={"organization_slug": self.project.organization.slug},
  17. )
  18. def test_simple(self):
  19. project2 = self.create_project()
  20. self.store_event(data={"timestamp": iso_format(self.min_ago)}, project_id=self.project.id)
  21. self.store_event(data={"timestamp": iso_format(self.min_ago)}, project_id=project2.id)
  22. response = self.client.get(self.url, format="json")
  23. assert response.status_code == 200, response.content
  24. assert response.data["count"] == 2
  25. def test_search(self):
  26. self.store_event(
  27. data={"timestamp": iso_format(self.min_ago), "message": "how to make fast"},
  28. project_id=self.project.id,
  29. )
  30. self.store_event(
  31. data={"timestamp": iso_format(self.min_ago), "message": "Delet the Data"},
  32. project_id=self.project.id,
  33. )
  34. response = self.client.get(self.url, {"query": "delet"}, format="json")
  35. assert response.status_code == 200, response.content
  36. assert response.data["count"] == 1
  37. def test_invalid_query(self):
  38. response = self.client.get(self.url, {"query": "is:unresolved"}, format="json")
  39. assert response.status_code == 400, response.content
  40. def test_no_projects(self):
  41. no_project_org = self.create_organization(owner=self.user)
  42. url = reverse(
  43. "sentry-api-0-organization-events-meta",
  44. kwargs={"organization_slug": no_project_org.slug},
  45. )
  46. response = self.client.get(url, format="json")
  47. assert response.status_code == 200, response.content
  48. assert response.data["count"] == 0
  49. def test_transaction_event(self):
  50. data = {
  51. "event_id": "a" * 32,
  52. "type": "transaction",
  53. "transaction": "api.issue.delete",
  54. "spans": [],
  55. "contexts": {"trace": {"op": "foobar", "trace_id": "a" * 32, "span_id": "a" * 16}},
  56. "tags": {"important": "yes"},
  57. "timestamp": iso_format(before_now(minutes=1)),
  58. "start_timestamp": iso_format(before_now(minutes=1, seconds=3)),
  59. }
  60. self.store_event(data=data, project_id=self.project.id)
  61. url = reverse(
  62. "sentry-api-0-organization-events-meta",
  63. kwargs={"organization_slug": self.project.organization.slug},
  64. )
  65. response = self.client.get(url, {"query": "transaction.duration:>1"}, format="json")
  66. assert response.status_code == 200, response.content
  67. assert response.data["count"] == 1
  68. def test_transaction_event_with_last_seen(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. response = self.client.get(
  81. self.url, {"query": "event.type:transaction last_seen:>2012-12-31"}, format="json"
  82. )
  83. assert response.status_code == 200, response.content
  84. assert response.data["count"] == 1
  85. def test_out_of_retention(self):
  86. with self.options({"system.event-retention-days": 10}):
  87. response = self.client.get(
  88. self.url,
  89. format="json",
  90. data={
  91. "start": iso_format(before_now(days=20)),
  92. "end": iso_format(before_now(days=15)),
  93. },
  94. )
  95. assert response.status_code == 400
  96. @mock.patch("sentry.snuba.discover.raw_query")
  97. def test_handling_snuba_errors(self, mock_query):
  98. mock_query.side_effect = ParseError("test")
  99. with self.feature("organizations:discover-basic"):
  100. response = self.client.get(self.url, format="json")
  101. assert response.status_code == 400, response.content
  102. @mock.patch("sentry.utils.snuba.quantize_time")
  103. def test_quantize_dates(self, mock_quantize):
  104. mock_quantize.return_value = before_now(days=1).replace(tzinfo=utc)
  105. with self.feature("organizations:discover-basic"):
  106. # Don't quantize short time periods
  107. self.client.get(
  108. self.url,
  109. format="json",
  110. data={"statsPeriod": "1h", "query": "", "field": ["id", "timestamp"]},
  111. )
  112. # Don't quantize absolute date periods
  113. self.client.get(
  114. self.url,
  115. format="json",
  116. data={
  117. "start": iso_format(before_now(days=20)),
  118. "end": iso_format(before_now(days=15)),
  119. "query": "",
  120. "field": ["id", "timestamp"],
  121. },
  122. )
  123. assert len(mock_quantize.mock_calls) == 0
  124. # Quantize long date periods
  125. self.client.get(
  126. self.url,
  127. format="json",
  128. data={"field": ["id", "timestamp"], "statsPeriod": "90d", "query": ""},
  129. )
  130. assert len(mock_quantize.mock_calls) == 2
  131. class OrganizationEventBaselineEndpoint(APITestCase, SnubaTestCase):
  132. def setUp(self):
  133. super(OrganizationEventBaselineEndpoint, self).setUp()
  134. self.login_as(user=self.user)
  135. self.project = self.create_project()
  136. self.prototype = {
  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. }
  143. self.url = reverse(
  144. "sentry-api-0-organization-event-baseline",
  145. kwargs={"organization_slug": self.project.organization.slug},
  146. )
  147. def test_get_baseline_simple(self):
  148. for index, event_id in enumerate(["a" * 32, "b" * 32, "c" * 32]):
  149. data = self.prototype.copy()
  150. data["start_timestamp"] = iso_format(before_now(minutes=2 + index))
  151. data["timestamp"] = iso_format(before_now(minutes=1))
  152. data["event_id"] = event_id
  153. self.store_event(data=data, project_id=self.project.id)
  154. response = self.client.get(
  155. self.url,
  156. {"query": "event.type:transaction transaction:{}".format(data["transaction"])},
  157. format="json",
  158. )
  159. assert response.status_code == 200, response.content
  160. data = response.data
  161. assert data["id"] == "b" * 32
  162. assert data["transaction.duration"] == 120000
  163. assert data["p50"] == 120000.0
  164. assert data["project"] == self.project.slug
  165. def test_get_baseline_duration_tie(self):
  166. for index, event_id in enumerate(
  167. ["b" * 32, "a" * 32]
  168. ): # b then a so we know its not id breaking the tie
  169. data = self.prototype.copy()
  170. data["start_timestamp"] = iso_format(before_now(minutes=2 + index))
  171. data["timestamp"] = iso_format(before_now(minutes=1 + index))
  172. data["event_id"] = event_id
  173. self.store_event(data=data, project_id=self.project.id)
  174. response = self.client.get(
  175. self.url,
  176. {"query": "event.type:transaction transaction:{}".format(data["transaction"])},
  177. format="json",
  178. )
  179. assert response.status_code == 200, response.content
  180. data = response.data
  181. assert data["id"] == "b" * 32
  182. assert data["transaction.duration"] == 60000
  183. assert data["p50"] == 60000
  184. def test_get_baseline_duration_and_timestamp_tie(self):
  185. for event_id in ["b" * 32, "a" * 32]: # b then a so we know its not id breaking the tie
  186. data = self.prototype.copy()
  187. data["start_timestamp"] = iso_format(before_now(minutes=2))
  188. data["timestamp"] = iso_format(before_now(minutes=1))
  189. data["event_id"] = event_id
  190. self.store_event(data=data, project_id=self.project.id)
  191. response = self.client.get(
  192. self.url,
  193. {"query": "event.type:transaction transaction:{}".format(data["transaction"])},
  194. format="json",
  195. )
  196. assert response.status_code == 200, response.content
  197. data = response.data
  198. assert data["id"] == "a" * 32
  199. assert data["transaction.duration"] == 60000
  200. assert data["p50"] == 60000
  201. def test_get_baseline_with_computed_value(self):
  202. data = self.prototype.copy()
  203. data["start_timestamp"] = iso_format(before_now(minutes=2))
  204. data["timestamp"] = iso_format(before_now(minutes=1))
  205. data["event_id"] = "a" * 32
  206. self.store_event(data=data, project_id=self.project.id)
  207. response = self.client.get(
  208. self.url,
  209. {
  210. "query": "event.type:transaction transaction:{}".format(data["transaction"]),
  211. "baselineValue": 80000,
  212. },
  213. format="json",
  214. )
  215. assert response.status_code == 200, response.content
  216. data = response.data
  217. assert data["id"] == "a" * 32
  218. assert data["transaction.duration"] == 60000
  219. assert data["p50"] == "80000"
  220. def test_get_baseline_with_different_function(self):
  221. for index, event_id in enumerate(["a" * 32, "b" * 32]):
  222. data = self.prototype.copy()
  223. data["start_timestamp"] = iso_format(before_now(minutes=2 + index))
  224. data["timestamp"] = iso_format(before_now(minutes=1))
  225. data["event_id"] = event_id
  226. self.store_event(data=data, project_id=self.project.id)
  227. response = self.client.get(
  228. self.url,
  229. {
  230. "query": "event.type:transaction transaction:{}".format(data["transaction"]),
  231. "baselineFunction": "max(transaction.duration)",
  232. },
  233. format="json",
  234. )
  235. assert response.status_code == 200, response.content
  236. data = response.data
  237. assert data["id"] == "b" * 32
  238. assert data["transaction.duration"] == 120000
  239. assert data["max_transaction_duration"] == 120000
  240. class OrganizationEventsRelatedIssuesEndpoint(APITestCase, SnubaTestCase):
  241. def setUp(self):
  242. super(OrganizationEventsRelatedIssuesEndpoint, self).setUp()
  243. def test_find_related_issue(self):
  244. self.login_as(user=self.user)
  245. project = self.create_project()
  246. event1 = self.store_event(
  247. data={"timestamp": iso_format(before_now(minutes=1)), "transaction": "/beth/sanchez"},
  248. project_id=project.id,
  249. )
  250. url = reverse(
  251. "sentry-api-0-organization-related-issues",
  252. kwargs={"organization_slug": project.organization.slug},
  253. )
  254. response = self.client.get(url, {"transaction": "/beth/sanchez"}, format="json")
  255. assert response.status_code == 200, response.content
  256. assert len(response.data) == 1
  257. assert response.data[0]["shortId"] == event1.group.qualified_short_id
  258. assert int(response.data[0]["id"]) == event1.group_id
  259. def test_related_issues_no_transaction(self):
  260. self.login_as(user=self.user)
  261. project = self.create_project()
  262. self.store_event(
  263. data={"timestamp": iso_format(before_now(minutes=1)), "transaction": "/beth/sanchez"},
  264. project_id=project.id,
  265. )
  266. url = reverse(
  267. "sentry-api-0-organization-related-issues",
  268. kwargs={"organization_slug": project.organization.slug},
  269. )
  270. response = self.client.get(url, format="json")
  271. assert response.status_code == 400, response.content
  272. assert (
  273. response.data["detail"]
  274. == "Must provide one of ['transaction'] in order to find related events"
  275. )
  276. def test_related_issues_no_matching_groups(self):
  277. self.login_as(user=self.user)
  278. project = self.create_project()
  279. self.store_event(
  280. data={"timestamp": iso_format(before_now(minutes=1)), "transaction": "/beth/sanchez"},
  281. project_id=project.id,
  282. )
  283. url = reverse(
  284. "sentry-api-0-organization-related-issues",
  285. kwargs={"organization_slug": project.organization.slug},
  286. )
  287. response = self.client.get(url, {"transaction": "/morty/sanchez"}, format="json")
  288. assert response.status_code == 200, response.content
  289. assert len(response.data) == 0
  290. def test_related_issues_only_issues_in_date(self):
  291. self.login_as(user=self.user)
  292. project = self.create_project()
  293. self.store_event(
  294. data={
  295. "event_id": "a" * 32,
  296. "timestamp": iso_format(before_now(days=2)),
  297. "transaction": "/beth/sanchez",
  298. },
  299. project_id=project.id,
  300. )
  301. event2 = self.store_event(
  302. data={
  303. "event_id": "b" * 32,
  304. "timestamp": iso_format(before_now(minutes=1)),
  305. "transaction": "/beth/sanchez",
  306. },
  307. project_id=project.id,
  308. )
  309. url = reverse(
  310. "sentry-api-0-organization-related-issues",
  311. kwargs={"organization_slug": project.organization.slug},
  312. )
  313. response = self.client.get(
  314. url, {"transaction": "/beth/sanchez", "statsPeriod": "24h"}, format="json"
  315. )
  316. assert response.status_code == 200, response.content
  317. assert len(response.data) == 1
  318. assert response.data[0]["shortId"] == event2.group.qualified_short_id
  319. assert int(response.data[0]["id"]) == event2.group_id
  320. def test_related_issues_transactions_from_different_projects(self):
  321. self.login_as(user=self.user)
  322. project1 = self.create_project()
  323. project2 = self.create_project()
  324. event1 = self.store_event(
  325. data={
  326. "event_id": "a" * 32,
  327. "timestamp": iso_format(before_now(minutes=1)),
  328. "transaction": "/beth/sanchez",
  329. },
  330. project_id=project1.id,
  331. )
  332. self.store_event(
  333. data={
  334. "event_id": "b" * 32,
  335. "timestamp": iso_format(before_now(minutes=1)),
  336. "transaction": "/beth/sanchez",
  337. },
  338. project_id=project2.id,
  339. )
  340. url = reverse(
  341. "sentry-api-0-organization-related-issues",
  342. kwargs={"organization_slug": project1.organization.slug},
  343. )
  344. response = self.client.get(
  345. url, {"transaction": "/beth/sanchez", "project": project1.id}, 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": iso_format(before_now(minutes=1)),
  358. "transaction": '/beth/"sanchez"',
  359. },
  360. project_id=project.id,
  361. )
  362. url = reverse(
  363. "sentry-api-0-organization-related-issues",
  364. kwargs={"organization_slug": project.organization.slug},
  365. )
  366. response = self.client.get(
  367. url, {"transaction": '/beth/"sanchez"', "project": project.id}, format="json",
  368. )
  369. assert response.status_code == 200, response.content
  370. assert len(response.data) == 1
  371. assert response.data[0]["shortId"] == event.group.qualified_short_id
  372. assert int(response.data[0]["id"]) == event.group_id
  373. url = reverse(
  374. "sentry-api-0-organization-related-issues",
  375. kwargs={"organization_slug": project.organization.slug},
  376. )
  377. response = self.client.get(
  378. url, {"transaction": '/beth/\\"sanchez\\"', "project": project.id}, format="json",
  379. )
  380. assert response.status_code == 200, response.content
  381. assert len(response.data) == 1
  382. assert response.data[0]["shortId"] == event.group.qualified_short_id
  383. assert int(response.data[0]["id"]) == event.group_id