test_organization_events_span_metrics.py 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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.task.celery"},
  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.task.celery)",
  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.task.celery)"] == 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.task.celery)"] == "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_http_response_rate(self):
  985. self.store_span_metric(
  986. 1,
  987. internal_metric=constants.SELF_TIME_LIGHT,
  988. timestamp=self.min_ago,
  989. tags={"span.status_code": "200"},
  990. )
  991. self.store_span_metric(
  992. 3,
  993. internal_metric=constants.SELF_TIME_LIGHT,
  994. timestamp=self.min_ago,
  995. tags={"span.status_code": "301"},
  996. )
  997. self.store_span_metric(
  998. 3,
  999. internal_metric=constants.SELF_TIME_LIGHT,
  1000. timestamp=self.min_ago,
  1001. tags={"span.status_code": "404"},
  1002. )
  1003. self.store_span_metric(
  1004. 4,
  1005. internal_metric=constants.SELF_TIME_LIGHT,
  1006. timestamp=self.min_ago,
  1007. tags={"span.status_code": "503"},
  1008. )
  1009. self.store_span_metric(
  1010. 5,
  1011. internal_metric=constants.SELF_TIME_LIGHT,
  1012. timestamp=self.min_ago,
  1013. tags={"span.status_code": "501"},
  1014. )
  1015. response = self.do_request(
  1016. {
  1017. "field": [
  1018. "http_response_rate(200)", # By exact code
  1019. "http_response_rate(3)", # By code class
  1020. "http_response_rate(4)",
  1021. "http_response_rate(5)",
  1022. ],
  1023. "query": "",
  1024. "project": self.project.id,
  1025. "dataset": "spansMetrics",
  1026. }
  1027. )
  1028. assert response.status_code == 200, response.content
  1029. data = response.data["data"]
  1030. assert len(data) == 1
  1031. assert data[0]["http_response_rate(200)"] == 0.2
  1032. assert data[0]["http_response_rate(3)"] == 0.2
  1033. assert data[0]["http_response_rate(4)"] == 0.2
  1034. assert data[0]["http_response_rate(5)"] == 0.4
  1035. meta = response.data["meta"]
  1036. assert meta["dataset"] == "spansMetrics"
  1037. assert meta["fields"]["http_response_rate(200)"] == "percentage"
  1038. def test_regression_score_regression(self):
  1039. # This span increases in duration
  1040. self.store_span_metric(
  1041. 1,
  1042. timestamp=self.six_min_ago,
  1043. tags={"transaction": "/api/0/projects/", "span.description": "Regressed Span"},
  1044. project=self.project.id,
  1045. )
  1046. self.store_span_metric(
  1047. 100,
  1048. timestamp=self.min_ago,
  1049. tags={"transaction": "/api/0/projects/", "span.description": "Regressed Span"},
  1050. project=self.project.id,
  1051. )
  1052. # This span stays the same
  1053. self.store_span_metric(
  1054. 1,
  1055. timestamp=self.three_days_ago,
  1056. tags={"transaction": "/api/0/projects/", "span.description": "Non-regressed"},
  1057. project=self.project.id,
  1058. )
  1059. self.store_span_metric(
  1060. 1,
  1061. timestamp=self.min_ago,
  1062. tags={"transaction": "/api/0/projects/", "span.description": "Non-regressed"},
  1063. project=self.project.id,
  1064. )
  1065. response = self.do_request(
  1066. {
  1067. "field": [
  1068. "span.description",
  1069. f"regression_score(span.self_time,{int(self.two_min_ago.timestamp())})",
  1070. ],
  1071. "query": "transaction:/api/0/projects/",
  1072. "dataset": "spansMetrics",
  1073. "orderby": [
  1074. f"-regression_score(span.self_time,{int(self.two_min_ago.timestamp())})"
  1075. ],
  1076. "start": (self.six_min_ago - timedelta(minutes=1)).isoformat(),
  1077. "end": before_now(minutes=0),
  1078. }
  1079. )
  1080. assert response.status_code == 200, response.content
  1081. data = response.data["data"]
  1082. assert len(data) == 2
  1083. assert [row["span.description"] for row in data] == ["Regressed Span", "Non-regressed"]
  1084. def test_regression_score_added_span(self):
  1085. # This span only exists after the breakpoint
  1086. self.store_span_metric(
  1087. 100,
  1088. timestamp=self.min_ago,
  1089. tags={"transaction": "/api/0/projects/", "span.description": "Added span"},
  1090. project=self.project.id,
  1091. )
  1092. # This span stays the same
  1093. self.store_span_metric(
  1094. 1,
  1095. timestamp=self.three_days_ago,
  1096. tags={"transaction": "/api/0/projects/", "span.description": "Non-regressed"},
  1097. project=self.project.id,
  1098. )
  1099. self.store_span_metric(
  1100. 1,
  1101. timestamp=self.min_ago,
  1102. tags={"transaction": "/api/0/projects/", "span.description": "Non-regressed"},
  1103. project=self.project.id,
  1104. )
  1105. response = self.do_request(
  1106. {
  1107. "field": [
  1108. "span.description",
  1109. f"regression_score(span.self_time,{int(self.two_min_ago.timestamp())})",
  1110. ],
  1111. "query": "transaction:/api/0/projects/",
  1112. "dataset": "spansMetrics",
  1113. "orderby": [
  1114. f"-regression_score(span.self_time,{int(self.two_min_ago.timestamp())})"
  1115. ],
  1116. "start": (self.six_min_ago - timedelta(minutes=1)).isoformat(),
  1117. "end": before_now(minutes=0),
  1118. }
  1119. )
  1120. assert response.status_code == 200, response.content
  1121. data = response.data["data"]
  1122. assert len(data) == 2
  1123. assert [row["span.description"] for row in data] == ["Added span", "Non-regressed"]
  1124. def test_regression_score_removed_span(self):
  1125. # This span only exists before the breakpoint
  1126. self.store_span_metric(
  1127. 100,
  1128. timestamp=self.six_min_ago,
  1129. tags={"transaction": "/api/0/projects/", "span.description": "Removed span"},
  1130. project=self.project.id,
  1131. )
  1132. # This span stays the same
  1133. self.store_span_metric(
  1134. 1,
  1135. timestamp=self.three_days_ago,
  1136. tags={"transaction": "/api/0/projects/", "span.description": "Non-regressed"},
  1137. project=self.project.id,
  1138. )
  1139. self.store_span_metric(
  1140. 1,
  1141. timestamp=self.min_ago,
  1142. tags={"transaction": "/api/0/projects/", "span.description": "Non-regressed"},
  1143. project=self.project.id,
  1144. )
  1145. response = self.do_request(
  1146. {
  1147. "field": [
  1148. "span.description",
  1149. f"regression_score(span.self_time,{int(self.two_min_ago.timestamp())})",
  1150. ],
  1151. "query": "transaction:/api/0/projects/",
  1152. "dataset": "spansMetrics",
  1153. "orderby": [
  1154. f"-regression_score(span.self_time,{int(self.two_min_ago.timestamp())})"
  1155. ],
  1156. "start": (self.six_min_ago - timedelta(minutes=1)).isoformat(),
  1157. "end": before_now(minutes=0),
  1158. }
  1159. )
  1160. assert response.status_code == 200, response.content
  1161. data = response.data["data"]
  1162. assert len(data) == 2
  1163. assert [row["span.description"] for row in data] == ["Non-regressed", "Removed span"]
  1164. # The regression score is <0 for removed spans, this can act as
  1165. # a way to filter out removed spans when necessary
  1166. assert data[1][f"regression_score(span.self_time,{int(self.two_min_ago.timestamp())})"] < 0
  1167. def test_avg_self_time_by_timestamp(self):
  1168. self.store_span_metric(
  1169. 1,
  1170. internal_metric=constants.SELF_TIME_LIGHT,
  1171. timestamp=self.six_min_ago,
  1172. tags={},
  1173. )
  1174. self.store_span_metric(
  1175. 3,
  1176. internal_metric=constants.SELF_TIME_LIGHT,
  1177. timestamp=self.min_ago,
  1178. tags={},
  1179. )
  1180. response = self.do_request(
  1181. {
  1182. "field": [
  1183. f"avg_by_timestamp(span.self_time,less,{int(self.two_min_ago.timestamp())})",
  1184. f"avg_by_timestamp(span.self_time,greater,{int(self.two_min_ago.timestamp())})",
  1185. ],
  1186. "query": "",
  1187. "project": self.project.id,
  1188. "dataset": "spansMetrics",
  1189. "statsPeriod": "1h",
  1190. }
  1191. )
  1192. assert response.status_code == 200, response.content
  1193. data = response.data["data"]
  1194. assert len(data) == 1
  1195. assert data[0] == {
  1196. f"avg_by_timestamp(span.self_time,less,{int(self.two_min_ago.timestamp())})": 1.0,
  1197. f"avg_by_timestamp(span.self_time,greater,{int(self.two_min_ago.timestamp())})": 3.0,
  1198. }
  1199. def test_avg_self_time_by_timestamp_invalid_condition(self):
  1200. response = self.do_request(
  1201. {
  1202. "field": [
  1203. f"avg_by_timestamp(span.self_time,INVALID_ARG,{int(self.two_min_ago.timestamp())})",
  1204. ],
  1205. "query": "",
  1206. "project": self.project.id,
  1207. "dataset": "spansMetrics",
  1208. "statsPeriod": "1h",
  1209. }
  1210. )
  1211. assert response.status_code == 400, response.content
  1212. assert (
  1213. response.data["detail"]
  1214. == "avg_by_timestamp: condition argument invalid: string must be one of ['greater', 'less']"
  1215. )
  1216. def test_epm_by_timestamp(self):
  1217. self.store_span_metric(
  1218. 1,
  1219. internal_metric=constants.SELF_TIME_LIGHT,
  1220. timestamp=self.six_min_ago,
  1221. tags={},
  1222. )
  1223. # More events occur after the timestamp
  1224. for _ in range(3):
  1225. self.store_span_metric(
  1226. 3,
  1227. internal_metric=constants.SELF_TIME_LIGHT,
  1228. timestamp=self.min_ago,
  1229. tags={},
  1230. )
  1231. response = self.do_request(
  1232. {
  1233. "field": [
  1234. f"epm_by_timestamp(less,{int(self.two_min_ago.timestamp())})",
  1235. f"epm_by_timestamp(greater,{int(self.two_min_ago.timestamp())})",
  1236. ],
  1237. "query": "",
  1238. "project": self.project.id,
  1239. "dataset": "spansMetrics",
  1240. "statsPeriod": "1h",
  1241. }
  1242. )
  1243. assert response.status_code == 200, response.content
  1244. data = response.data["data"]
  1245. assert len(data) == 1
  1246. assert data[0][f"epm_by_timestamp(less,{int(self.two_min_ago.timestamp())})"] < 1.0
  1247. assert data[0][f"epm_by_timestamp(greater,{int(self.two_min_ago.timestamp())})"] > 1.0
  1248. def test_epm_by_timestamp_invalid_condition(self):
  1249. response = self.do_request(
  1250. {
  1251. "field": [
  1252. f"epm_by_timestamp(INVALID_ARG,{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 == 400, response.content
  1261. assert (
  1262. response.data["detail"]
  1263. == "epm_by_timestamp: condition argument invalid: string must be one of ['greater', 'less']"
  1264. )
  1265. def test_any_function(self):
  1266. for char in "abc":
  1267. for transaction in ["foo", "bar"]:
  1268. self.store_span_metric(
  1269. 1,
  1270. internal_metric=constants.SELF_TIME_LIGHT,
  1271. timestamp=self.six_min_ago,
  1272. tags={"span.description": char, "transaction": transaction},
  1273. )
  1274. response = self.do_request(
  1275. {
  1276. "field": [
  1277. "transaction",
  1278. "any(span.description)",
  1279. ],
  1280. "query": "",
  1281. "orderby": ["transaction"],
  1282. "project": self.project.id,
  1283. "dataset": "spansMetrics",
  1284. "statsPeriod": "1h",
  1285. }
  1286. )
  1287. assert response.status_code == 200, response.content
  1288. assert response.data["data"] == [
  1289. {"transaction": "bar", "any(span.description)": "a"},
  1290. {"transaction": "foo", "any(span.description)": "a"},
  1291. ]
  1292. def test_count_op(self):
  1293. self.store_span_metric(
  1294. 1,
  1295. internal_metric=constants.SELF_TIME_LIGHT,
  1296. timestamp=self.six_min_ago,
  1297. tags={"span.op": "queue.submit.celery"},
  1298. )
  1299. self.store_span_metric(
  1300. 1,
  1301. internal_metric=constants.SELF_TIME_LIGHT,
  1302. timestamp=self.six_min_ago,
  1303. tags={"span.op": "queue.task.celery"},
  1304. )
  1305. response = self.do_request(
  1306. {
  1307. "field": [
  1308. "count_op(queue.submit.celery)",
  1309. "count_op(queue.task.celery)",
  1310. ],
  1311. "query": "",
  1312. "project": self.project.id,
  1313. "dataset": "spansMetrics",
  1314. "statsPeriod": "1h",
  1315. }
  1316. )
  1317. assert response.status_code == 200, response.content
  1318. data = response.data["data"]
  1319. assert data == [
  1320. {"count_op(queue.submit.celery)": 1, "count_op(queue.task.celery)": 1},
  1321. ]
  1322. class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithMetricLayer(
  1323. OrganizationEventsMetricsEnhancedPerformanceEndpointTest
  1324. ):
  1325. def setUp(self):
  1326. super().setUp()
  1327. self.features["organizations:use-metrics-layer"] = True
  1328. @pytest.mark.xfail(reason="Not implemented")
  1329. def test_time_spent_percentage(self):
  1330. super().test_time_spent_percentage()
  1331. @pytest.mark.xfail(reason="Not implemented")
  1332. def test_time_spent_percentage_local(self):
  1333. super().test_time_spent_percentage_local()
  1334. @pytest.mark.xfail(reason="Cannot group by function 'if'")
  1335. def test_span_module(self):
  1336. super().test_span_module()
  1337. @pytest.mark.xfail(reason="Cannot search by tags")
  1338. def test_tag_search(self):
  1339. super().test_tag_search()
  1340. @pytest.mark.xfail(reason="Cannot search by tags")
  1341. def test_free_text_search(self):
  1342. super().test_free_text_search()
  1343. @pytest.mark.xfail(reason="Not implemented")
  1344. def test_avg_compare(self):
  1345. super().test_avg_compare()
  1346. @pytest.mark.xfail(reason="Not implemented")
  1347. def test_span_domain_array(self):
  1348. super().test_span_domain_array()
  1349. @pytest.mark.xfail(reason="Not implemented")
  1350. def test_span_domain_array_filter(self):
  1351. super().test_span_domain_array_filter()
  1352. @pytest.mark.xfail(reason="Not implemented")
  1353. def test_span_domain_array_filter_wildcard(self):
  1354. super().test_span_domain_array_filter_wildcard()
  1355. @pytest.mark.xfail(reason="Not implemented")
  1356. def test_span_domain_array_has_filter(self):
  1357. super().test_span_domain_array_has_filter()
  1358. @pytest.mark.xfail(reason="Not implemented")
  1359. def test_unique_values_span_domain(self):
  1360. super().test_unique_values_span_domain()
  1361. @pytest.mark.xfail(reason="Not implemented")
  1362. def test_unique_values_span_domain_with_filter(self):
  1363. super().test_unique_values_span_domain_with_filter()
  1364. @pytest.mark.xfail(reason="Not implemented")
  1365. def test_avg_if(self):
  1366. super().test_avg_if()
  1367. @pytest.mark.xfail(reason="Not implemented")
  1368. def test_device_class_filter(self):
  1369. super().test_device_class_filter()
  1370. @pytest.mark.xfail(reason="Not implemented")
  1371. def test_device_class(self):
  1372. super().test_device_class()
  1373. @pytest.mark.xfail(reason="Not implemented")
  1374. def test_count_op(self):
  1375. super().test_count_op()