test_organization_events_vitals.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. from datetime import timedelta
  2. from django.urls import reverse
  3. from sentry.models.transaction_threshold import ProjectTransactionThreshold, TransactionMetric
  4. from sentry.testutils import APITestCase, MetricsEnhancedPerformanceTestCase, SnubaTestCase
  5. from sentry.testutils.helpers.datetime import before_now, iso_format
  6. from sentry.utils.samples import load_data
  7. class OrganizationEventsVitalsEndpointTest(APITestCase, SnubaTestCase):
  8. def setUp(self):
  9. super().setUp()
  10. self.start = before_now(days=1).replace(hour=10, minute=0, second=0, microsecond=0)
  11. self.end = self.start + timedelta(hours=6)
  12. self.transaction_data = load_data("transaction", timestamp=self.start)
  13. self.query = {
  14. "start": iso_format(self.start),
  15. "end": iso_format(self.end),
  16. }
  17. self.features = {}
  18. def store_event(self, data, measurements=None, **kwargs):
  19. if measurements:
  20. for vital, value in measurements.items():
  21. data["measurements"][vital]["value"] = value
  22. return super().store_event(
  23. data.copy(),
  24. project_id=self.project.id,
  25. )
  26. def do_request(self, query=None, features=None):
  27. if features is None:
  28. features = {"organizations:discover-basic": True}
  29. features.update(self.features)
  30. if query is None:
  31. query = self.query
  32. self.login_as(user=self.user)
  33. url = reverse(
  34. "sentry-api-0-organization-events-vitals",
  35. kwargs={"organization_slug": self.organization.slug},
  36. )
  37. with self.feature(features):
  38. return self.client.get(url, query, format="json")
  39. def test_no_projects(self):
  40. response = self.do_request()
  41. assert response.status_code == 200, response.content
  42. assert len(response.data) == 0
  43. def test_no_vitals(self):
  44. self.store_event(
  45. self.transaction_data,
  46. project_id=self.project.id,
  47. )
  48. self.query.update({"vital": []})
  49. response = self.do_request()
  50. assert response.status_code == 400, response.content
  51. assert "Need to pass at least one vital" == response.data["detail"]
  52. def test_bad_vital(self):
  53. self.store_event(
  54. self.transaction_data,
  55. project_id=self.project.id,
  56. )
  57. self.query.update({"vital": ["foobar"]})
  58. response = self.do_request()
  59. assert response.status_code == 400, response.content
  60. assert "foobar is not a valid vital" == response.data["detail"]
  61. def test_simple(self):
  62. data = self.transaction_data.copy()
  63. for lcp in [2000, 3000, 5000]:
  64. self.store_event(
  65. data,
  66. {"lcp": lcp},
  67. project_id=self.project.id,
  68. )
  69. self.query.update({"vital": ["measurements.lcp"]})
  70. response = self.do_request()
  71. assert response.status_code == 200, response.content
  72. assert not response.data["meta"]["isMetricsData"]
  73. assert response.data["measurements.lcp"] == {
  74. "good": 1,
  75. "meh": 1,
  76. "poor": 1,
  77. "total": 3,
  78. "p75": 4000,
  79. }
  80. def test_simple_with_refining_user_misery_filter(self):
  81. project1 = self.create_project(organization=self.organization)
  82. project2 = self.create_project(organization=self.organization)
  83. ProjectTransactionThreshold.objects.create(
  84. project=project1,
  85. organization=project1.organization,
  86. threshold=100,
  87. metric=TransactionMetric.LCP.value,
  88. )
  89. ProjectTransactionThreshold.objects.create(
  90. project=project2,
  91. organization=project2.organization,
  92. threshold=1000,
  93. metric=TransactionMetric.LCP.value,
  94. )
  95. data = self.transaction_data.copy()
  96. for project in [project1, project2]:
  97. for lcp in [2000, 3000, 5000]:
  98. self.store_event(
  99. data,
  100. {"lcp": lcp},
  101. project_id=project.id,
  102. )
  103. self.query.update({"vital": ["measurements.lcp"]})
  104. response = self.do_request(
  105. features={"organizations:global-views": True, "organizations:discover-basic": True}
  106. )
  107. assert response.status_code == 200, response.content
  108. assert not response.data["meta"]["isMetricsData"]
  109. assert response.data["measurements.lcp"] == {
  110. "good": 0,
  111. "meh": 1,
  112. "poor": 1,
  113. "total": 2,
  114. "p75": 4500,
  115. }
  116. self.query.update({"query": "user_misery():<0.04"})
  117. response = self.do_request(
  118. features={"organizations:global-views": True, "organizations:discover-basic": True}
  119. )
  120. assert response.status_code == 200, response.content
  121. assert len(response.data) == 2
  122. assert not response.data["meta"]["isMetricsData"]
  123. assert response.data["measurements.lcp"] == {
  124. "good": 0,
  125. "meh": 1,
  126. "poor": 1,
  127. "total": 2,
  128. "p75": 4500,
  129. }
  130. def test_grouping(self):
  131. counts = [
  132. (100, 2),
  133. (3000, 3),
  134. (4500, 1),
  135. ]
  136. for duration, count in counts:
  137. for _ in range(count):
  138. self.store_event(
  139. load_data("transaction", timestamp=self.start),
  140. {"lcp": duration},
  141. project_id=self.project.id,
  142. )
  143. self.query.update({"vital": ["measurements.lcp"]})
  144. response = self.do_request()
  145. assert response.status_code == 200
  146. assert not response.data["meta"]["isMetricsData"]
  147. assert response.data["measurements.lcp"] == {
  148. "good": 2,
  149. "meh": 3,
  150. "poor": 1,
  151. "total": 6,
  152. "p75": 3000,
  153. }
  154. def test_multiple_vitals(self):
  155. vitals = {"lcp": 3000, "fid": 50, "cls": 0.15, "fcp": 5000, "fp": 4000}
  156. self.store_event(
  157. load_data("transaction", timestamp=self.start),
  158. vitals,
  159. project_id=self.project.id,
  160. )
  161. self.query.update(
  162. {
  163. "vital": [
  164. "measurements.lcp",
  165. "measurements.fid",
  166. "measurements.cls",
  167. "measurements.fcp",
  168. "measurements.fp",
  169. ]
  170. }
  171. )
  172. response = self.do_request()
  173. assert response.status_code == 200
  174. assert not response.data["meta"]["isMetricsData"]
  175. assert response.data["measurements.lcp"] == {
  176. "good": 0,
  177. "meh": 1,
  178. "poor": 0,
  179. "total": 1,
  180. "p75": 3000,
  181. }
  182. assert response.data["measurements.fid"] == {
  183. "good": 1,
  184. "meh": 0,
  185. "poor": 0,
  186. "total": 1,
  187. "p75": 50,
  188. }
  189. assert response.data["measurements.cls"] == {
  190. "good": 0,
  191. "meh": 1,
  192. "poor": 0,
  193. "total": 1,
  194. "p75": 0.15,
  195. }
  196. assert response.data["measurements.fcp"] == {
  197. "good": 0,
  198. "meh": 0,
  199. "poor": 1,
  200. "total": 1,
  201. "p75": 5000,
  202. }
  203. assert response.data["measurements.fp"] == {
  204. "good": 0,
  205. "meh": 0,
  206. "poor": 1,
  207. "total": 1,
  208. "p75": 4000,
  209. }
  210. def test_transactions_without_vitals(self):
  211. del self.transaction_data["measurements"]
  212. self.store_event(
  213. self.transaction_data,
  214. project_id=self.project.id,
  215. )
  216. self.query.update({"vital": ["measurements.lcp", "measurements.fcp"]})
  217. response = self.do_request()
  218. assert response.status_code == 200, response.data
  219. assert not response.data["meta"]["isMetricsData"]
  220. assert response.data["measurements.lcp"] == {
  221. "good": 0,
  222. "meh": 0,
  223. "poor": 0,
  224. "total": 0,
  225. "p75": None,
  226. }
  227. assert response.data["measurements.fcp"] == {
  228. "good": 0,
  229. "meh": 0,
  230. "poor": 0,
  231. "total": 0,
  232. "p75": None,
  233. }
  234. def test_edges_of_vital_thresholds(self):
  235. self.store_event(
  236. load_data("transaction", timestamp=self.start),
  237. {"lcp": 4000, "fp": 1000, "fcp": 0},
  238. project_id=self.project.id,
  239. )
  240. self.query.update({"vital": ["measurements.lcp", "measurements.fp", "measurements.fcp"]})
  241. response = self.do_request()
  242. assert response.status_code == 200, response.data
  243. assert not response.data["meta"]["isMetricsData"]
  244. assert response.data["measurements.lcp"] == {
  245. "good": 0,
  246. "meh": 0,
  247. "poor": 1,
  248. "total": 1,
  249. "p75": 4000,
  250. }
  251. assert response.data["measurements.fp"] == {
  252. "good": 0,
  253. "meh": 1,
  254. "poor": 0,
  255. "total": 1,
  256. "p75": 1000,
  257. }
  258. assert response.data["measurements.fcp"] == {
  259. "good": 1,
  260. "meh": 0,
  261. "poor": 0,
  262. "total": 1,
  263. "p75": 0,
  264. }
  265. class OrganizationEventsMetricsEnhancedPerformanceEndpointTest(MetricsEnhancedPerformanceTestCase):
  266. METRIC_STRINGS = ["measurement_rating"]
  267. def setUp(self):
  268. super().setUp()
  269. self.start = before_now(days=1).replace(hour=10, minute=0, second=0, microsecond=0)
  270. self.end = self.start + timedelta(hours=6)
  271. self.query = {
  272. "start": iso_format(self.start),
  273. "end": iso_format(self.end),
  274. }
  275. self.features = {"organizations:performance-use-metrics": True}
  276. def do_request(self, query=None, features=None):
  277. if features is None:
  278. features = {"organizations:discover-basic": True}
  279. features.update(self.features)
  280. if query is None:
  281. query = self.query
  282. query["dataset"] = "metricsEnhanced"
  283. self.login_as(user=self.user)
  284. with self.feature(features):
  285. url = reverse(
  286. "sentry-api-0-organization-events-vitals",
  287. kwargs={"organization_slug": self.organization.slug},
  288. )
  289. with self.feature(features):
  290. return self.client.get(url, query, format="json")
  291. def test_no_projects(self):
  292. response = self.do_request()
  293. assert response.status_code == 200, response.content
  294. assert len(response.data) == 0
  295. def test_no_vitals(self):
  296. self.query.update({"vital": [], "project": self.project.id})
  297. response = self.do_request()
  298. assert response.status_code == 400, response.content
  299. assert "Need to pass at least one vital" == response.data["detail"]
  300. def test_simple(self):
  301. for rating, lcp in [("good", 2000), ("meh", 3000), ("poor", 5000)]:
  302. self.store_metric(
  303. lcp,
  304. metric="measurements.lcp",
  305. tags={"transaction": "foo_transaction", "measurement_rating": rating},
  306. timestamp=self.start + timedelta(minutes=5),
  307. )
  308. self.query.update({"vital": ["measurements.lcp"]})
  309. response = self.do_request()
  310. assert response.status_code == 200, response.content
  311. assert response.data["meta"]["isMetricsData"]
  312. assert response.data["measurements.lcp"] == {
  313. "good": 1,
  314. "meh": 1,
  315. "poor": 1,
  316. "total": 3,
  317. "p75": 4000,
  318. }
  319. def test_grouping(self):
  320. counts = [
  321. ("good", 100, 2),
  322. ("meh", 3000, 3),
  323. ("poor", 4500, 1),
  324. ]
  325. for rating, duration, count in counts:
  326. for _ in range(count):
  327. self.store_metric(
  328. duration,
  329. metric="measurements.lcp",
  330. tags={"transaction": "foo_transaction", "measurement_rating": rating},
  331. timestamp=self.start + timedelta(minutes=5),
  332. )
  333. self.query.update({"vital": ["measurements.lcp"]})
  334. response = self.do_request()
  335. assert response.status_code == 200
  336. assert response.data["meta"]["isMetricsData"]
  337. assert response.data["measurements.lcp"] == {
  338. "good": 2,
  339. "meh": 3,
  340. "poor": 1,
  341. "total": 6,
  342. "p75": 3000,
  343. }
  344. def test_multiple_vitals(self):
  345. vitals = [
  346. ("measurements.lcp", 3000, "meh"),
  347. ("measurements.fid", 50, "good"),
  348. ("measurements.cls", 0.15, "meh"),
  349. ("measurements.fcp", 5000, "poor"),
  350. ("measurements.fp", 4000, "poor"),
  351. ]
  352. for vital, duration, rating in vitals:
  353. self.store_metric(
  354. duration,
  355. metric=vital,
  356. tags={"transaction": "foo_transaction", "measurement_rating": rating},
  357. timestamp=self.start + timedelta(minutes=5),
  358. )
  359. self.query.update(
  360. {
  361. "vital": [
  362. "measurements.lcp",
  363. "measurements.fid",
  364. "measurements.cls",
  365. "measurements.fcp",
  366. "measurements.fp",
  367. ]
  368. }
  369. )
  370. response = self.do_request()
  371. assert response.status_code == 200
  372. assert response.data["meta"]["isMetricsData"]
  373. assert response.data["measurements.lcp"] == {
  374. "good": 0,
  375. "meh": 1,
  376. "poor": 0,
  377. "total": 1,
  378. "p75": 3000,
  379. }
  380. assert response.data["measurements.fid"] == {
  381. "good": 1,
  382. "meh": 0,
  383. "poor": 0,
  384. "total": 1,
  385. "p75": 50,
  386. }
  387. assert response.data["measurements.cls"] == {
  388. "good": 0,
  389. "meh": 1,
  390. "poor": 0,
  391. "total": 1,
  392. "p75": 0.15,
  393. }
  394. assert response.data["measurements.fcp"] == {
  395. "good": 0,
  396. "meh": 0,
  397. "poor": 1,
  398. "total": 1,
  399. "p75": 5000,
  400. }
  401. assert response.data["measurements.fp"] == {
  402. "good": 0,
  403. "meh": 0,
  404. "poor": 1,
  405. "total": 1,
  406. "p75": 4000,
  407. }
  408. def test_transactions_without_vitals(self):
  409. self.query.update(
  410. {"vital": ["measurements.lcp", "measurements.fcp"], "project": self.project.id}
  411. )
  412. response = self.do_request()
  413. assert response.status_code == 200, response.data
  414. assert response.data["meta"]["isMetricsData"]
  415. assert response.data["measurements.lcp"] == {
  416. "good": 0,
  417. "meh": 0,
  418. "poor": 0,
  419. "total": 0,
  420. "p75": 0,
  421. }
  422. assert response.data["measurements.fcp"] == {
  423. "good": 0,
  424. "meh": 0,
  425. "poor": 0,
  426. "total": 0,
  427. "p75": 0,
  428. }