test_organization_events_span_metrics.py 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939
  1. from datetime import timedelta
  2. import pytest
  3. from django.urls import reverse
  4. from sentry.search.events import constants
  5. from sentry.search.utils import map_device_class_level
  6. from sentry.testutils.cases import MetricsEnhancedPerformanceTestCase
  7. from sentry.testutils.helpers.datetime import before_now
  8. pytestmark = pytest.mark.sentry_metrics
  9. SPAN_DURATION_MRI = "d:spans/duration@millisecond"
  10. class OrganizationEventsMetricsEnhancedPerformanceEndpointTest(MetricsEnhancedPerformanceTestCase):
  11. viewname = "sentry-api-0-organization-events"
  12. # Poor intentionally omitted for test_measurement_rating_that_does_not_exist
  13. METRIC_STRINGS = [
  14. "foo_transaction",
  15. "bar_transaction",
  16. ]
  17. def setUp(self):
  18. super().setUp()
  19. self.min_ago = before_now(minutes=1)
  20. self.six_min_ago = before_now(minutes=6)
  21. self.three_days_ago = before_now(days=3)
  22. self.features = {
  23. "organizations:starfish-view": True,
  24. }
  25. def do_request(self, query, features=None):
  26. if features is None:
  27. features = {"organizations:discover-basic": True}
  28. features.update(self.features)
  29. self.login_as(user=self.user)
  30. url = reverse(
  31. self.viewname,
  32. kwargs={"organization_id_or_slug": self.organization.slug},
  33. )
  34. with self.feature(features):
  35. return self.client.get(url, query, format="json")
  36. def test_p50_with_no_data(self):
  37. response = self.do_request(
  38. {
  39. "field": ["p50()"],
  40. "query": "",
  41. "project": self.project.id,
  42. "dataset": "spansMetrics",
  43. }
  44. )
  45. assert response.status_code == 200, response.content
  46. data = response.data["data"]
  47. meta = response.data["meta"]
  48. assert len(data) == 1
  49. assert data[0]["p50()"] == 0
  50. assert meta["dataset"] == "spansMetrics"
  51. def test_count(self):
  52. self.store_span_metric(
  53. 1,
  54. internal_metric=constants.SELF_TIME_LIGHT,
  55. timestamp=self.three_days_ago,
  56. )
  57. response = self.do_request(
  58. {
  59. "field": ["count()"],
  60. "query": "",
  61. "project": self.project.id,
  62. "dataset": "spansMetrics",
  63. "statsPeriod": "7d",
  64. }
  65. )
  66. assert response.status_code == 200, response.content
  67. data = response.data["data"]
  68. meta = response.data["meta"]
  69. assert len(data) == 1
  70. assert data[0]["count()"] == 1
  71. assert meta["dataset"] == "spansMetrics"
  72. def test_count_if(self):
  73. self.store_span_metric(
  74. 2,
  75. internal_metric=constants.SELF_TIME_LIGHT,
  76. timestamp=self.three_days_ago,
  77. tags={"release": "1.0.0"},
  78. )
  79. self.store_span_metric(
  80. 2,
  81. internal_metric=constants.SELF_TIME_LIGHT,
  82. timestamp=self.three_days_ago,
  83. tags={"release": "1.0.0"},
  84. )
  85. self.store_span_metric(
  86. 2,
  87. internal_metric=constants.SELF_TIME_LIGHT,
  88. timestamp=self.three_days_ago,
  89. tags={"release": "2.0.0"},
  90. )
  91. fieldRelease1 = "count_if(release,1.0.0)"
  92. fieldRelease2 = "count_if(release,2.0.0)"
  93. response = self.do_request(
  94. {
  95. "field": [fieldRelease1, fieldRelease2],
  96. "query": "",
  97. "project": self.project.id,
  98. "dataset": "spansMetrics",
  99. "statsPeriod": "7d",
  100. }
  101. )
  102. assert response.status_code == 200, response.content
  103. data = response.data["data"]
  104. meta = response.data["meta"]
  105. assert len(data) == 1
  106. assert data[0][fieldRelease1] == 2
  107. assert data[0][fieldRelease2] == 1
  108. assert meta["dataset"] == "spansMetrics"
  109. def test_count_unique(self):
  110. self.store_span_metric(
  111. 1,
  112. "user",
  113. timestamp=self.min_ago,
  114. )
  115. self.store_span_metric(
  116. 2,
  117. "user",
  118. timestamp=self.min_ago,
  119. )
  120. response = self.do_request(
  121. {
  122. "field": ["count_unique(user)"],
  123. "query": "",
  124. "project": self.project.id,
  125. "dataset": "spansMetrics",
  126. }
  127. )
  128. assert response.status_code == 200, response.content
  129. data = response.data["data"]
  130. meta = response.data["meta"]
  131. assert len(data) == 1
  132. assert data[0]["count_unique(user)"] == 2
  133. assert meta["dataset"] == "spansMetrics"
  134. def test_sum(self):
  135. self.store_span_metric(
  136. 321,
  137. internal_metric=constants.SELF_TIME_LIGHT,
  138. timestamp=self.min_ago,
  139. )
  140. self.store_span_metric(
  141. 99,
  142. internal_metric=constants.SELF_TIME_LIGHT,
  143. timestamp=self.min_ago,
  144. )
  145. response = self.do_request(
  146. {
  147. "field": ["sum(span.self_time)"],
  148. "query": "",
  149. "project": self.project.id,
  150. "dataset": "spansMetrics",
  151. }
  152. )
  153. assert response.status_code == 200, response.content
  154. data = response.data["data"]
  155. meta = response.data["meta"]
  156. assert len(data) == 1
  157. assert data[0]["sum(span.self_time)"] == 420
  158. assert meta["dataset"] == "spansMetrics"
  159. def test_percentile(self):
  160. self.store_span_metric(
  161. 1,
  162. internal_metric=constants.SELF_TIME_LIGHT,
  163. timestamp=self.min_ago,
  164. )
  165. response = self.do_request(
  166. {
  167. "field": ["percentile(span.self_time, 0.95)"],
  168. "query": "",
  169. "project": self.project.id,
  170. "dataset": "spansMetrics",
  171. }
  172. )
  173. assert response.status_code == 200, response.content
  174. data = response.data["data"]
  175. meta = response.data["meta"]
  176. assert len(data) == 1
  177. assert data[0]["percentile(span.self_time, 0.95)"] == 1
  178. assert meta["dataset"] == "spansMetrics"
  179. def test_fixed_percentile_functions(self):
  180. self.store_span_metric(
  181. 1,
  182. internal_metric=constants.SELF_TIME_LIGHT,
  183. timestamp=self.min_ago,
  184. )
  185. for function in ["p50()", "p75()", "p95()", "p99()", "p100()"]:
  186. response = self.do_request(
  187. {
  188. "field": [function],
  189. "query": "",
  190. "project": self.project.id,
  191. "dataset": "spansMetrics",
  192. }
  193. )
  194. assert response.status_code == 200, response.content
  195. data = response.data["data"]
  196. meta = response.data["meta"]
  197. assert len(data) == 1
  198. assert data[0][function] == 1, function
  199. assert meta["dataset"] == "spansMetrics", function
  200. assert meta["fields"][function] == "duration", function
  201. def test_fixed_percentile_functions_with_duration(self):
  202. self.store_span_metric(
  203. 1,
  204. internal_metric=constants.SPAN_METRICS_MAP["span.duration"],
  205. timestamp=self.min_ago,
  206. )
  207. for function in [
  208. "p50(span.duration)",
  209. "p75(span.duration)",
  210. "p95(span.duration)",
  211. "p99(span.duration)",
  212. "p100(span.duration)",
  213. ]:
  214. response = self.do_request(
  215. {
  216. "field": [function],
  217. "query": "",
  218. "project": self.project.id,
  219. "dataset": "spansMetrics",
  220. }
  221. )
  222. assert response.status_code == 200, response.content
  223. data = response.data["data"]
  224. meta = response.data["meta"]
  225. assert len(data) == 1, function
  226. assert data[0][function] == 1, function
  227. assert meta["dataset"] == "spansMetrics", function
  228. assert meta["fields"][function] == "duration", function
  229. def test_avg(self):
  230. self.store_span_metric(
  231. 1,
  232. internal_metric=constants.SELF_TIME_LIGHT,
  233. timestamp=self.min_ago,
  234. )
  235. response = self.do_request(
  236. {
  237. "field": ["avg()"],
  238. "query": "",
  239. "project": self.project.id,
  240. "dataset": "spansMetrics",
  241. }
  242. )
  243. assert response.status_code == 200, response.content
  244. data = response.data["data"]
  245. meta = response.data["meta"]
  246. assert len(data) == 1
  247. assert data[0]["avg()"] == 1
  248. assert meta["dataset"] == "spansMetrics"
  249. def test_eps(self):
  250. for _ in range(6):
  251. self.store_span_metric(
  252. 1,
  253. internal_metric=constants.SELF_TIME_LIGHT,
  254. timestamp=self.min_ago,
  255. )
  256. response = self.do_request(
  257. {
  258. "field": ["eps()", "sps()"],
  259. "query": "",
  260. "project": self.project.id,
  261. "dataset": "spansMetrics",
  262. "statsPeriod": "10m",
  263. }
  264. )
  265. assert response.status_code == 200, response.content
  266. data = response.data["data"]
  267. meta = response.data["meta"]
  268. assert len(data) == 1
  269. assert data[0]["eps()"] == 0.01
  270. assert data[0]["sps()"] == 0.01
  271. assert meta["fields"]["eps()"] == "rate"
  272. assert meta["fields"]["sps()"] == "rate"
  273. assert meta["units"]["eps()"] == "1/second"
  274. assert meta["units"]["sps()"] == "1/second"
  275. assert meta["dataset"] == "spansMetrics"
  276. def test_epm(self):
  277. for _ in range(6):
  278. self.store_span_metric(
  279. 1,
  280. internal_metric=constants.SELF_TIME_LIGHT,
  281. timestamp=self.min_ago,
  282. )
  283. response = self.do_request(
  284. {
  285. "field": ["epm()", "spm()"],
  286. "query": "",
  287. "project": self.project.id,
  288. "dataset": "spansMetrics",
  289. "statsPeriod": "10m",
  290. }
  291. )
  292. assert response.status_code == 200, response.content
  293. data = response.data["data"]
  294. meta = response.data["meta"]
  295. assert len(data) == 1
  296. assert data[0]["epm()"] == 0.6
  297. assert data[0]["spm()"] == 0.6
  298. assert meta["fields"]["epm()"] == "rate"
  299. assert meta["fields"]["spm()"] == "rate"
  300. assert meta["units"]["epm()"] == "1/minute"
  301. assert meta["units"]["spm()"] == "1/minute"
  302. assert meta["dataset"] == "spansMetrics"
  303. def test_time_spent_percentage(self):
  304. for _ in range(4):
  305. self.store_span_metric(
  306. 1,
  307. internal_metric=constants.SELF_TIME_LIGHT,
  308. tags={"transaction": "foo_transaction"},
  309. timestamp=self.min_ago,
  310. )
  311. self.store_span_metric(
  312. 1,
  313. tags={"transaction": "foo_transaction"},
  314. timestamp=self.min_ago,
  315. )
  316. self.store_span_metric(
  317. 1,
  318. internal_metric=constants.SELF_TIME_LIGHT,
  319. tags={"transaction": "bar_transaction"},
  320. timestamp=self.min_ago,
  321. )
  322. self.store_span_metric(
  323. 1,
  324. tags={"transaction": "bar_transaction"},
  325. timestamp=self.min_ago,
  326. )
  327. response = self.do_request(
  328. {
  329. "field": ["transaction", "time_spent_percentage()"],
  330. "query": "",
  331. "orderby": ["-time_spent_percentage()"],
  332. "project": self.project.id,
  333. "dataset": "spansMetrics",
  334. "statsPeriod": "10m",
  335. }
  336. )
  337. assert response.status_code == 200, response.content
  338. data = response.data["data"]
  339. meta = response.data["meta"]
  340. assert len(data) == 2
  341. assert data[0]["time_spent_percentage()"] == 0.8
  342. assert data[0]["transaction"] == "foo_transaction"
  343. assert data[1]["time_spent_percentage()"] == 0.2
  344. assert data[1]["transaction"] == "bar_transaction"
  345. assert meta["dataset"] == "spansMetrics"
  346. def test_time_spent_percentage_local(self):
  347. response = self.do_request(
  348. {
  349. "field": ["time_spent_percentage(local)"],
  350. "query": "",
  351. "orderby": ["-time_spent_percentage(local)"],
  352. "project": self.project.id,
  353. "dataset": "spansMetrics",
  354. "statsPeriod": "10m",
  355. }
  356. )
  357. assert response.status_code == 200, response.content
  358. data = response.data["data"]
  359. meta = response.data["meta"]
  360. assert len(data) == 1
  361. assert data[0]["time_spent_percentage(local)"] is None
  362. assert meta["dataset"] == "spansMetrics"
  363. def test_time_spent_percentage_on_span_duration(self):
  364. for _ in range(4):
  365. self.store_span_metric(
  366. 1,
  367. internal_metric=constants.SPAN_METRICS_MAP["span.duration"],
  368. tags={"transaction": "foo_transaction"},
  369. timestamp=self.min_ago,
  370. )
  371. self.store_span_metric(
  372. 1,
  373. internal_metric=constants.SPAN_METRICS_MAP["span.duration"],
  374. tags={"transaction": "bar_transaction"},
  375. timestamp=self.min_ago,
  376. )
  377. response = self.do_request(
  378. {
  379. "field": ["transaction", "time_spent_percentage(app,span.duration)"],
  380. "query": "",
  381. "orderby": ["-time_spent_percentage(app,span.duration)"],
  382. "project": self.project.id,
  383. "dataset": "spansMetrics",
  384. "statsPeriod": "10m",
  385. }
  386. )
  387. assert response.status_code == 200, response.content
  388. data = response.data["data"]
  389. meta = response.data["meta"]
  390. assert len(data) == 2
  391. assert data[0]["time_spent_percentage(app,span.duration)"] == 0.8
  392. assert data[0]["transaction"] == "foo_transaction"
  393. assert data[1]["time_spent_percentage(app,span.duration)"] == 0.2
  394. assert data[1]["transaction"] == "bar_transaction"
  395. assert meta["dataset"] == "spansMetrics"
  396. def test_http_error_rate_and_count(self):
  397. for _ in range(4):
  398. self.store_span_metric(
  399. 1,
  400. internal_metric=constants.SELF_TIME_LIGHT,
  401. tags={"span.status_code": "500"},
  402. timestamp=self.min_ago,
  403. )
  404. self.store_span_metric(
  405. 1,
  406. internal_metric=constants.SELF_TIME_LIGHT,
  407. tags={"span.status_code": "200"},
  408. timestamp=self.min_ago,
  409. )
  410. response = self.do_request(
  411. {
  412. "field": ["http_error_count()", "http_error_rate()"],
  413. "query": "",
  414. "orderby": ["-http_error_rate()"],
  415. "project": self.project.id,
  416. "dataset": "spansMetrics",
  417. "statsPeriod": "10m",
  418. }
  419. )
  420. assert response.status_code == 200, response.content
  421. data = response.data["data"]
  422. meta = response.data["meta"]
  423. assert len(data) == 1
  424. assert data[0]["http_error_rate()"] == 0.8
  425. assert meta["dataset"] == "spansMetrics"
  426. assert meta["fields"]["http_error_count()"] == "integer"
  427. assert meta["fields"]["http_error_rate()"] == "percentage"
  428. def test_ttid_rate_and_count(self):
  429. for _ in range(8):
  430. self.store_span_metric(
  431. 1,
  432. internal_metric=constants.SELF_TIME_LIGHT,
  433. tags={"ttid": "ttid", "ttfd": "ttfd"},
  434. timestamp=self.min_ago,
  435. )
  436. self.store_span_metric(
  437. 1,
  438. internal_metric=constants.SELF_TIME_LIGHT,
  439. tags={"ttfd": "ttfd", "ttid": ""},
  440. timestamp=self.min_ago,
  441. )
  442. self.store_span_metric(
  443. 1,
  444. internal_metric=constants.SELF_TIME_LIGHT,
  445. tags={"ttfd": "", "ttid": ""},
  446. timestamp=self.min_ago,
  447. )
  448. response = self.do_request(
  449. {
  450. "field": [
  451. "ttid_contribution_rate()",
  452. "ttid_count()",
  453. "ttfd_contribution_rate()",
  454. "ttfd_count()",
  455. ],
  456. "query": "",
  457. "orderby": ["-ttid_contribution_rate()"],
  458. "project": self.project.id,
  459. "dataset": "spansMetrics",
  460. "statsPeriod": "10m",
  461. }
  462. )
  463. assert response.status_code == 200, response.content
  464. data = response.data["data"]
  465. meta = response.data["meta"]
  466. assert len(data) == 1
  467. assert data[0]["ttid_contribution_rate()"] == 0.8
  468. assert data[0]["ttid_count()"] == 8
  469. assert data[0]["ttfd_contribution_rate()"] == 0.9
  470. assert data[0]["ttfd_count()"] == 9
  471. assert meta["dataset"] == "spansMetrics"
  472. assert meta["fields"]["ttid_count()"] == "integer"
  473. assert meta["fields"]["ttid_contribution_rate()"] == "percentage"
  474. assert meta["fields"]["ttfd_count()"] == "integer"
  475. assert meta["fields"]["ttfd_contribution_rate()"] == "percentage"
  476. def test_main_thread_count(self):
  477. for _ in range(8):
  478. self.store_span_metric(
  479. 1,
  480. internal_metric=constants.SELF_TIME_LIGHT,
  481. tags={"span.main_thread": "true"},
  482. timestamp=self.min_ago,
  483. )
  484. self.store_span_metric(
  485. 1,
  486. internal_metric=constants.SELF_TIME_LIGHT,
  487. tags={},
  488. timestamp=self.min_ago,
  489. )
  490. self.store_span_metric(
  491. 1,
  492. internal_metric=constants.SELF_TIME_LIGHT,
  493. tags={"span.main_thread": ""},
  494. timestamp=self.min_ago,
  495. )
  496. response = self.do_request(
  497. {
  498. "field": [
  499. "main_thread_count()",
  500. ],
  501. "query": "",
  502. "orderby": ["-main_thread_count()"],
  503. "project": self.project.id,
  504. "dataset": "spansMetrics",
  505. "statsPeriod": "10m",
  506. }
  507. )
  508. assert response.status_code == 200, response.content
  509. data = response.data["data"]
  510. meta = response.data["meta"]
  511. assert len(data) == 1
  512. assert data[0]["main_thread_count()"] == 8
  513. assert meta["dataset"] == "spansMetrics"
  514. assert meta["fields"]["main_thread_count()"] == "integer"
  515. def test_use_self_time_light(self):
  516. self.store_span_metric(
  517. 100,
  518. internal_metric=constants.SELF_TIME_LIGHT,
  519. tags={"transaction": "foo_transaction"},
  520. timestamp=self.min_ago,
  521. )
  522. response = self.do_request(
  523. {
  524. "field": ["p50(span.self_time)"],
  525. # Should be 0 since its filtering on transaction
  526. "query": "transaction:foo_transaction",
  527. "orderby": ["-p50(span.self_time)"],
  528. "project": self.project.id,
  529. "dataset": "spansMetrics",
  530. "statsPeriod": "10m",
  531. }
  532. )
  533. assert response.status_code == 200, response.content
  534. data = response.data["data"]
  535. meta = response.data["meta"]
  536. assert len(data) == 1
  537. assert data[0]["p50(span.self_time)"] == 0
  538. assert meta["dataset"] == "spansMetrics"
  539. assert meta["fields"]["p50(span.self_time)"] == "duration"
  540. response = self.do_request(
  541. {
  542. # Should be 0 since it has a transaction column
  543. "field": ["transaction", "p50(span.self_time)"],
  544. "query": "",
  545. "orderby": ["-p50(span.self_time)"],
  546. "project": self.project.id,
  547. "dataset": "spansMetrics",
  548. "statsPeriod": "10m",
  549. }
  550. )
  551. assert response.status_code == 200, response.content
  552. data = response.data["data"]
  553. meta = response.data["meta"]
  554. assert len(data) == 0
  555. response = self.do_request(
  556. {
  557. "field": ["p50(span.self_time)"],
  558. # Should be 100 since its not filtering on transaction
  559. "query": "",
  560. "orderby": ["-p50(span.self_time)"],
  561. "project": self.project.id,
  562. "dataset": "spansMetrics",
  563. "statsPeriod": "10m",
  564. }
  565. )
  566. assert response.status_code == 200, response.content
  567. data = response.data["data"]
  568. meta = response.data["meta"]
  569. assert len(data) == 1
  570. assert data[0]["p50(span.self_time)"] == 100
  571. assert meta["dataset"] == "spansMetrics"
  572. assert meta["fields"]["p50(span.self_time)"] == "duration"
  573. def test_span_module(self):
  574. self.store_span_metric(
  575. 1,
  576. internal_metric=constants.SELF_TIME_LIGHT,
  577. timestamp=self.six_min_ago,
  578. tags={"span.category": "http", "span.description": "f"},
  579. )
  580. self.store_span_metric(
  581. 3,
  582. internal_metric=constants.SELF_TIME_LIGHT,
  583. timestamp=self.six_min_ago,
  584. tags={"span.category": "db", "span.description": "e"},
  585. )
  586. self.store_span_metric(
  587. 5,
  588. internal_metric=constants.SELF_TIME_LIGHT,
  589. timestamp=self.six_min_ago,
  590. tags={"span.category": "foobar", "span.description": "d"},
  591. )
  592. self.store_span_metric(
  593. 7,
  594. internal_metric=constants.SELF_TIME_LIGHT,
  595. timestamp=self.six_min_ago,
  596. tags={"span.category": "cache", "span.description": "c"},
  597. )
  598. self.store_span_metric(
  599. 9,
  600. internal_metric=constants.SELF_TIME_LIGHT,
  601. timestamp=self.six_min_ago,
  602. tags={"span.category": "db", "span.op": "db.redis", "span.description": "b"},
  603. )
  604. self.store_span_metric(
  605. 11,
  606. internal_metric=constants.SELF_TIME_LIGHT,
  607. timestamp=self.six_min_ago,
  608. tags={"span.category": "db", "span.op": "db.sql.room", "span.description": "a"},
  609. )
  610. response = self.do_request(
  611. {
  612. "field": ["span.module", "span.description", "p50(span.self_time)"],
  613. "query": "",
  614. "orderby": ["-p50(span.self_time)"],
  615. "project": self.project.id,
  616. "dataset": "spansMetrics",
  617. "statsPeriod": "10m",
  618. }
  619. )
  620. assert response.status_code == 200, response.content
  621. data = response.data["data"]
  622. meta = response.data["meta"]
  623. assert len(data) == 6
  624. assert data[0]["p50(span.self_time)"] == 11
  625. assert data[0]["span.module"] == "other"
  626. assert data[0]["span.description"] == "a"
  627. assert data[1]["p50(span.self_time)"] == 9
  628. assert data[1]["span.module"] == "cache"
  629. assert data[1]["span.description"] == "b"
  630. assert data[2]["p50(span.self_time)"] == 7
  631. assert data[2]["span.module"] == "cache"
  632. assert data[2]["span.description"] == "c"
  633. assert data[3]["p50(span.self_time)"] == 5
  634. assert data[3]["span.module"] == "other"
  635. assert data[3]["span.description"] == "d"
  636. assert data[4]["p50(span.self_time)"] == 3
  637. assert data[4]["span.module"] == "db"
  638. assert data[4]["span.description"] == "e"
  639. assert data[5]["p50(span.self_time)"] == 1
  640. assert data[5]["span.module"] == "http"
  641. assert data[5]["span.description"] == "f"
  642. assert meta["dataset"] == "spansMetrics"
  643. assert meta["fields"]["p50(span.self_time)"] == "duration"
  644. def test_tag_search(self):
  645. self.store_span_metric(
  646. 321,
  647. internal_metric=constants.SELF_TIME_LIGHT,
  648. timestamp=self.min_ago,
  649. tags={"span.description": "foo"},
  650. )
  651. self.store_span_metric(
  652. 99,
  653. internal_metric=constants.SELF_TIME_LIGHT,
  654. timestamp=self.min_ago,
  655. tags={"span.description": "bar"},
  656. )
  657. response = self.do_request(
  658. {
  659. "field": ["sum(span.self_time)"],
  660. "query": "span.description:bar",
  661. "project": self.project.id,
  662. "dataset": "spansMetrics",
  663. }
  664. )
  665. assert response.status_code == 200, response.content
  666. data = response.data["data"]
  667. meta = response.data["meta"]
  668. assert len(data) == 1
  669. assert data[0]["sum(span.self_time)"] == 99
  670. assert meta["dataset"] == "spansMetrics"
  671. def test_free_text_search(self):
  672. self.store_span_metric(
  673. 321,
  674. internal_metric=constants.SELF_TIME_LIGHT,
  675. timestamp=self.min_ago,
  676. tags={"span.description": "foo"},
  677. )
  678. self.store_span_metric(
  679. 99,
  680. internal_metric=constants.SELF_TIME_LIGHT,
  681. timestamp=self.min_ago,
  682. tags={"span.description": "bar"},
  683. )
  684. response = self.do_request(
  685. {
  686. "field": ["sum(span.self_time)"],
  687. "query": "foo",
  688. "project": self.project.id,
  689. "dataset": "spansMetrics",
  690. }
  691. )
  692. assert response.status_code == 200, response.content
  693. data = response.data["data"]
  694. meta = response.data["meta"]
  695. assert len(data) == 1
  696. assert data[0]["sum(span.self_time)"] == 321
  697. assert meta["dataset"] == "spansMetrics"
  698. def test_avg_compare(self):
  699. self.store_span_metric(
  700. 100,
  701. internal_metric=constants.SELF_TIME_LIGHT,
  702. timestamp=self.min_ago,
  703. tags={"release": "foo"},
  704. )
  705. self.store_span_metric(
  706. 10,
  707. internal_metric=constants.SELF_TIME_LIGHT,
  708. timestamp=self.min_ago,
  709. tags={"release": "bar"},
  710. )
  711. for function_name in [
  712. "avg_compare(span.self_time, release, foo, bar)",
  713. 'avg_compare(span.self_time, release, "foo", "bar")',
  714. ]:
  715. response = self.do_request(
  716. {
  717. "field": [function_name],
  718. "query": "",
  719. "project": self.project.id,
  720. "dataset": "spansMetrics",
  721. }
  722. )
  723. assert response.status_code == 200, response.content
  724. data = response.data["data"]
  725. meta = response.data["meta"]
  726. assert len(data) == 1
  727. assert data[0][function_name] == -0.9
  728. assert meta["dataset"] == "spansMetrics"
  729. assert meta["fields"][function_name] == "percent_change"
  730. def test_avg_compare_invalid_column(self):
  731. response = self.do_request(
  732. {
  733. "field": ["avg_compare(span.self_time, transaction, foo, bar)"],
  734. "query": "",
  735. "project": self.project.id,
  736. "dataset": "spansMetrics",
  737. }
  738. )
  739. assert response.status_code == 400, response.content
  740. def test_span_domain_array(self):
  741. self.store_span_metric(
  742. 321,
  743. internal_metric=constants.SELF_TIME_LIGHT,
  744. timestamp=self.min_ago,
  745. tags={"span.domain": ",sentry_table1,"},
  746. )
  747. self.store_span_metric(
  748. 21,
  749. internal_metric=constants.SELF_TIME_LIGHT,
  750. timestamp=self.min_ago,
  751. tags={"span.domain": ",sentry_table1,sentry_table2,"},
  752. )
  753. response = self.do_request(
  754. {
  755. "field": ["span.domain", "p75(span.self_time)"],
  756. "query": "",
  757. "project": self.project.id,
  758. "orderby": ["-p75(span.self_time)"],
  759. "dataset": "spansMetrics",
  760. }
  761. )
  762. assert response.status_code == 200, response.content
  763. data = response.data["data"]
  764. meta = response.data["meta"]
  765. assert len(data) == 2
  766. assert data[0]["span.domain"] == ["sentry_table1"]
  767. assert data[1]["span.domain"] == ["sentry_table1", "sentry_table2"]
  768. assert meta["dataset"] == "spansMetrics"
  769. assert meta["fields"]["span.domain"] == "array"
  770. def test_span_domain_array_filter(self):
  771. self.store_span_metric(
  772. 321,
  773. internal_metric=constants.SELF_TIME_LIGHT,
  774. timestamp=self.min_ago,
  775. tags={"span.domain": ",sentry_table1,"},
  776. )
  777. self.store_span_metric(
  778. 21,
  779. internal_metric=constants.SELF_TIME_LIGHT,
  780. timestamp=self.min_ago,
  781. tags={"span.domain": ",sentry_table1,sentry_table2,"},
  782. )
  783. response = self.do_request(
  784. {
  785. "field": ["span.domain", "p75(span.self_time)"],
  786. "query": "span.domain:sentry_table2",
  787. "project": self.project.id,
  788. "dataset": "spansMetrics",
  789. }
  790. )
  791. assert response.status_code == 200, response.content
  792. data = response.data["data"]
  793. meta = response.data["meta"]
  794. assert len(data) == 1
  795. assert data[0]["span.domain"] == ["sentry_table1", "sentry_table2"]
  796. assert meta["dataset"] == "spansMetrics"
  797. assert meta["fields"]["span.domain"] == "array"
  798. def test_span_domain_array_filter_wildcard(self):
  799. self.store_span_metric(
  800. 321,
  801. internal_metric=constants.SELF_TIME_LIGHT,
  802. timestamp=self.min_ago,
  803. tags={"span.domain": ",sentry_table1,"},
  804. )
  805. self.store_span_metric(
  806. 21,
  807. internal_metric=constants.SELF_TIME_LIGHT,
  808. timestamp=self.min_ago,
  809. tags={"span.domain": ",sentry_table1,sentry_table2,"},
  810. )
  811. for query in ["sentry*2", "*table2", "sentry_table2*"]:
  812. response = self.do_request(
  813. {
  814. "field": ["span.domain", "p75(span.self_time)"],
  815. "query": f"span.domain:{query}",
  816. "project": self.project.id,
  817. "dataset": "spansMetrics",
  818. }
  819. )
  820. assert response.status_code == 200, response.content
  821. data = response.data["data"]
  822. meta = response.data["meta"]
  823. assert len(data) == 1, query
  824. assert data[0]["span.domain"] == ["sentry_table1", "sentry_table2"], query
  825. assert meta["dataset"] == "spansMetrics", query
  826. assert meta["fields"]["span.domain"] == "array"
  827. def test_span_domain_array_has_filter(self):
  828. self.store_span_metric(
  829. 321,
  830. internal_metric=constants.SELF_TIME_LIGHT,
  831. timestamp=self.min_ago,
  832. tags={"span.domain": ""},
  833. )
  834. self.store_span_metric(
  835. 21,
  836. internal_metric=constants.SELF_TIME_LIGHT,
  837. timestamp=self.min_ago,
  838. tags={"span.domain": ",sentry_table1,sentry_table2,"},
  839. )
  840. response = self.do_request(
  841. {
  842. "field": ["span.domain", "p75(span.self_time)"],
  843. "query": "has:span.domain",
  844. "project": self.project.id,
  845. "dataset": "spansMetrics",
  846. }
  847. )
  848. assert response.status_code == 200, response.content
  849. data = response.data["data"]
  850. meta = response.data["meta"]
  851. assert len(data) == 1
  852. assert data[0]["span.domain"] == ["sentry_table1", "sentry_table2"]
  853. assert meta["dataset"] == "spansMetrics"
  854. response = self.do_request(
  855. {
  856. "field": ["span.domain", "p75(span.self_time)"],
  857. "query": "!has:span.domain",
  858. "project": self.project.id,
  859. "dataset": "spansMetrics",
  860. }
  861. )
  862. assert response.status_code == 200, response.content
  863. data = response.data["data"]
  864. meta = response.data["meta"]
  865. assert len(data) == 1
  866. assert meta["dataset"] == "spansMetrics"
  867. assert meta["fields"]["span.domain"] == "array"
  868. def test_unique_values_span_domain(self):
  869. self.store_span_metric(
  870. 321,
  871. internal_metric=constants.SELF_TIME_LIGHT,
  872. timestamp=self.min_ago,
  873. tags={"span.domain": ",sentry_table1,"},
  874. )
  875. self.store_span_metric(
  876. 21,
  877. internal_metric=constants.SELF_TIME_LIGHT,
  878. timestamp=self.min_ago,
  879. tags={"span.domain": ",sentry_table2,sentry_table3,"},
  880. )
  881. response = self.do_request(
  882. {
  883. "field": ["unique.span_domains", "count()"],
  884. "query": "",
  885. "orderby": "unique.span_domains",
  886. "project": self.project.id,
  887. "dataset": "spansMetrics",
  888. }
  889. )
  890. assert response.status_code == 200, response.content
  891. data = response.data["data"]
  892. meta = response.data["meta"]
  893. assert len(data) == 3
  894. assert data[0]["unique.span_domains"] == "sentry_table1"
  895. assert data[1]["unique.span_domains"] == "sentry_table2"
  896. assert data[2]["unique.span_domains"] == "sentry_table3"
  897. assert meta["fields"]["unique.span_domains"] == "string"
  898. def test_unique_values_span_domain_with_filter(self):
  899. self.store_span_metric(
  900. 321,
  901. internal_metric=constants.SELF_TIME_LIGHT,
  902. timestamp=self.min_ago,
  903. tags={"span.domain": ",sentry_tible1,"},
  904. )
  905. self.store_span_metric(
  906. 21,
  907. internal_metric=constants.SELF_TIME_LIGHT,
  908. timestamp=self.min_ago,
  909. tags={"span.domain": ",sentry_table2,sentry_table3,"},
  910. )
  911. response = self.do_request(
  912. {
  913. "field": ["unique.span_domains", "count()"],
  914. "query": "span.domain:sentry_tab*",
  915. "orderby": "unique.span_domains",
  916. "project": self.project.id,
  917. "dataset": "spansMetrics",
  918. }
  919. )
  920. assert response.status_code == 200, response.content
  921. data = response.data["data"]
  922. meta = response.data["meta"]
  923. assert len(data) == 2
  924. assert data[0]["unique.span_domains"] == "sentry_table2"
  925. assert data[1]["unique.span_domains"] == "sentry_table3"
  926. assert meta["fields"]["unique.span_domains"] == "string"
  927. def test_avg_if(self):
  928. self.store_span_metric(
  929. 100,
  930. internal_metric=constants.SELF_TIME_LIGHT,
  931. timestamp=self.min_ago,
  932. tags={"release": "foo"},
  933. )
  934. self.store_span_metric(
  935. 200,
  936. internal_metric=constants.SELF_TIME_LIGHT,
  937. timestamp=self.min_ago,
  938. tags={"release": "foo"},
  939. )
  940. self.store_span_metric(
  941. 10,
  942. internal_metric=constants.SELF_TIME_LIGHT,
  943. timestamp=self.min_ago,
  944. tags={"release": "bar"},
  945. )
  946. self.store_span_metric(
  947. 300,
  948. internal_metric=constants.SELF_TIME_LIGHT,
  949. timestamp=self.min_ago,
  950. tags={"span.op": "queue.process"},
  951. )
  952. response = self.do_request(
  953. {
  954. "field": [
  955. "avg_if(span.self_time, release, foo)",
  956. "avg_if(span.self_time, span.op, queue.process)",
  957. ],
  958. "query": "",
  959. "project": self.project.id,
  960. "dataset": "spansMetrics",
  961. }
  962. )
  963. assert response.status_code == 200, response.content
  964. data = response.data["data"]
  965. meta = response.data["meta"]
  966. assert len(data) == 1
  967. assert data[0]["avg_if(span.self_time, release, foo)"] == 150
  968. assert data[0]["avg_if(span.self_time, span.op, queue.process)"] == 300
  969. assert meta["dataset"] == "spansMetrics"
  970. assert meta["fields"]["avg_if(span.self_time, release, foo)"] == "duration"
  971. assert meta["fields"]["avg_if(span.self_time, span.op, queue.process)"] == "duration"
  972. def test_device_class(self):
  973. self.store_span_metric(
  974. 123,
  975. internal_metric=constants.SELF_TIME_LIGHT,
  976. timestamp=self.min_ago,
  977. tags={"device.class": "1"},
  978. )
  979. self.store_span_metric(
  980. 678,
  981. internal_metric=constants.SELF_TIME_LIGHT,
  982. timestamp=self.min_ago,
  983. tags={"device.class": "2"},
  984. )
  985. self.store_span_metric(
  986. 999,
  987. internal_metric=constants.SELF_TIME_LIGHT,
  988. timestamp=self.min_ago,
  989. tags={"device.class": ""},
  990. )
  991. response = self.do_request(
  992. {
  993. "field": ["device.class", "p95()"],
  994. "query": "",
  995. "orderby": "p95()",
  996. "project": self.project.id,
  997. "dataset": "spansMetrics",
  998. }
  999. )
  1000. assert response.status_code == 200, response.content
  1001. data = response.data["data"]
  1002. meta = response.data["meta"]
  1003. assert len(data) == 3
  1004. # Need to actually check the dict since the level for 1 isn't guaranteed to stay `low` or `medium`
  1005. assert data[0]["device.class"] == map_device_class_level("1")
  1006. assert data[1]["device.class"] == map_device_class_level("2")
  1007. assert data[2]["device.class"] == "Unknown"
  1008. assert meta["fields"]["device.class"] == "string"
  1009. def test_device_class_filter(self):
  1010. self.store_span_metric(
  1011. 123,
  1012. internal_metric=constants.SELF_TIME_LIGHT,
  1013. timestamp=self.min_ago,
  1014. tags={"device.class": "1"},
  1015. )
  1016. # Need to actually check the dict since the level for 1 isn't guaranteed to stay `low`
  1017. level = map_device_class_level("1")
  1018. response = self.do_request(
  1019. {
  1020. "field": ["device.class", "count()"],
  1021. "query": f"device.class:{level}",
  1022. "orderby": "count()",
  1023. "project": self.project.id,
  1024. "dataset": "spansMetrics",
  1025. }
  1026. )
  1027. assert response.status_code == 200, response.content
  1028. data = response.data["data"]
  1029. meta = response.data["meta"]
  1030. assert len(data) == 1
  1031. assert data[0]["device.class"] == level
  1032. assert meta["fields"]["device.class"] == "string"
  1033. def test_device_class_filter_unknown(self):
  1034. self.store_span_metric(
  1035. 123,
  1036. internal_metric=constants.SELF_TIME_LIGHT,
  1037. timestamp=self.min_ago,
  1038. tags={"device.class": ""},
  1039. )
  1040. response = self.do_request(
  1041. {
  1042. "field": ["device.class", "count()"],
  1043. "query": "device.class:Unknown",
  1044. "orderby": "count()",
  1045. "project": self.project.id,
  1046. "dataset": "spansMetrics",
  1047. }
  1048. )
  1049. assert response.status_code == 200, response.content
  1050. data = response.data["data"]
  1051. meta = response.data["meta"]
  1052. assert len(data) == 1
  1053. assert data[0]["device.class"] == "Unknown"
  1054. assert meta["fields"]["device.class"] == "string"
  1055. def test_cache_hit_rate(self):
  1056. self.store_span_metric(
  1057. 1,
  1058. internal_metric=constants.SELF_TIME_LIGHT,
  1059. timestamp=self.min_ago,
  1060. tags={"cache.hit": "true"},
  1061. )
  1062. self.store_span_metric(
  1063. 1,
  1064. internal_metric=constants.SELF_TIME_LIGHT,
  1065. timestamp=self.min_ago,
  1066. tags={"cache.hit": "false"},
  1067. )
  1068. response = self.do_request(
  1069. {
  1070. "field": ["cache_hit_rate()"],
  1071. "query": "",
  1072. "project": self.project.id,
  1073. "dataset": "spansMetrics",
  1074. }
  1075. )
  1076. assert response.status_code == 200, response.content
  1077. data = response.data["data"]
  1078. meta = response.data["meta"]
  1079. assert len(data) == 1
  1080. assert data[0]["cache_hit_rate()"] == 0.5
  1081. assert meta["dataset"] == "spansMetrics"
  1082. assert meta["fields"]["cache_hit_rate()"] == "percentage"
  1083. def test_cache_miss_rate(self):
  1084. self.store_span_metric(
  1085. 1,
  1086. internal_metric=constants.SELF_TIME_LIGHT,
  1087. timestamp=self.min_ago,
  1088. tags={"cache.hit": "true"},
  1089. )
  1090. self.store_span_metric(
  1091. 1,
  1092. internal_metric=constants.SELF_TIME_LIGHT,
  1093. timestamp=self.min_ago,
  1094. tags={"cache.hit": "false"},
  1095. )
  1096. self.store_span_metric(
  1097. 1,
  1098. internal_metric=constants.SELF_TIME_LIGHT,
  1099. timestamp=self.min_ago,
  1100. tags={"cache.hit": "false"},
  1101. )
  1102. self.store_span_metric(
  1103. 1,
  1104. internal_metric=constants.SELF_TIME_LIGHT,
  1105. timestamp=self.min_ago,
  1106. tags={"cache.hit": "false"},
  1107. )
  1108. response = self.do_request(
  1109. {
  1110. "field": ["cache_miss_rate()"],
  1111. "query": "",
  1112. "project": self.project.id,
  1113. "dataset": "spansMetrics",
  1114. }
  1115. )
  1116. assert response.status_code == 200, response.content
  1117. data = response.data["data"]
  1118. meta = response.data["meta"]
  1119. assert len(data) == 1
  1120. assert data[0]["cache_miss_rate()"] == 0.75
  1121. assert meta["dataset"] == "spansMetrics"
  1122. assert meta["fields"]["cache_miss_rate()"] == "percentage"
  1123. def test_http_response_rate(self):
  1124. self.store_span_metric(
  1125. 1,
  1126. internal_metric=constants.SELF_TIME_LIGHT,
  1127. timestamp=self.min_ago,
  1128. tags={"span.status_code": "200"},
  1129. )
  1130. self.store_span_metric(
  1131. 3,
  1132. internal_metric=constants.SELF_TIME_LIGHT,
  1133. timestamp=self.min_ago,
  1134. tags={"span.status_code": "301"},
  1135. )
  1136. self.store_span_metric(
  1137. 3,
  1138. internal_metric=constants.SELF_TIME_LIGHT,
  1139. timestamp=self.min_ago,
  1140. tags={"span.status_code": "404"},
  1141. )
  1142. self.store_span_metric(
  1143. 4,
  1144. internal_metric=constants.SELF_TIME_LIGHT,
  1145. timestamp=self.min_ago,
  1146. tags={"span.status_code": "503"},
  1147. )
  1148. self.store_span_metric(
  1149. 5,
  1150. internal_metric=constants.SELF_TIME_LIGHT,
  1151. timestamp=self.min_ago,
  1152. tags={"span.status_code": "501"},
  1153. )
  1154. response = self.do_request(
  1155. {
  1156. "field": [
  1157. "http_response_rate(200)", # By exact code
  1158. "http_response_rate(3)", # By code class
  1159. "http_response_rate(4)",
  1160. "http_response_rate(5)",
  1161. ],
  1162. "query": "",
  1163. "project": self.project.id,
  1164. "dataset": "spansMetrics",
  1165. }
  1166. )
  1167. assert response.status_code == 200, response.content
  1168. data = response.data["data"]
  1169. assert len(data) == 1
  1170. assert data[0]["http_response_rate(200)"] == 0.2
  1171. assert data[0]["http_response_rate(3)"] == 0.2
  1172. assert data[0]["http_response_rate(4)"] == 0.2
  1173. assert data[0]["http_response_rate(5)"] == 0.4
  1174. meta = response.data["meta"]
  1175. assert meta["dataset"] == "spansMetrics"
  1176. assert meta["fields"]["http_response_rate(200)"] == "percentage"
  1177. def test_regression_score_regression(self):
  1178. # This span increases in duration
  1179. self.store_span_metric(
  1180. 1,
  1181. internal_metric=SPAN_DURATION_MRI,
  1182. timestamp=self.six_min_ago,
  1183. tags={"transaction": "/api/0/projects/", "span.description": "Regressed Span"},
  1184. project=self.project.id,
  1185. )
  1186. self.store_span_metric(
  1187. 100,
  1188. internal_metric=SPAN_DURATION_MRI,
  1189. timestamp=self.min_ago,
  1190. tags={"transaction": "/api/0/projects/", "span.description": "Regressed Span"},
  1191. project=self.project.id,
  1192. )
  1193. # This span stays the same
  1194. self.store_span_metric(
  1195. 1,
  1196. internal_metric=SPAN_DURATION_MRI,
  1197. timestamp=self.three_days_ago,
  1198. tags={"transaction": "/api/0/projects/", "span.description": "Non-regressed"},
  1199. project=self.project.id,
  1200. )
  1201. self.store_span_metric(
  1202. 1,
  1203. internal_metric=SPAN_DURATION_MRI,
  1204. timestamp=self.min_ago,
  1205. tags={"transaction": "/api/0/projects/", "span.description": "Non-regressed"},
  1206. project=self.project.id,
  1207. )
  1208. response = self.do_request(
  1209. {
  1210. "field": [
  1211. "span.description",
  1212. f"regression_score(span.duration,{int(self.two_min_ago.timestamp())})",
  1213. ],
  1214. "query": "transaction:/api/0/projects/",
  1215. "dataset": "spansMetrics",
  1216. "orderby": [
  1217. f"-regression_score(span.duration,{int(self.two_min_ago.timestamp())})"
  1218. ],
  1219. "start": (self.six_min_ago - timedelta(minutes=1)).isoformat(),
  1220. "end": before_now(minutes=0),
  1221. }
  1222. )
  1223. assert response.status_code == 200, response.content
  1224. data = response.data["data"]
  1225. assert len(data) == 2
  1226. assert [row["span.description"] for row in data] == ["Regressed Span", "Non-regressed"]
  1227. def test_regression_score_added_span(self):
  1228. # This span only exists after the breakpoint
  1229. self.store_span_metric(
  1230. 100,
  1231. internal_metric=SPAN_DURATION_MRI,
  1232. timestamp=self.min_ago,
  1233. tags={"transaction": "/api/0/projects/", "span.description": "Added span"},
  1234. project=self.project.id,
  1235. )
  1236. # This span stays the same
  1237. self.store_span_metric(
  1238. 1,
  1239. internal_metric=SPAN_DURATION_MRI,
  1240. timestamp=self.three_days_ago,
  1241. tags={"transaction": "/api/0/projects/", "span.description": "Non-regressed"},
  1242. project=self.project.id,
  1243. )
  1244. self.store_span_metric(
  1245. 1,
  1246. internal_metric=SPAN_DURATION_MRI,
  1247. timestamp=self.min_ago,
  1248. tags={"transaction": "/api/0/projects/", "span.description": "Non-regressed"},
  1249. project=self.project.id,
  1250. )
  1251. response = self.do_request(
  1252. {
  1253. "field": [
  1254. "span.description",
  1255. f"regression_score(span.duration,{int(self.two_min_ago.timestamp())})",
  1256. ],
  1257. "query": "transaction:/api/0/projects/",
  1258. "dataset": "spansMetrics",
  1259. "orderby": [
  1260. f"-regression_score(span.duration,{int(self.two_min_ago.timestamp())})"
  1261. ],
  1262. "start": (self.six_min_ago - timedelta(minutes=1)).isoformat(),
  1263. "end": before_now(minutes=0),
  1264. }
  1265. )
  1266. assert response.status_code == 200, response.content
  1267. data = response.data["data"]
  1268. assert len(data) == 2
  1269. assert [row["span.description"] for row in data] == ["Added span", "Non-regressed"]
  1270. def test_regression_score_removed_span(self):
  1271. # This span only exists before the breakpoint
  1272. self.store_span_metric(
  1273. 100,
  1274. internal_metric=SPAN_DURATION_MRI,
  1275. timestamp=self.six_min_ago,
  1276. tags={"transaction": "/api/0/projects/", "span.description": "Removed span"},
  1277. project=self.project.id,
  1278. )
  1279. # This span stays the same
  1280. self.store_span_metric(
  1281. 1,
  1282. internal_metric=SPAN_DURATION_MRI,
  1283. timestamp=self.three_days_ago,
  1284. tags={"transaction": "/api/0/projects/", "span.description": "Non-regressed"},
  1285. project=self.project.id,
  1286. )
  1287. self.store_span_metric(
  1288. 1,
  1289. internal_metric=SPAN_DURATION_MRI,
  1290. timestamp=self.min_ago,
  1291. tags={"transaction": "/api/0/projects/", "span.description": "Non-regressed"},
  1292. project=self.project.id,
  1293. )
  1294. response = self.do_request(
  1295. {
  1296. "field": [
  1297. "span.description",
  1298. f"regression_score(span.duration,{int(self.two_min_ago.timestamp())})",
  1299. ],
  1300. "query": "transaction:/api/0/projects/",
  1301. "dataset": "spansMetrics",
  1302. "orderby": [
  1303. f"-regression_score(span.duration,{int(self.two_min_ago.timestamp())})"
  1304. ],
  1305. "start": (self.six_min_ago - timedelta(minutes=1)).isoformat(),
  1306. "end": before_now(minutes=0),
  1307. }
  1308. )
  1309. assert response.status_code == 200, response.content
  1310. data = response.data["data"]
  1311. assert len(data) == 2
  1312. assert [row["span.description"] for row in data] == ["Non-regressed", "Removed span"]
  1313. # The regression score is <0 for removed spans, this can act as
  1314. # a way to filter out removed spans when necessary
  1315. assert data[1][f"regression_score(span.duration,{int(self.two_min_ago.timestamp())})"] < 0
  1316. def test_avg_self_time_by_timestamp(self):
  1317. self.store_span_metric(
  1318. 1,
  1319. internal_metric=constants.SELF_TIME_LIGHT,
  1320. timestamp=self.six_min_ago,
  1321. tags={},
  1322. )
  1323. self.store_span_metric(
  1324. 3,
  1325. internal_metric=constants.SELF_TIME_LIGHT,
  1326. timestamp=self.min_ago,
  1327. tags={},
  1328. )
  1329. response = self.do_request(
  1330. {
  1331. "field": [
  1332. f"avg_by_timestamp(span.self_time,less,{int(self.two_min_ago.timestamp())})",
  1333. f"avg_by_timestamp(span.self_time,greater,{int(self.two_min_ago.timestamp())})",
  1334. ],
  1335. "query": "",
  1336. "project": self.project.id,
  1337. "dataset": "spansMetrics",
  1338. "statsPeriod": "1h",
  1339. }
  1340. )
  1341. assert response.status_code == 200, response.content
  1342. data = response.data["data"]
  1343. assert len(data) == 1
  1344. assert data[0] == {
  1345. f"avg_by_timestamp(span.self_time,less,{int(self.two_min_ago.timestamp())})": 1.0,
  1346. f"avg_by_timestamp(span.self_time,greater,{int(self.two_min_ago.timestamp())})": 3.0,
  1347. }
  1348. def test_avg_self_time_by_timestamp_invalid_condition(self):
  1349. response = self.do_request(
  1350. {
  1351. "field": [
  1352. f"avg_by_timestamp(span.self_time,INVALID_ARG,{int(self.two_min_ago.timestamp())})",
  1353. ],
  1354. "query": "",
  1355. "project": self.project.id,
  1356. "dataset": "spansMetrics",
  1357. "statsPeriod": "1h",
  1358. }
  1359. )
  1360. assert response.status_code == 400, response.content
  1361. assert (
  1362. response.data["detail"]
  1363. == "avg_by_timestamp: condition argument invalid: string must be one of ['greater', 'less']"
  1364. )
  1365. def test_epm_by_timestamp(self):
  1366. self.store_span_metric(
  1367. 1,
  1368. internal_metric=SPAN_DURATION_MRI,
  1369. timestamp=self.six_min_ago,
  1370. tags={},
  1371. )
  1372. # More events occur after the timestamp
  1373. for _ in range(3):
  1374. self.store_span_metric(
  1375. 3,
  1376. internal_metric=SPAN_DURATION_MRI,
  1377. timestamp=self.min_ago,
  1378. tags={},
  1379. )
  1380. response = self.do_request(
  1381. {
  1382. "field": [
  1383. f"epm_by_timestamp(less,{int(self.two_min_ago.timestamp())})",
  1384. f"epm_by_timestamp(greater,{int(self.two_min_ago.timestamp())})",
  1385. ],
  1386. "query": "",
  1387. "project": self.project.id,
  1388. "dataset": "spansMetrics",
  1389. "statsPeriod": "1h",
  1390. }
  1391. )
  1392. assert response.status_code == 200, response.content
  1393. data = response.data["data"]
  1394. assert len(data) == 1
  1395. assert data[0][f"epm_by_timestamp(less,{int(self.two_min_ago.timestamp())})"] < 1.0
  1396. assert data[0][f"epm_by_timestamp(greater,{int(self.two_min_ago.timestamp())})"] > 1.0
  1397. def test_epm_by_timestamp_invalid_condition(self):
  1398. response = self.do_request(
  1399. {
  1400. "field": [
  1401. f"epm_by_timestamp(INVALID_ARG,{int(self.two_min_ago.timestamp())})",
  1402. ],
  1403. "query": "",
  1404. "project": self.project.id,
  1405. "dataset": "spansMetrics",
  1406. "statsPeriod": "1h",
  1407. }
  1408. )
  1409. assert response.status_code == 400, response.content
  1410. assert (
  1411. response.data["detail"]
  1412. == "epm_by_timestamp: condition argument invalid: string must be one of ['greater', 'less']"
  1413. )
  1414. def test_any_function(self):
  1415. for char in "abc":
  1416. for transaction in ["foo", "bar"]:
  1417. self.store_span_metric(
  1418. 1,
  1419. internal_metric=constants.SELF_TIME_LIGHT,
  1420. timestamp=self.six_min_ago,
  1421. tags={"span.description": char, "transaction": transaction},
  1422. )
  1423. response = self.do_request(
  1424. {
  1425. "field": [
  1426. "transaction",
  1427. "any(span.description)",
  1428. ],
  1429. "query": "",
  1430. "orderby": ["transaction"],
  1431. "project": self.project.id,
  1432. "dataset": "spansMetrics",
  1433. "statsPeriod": "1h",
  1434. }
  1435. )
  1436. assert response.status_code == 200, response.content
  1437. assert response.data["data"] == [
  1438. {"transaction": "bar", "any(span.description)": "a"},
  1439. {"transaction": "foo", "any(span.description)": "a"},
  1440. ]
  1441. def test_count_op(self):
  1442. self.store_span_metric(
  1443. 1,
  1444. internal_metric=constants.SELF_TIME_LIGHT,
  1445. timestamp=self.six_min_ago,
  1446. tags={"span.op": "queue.publish"},
  1447. )
  1448. self.store_span_metric(
  1449. 1,
  1450. internal_metric=constants.SELF_TIME_LIGHT,
  1451. timestamp=self.six_min_ago,
  1452. tags={"span.op": "queue.process"},
  1453. )
  1454. response = self.do_request(
  1455. {
  1456. "field": [
  1457. "count_op(queue.publish)",
  1458. "count_op(queue.process)",
  1459. ],
  1460. "query": "",
  1461. "project": self.project.id,
  1462. "dataset": "spansMetrics",
  1463. "statsPeriod": "1h",
  1464. }
  1465. )
  1466. assert response.status_code == 200, response.content
  1467. data = response.data["data"]
  1468. assert data == [
  1469. {"count_op(queue.publish)": 1, "count_op(queue.process)": 1},
  1470. ]
  1471. def test_project_mapping(self):
  1472. self.store_span_metric(
  1473. 1,
  1474. internal_metric=constants.SELF_TIME_LIGHT,
  1475. timestamp=self.six_min_ago,
  1476. tags={},
  1477. )
  1478. # More events occur after the timestamp
  1479. for _ in range(3):
  1480. self.store_span_metric(
  1481. 3,
  1482. internal_metric=constants.SELF_TIME_LIGHT,
  1483. timestamp=self.min_ago,
  1484. tags={},
  1485. )
  1486. response = self.do_request(
  1487. {
  1488. "field": ["project", "project.name", "count()"],
  1489. "query": "",
  1490. "project": self.project.id,
  1491. "dataset": "spansMetrics",
  1492. "statsPeriod": "1h",
  1493. }
  1494. )
  1495. assert response.status_code == 200, response.content
  1496. data = response.data["data"]
  1497. assert data[0]["project"] == self.project.slug
  1498. assert data[0]["project.name"] == self.project.slug
  1499. def test_slow_frames_gauge_metric(self):
  1500. self.store_span_metric(
  1501. {
  1502. "min": 5,
  1503. "max": 5,
  1504. "sum": 5,
  1505. "count": 1,
  1506. "last": 5,
  1507. },
  1508. entity="metrics_gauges",
  1509. metric="mobile.slow_frames",
  1510. timestamp=self.six_min_ago,
  1511. tags={"release": "foo"},
  1512. )
  1513. self.store_span_metric(
  1514. {
  1515. "min": 10,
  1516. "max": 10,
  1517. "sum": 10,
  1518. "count": 1,
  1519. "last": 10,
  1520. },
  1521. entity="metrics_gauges",
  1522. metric="mobile.slow_frames",
  1523. timestamp=self.six_min_ago,
  1524. tags={"release": "bar"},
  1525. )
  1526. response = self.do_request(
  1527. {
  1528. "field": [
  1529. "avg_if(mobile.slow_frames,release,foo)",
  1530. "avg_if(mobile.slow_frames,release,bar)",
  1531. "avg_compare(mobile.slow_frames,release,foo,bar)",
  1532. ],
  1533. "query": "",
  1534. "project": self.project.id,
  1535. "dataset": "spansMetrics",
  1536. "statsPeriod": "1h",
  1537. }
  1538. )
  1539. assert response.status_code == 200, response.content
  1540. data = response.data["data"]
  1541. assert data == [
  1542. {
  1543. "avg_compare(mobile.slow_frames,release,foo,bar)": 1.0,
  1544. "avg_if(mobile.slow_frames,release,foo)": 5.0,
  1545. "avg_if(mobile.slow_frames,release,bar)": 10.0,
  1546. }
  1547. ]
  1548. def test_resolve_messaging_message_receive_latency_gauge(self):
  1549. self.store_span_metric(
  1550. {
  1551. "min": 5,
  1552. "max": 5,
  1553. "sum": 5,
  1554. "count": 1,
  1555. "last": 5,
  1556. },
  1557. entity="metrics_gauges",
  1558. metric="messaging.message.receive.latency",
  1559. timestamp=self.six_min_ago,
  1560. tags={"messaging.destination.name": "foo", "trace.status": "ok"},
  1561. )
  1562. self.store_span_metric(
  1563. {
  1564. "min": 10,
  1565. "max": 10,
  1566. "sum": 10,
  1567. "count": 1,
  1568. "last": 10,
  1569. },
  1570. entity="metrics_gauges",
  1571. metric="messaging.message.receive.latency",
  1572. timestamp=self.six_min_ago,
  1573. tags={"messaging.destination.name": "bar", "trace.status": "ok"},
  1574. )
  1575. response = self.do_request(
  1576. {
  1577. "field": [
  1578. "messaging.destination.name",
  1579. "trace.status",
  1580. "avg(messaging.message.receive.latency)",
  1581. ],
  1582. "query": "",
  1583. "project": self.project.id,
  1584. "dataset": "spansMetrics",
  1585. "statsPeriod": "1h",
  1586. }
  1587. )
  1588. assert response.status_code == 200, response.content
  1589. data = response.data["data"]
  1590. assert data == [
  1591. {
  1592. "messaging.destination.name": "bar",
  1593. "trace.status": "ok",
  1594. "avg(messaging.message.receive.latency)": 10.0,
  1595. },
  1596. {
  1597. "messaging.destination.name": "foo",
  1598. "trace.status": "ok",
  1599. "avg(messaging.message.receive.latency)": 5.0,
  1600. },
  1601. ]
  1602. def test_messaging_does_not_exist_as_metric(self):
  1603. self.store_span_metric(
  1604. 100,
  1605. internal_metric=constants.SPAN_METRICS_MAP["span.duration"],
  1606. tags={"messaging.destination.name": "foo", "trace.status": "ok"},
  1607. timestamp=self.min_ago,
  1608. )
  1609. response = self.do_request(
  1610. {
  1611. "field": [
  1612. "messaging.destination.name",
  1613. "trace.status",
  1614. "avg(messaging.message.receive.latency)",
  1615. "avg(span.duration)",
  1616. ],
  1617. "query": "",
  1618. "project": self.project.id,
  1619. "dataset": "spansMetrics",
  1620. "statsPeriod": "1h",
  1621. }
  1622. )
  1623. assert response.status_code == 200, response.content
  1624. data = response.data["data"]
  1625. assert data == [
  1626. {
  1627. "messaging.destination.name": "foo",
  1628. "trace.status": "ok",
  1629. "avg(messaging.message.receive.latency)": None,
  1630. "avg(span.duration)": 100,
  1631. },
  1632. ]
  1633. meta = response.data["meta"]
  1634. assert meta["fields"]["avg(messaging.message.receive.latency)"] == "null"
  1635. def test_trace_status_rate(self):
  1636. self.store_span_metric(
  1637. 1,
  1638. internal_metric=constants.SELF_TIME_LIGHT,
  1639. timestamp=self.min_ago,
  1640. tags={"trace.status": "unknown"},
  1641. )
  1642. self.store_span_metric(
  1643. 3,
  1644. internal_metric=constants.SELF_TIME_LIGHT,
  1645. timestamp=self.min_ago,
  1646. tags={"trace.status": "internal_error"},
  1647. )
  1648. self.store_span_metric(
  1649. 3,
  1650. internal_metric=constants.SELF_TIME_LIGHT,
  1651. timestamp=self.min_ago,
  1652. tags={"trace.status": "unauthenticated"},
  1653. )
  1654. self.store_span_metric(
  1655. 4,
  1656. internal_metric=constants.SELF_TIME_LIGHT,
  1657. timestamp=self.min_ago,
  1658. tags={"trace.status": "ok"},
  1659. )
  1660. self.store_span_metric(
  1661. 5,
  1662. internal_metric=constants.SELF_TIME_LIGHT,
  1663. timestamp=self.min_ago,
  1664. tags={"trace.status": "ok"},
  1665. )
  1666. response = self.do_request(
  1667. {
  1668. "field": [
  1669. "trace_status_rate(ok)",
  1670. "trace_status_rate(unknown)",
  1671. "trace_status_rate(internal_error)",
  1672. "trace_status_rate(unauthenticated)",
  1673. ],
  1674. "query": "",
  1675. "project": self.project.id,
  1676. "dataset": "spansMetrics",
  1677. "statsPeriod": "1h",
  1678. }
  1679. )
  1680. assert response.status_code == 200, response.content
  1681. data = response.data["data"]
  1682. assert len(data) == 1
  1683. assert data[0]["trace_status_rate(ok)"] == 0.4
  1684. assert data[0]["trace_status_rate(unknown)"] == 0.2
  1685. assert data[0]["trace_status_rate(internal_error)"] == 0.2
  1686. assert data[0]["trace_status_rate(unauthenticated)"] == 0.2
  1687. meta = response.data["meta"]
  1688. assert meta["dataset"] == "spansMetrics"
  1689. assert meta["fields"]["trace_status_rate(ok)"] == "percentage"
  1690. assert meta["fields"]["trace_status_rate(unknown)"] == "percentage"
  1691. assert meta["fields"]["trace_status_rate(internal_error)"] == "percentage"
  1692. assert meta["fields"]["trace_status_rate(unauthenticated)"] == "percentage"
  1693. def test_trace_error_rate(self):
  1694. self.store_span_metric(
  1695. 1,
  1696. internal_metric=constants.SELF_TIME_LIGHT,
  1697. timestamp=self.min_ago,
  1698. tags={"trace.status": "unknown"},
  1699. )
  1700. self.store_span_metric(
  1701. 3,
  1702. internal_metric=constants.SELF_TIME_LIGHT,
  1703. timestamp=self.min_ago,
  1704. tags={"trace.status": "internal_error"},
  1705. )
  1706. self.store_span_metric(
  1707. 3,
  1708. internal_metric=constants.SELF_TIME_LIGHT,
  1709. timestamp=self.min_ago,
  1710. tags={"trace.status": "unauthenticated"},
  1711. )
  1712. self.store_span_metric(
  1713. 4,
  1714. internal_metric=constants.SELF_TIME_LIGHT,
  1715. timestamp=self.min_ago,
  1716. tags={"trace.status": "ok"},
  1717. )
  1718. self.store_span_metric(
  1719. 5,
  1720. internal_metric=constants.SELF_TIME_LIGHT,
  1721. timestamp=self.min_ago,
  1722. tags={"trace.status": "ok"},
  1723. )
  1724. response = self.do_request(
  1725. {
  1726. "field": [
  1727. "trace_error_rate()",
  1728. ],
  1729. "query": "",
  1730. "project": self.project.id,
  1731. "dataset": "spansMetrics",
  1732. }
  1733. )
  1734. assert response.status_code == 200, response.content
  1735. data = response.data["data"]
  1736. assert len(data) == 1
  1737. assert data[0]["trace_error_rate()"] == 0.4
  1738. meta = response.data["meta"]
  1739. assert meta["dataset"] == "spansMetrics"
  1740. assert meta["fields"]["trace_error_rate()"] == "percentage"
  1741. class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithMetricLayer(
  1742. OrganizationEventsMetricsEnhancedPerformanceEndpointTest
  1743. ):
  1744. def setUp(self):
  1745. super().setUp()
  1746. self.features["organizations:use-metrics-layer"] = True
  1747. @pytest.mark.xfail(reason="Not implemented")
  1748. def test_time_spent_percentage(self):
  1749. super().test_time_spent_percentage()
  1750. @pytest.mark.xfail(reason="Not implemented")
  1751. def test_time_spent_percentage_local(self):
  1752. super().test_time_spent_percentage_local()
  1753. @pytest.mark.xfail(reason="Not implemented")
  1754. def test_time_spent_percentage_on_span_duration(self):
  1755. super().test_time_spent_percentage_on_span_duration()
  1756. @pytest.mark.xfail(reason="Cannot group by function 'if'")
  1757. def test_span_module(self):
  1758. super().test_span_module()
  1759. @pytest.mark.xfail(reason="Cannot search by tags")
  1760. def test_tag_search(self):
  1761. super().test_tag_search()
  1762. @pytest.mark.xfail(reason="Cannot search by tags")
  1763. def test_free_text_search(self):
  1764. super().test_free_text_search()
  1765. @pytest.mark.xfail(reason="Not implemented")
  1766. def test_avg_compare(self):
  1767. super().test_avg_compare()
  1768. @pytest.mark.xfail(reason="Not implemented")
  1769. def test_span_domain_array(self):
  1770. super().test_span_domain_array()
  1771. @pytest.mark.xfail(reason="Not implemented")
  1772. def test_span_domain_array_filter(self):
  1773. super().test_span_domain_array_filter()
  1774. @pytest.mark.xfail(reason="Not implemented")
  1775. def test_span_domain_array_filter_wildcard(self):
  1776. super().test_span_domain_array_filter_wildcard()
  1777. @pytest.mark.xfail(reason="Not implemented")
  1778. def test_span_domain_array_has_filter(self):
  1779. super().test_span_domain_array_has_filter()
  1780. @pytest.mark.xfail(reason="Not implemented")
  1781. def test_unique_values_span_domain(self):
  1782. super().test_unique_values_span_domain()
  1783. @pytest.mark.xfail(reason="Not implemented")
  1784. def test_unique_values_span_domain_with_filter(self):
  1785. super().test_unique_values_span_domain_with_filter()
  1786. @pytest.mark.xfail(reason="Not implemented")
  1787. def test_avg_if(self):
  1788. super().test_avg_if()
  1789. @pytest.mark.xfail(reason="Not implemented")
  1790. def test_device_class_filter(self):
  1791. super().test_device_class_filter()
  1792. @pytest.mark.xfail(reason="Not implemented")
  1793. def test_device_class(self):
  1794. super().test_device_class()
  1795. @pytest.mark.xfail(reason="Not implemented")
  1796. def test_count_op(self):
  1797. super().test_count_op()