test_organization_events_span_metrics.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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_use_self_time_light(self):
  406. self.store_span_metric(
  407. 100,
  408. internal_metric=constants.SELF_TIME_LIGHT,
  409. tags={"transaction": "foo_transaction"},
  410. timestamp=self.min_ago,
  411. )
  412. response = self.do_request(
  413. {
  414. "field": ["p50(span.self_time)"],
  415. # Should be 0 since its filtering on transaction
  416. "query": "transaction:foo_transaction",
  417. "orderby": ["-p50(span.self_time)"],
  418. "project": self.project.id,
  419. "dataset": "spansMetrics",
  420. "statsPeriod": "10m",
  421. }
  422. )
  423. assert response.status_code == 200, response.content
  424. data = response.data["data"]
  425. meta = response.data["meta"]
  426. assert len(data) == 1
  427. assert data[0]["p50(span.self_time)"] == 0
  428. assert meta["dataset"] == "spansMetrics"
  429. assert meta["fields"]["p50(span.self_time)"] == "duration"
  430. response = self.do_request(
  431. {
  432. # Should be 0 since it has a transaction column
  433. "field": ["transaction", "p50(span.self_time)"],
  434. "query": "",
  435. "orderby": ["-p50(span.self_time)"],
  436. "project": self.project.id,
  437. "dataset": "spansMetrics",
  438. "statsPeriod": "10m",
  439. }
  440. )
  441. assert response.status_code == 200, response.content
  442. data = response.data["data"]
  443. meta = response.data["meta"]
  444. assert len(data) == 0
  445. response = self.do_request(
  446. {
  447. "field": ["p50(span.self_time)"],
  448. # Should be 100 since its not filtering on transaction
  449. "query": "",
  450. "orderby": ["-p50(span.self_time)"],
  451. "project": self.project.id,
  452. "dataset": "spansMetrics",
  453. "statsPeriod": "10m",
  454. }
  455. )
  456. assert response.status_code == 200, response.content
  457. data = response.data["data"]
  458. meta = response.data["meta"]
  459. assert len(data) == 1
  460. assert data[0]["p50(span.self_time)"] == 100
  461. assert meta["dataset"] == "spansMetrics"
  462. assert meta["fields"]["p50(span.self_time)"] == "duration"
  463. def test_span_module(self):
  464. self.store_span_metric(
  465. 1,
  466. internal_metric=constants.SELF_TIME_LIGHT,
  467. timestamp=self.six_min_ago,
  468. tags={"span.category": "http", "span.description": "f"},
  469. )
  470. self.store_span_metric(
  471. 3,
  472. internal_metric=constants.SELF_TIME_LIGHT,
  473. timestamp=self.six_min_ago,
  474. tags={"span.category": "db", "span.description": "e"},
  475. )
  476. self.store_span_metric(
  477. 5,
  478. internal_metric=constants.SELF_TIME_LIGHT,
  479. timestamp=self.six_min_ago,
  480. tags={"span.category": "foobar", "span.description": "d"},
  481. )
  482. self.store_span_metric(
  483. 7,
  484. internal_metric=constants.SELF_TIME_LIGHT,
  485. timestamp=self.six_min_ago,
  486. tags={"span.category": "cache", "span.description": "c"},
  487. )
  488. self.store_span_metric(
  489. 9,
  490. internal_metric=constants.SELF_TIME_LIGHT,
  491. timestamp=self.six_min_ago,
  492. tags={"span.category": "db", "span.op": "db.redis", "span.description": "b"},
  493. )
  494. self.store_span_metric(
  495. 11,
  496. internal_metric=constants.SELF_TIME_LIGHT,
  497. timestamp=self.six_min_ago,
  498. tags={"span.category": "db", "span.op": "db.sql.room", "span.description": "a"},
  499. )
  500. response = self.do_request(
  501. {
  502. "field": ["span.module", "span.description", "p50(span.self_time)"],
  503. "query": "",
  504. "orderby": ["-p50(span.self_time)"],
  505. "project": self.project.id,
  506. "dataset": "spansMetrics",
  507. "statsPeriod": "10m",
  508. }
  509. )
  510. assert response.status_code == 200, response.content
  511. data = response.data["data"]
  512. meta = response.data["meta"]
  513. assert len(data) == 6
  514. assert data[0]["p50(span.self_time)"] == 11
  515. assert data[0]["span.module"] == "other"
  516. assert data[0]["span.description"] == "a"
  517. assert data[1]["p50(span.self_time)"] == 9
  518. assert data[1]["span.module"] == "cache"
  519. assert data[1]["span.description"] == "b"
  520. assert data[2]["p50(span.self_time)"] == 7
  521. assert data[2]["span.module"] == "cache"
  522. assert data[2]["span.description"] == "c"
  523. assert data[3]["p50(span.self_time)"] == 5
  524. assert data[3]["span.module"] == "other"
  525. assert data[3]["span.description"] == "d"
  526. assert data[4]["p50(span.self_time)"] == 3
  527. assert data[4]["span.module"] == "db"
  528. assert data[4]["span.description"] == "e"
  529. assert data[5]["p50(span.self_time)"] == 1
  530. assert data[5]["span.module"] == "http"
  531. assert data[5]["span.description"] == "f"
  532. assert meta["dataset"] == "spansMetrics"
  533. assert meta["fields"]["p50(span.self_time)"] == "duration"
  534. def test_tag_search(self):
  535. self.store_span_metric(
  536. 321,
  537. internal_metric=constants.SELF_TIME_LIGHT,
  538. timestamp=self.min_ago,
  539. tags={"span.description": "foo"},
  540. )
  541. self.store_span_metric(
  542. 99,
  543. internal_metric=constants.SELF_TIME_LIGHT,
  544. timestamp=self.min_ago,
  545. tags={"span.description": "bar"},
  546. )
  547. response = self.do_request(
  548. {
  549. "field": ["sum(span.self_time)"],
  550. "query": "span.description:bar",
  551. "project": self.project.id,
  552. "dataset": "spansMetrics",
  553. }
  554. )
  555. assert response.status_code == 200, response.content
  556. data = response.data["data"]
  557. meta = response.data["meta"]
  558. assert len(data) == 1
  559. assert data[0]["sum(span.self_time)"] == 99
  560. assert meta["dataset"] == "spansMetrics"
  561. def test_free_text_search(self):
  562. self.store_span_metric(
  563. 321,
  564. internal_metric=constants.SELF_TIME_LIGHT,
  565. timestamp=self.min_ago,
  566. tags={"span.description": "foo"},
  567. )
  568. self.store_span_metric(
  569. 99,
  570. internal_metric=constants.SELF_TIME_LIGHT,
  571. timestamp=self.min_ago,
  572. tags={"span.description": "bar"},
  573. )
  574. response = self.do_request(
  575. {
  576. "field": ["sum(span.self_time)"],
  577. "query": "foo",
  578. "project": self.project.id,
  579. "dataset": "spansMetrics",
  580. }
  581. )
  582. assert response.status_code == 200, response.content
  583. data = response.data["data"]
  584. meta = response.data["meta"]
  585. assert len(data) == 1
  586. assert data[0]["sum(span.self_time)"] == 321
  587. assert meta["dataset"] == "spansMetrics"
  588. def test_avg_compare(self):
  589. self.store_span_metric(
  590. 100,
  591. internal_metric=constants.SELF_TIME_LIGHT,
  592. timestamp=self.min_ago,
  593. tags={"release": "foo"},
  594. )
  595. self.store_span_metric(
  596. 10,
  597. internal_metric=constants.SELF_TIME_LIGHT,
  598. timestamp=self.min_ago,
  599. tags={"release": "bar"},
  600. )
  601. for function_name in [
  602. "avg_compare(span.self_time, release, foo, bar)",
  603. 'avg_compare(span.self_time, release, "foo", "bar")',
  604. ]:
  605. response = self.do_request(
  606. {
  607. "field": [function_name],
  608. "query": "",
  609. "project": self.project.id,
  610. "dataset": "spansMetrics",
  611. }
  612. )
  613. assert response.status_code == 200, response.content
  614. data = response.data["data"]
  615. meta = response.data["meta"]
  616. assert len(data) == 1
  617. assert data[0][function_name] == -0.9
  618. assert meta["dataset"] == "spansMetrics"
  619. assert meta["fields"][function_name] == "percent_change"
  620. def test_avg_compare_invalid_column(self):
  621. response = self.do_request(
  622. {
  623. "field": ["avg_compare(span.self_time, transaction, foo, bar)"],
  624. "query": "",
  625. "project": self.project.id,
  626. "dataset": "spansMetrics",
  627. }
  628. )
  629. assert response.status_code == 400, response.content
  630. def test_span_domain_array(self):
  631. self.store_span_metric(
  632. 321,
  633. internal_metric=constants.SELF_TIME_LIGHT,
  634. timestamp=self.min_ago,
  635. tags={"span.domain": ",sentry_table1,"},
  636. )
  637. self.store_span_metric(
  638. 21,
  639. internal_metric=constants.SELF_TIME_LIGHT,
  640. timestamp=self.min_ago,
  641. tags={"span.domain": ",sentry_table1,sentry_table2,"},
  642. )
  643. response = self.do_request(
  644. {
  645. "field": ["span.domain", "p75(span.self_time)"],
  646. "query": "",
  647. "project": self.project.id,
  648. "orderby": ["-p75(span.self_time)"],
  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) == 2
  656. assert data[0]["span.domain"] == ["sentry_table1"]
  657. assert data[1]["span.domain"] == ["sentry_table1", "sentry_table2"]
  658. assert meta["dataset"] == "spansMetrics"
  659. assert meta["fields"]["span.domain"] == "array"
  660. def test_span_domain_array_filter(self):
  661. self.store_span_metric(
  662. 321,
  663. internal_metric=constants.SELF_TIME_LIGHT,
  664. timestamp=self.min_ago,
  665. tags={"span.domain": ",sentry_table1,"},
  666. )
  667. self.store_span_metric(
  668. 21,
  669. internal_metric=constants.SELF_TIME_LIGHT,
  670. timestamp=self.min_ago,
  671. tags={"span.domain": ",sentry_table1,sentry_table2,"},
  672. )
  673. response = self.do_request(
  674. {
  675. "field": ["span.domain", "p75(span.self_time)"],
  676. "query": "span.domain:sentry_table2",
  677. "project": self.project.id,
  678. "dataset": "spansMetrics",
  679. }
  680. )
  681. assert response.status_code == 200, response.content
  682. data = response.data["data"]
  683. meta = response.data["meta"]
  684. assert len(data) == 1
  685. assert data[0]["span.domain"] == ["sentry_table1", "sentry_table2"]
  686. assert meta["dataset"] == "spansMetrics"
  687. assert meta["fields"]["span.domain"] == "array"
  688. def test_span_domain_array_filter_wildcard(self):
  689. self.store_span_metric(
  690. 321,
  691. internal_metric=constants.SELF_TIME_LIGHT,
  692. timestamp=self.min_ago,
  693. tags={"span.domain": ",sentry_table1,"},
  694. )
  695. self.store_span_metric(
  696. 21,
  697. internal_metric=constants.SELF_TIME_LIGHT,
  698. timestamp=self.min_ago,
  699. tags={"span.domain": ",sentry_table1,sentry_table2,"},
  700. )
  701. for query in ["sentry*2", "*table2", "sentry_table2*"]:
  702. response = self.do_request(
  703. {
  704. "field": ["span.domain", "p75(span.self_time)"],
  705. "query": f"span.domain:{query}",
  706. "project": self.project.id,
  707. "dataset": "spansMetrics",
  708. }
  709. )
  710. assert response.status_code == 200, response.content
  711. data = response.data["data"]
  712. meta = response.data["meta"]
  713. assert len(data) == 1, query
  714. assert data[0]["span.domain"] == ["sentry_table1", "sentry_table2"], query
  715. assert meta["dataset"] == "spansMetrics", query
  716. assert meta["fields"]["span.domain"] == "array"
  717. def test_span_domain_array_has_filter(self):
  718. self.store_span_metric(
  719. 321,
  720. internal_metric=constants.SELF_TIME_LIGHT,
  721. timestamp=self.min_ago,
  722. tags={"span.domain": ""},
  723. )
  724. self.store_span_metric(
  725. 21,
  726. internal_metric=constants.SELF_TIME_LIGHT,
  727. timestamp=self.min_ago,
  728. tags={"span.domain": ",sentry_table1,sentry_table2,"},
  729. )
  730. response = self.do_request(
  731. {
  732. "field": ["span.domain", "p75(span.self_time)"],
  733. "query": "has:span.domain",
  734. "project": self.project.id,
  735. "dataset": "spansMetrics",
  736. }
  737. )
  738. assert response.status_code == 200, response.content
  739. data = response.data["data"]
  740. meta = response.data["meta"]
  741. assert len(data) == 1
  742. assert data[0]["span.domain"] == ["sentry_table1", "sentry_table2"]
  743. assert meta["dataset"] == "spansMetrics"
  744. response = self.do_request(
  745. {
  746. "field": ["span.domain", "p75(span.self_time)"],
  747. "query": "!has:span.domain",
  748. "project": self.project.id,
  749. "dataset": "spansMetrics",
  750. }
  751. )
  752. assert response.status_code == 200, response.content
  753. data = response.data["data"]
  754. meta = response.data["meta"]
  755. assert len(data) == 1
  756. assert meta["dataset"] == "spansMetrics"
  757. assert meta["fields"]["span.domain"] == "array"
  758. def test_unique_values_span_domain(self):
  759. self.store_span_metric(
  760. 321,
  761. internal_metric=constants.SELF_TIME_LIGHT,
  762. timestamp=self.min_ago,
  763. tags={"span.domain": ",sentry_table1,"},
  764. )
  765. self.store_span_metric(
  766. 21,
  767. internal_metric=constants.SELF_TIME_LIGHT,
  768. timestamp=self.min_ago,
  769. tags={"span.domain": ",sentry_table2,sentry_table3,"},
  770. )
  771. response = self.do_request(
  772. {
  773. "field": ["unique.span_domains", "count()"],
  774. "query": "",
  775. "orderby": "unique.span_domains",
  776. "project": self.project.id,
  777. "dataset": "spansMetrics",
  778. }
  779. )
  780. assert response.status_code == 200, response.content
  781. data = response.data["data"]
  782. meta = response.data["meta"]
  783. assert len(data) == 3
  784. assert data[0]["unique.span_domains"] == "sentry_table1"
  785. assert data[1]["unique.span_domains"] == "sentry_table2"
  786. assert data[2]["unique.span_domains"] == "sentry_table3"
  787. assert meta["fields"]["unique.span_domains"] == "string"
  788. def test_unique_values_span_domain_with_filter(self):
  789. self.store_span_metric(
  790. 321,
  791. internal_metric=constants.SELF_TIME_LIGHT,
  792. timestamp=self.min_ago,
  793. tags={"span.domain": ",sentry_tible1,"},
  794. )
  795. self.store_span_metric(
  796. 21,
  797. internal_metric=constants.SELF_TIME_LIGHT,
  798. timestamp=self.min_ago,
  799. tags={"span.domain": ",sentry_table2,sentry_table3,"},
  800. )
  801. response = self.do_request(
  802. {
  803. "field": ["unique.span_domains", "count()"],
  804. "query": "span.domain:sentry_tab*",
  805. "orderby": "unique.span_domains",
  806. "project": self.project.id,
  807. "dataset": "spansMetrics",
  808. }
  809. )
  810. assert response.status_code == 200, response.content
  811. data = response.data["data"]
  812. meta = response.data["meta"]
  813. assert len(data) == 2
  814. assert data[0]["unique.span_domains"] == "sentry_table2"
  815. assert data[1]["unique.span_domains"] == "sentry_table3"
  816. assert meta["fields"]["unique.span_domains"] == "string"
  817. def test_avg_if(self):
  818. self.store_span_metric(
  819. 100,
  820. internal_metric=constants.SELF_TIME_LIGHT,
  821. timestamp=self.min_ago,
  822. tags={"release": "foo"},
  823. )
  824. self.store_span_metric(
  825. 200,
  826. internal_metric=constants.SELF_TIME_LIGHT,
  827. timestamp=self.min_ago,
  828. tags={"release": "foo"},
  829. )
  830. self.store_span_metric(
  831. 10,
  832. internal_metric=constants.SELF_TIME_LIGHT,
  833. timestamp=self.min_ago,
  834. tags={"release": "bar"},
  835. )
  836. response = self.do_request(
  837. {
  838. "field": ["avg_if(span.self_time, release, foo)"],
  839. "query": "",
  840. "project": self.project.id,
  841. "dataset": "spansMetrics",
  842. }
  843. )
  844. assert response.status_code == 200, response.content
  845. data = response.data["data"]
  846. meta = response.data["meta"]
  847. assert len(data) == 1
  848. assert data[0]["avg_if(span.self_time, release, foo)"] == 150
  849. assert meta["dataset"] == "spansMetrics"
  850. assert meta["fields"]["avg_if(span.self_time, release, foo)"] == "duration"
  851. def test_device_class(self):
  852. self.store_span_metric(
  853. 123,
  854. internal_metric=constants.SELF_TIME_LIGHT,
  855. timestamp=self.min_ago,
  856. tags={"device.class": "1"},
  857. )
  858. self.store_span_metric(
  859. 678,
  860. internal_metric=constants.SELF_TIME_LIGHT,
  861. timestamp=self.min_ago,
  862. tags={"device.class": "2"},
  863. )
  864. self.store_span_metric(
  865. 999,
  866. internal_metric=constants.SELF_TIME_LIGHT,
  867. timestamp=self.min_ago,
  868. tags={"device.class": ""},
  869. )
  870. response = self.do_request(
  871. {
  872. "field": ["device.class", "p95()"],
  873. "query": "",
  874. "orderby": "p95()",
  875. "project": self.project.id,
  876. "dataset": "spansMetrics",
  877. }
  878. )
  879. assert response.status_code == 200, response.content
  880. data = response.data["data"]
  881. meta = response.data["meta"]
  882. assert len(data) == 3
  883. # Need to actually check the dict since the level for 1 isn't guaranteed to stay `low` or `medium`
  884. assert data[0]["device.class"] == map_device_class_level("1")
  885. assert data[1]["device.class"] == map_device_class_level("2")
  886. assert data[2]["device.class"] == "Unknown"
  887. assert meta["fields"]["device.class"] == "string"
  888. def test_device_class_filter(self):
  889. self.store_span_metric(
  890. 123,
  891. internal_metric=constants.SELF_TIME_LIGHT,
  892. timestamp=self.min_ago,
  893. tags={"device.class": "1"},
  894. )
  895. # Need to actually check the dict since the level for 1 isn't guaranteed to stay `low`
  896. level = map_device_class_level("1")
  897. response = self.do_request(
  898. {
  899. "field": ["device.class", "count()"],
  900. "query": f"device.class:{level}",
  901. "orderby": "count()",
  902. "project": self.project.id,
  903. "dataset": "spansMetrics",
  904. }
  905. )
  906. assert response.status_code == 200, response.content
  907. data = response.data["data"]
  908. meta = response.data["meta"]
  909. assert len(data) == 1
  910. assert data[0]["device.class"] == level
  911. assert meta["fields"]["device.class"] == "string"
  912. @region_silo_test
  913. class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithMetricLayer(
  914. OrganizationEventsMetricsEnhancedPerformanceEndpointTest
  915. ):
  916. def setUp(self):
  917. super().setUp()
  918. self.features["organizations:use-metrics-layer"] = True
  919. @pytest.mark.xfail(reason="Not implemented")
  920. def test_time_spent_percentage(self):
  921. super().test_time_spent_percentage()
  922. @pytest.mark.xfail(reason="Not implemented")
  923. def test_time_spent_percentage_local(self):
  924. super().test_time_spent_percentage_local()
  925. @pytest.mark.xfail(reason="Cannot group by function 'if'")
  926. def test_span_module(self):
  927. super().test_span_module()
  928. @pytest.mark.xfail(reason="Cannot search by tags")
  929. def test_tag_search(self):
  930. super().test_tag_search()
  931. @pytest.mark.xfail(reason="Cannot search by tags")
  932. def test_free_text_search(self):
  933. super().test_free_text_search()
  934. @pytest.mark.xfail(reason="Not implemented")
  935. def test_avg_compare(self):
  936. super().test_avg_compare()
  937. @pytest.mark.xfail(reason="Not implemented")
  938. def test_span_domain_array(self):
  939. super().test_span_domain_array()
  940. @pytest.mark.xfail(reason="Not implemented")
  941. def test_span_domain_array_filter(self):
  942. super().test_span_domain_array_filter()
  943. @pytest.mark.xfail(reason="Not implemented")
  944. def test_span_domain_array_filter_wildcard(self):
  945. super().test_span_domain_array_filter_wildcard()
  946. @pytest.mark.xfail(reason="Not implemented")
  947. def test_span_domain_array_has_filter(self):
  948. super().test_span_domain_array_has_filter()
  949. @pytest.mark.xfail(reason="Not implemented")
  950. def test_unique_values_span_domain(self):
  951. super().test_unique_values_span_domain()
  952. @pytest.mark.xfail(reason="Not implemented")
  953. def test_unique_values_span_domain_with_filter(self):
  954. super().test_unique_values_span_domain_with_filter()
  955. @pytest.mark.xfail(reason="Not implemented")
  956. def test_avg_if(self):
  957. super().test_avg_if()
  958. @pytest.mark.xfail(reason="Not implemented")
  959. def test_device_class_filter(self):
  960. super().test_device_class_filter()
  961. @pytest.mark.xfail(reason="Not implemented")
  962. def test_device_class(self):
  963. super().test_device_class()