test_organization_events_span_metrics.py 58 KB

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