test_organization_events_span_metrics.py 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. import pytest
  2. from django.urls import reverse
  3. from sentry.search.events import constants
  4. from sentry.search.utils import map_device_class_level
  5. from sentry.testutils.cases import MetricsEnhancedPerformanceTestCase
  6. from sentry.testutils.helpers.datetime import before_now
  7. from sentry.testutils.silo import region_silo_test
  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. response = self.do_request(
  876. {
  877. "field": ["avg_if(span.self_time, release, foo)"],
  878. "query": "",
  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) == 1
  887. assert data[0]["avg_if(span.self_time, release, foo)"] == 150
  888. assert meta["dataset"] == "spansMetrics"
  889. assert meta["fields"]["avg_if(span.self_time, release, foo)"] == "duration"
  890. def test_device_class(self):
  891. self.store_span_metric(
  892. 123,
  893. internal_metric=constants.SELF_TIME_LIGHT,
  894. timestamp=self.min_ago,
  895. tags={"device.class": "1"},
  896. )
  897. self.store_span_metric(
  898. 678,
  899. internal_metric=constants.SELF_TIME_LIGHT,
  900. timestamp=self.min_ago,
  901. tags={"device.class": "2"},
  902. )
  903. self.store_span_metric(
  904. 999,
  905. internal_metric=constants.SELF_TIME_LIGHT,
  906. timestamp=self.min_ago,
  907. tags={"device.class": ""},
  908. )
  909. response = self.do_request(
  910. {
  911. "field": ["device.class", "p95()"],
  912. "query": "",
  913. "orderby": "p95()",
  914. "project": self.project.id,
  915. "dataset": "spansMetrics",
  916. }
  917. )
  918. assert response.status_code == 200, response.content
  919. data = response.data["data"]
  920. meta = response.data["meta"]
  921. assert len(data) == 3
  922. # Need to actually check the dict since the level for 1 isn't guaranteed to stay `low` or `medium`
  923. assert data[0]["device.class"] == map_device_class_level("1")
  924. assert data[1]["device.class"] == map_device_class_level("2")
  925. assert data[2]["device.class"] == "Unknown"
  926. assert meta["fields"]["device.class"] == "string"
  927. def test_device_class_filter(self):
  928. self.store_span_metric(
  929. 123,
  930. internal_metric=constants.SELF_TIME_LIGHT,
  931. timestamp=self.min_ago,
  932. tags={"device.class": "1"},
  933. )
  934. # Need to actually check the dict since the level for 1 isn't guaranteed to stay `low`
  935. level = map_device_class_level("1")
  936. response = self.do_request(
  937. {
  938. "field": ["device.class", "count()"],
  939. "query": f"device.class:{level}",
  940. "orderby": "count()",
  941. "project": self.project.id,
  942. "dataset": "spansMetrics",
  943. }
  944. )
  945. assert response.status_code == 200, response.content
  946. data = response.data["data"]
  947. meta = response.data["meta"]
  948. assert len(data) == 1
  949. assert data[0]["device.class"] == level
  950. assert meta["fields"]["device.class"] == "string"
  951. @region_silo_test
  952. class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithMetricLayer(
  953. OrganizationEventsMetricsEnhancedPerformanceEndpointTest
  954. ):
  955. def setUp(self):
  956. super().setUp()
  957. self.features["organizations:use-metrics-layer"] = True
  958. @pytest.mark.xfail(reason="Not implemented")
  959. def test_time_spent_percentage(self):
  960. super().test_time_spent_percentage()
  961. @pytest.mark.xfail(reason="Not implemented")
  962. def test_time_spent_percentage_local(self):
  963. super().test_time_spent_percentage_local()
  964. @pytest.mark.xfail(reason="Cannot group by function 'if'")
  965. def test_span_module(self):
  966. super().test_span_module()
  967. @pytest.mark.xfail(reason="Cannot search by tags")
  968. def test_tag_search(self):
  969. super().test_tag_search()
  970. @pytest.mark.xfail(reason="Cannot search by tags")
  971. def test_free_text_search(self):
  972. super().test_free_text_search()
  973. @pytest.mark.xfail(reason="Not implemented")
  974. def test_avg_compare(self):
  975. super().test_avg_compare()
  976. @pytest.mark.xfail(reason="Not implemented")
  977. def test_span_domain_array(self):
  978. super().test_span_domain_array()
  979. @pytest.mark.xfail(reason="Not implemented")
  980. def test_span_domain_array_filter(self):
  981. super().test_span_domain_array_filter()
  982. @pytest.mark.xfail(reason="Not implemented")
  983. def test_span_domain_array_filter_wildcard(self):
  984. super().test_span_domain_array_filter_wildcard()
  985. @pytest.mark.xfail(reason="Not implemented")
  986. def test_span_domain_array_has_filter(self):
  987. super().test_span_domain_array_has_filter()
  988. @pytest.mark.xfail(reason="Not implemented")
  989. def test_unique_values_span_domain(self):
  990. super().test_unique_values_span_domain()
  991. @pytest.mark.xfail(reason="Not implemented")
  992. def test_unique_values_span_domain_with_filter(self):
  993. super().test_unique_values_span_domain_with_filter()
  994. @pytest.mark.xfail(reason="Not implemented")
  995. def test_avg_if(self):
  996. super().test_avg_if()
  997. @pytest.mark.xfail(reason="Not implemented")
  998. def test_device_class_filter(self):
  999. super().test_device_class_filter()
  1000. @pytest.mark.xfail(reason="Not implemented")
  1001. def test_device_class(self):
  1002. super().test_device_class()