test_organization_events_span_metrics.py 65 KB

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