test_organization_events_vitals.py 15 KB

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