test_organization_events_vitals.py 15 KB

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