test_organization_events_mep.py 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491
  1. from unittest import mock
  2. import pytest
  3. from django.urls import reverse
  4. from sentry.api.bases.organization_events import DATASET_OPTIONS
  5. from sentry.discover.models import TeamKeyTransaction
  6. from sentry.models.projectteam import ProjectTeam
  7. from sentry.models.transaction_threshold import (
  8. ProjectTransactionThreshold,
  9. ProjectTransactionThresholdOverride,
  10. TransactionMetric,
  11. )
  12. from sentry.search.events import constants
  13. from sentry.snuba.metrics.naming_layer.mri import TransactionMRI
  14. from sentry.snuba.metrics.naming_layer.public import TransactionMetricKey
  15. from sentry.testutils.cases import MetricsEnhancedPerformanceTestCase
  16. from sentry.testutils.helpers.datetime import before_now, iso_format
  17. from sentry.testutils.silo import region_silo_test
  18. from sentry.utils.samples import load_data
  19. pytestmark = pytest.mark.sentry_metrics
  20. @region_silo_test
  21. class OrganizationEventsMetricsEnhancedPerformanceEndpointTest(MetricsEnhancedPerformanceTestCase):
  22. viewname = "sentry-api-0-organization-events"
  23. # Poor intentionally omitted for test_measurement_rating_that_does_not_exist
  24. METRIC_STRINGS = [
  25. "foo_transaction",
  26. "bar_transaction",
  27. "baz_transaction",
  28. "staging",
  29. "measurement_rating",
  30. "good",
  31. "meh",
  32. "d:transactions/measurements.something_custom@millisecond",
  33. "d:transactions/measurements.runtime@hour",
  34. "d:transactions/measurements.bytes_transfered@byte",
  35. "d:transactions/measurements.datacenter_memory@petabyte",
  36. "d:transactions/measurements.custom.kilobyte@kilobyte",
  37. "d:transactions/measurements.longtaskcount@none",
  38. "d:transactions/measurements.percent@ratio",
  39. "d:transactions/measurements.custom_type@somethingcustom",
  40. ]
  41. def setUp(self):
  42. super().setUp()
  43. self.min_ago = before_now(minutes=1)
  44. self.two_min_ago = before_now(minutes=2)
  45. self.transaction_data = load_data("transaction", timestamp=before_now(minutes=1))
  46. self.features = {
  47. "organizations:performance-use-metrics": True,
  48. }
  49. def do_request(self, query, features=None):
  50. if features is None:
  51. features = {"organizations:discover-basic": True}
  52. features.update(self.features)
  53. self.login_as(user=self.user)
  54. url = reverse(
  55. self.viewname,
  56. kwargs={"organization_slug": self.organization.slug},
  57. )
  58. with self.feature(features):
  59. return self.client.get(url, query, format="json")
  60. def test_no_projects(self):
  61. response = self.do_request(
  62. {
  63. "dataset": "metricsEnhanced",
  64. }
  65. )
  66. assert response.status_code == 200, response.content
  67. def test_invalid_dataset(self):
  68. response = self.do_request(
  69. {
  70. "dataset": "aFakeDataset",
  71. "project": self.project.id,
  72. }
  73. )
  74. assert response.status_code == 400, response.content
  75. assert (
  76. response.data["detail"]
  77. == f"dataset must be one of: {', '.join([key for key in DATASET_OPTIONS.keys()])}"
  78. )
  79. def test_out_of_retention(self):
  80. self.create_project()
  81. with self.options({"system.event-retention-days": 10}):
  82. query = {
  83. "field": ["id", "timestamp"],
  84. "orderby": ["-timestamp", "-id"],
  85. "query": "event.type:transaction",
  86. "start": iso_format(before_now(days=20)),
  87. "end": iso_format(before_now(days=15)),
  88. "dataset": "metricsEnhanced",
  89. }
  90. response = self.do_request(query)
  91. assert response.status_code == 400, response.content
  92. assert response.data["detail"] == "Invalid date range. Please try a more recent date range."
  93. def test_invalid_search_terms(self):
  94. response = self.do_request(
  95. {
  96. "field": ["epm()"],
  97. "query": "hi \n there",
  98. "project": self.project.id,
  99. "dataset": "metricsEnhanced",
  100. }
  101. )
  102. assert response.status_code == 400, response.content
  103. assert (
  104. response.data["detail"]
  105. == "Parse error at 'hi \n ther' (column 4). This is commonly caused by unmatched parentheses. Enclose any text in double quotes."
  106. )
  107. def test_percentile_with_no_data(self):
  108. response = self.do_request(
  109. {
  110. "field": ["p50()"],
  111. "query": "",
  112. "project": self.project.id,
  113. "dataset": "metricsEnhanced",
  114. }
  115. )
  116. assert response.status_code == 200, response.content
  117. data = response.data["data"]
  118. assert len(data) == 1
  119. assert data[0]["p50()"] == 0
  120. def test_project_name(self):
  121. self.store_transaction_metric(
  122. 1,
  123. tags={"environment": "staging"},
  124. timestamp=self.min_ago,
  125. )
  126. response = self.do_request(
  127. {
  128. "field": ["project.name", "environment", "epm()"],
  129. "query": "event.type:transaction",
  130. "dataset": "metricsEnhanced",
  131. "per_page": 50,
  132. }
  133. )
  134. assert response.status_code == 200, response.content
  135. assert len(response.data["data"]) == 1
  136. data = response.data["data"]
  137. meta = response.data["meta"]
  138. field_meta = meta["fields"]
  139. assert data[0]["project.name"] == self.project.slug
  140. assert "project.id" not in data[0]
  141. assert data[0]["environment"] == "staging"
  142. assert meta["isMetricsData"]
  143. assert field_meta["project.name"] == "string"
  144. assert field_meta["environment"] == "string"
  145. assert field_meta["epm()"] == "rate"
  146. def test_project_id(self):
  147. self.store_transaction_metric(
  148. 1,
  149. tags={"environment": "staging"},
  150. timestamp=self.min_ago,
  151. )
  152. response = self.do_request(
  153. {
  154. "field": ["project_id", "environment", "epm()"],
  155. "query": "event.type:transaction",
  156. "dataset": "metrics",
  157. "per_page": 50,
  158. }
  159. )
  160. assert response.status_code == 200, response.content
  161. assert len(response.data["data"]) == 1
  162. data = response.data["data"]
  163. meta = response.data["meta"]
  164. field_meta = meta["fields"]
  165. assert data[0]["project_id"] == self.project.id
  166. assert data[0]["environment"] == "staging"
  167. assert meta["isMetricsData"]
  168. assert field_meta["project_id"] == "integer"
  169. assert field_meta["environment"] == "string"
  170. assert field_meta["epm()"] == "rate"
  171. def test_project_dot_id(self):
  172. self.store_transaction_metric(
  173. 1,
  174. tags={"environment": "staging"},
  175. timestamp=self.min_ago,
  176. )
  177. response = self.do_request(
  178. {
  179. "field": ["project.id", "environment", "epm()"],
  180. "query": "event.type:transaction",
  181. "dataset": "metrics",
  182. "per_page": 50,
  183. }
  184. )
  185. assert response.status_code == 200, response.content
  186. assert len(response.data["data"]) == 1
  187. data = response.data["data"]
  188. meta = response.data["meta"]
  189. field_meta = meta["fields"]
  190. assert data[0]["project.id"] == self.project.id
  191. assert data[0]["environment"] == "staging"
  192. assert meta["isMetricsData"]
  193. assert field_meta["project.id"] == "integer"
  194. assert field_meta["environment"] == "string"
  195. assert field_meta["epm()"] == "rate"
  196. def test_title_alias(self):
  197. """title is an alias to transaction name"""
  198. self.store_transaction_metric(
  199. 1,
  200. tags={"transaction": "foo_transaction"},
  201. timestamp=self.min_ago,
  202. )
  203. response = self.do_request(
  204. {
  205. "field": ["title", "p50()"],
  206. "query": "event.type:transaction",
  207. "dataset": "metricsEnhanced",
  208. "per_page": 50,
  209. }
  210. )
  211. assert response.status_code == 200, response.content
  212. assert len(response.data["data"]) == 1
  213. data = response.data["data"]
  214. meta = response.data["meta"]
  215. field_meta = meta["fields"]
  216. assert data[0]["title"] == "foo_transaction"
  217. assert data[0]["p50()"] == 1
  218. assert meta["isMetricsData"]
  219. assert field_meta["title"] == "string"
  220. assert field_meta["p50()"] == "duration"
  221. def test_having_condition(self):
  222. self.store_transaction_metric(
  223. 1,
  224. tags={"environment": "staging", "transaction": "foo_transaction"},
  225. timestamp=self.min_ago,
  226. )
  227. self.store_transaction_metric(
  228. # shouldn't show up
  229. 100,
  230. tags={"environment": "staging", "transaction": "bar_transaction"},
  231. timestamp=self.min_ago,
  232. )
  233. response = self.do_request(
  234. {
  235. "field": ["transaction", "project", "p50(transaction.duration)"],
  236. "query": "event.type:transaction p50(transaction.duration):<50",
  237. "dataset": "metricsEnhanced",
  238. "per_page": 50,
  239. }
  240. )
  241. assert response.status_code == 200, response.content
  242. assert len(response.data["data"]) == 1
  243. data = response.data["data"]
  244. meta = response.data["meta"]
  245. field_meta = meta["fields"]
  246. assert data[0]["transaction"] == "foo_transaction"
  247. assert data[0]["project"] == self.project.slug
  248. assert data[0]["p50(transaction.duration)"] == 1
  249. assert meta["isMetricsData"]
  250. assert field_meta["transaction"] == "string"
  251. assert field_meta["project"] == "string"
  252. assert field_meta["p50(transaction.duration)"] == "duration"
  253. def test_having_condition_with_preventing_aggregates(self):
  254. self.store_transaction_metric(
  255. 1,
  256. tags={"environment": "staging", "transaction": "foo_transaction"},
  257. timestamp=self.min_ago,
  258. )
  259. self.store_transaction_metric(
  260. 100,
  261. tags={"environment": "staging", "transaction": "bar_transaction"},
  262. timestamp=self.min_ago,
  263. )
  264. response = self.do_request(
  265. {
  266. "field": ["transaction", "project", "p50(transaction.duration)"],
  267. "query": "event.type:transaction p50(transaction.duration):<50",
  268. "dataset": "metricsEnhanced",
  269. "preventMetricAggregates": "1",
  270. "per_page": 50,
  271. }
  272. )
  273. assert response.status_code == 200, response.content
  274. assert len(response.data["data"]) == 0
  275. meta = response.data["meta"]
  276. field_meta = meta["fields"]
  277. assert not meta["isMetricsData"]
  278. assert field_meta["transaction"] == "string"
  279. assert field_meta["project"] == "string"
  280. assert field_meta["p50(transaction.duration)"] == "duration"
  281. def test_having_condition_with_preventing_aggregate_metrics_only(self):
  282. """same as the previous test, but with the dataset on explicit metrics
  283. which should throw a 400 error instead"""
  284. response = self.do_request(
  285. {
  286. "field": ["transaction", "project", "p50(transaction.duration)"],
  287. "query": "event.type:transaction p50(transaction.duration):<50",
  288. "dataset": "metrics",
  289. "preventMetricAggregates": "1",
  290. "per_page": 50,
  291. "project": self.project.id,
  292. }
  293. )
  294. assert response.status_code == 400, response.content
  295. def test_having_condition_not_selected(self):
  296. self.store_transaction_metric(
  297. 1,
  298. tags={"environment": "staging", "transaction": "foo_transaction"},
  299. timestamp=self.min_ago,
  300. )
  301. self.store_transaction_metric(
  302. # shouldn't show up
  303. 100,
  304. tags={"environment": "staging", "transaction": "bar_transaction"},
  305. timestamp=self.min_ago,
  306. )
  307. response = self.do_request(
  308. {
  309. "field": ["transaction", "project", "p50(transaction.duration)"],
  310. "query": "event.type:transaction p75(transaction.duration):<50",
  311. "dataset": "metricsEnhanced",
  312. "per_page": 50,
  313. }
  314. )
  315. assert response.status_code == 200, response.content
  316. assert len(response.data["data"]) == 1
  317. data = response.data["data"]
  318. meta = response.data["meta"]
  319. field_meta = meta["fields"]
  320. assert data[0]["transaction"] == "foo_transaction"
  321. assert data[0]["project"] == self.project.slug
  322. assert data[0]["p50(transaction.duration)"] == 1
  323. assert meta["isMetricsData"]
  324. assert field_meta["transaction"] == "string"
  325. assert field_meta["project"] == "string"
  326. assert field_meta["p50(transaction.duration)"] == "duration"
  327. def test_non_metrics_tag_with_implicit_format(self):
  328. self.store_transaction_metric(
  329. 1,
  330. tags={"environment": "staging", "transaction": "foo_transaction"},
  331. timestamp=self.min_ago,
  332. )
  333. response = self.do_request(
  334. {
  335. "field": ["test", "p50(transaction.duration)"],
  336. "query": "event.type:transaction",
  337. "dataset": "metricsEnhanced",
  338. "per_page": 50,
  339. }
  340. )
  341. assert response.status_code == 200, response.content
  342. assert len(response.data["data"]) == 0
  343. assert not response.data["meta"]["isMetricsData"]
  344. def test_non_metrics_tag_with_implicit_format_metrics_dataset(self):
  345. self.store_transaction_metric(
  346. 1,
  347. tags={"environment": "staging", "transaction": "foo_transaction"},
  348. timestamp=self.min_ago,
  349. )
  350. response = self.do_request(
  351. {
  352. "field": ["test", "p50(transaction.duration)"],
  353. "query": "event.type:transaction",
  354. "dataset": "metrics",
  355. "per_page": 50,
  356. }
  357. )
  358. assert response.status_code == 400, response.content
  359. def test_performance_homepage_query(self):
  360. self.store_transaction_metric(
  361. 1,
  362. tags={
  363. "transaction": "foo_transaction",
  364. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  365. },
  366. timestamp=self.min_ago,
  367. )
  368. self.store_transaction_metric(
  369. 1,
  370. "measurements.fcp",
  371. tags={"transaction": "foo_transaction"},
  372. timestamp=self.min_ago,
  373. )
  374. self.store_transaction_metric(
  375. 2,
  376. "measurements.lcp",
  377. tags={"transaction": "foo_transaction"},
  378. timestamp=self.min_ago,
  379. )
  380. self.store_transaction_metric(
  381. 3,
  382. "measurements.fid",
  383. tags={"transaction": "foo_transaction"},
  384. timestamp=self.min_ago,
  385. )
  386. self.store_transaction_metric(
  387. 4,
  388. "measurements.cls",
  389. tags={"transaction": "foo_transaction"},
  390. timestamp=self.min_ago,
  391. )
  392. self.store_transaction_metric(
  393. 1,
  394. "user",
  395. tags={
  396. "transaction": "foo_transaction",
  397. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_FRUSTRATED_TAG_VALUE,
  398. },
  399. timestamp=self.min_ago,
  400. )
  401. for dataset in ["metrics", "metricsEnhanced"]:
  402. response = self.do_request(
  403. {
  404. "field": [
  405. "transaction",
  406. "project",
  407. "tpm()",
  408. "p75(measurements.fcp)",
  409. "p75(measurements.lcp)",
  410. "p75(measurements.fid)",
  411. "p75(measurements.cls)",
  412. "count_unique(user)",
  413. "apdex()",
  414. "count_miserable(user)",
  415. "user_misery()",
  416. "failure_rate()",
  417. "failure_count()",
  418. ],
  419. "orderby": "tpm()",
  420. "query": "event.type:transaction",
  421. "dataset": dataset,
  422. "per_page": 50,
  423. }
  424. )
  425. assert len(response.data["data"]) == 1
  426. data = response.data["data"][0]
  427. meta = response.data["meta"]
  428. field_meta = meta["fields"]
  429. assert data["transaction"] == "foo_transaction"
  430. assert data["project"] == self.project.slug
  431. assert data["p75(measurements.fcp)"] == 1.0
  432. assert data["p75(measurements.lcp)"] == 2.0
  433. assert data["p75(measurements.fid)"] == 3.0
  434. assert data["p75(measurements.cls)"] == 4.0
  435. assert data["apdex()"] == 1.0
  436. assert data["count_miserable(user)"] == 1.0
  437. assert data["user_misery()"] == 0.058
  438. assert data["failure_rate()"] == 1
  439. assert data["failure_count()"] == 1
  440. assert meta["isMetricsData"]
  441. assert field_meta["transaction"] == "string"
  442. assert field_meta["project"] == "string"
  443. assert field_meta["p75(measurements.fcp)"] == "duration"
  444. assert field_meta["p75(measurements.lcp)"] == "duration"
  445. assert field_meta["p75(measurements.fid)"] == "duration"
  446. assert field_meta["p75(measurements.cls)"] == "number"
  447. assert field_meta["apdex()"] == "number"
  448. assert field_meta["count_miserable(user)"] == "integer"
  449. assert field_meta["user_misery()"] == "number"
  450. assert field_meta["failure_rate()"] == "percentage"
  451. assert field_meta["failure_count()"] == "integer"
  452. assert field_meta["tpm()"] == "rate"
  453. assert meta["units"]["tpm()"] == "1/minute"
  454. def test_user_misery_and_team_key_sort(self):
  455. self.store_transaction_metric(
  456. 1,
  457. tags={
  458. "transaction": "foo_transaction",
  459. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  460. },
  461. timestamp=self.min_ago,
  462. )
  463. self.store_transaction_metric(
  464. 1,
  465. "measurements.fcp",
  466. tags={"transaction": "foo_transaction"},
  467. timestamp=self.min_ago,
  468. )
  469. self.store_transaction_metric(
  470. 2,
  471. "measurements.lcp",
  472. tags={"transaction": "foo_transaction"},
  473. timestamp=self.min_ago,
  474. )
  475. self.store_transaction_metric(
  476. 3,
  477. "measurements.fid",
  478. tags={"transaction": "foo_transaction"},
  479. timestamp=self.min_ago,
  480. )
  481. self.store_transaction_metric(
  482. 4,
  483. "measurements.cls",
  484. tags={"transaction": "foo_transaction"},
  485. timestamp=self.min_ago,
  486. )
  487. self.store_transaction_metric(
  488. 1,
  489. "user",
  490. tags={
  491. "transaction": "foo_transaction",
  492. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_FRUSTRATED_TAG_VALUE,
  493. },
  494. timestamp=self.min_ago,
  495. )
  496. response = self.do_request(
  497. {
  498. "field": [
  499. "team_key_transaction",
  500. "transaction",
  501. "project",
  502. "tpm()",
  503. "p75(measurements.fcp)",
  504. "p75(measurements.lcp)",
  505. "p75(measurements.fid)",
  506. "p75(measurements.cls)",
  507. "count_unique(user)",
  508. "apdex()",
  509. "count_miserable(user)",
  510. "user_misery()",
  511. "failure_rate()",
  512. "failure_count()",
  513. ],
  514. "orderby": ["team_key_transaction", "user_misery()"],
  515. "query": "event.type:transaction",
  516. "dataset": "metrics",
  517. "per_page": 50,
  518. }
  519. )
  520. assert response.status_code == 200, response.content
  521. assert len(response.data["data"]) == 1
  522. data = response.data["data"][0]
  523. meta = response.data["meta"]
  524. field_meta = meta["fields"]
  525. assert data["transaction"] == "foo_transaction"
  526. assert data["project"] == self.project.slug
  527. assert data["p75(measurements.fcp)"] == 1.0
  528. assert data["p75(measurements.lcp)"] == 2.0
  529. assert data["p75(measurements.fid)"] == 3.0
  530. assert data["p75(measurements.cls)"] == 4.0
  531. assert data["apdex()"] == 1.0
  532. assert data["count_miserable(user)"] == 1.0
  533. assert data["user_misery()"] == 0.058
  534. assert data["failure_rate()"] == 1
  535. assert data["failure_count()"] == 1
  536. assert meta["isMetricsData"]
  537. assert field_meta["transaction"] == "string"
  538. assert field_meta["project"] == "string"
  539. assert field_meta["p75(measurements.fcp)"] == "duration"
  540. assert field_meta["p75(measurements.lcp)"] == "duration"
  541. assert field_meta["p75(measurements.fid)"] == "duration"
  542. assert field_meta["p75(measurements.cls)"] == "number"
  543. assert field_meta["apdex()"] == "number"
  544. assert field_meta["count_miserable(user)"] == "integer"
  545. assert field_meta["user_misery()"] == "number"
  546. assert field_meta["failure_rate()"] == "percentage"
  547. assert field_meta["failure_count()"] == "integer"
  548. def test_no_team_key_transactions(self):
  549. self.store_transaction_metric(
  550. 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago
  551. )
  552. self.store_transaction_metric(
  553. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  554. )
  555. query = {
  556. "team": "myteams",
  557. "project": [self.project.id],
  558. # TODO sort by transaction here once that's possible for order to match the same test without metrics
  559. "orderby": "p95()",
  560. "field": [
  561. "team_key_transaction",
  562. "transaction",
  563. "transaction.status",
  564. "project",
  565. "epm()",
  566. "failure_rate()",
  567. "p95()",
  568. ],
  569. "per_page": 50,
  570. "dataset": "metricsEnhanced",
  571. }
  572. response = self.do_request(query)
  573. assert response.status_code == 200, response.content
  574. assert len(response.data["data"]) == 2
  575. data = response.data["data"]
  576. meta = response.data["meta"]
  577. field_meta = meta["fields"]
  578. assert data[0]["team_key_transaction"] == 0
  579. assert data[0]["transaction"] == "foo_transaction"
  580. assert data[1]["team_key_transaction"] == 0
  581. assert data[1]["transaction"] == "bar_transaction"
  582. assert meta["isMetricsData"]
  583. assert field_meta["team_key_transaction"] == "boolean"
  584. assert field_meta["transaction"] == "string"
  585. def test_team_key_transactions_my_teams(self):
  586. team1 = self.create_team(organization=self.organization, name="Team A")
  587. self.create_team_membership(team1, user=self.user)
  588. self.project.add_team(team1)
  589. team2 = self.create_team(organization=self.organization, name="Team B")
  590. self.project.add_team(team2)
  591. key_transactions = [
  592. (team1, "foo_transaction"),
  593. (team2, "baz_transaction"),
  594. ]
  595. # Not a key transaction
  596. self.store_transaction_metric(
  597. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  598. )
  599. for team, transaction in key_transactions:
  600. self.store_transaction_metric(
  601. 1, tags={"transaction": transaction}, timestamp=self.min_ago
  602. )
  603. TeamKeyTransaction.objects.create(
  604. organization=self.organization,
  605. transaction=transaction,
  606. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  607. )
  608. query = {
  609. "team": "myteams",
  610. "project": [self.project.id],
  611. "field": [
  612. "team_key_transaction",
  613. "transaction",
  614. "transaction.status",
  615. "project",
  616. "epm()",
  617. "failure_rate()",
  618. "p95()",
  619. ],
  620. "per_page": 50,
  621. "dataset": "metricsEnhanced",
  622. }
  623. query["orderby"] = ["team_key_transaction", "p95()"]
  624. response = self.do_request(query)
  625. assert response.status_code == 200, response.content
  626. assert len(response.data["data"]) == 3
  627. data = response.data["data"]
  628. meta = response.data["meta"]
  629. field_meta = meta["fields"]
  630. assert data[0]["team_key_transaction"] == 0
  631. assert data[0]["transaction"] == "baz_transaction"
  632. assert data[1]["team_key_transaction"] == 0
  633. assert data[1]["transaction"] == "bar_transaction"
  634. assert data[2]["team_key_transaction"] == 1
  635. assert data[2]["transaction"] == "foo_transaction"
  636. assert meta["isMetricsData"]
  637. assert field_meta["team_key_transaction"] == "boolean"
  638. assert field_meta["transaction"] == "string"
  639. # not specifying any teams should use my teams
  640. query = {
  641. "project": [self.project.id],
  642. "field": [
  643. "team_key_transaction",
  644. "transaction",
  645. "transaction.status",
  646. "project",
  647. "epm()",
  648. "failure_rate()",
  649. "p95()",
  650. ],
  651. "per_page": 50,
  652. "dataset": "metricsEnhanced",
  653. }
  654. query["orderby"] = ["team_key_transaction", "p95()"]
  655. response = self.do_request(query)
  656. assert response.status_code == 200, response.content
  657. assert len(response.data["data"]) == 3
  658. data = response.data["data"]
  659. meta = response.data["meta"]
  660. field_meta = meta["fields"]
  661. assert data[0]["team_key_transaction"] == 0
  662. assert data[0]["transaction"] == "baz_transaction"
  663. assert data[1]["team_key_transaction"] == 0
  664. assert data[1]["transaction"] == "bar_transaction"
  665. assert data[2]["team_key_transaction"] == 1
  666. assert data[2]["transaction"] == "foo_transaction"
  667. assert meta["isMetricsData"]
  668. assert field_meta["team_key_transaction"] == "boolean"
  669. assert field_meta["transaction"] == "string"
  670. def test_team_key_transactions_orderby(self):
  671. team1 = self.create_team(organization=self.organization, name="Team A")
  672. team2 = self.create_team(organization=self.organization, name="Team B")
  673. key_transactions = [
  674. (team1, "foo_transaction", 1),
  675. (team2, "baz_transaction", 100),
  676. ]
  677. # Not a key transaction
  678. self.store_transaction_metric(
  679. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  680. )
  681. for team, transaction, value in key_transactions:
  682. self.store_transaction_metric(
  683. value, tags={"transaction": transaction}, timestamp=self.min_ago
  684. )
  685. self.create_team_membership(team, user=self.user)
  686. self.project.add_team(team)
  687. TeamKeyTransaction.objects.create(
  688. organization=self.organization,
  689. transaction=transaction,
  690. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  691. )
  692. query = {
  693. "team": "myteams",
  694. "project": [self.project.id],
  695. "field": [
  696. "team_key_transaction",
  697. "transaction",
  698. "transaction.status",
  699. "project",
  700. "epm()",
  701. "failure_rate()",
  702. "p95()",
  703. ],
  704. "per_page": 50,
  705. "dataset": "metricsEnhanced",
  706. }
  707. # test ascending order
  708. query["orderby"] = ["team_key_transaction", "p95()"]
  709. response = self.do_request(query)
  710. assert response.status_code == 200, response.content
  711. assert len(response.data["data"]) == 3
  712. data = response.data["data"]
  713. meta = response.data["meta"]
  714. field_meta = meta["fields"]
  715. assert data[0]["team_key_transaction"] == 0
  716. assert data[0]["transaction"] == "bar_transaction"
  717. assert data[1]["team_key_transaction"] == 1
  718. assert data[1]["transaction"] == "foo_transaction"
  719. assert data[2]["team_key_transaction"] == 1
  720. assert data[2]["transaction"] == "baz_transaction"
  721. assert meta["isMetricsData"]
  722. assert field_meta["team_key_transaction"] == "boolean"
  723. assert field_meta["transaction"] == "string"
  724. # test descending order
  725. query["orderby"] = ["-team_key_transaction", "p95()"]
  726. response = self.do_request(query)
  727. assert response.status_code == 200, response.content
  728. assert len(response.data["data"]) == 3
  729. data = response.data["data"]
  730. meta = response.data["meta"]
  731. field_meta = meta["fields"]
  732. assert data[0]["team_key_transaction"] == 1
  733. assert data[0]["transaction"] == "foo_transaction"
  734. assert data[1]["team_key_transaction"] == 1
  735. assert data[1]["transaction"] == "baz_transaction"
  736. assert data[2]["team_key_transaction"] == 0
  737. assert data[2]["transaction"] == "bar_transaction"
  738. assert meta["isMetricsData"]
  739. assert field_meta["team_key_transaction"] == "boolean"
  740. assert field_meta["transaction"] == "string"
  741. def test_team_key_transactions_query(self):
  742. team1 = self.create_team(organization=self.organization, name="Team A")
  743. team2 = self.create_team(organization=self.organization, name="Team B")
  744. key_transactions = [
  745. (team1, "foo_transaction", 1),
  746. (team2, "baz_transaction", 100),
  747. ]
  748. # Not a key transaction
  749. self.store_transaction_metric(
  750. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  751. )
  752. for team, transaction, value in key_transactions:
  753. self.store_transaction_metric(
  754. value, tags={"transaction": transaction}, timestamp=self.min_ago
  755. )
  756. self.create_team_membership(team, user=self.user)
  757. self.project.add_team(team)
  758. TeamKeyTransaction.objects.create(
  759. organization=self.organization,
  760. transaction=transaction,
  761. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  762. )
  763. query = {
  764. "team": "myteams",
  765. "project": [self.project.id],
  766. # use the order by to ensure the result order
  767. "orderby": "p95()",
  768. "field": [
  769. "team_key_transaction",
  770. "transaction",
  771. "transaction.status",
  772. "project",
  773. "epm()",
  774. "failure_rate()",
  775. "p95()",
  776. ],
  777. "per_page": 50,
  778. "dataset": "metricsEnhanced",
  779. }
  780. # key transactions
  781. query["query"] = "has:team_key_transaction"
  782. response = self.do_request(query)
  783. assert response.status_code == 200, response.content
  784. assert len(response.data["data"]) == 2
  785. data = response.data["data"]
  786. meta = response.data["meta"]
  787. field_meta = meta["fields"]
  788. assert data[0]["team_key_transaction"] == 1
  789. assert data[0]["transaction"] == "foo_transaction"
  790. assert data[1]["team_key_transaction"] == 1
  791. assert data[1]["transaction"] == "baz_transaction"
  792. assert meta["isMetricsData"]
  793. assert field_meta["team_key_transaction"] == "boolean"
  794. assert field_meta["transaction"] == "string"
  795. # key transactions
  796. query["query"] = "team_key_transaction:true"
  797. response = self.do_request(query)
  798. assert response.status_code == 200, response.content
  799. assert len(response.data["data"]) == 2
  800. data = response.data["data"]
  801. meta = response.data["meta"]
  802. field_meta = meta["fields"]
  803. assert data[0]["team_key_transaction"] == 1
  804. assert data[0]["transaction"] == "foo_transaction"
  805. assert data[1]["team_key_transaction"] == 1
  806. assert data[1]["transaction"] == "baz_transaction"
  807. assert meta["isMetricsData"]
  808. assert field_meta["team_key_transaction"] == "boolean"
  809. assert field_meta["transaction"] == "string"
  810. # not key transactions
  811. query["query"] = "!has:team_key_transaction"
  812. response = self.do_request(query)
  813. assert response.status_code == 200, response.content
  814. assert len(response.data["data"]) == 1
  815. data = response.data["data"]
  816. meta = response.data["meta"]
  817. field_meta = meta["fields"]
  818. assert data[0]["team_key_transaction"] == 0
  819. assert data[0]["transaction"] == "bar_transaction"
  820. assert meta["isMetricsData"]
  821. assert field_meta["team_key_transaction"] == "boolean"
  822. assert field_meta["transaction"] == "string"
  823. # not key transactions
  824. query["query"] = "team_key_transaction:false"
  825. response = self.do_request(query)
  826. assert response.status_code == 200, response.content
  827. assert len(response.data["data"]) == 1
  828. data = response.data["data"]
  829. meta = response.data["meta"]
  830. field_meta = meta["fields"]
  831. assert data[0]["team_key_transaction"] == 0
  832. assert data[0]["transaction"] == "bar_transaction"
  833. assert meta["isMetricsData"]
  834. assert field_meta["team_key_transaction"] == "boolean"
  835. assert field_meta["transaction"] == "string"
  836. def test_team_key_transaction_not_exists(self):
  837. team1 = self.create_team(organization=self.organization, name="Team A")
  838. team2 = self.create_team(organization=self.organization, name="Team B")
  839. key_transactions = [
  840. (team1, "foo_transaction", 1),
  841. (team2, "baz_transaction", 100),
  842. ]
  843. for team, transaction, value in key_transactions:
  844. self.store_transaction_metric(
  845. value, tags={"transaction": transaction}, timestamp=self.min_ago
  846. )
  847. self.create_team_membership(team, user=self.user)
  848. self.project.add_team(team)
  849. TeamKeyTransaction.objects.create(
  850. organization=self.organization,
  851. transaction=transaction,
  852. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  853. )
  854. # Don't create a metric for this one
  855. TeamKeyTransaction.objects.create(
  856. organization=self.organization,
  857. transaction="not_in_metrics",
  858. project_team=ProjectTeam.objects.get(project=self.project, team=team1),
  859. )
  860. query = {
  861. "team": "myteams",
  862. "project": [self.project.id],
  863. # use the order by to ensure the result order
  864. "orderby": "p95()",
  865. "field": [
  866. "team_key_transaction",
  867. "transaction",
  868. "transaction.status",
  869. "project",
  870. "epm()",
  871. "failure_rate()",
  872. "p95()",
  873. ],
  874. "per_page": 50,
  875. "dataset": "metricsEnhanced",
  876. }
  877. # key transactions
  878. query["query"] = "has:team_key_transaction"
  879. response = self.do_request(query)
  880. assert response.status_code == 200, response.content
  881. assert len(response.data["data"]) == 2
  882. data = response.data["data"]
  883. meta = response.data["meta"]
  884. field_meta = meta["fields"]
  885. assert data[0]["team_key_transaction"] == 1
  886. assert data[0]["transaction"] == "foo_transaction"
  887. assert data[1]["team_key_transaction"] == 1
  888. assert data[1]["transaction"] == "baz_transaction"
  889. assert meta["isMetricsData"]
  890. assert field_meta["team_key_transaction"] == "boolean"
  891. assert field_meta["transaction"] == "string"
  892. # key transactions
  893. query["query"] = "team_key_transaction:true"
  894. response = self.do_request(query)
  895. assert response.status_code == 200, response.content
  896. assert len(response.data["data"]) == 2
  897. data = response.data["data"]
  898. meta = response.data["meta"]
  899. field_meta = meta["fields"]
  900. assert data[0]["team_key_transaction"] == 1
  901. assert data[0]["transaction"] == "foo_transaction"
  902. assert data[1]["team_key_transaction"] == 1
  903. assert data[1]["transaction"] == "baz_transaction"
  904. assert meta["isMetricsData"]
  905. assert field_meta["team_key_transaction"] == "boolean"
  906. assert field_meta["transaction"] == "string"
  907. # not key transactions
  908. query["query"] = "!has:team_key_transaction"
  909. response = self.do_request(query)
  910. assert response.status_code == 200, response.content
  911. assert len(response.data["data"]) == 0
  912. data = response.data["data"]
  913. meta = response.data["meta"]
  914. field_meta = meta["fields"]
  915. assert meta["isMetricsData"]
  916. assert field_meta["team_key_transaction"] == "boolean"
  917. assert field_meta["transaction"] == "string"
  918. # not key transactions
  919. query["query"] = "team_key_transaction:false"
  920. response = self.do_request(query)
  921. assert response.status_code == 200, response.content
  922. assert len(response.data["data"]) == 0
  923. data = response.data["data"]
  924. meta = response.data["meta"]
  925. field_meta = meta["fields"]
  926. assert meta["isMetricsData"]
  927. assert field_meta["team_key_transaction"] == "boolean"
  928. assert field_meta["transaction"] == "string"
  929. def test_too_many_team_key_transactions(self):
  930. MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS = 1
  931. with mock.patch(
  932. "sentry.search.events.fields.MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS",
  933. MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS,
  934. ):
  935. team = self.create_team(organization=self.organization, name="Team A")
  936. self.create_team_membership(team, user=self.user)
  937. self.project.add_team(team)
  938. project_team = ProjectTeam.objects.get(project=self.project, team=team)
  939. transactions = ["foo_transaction", "bar_transaction", "baz_transaction"]
  940. for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1):
  941. self.store_transaction_metric(
  942. 100, tags={"transaction": transactions[i]}, timestamp=self.min_ago
  943. )
  944. TeamKeyTransaction.objects.bulk_create(
  945. [
  946. TeamKeyTransaction(
  947. organization=self.organization,
  948. project_team=project_team,
  949. transaction=transactions[i],
  950. )
  951. for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1)
  952. ]
  953. )
  954. query = {
  955. "team": "myteams",
  956. "project": [self.project.id],
  957. "orderby": "p95()",
  958. "field": [
  959. "team_key_transaction",
  960. "transaction",
  961. "transaction.status",
  962. "project",
  963. "epm()",
  964. "failure_rate()",
  965. "p95()",
  966. ],
  967. "dataset": "metricsEnhanced",
  968. "per_page": 50,
  969. }
  970. response = self.do_request(query)
  971. assert response.status_code == 200, response.content
  972. assert len(response.data["data"]) == 2
  973. data = response.data["data"]
  974. meta = response.data["meta"]
  975. assert (
  976. sum(row["team_key_transaction"] for row in data)
  977. == MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS
  978. )
  979. assert meta["isMetricsData"]
  980. def test_measurement_rating(self):
  981. self.store_transaction_metric(
  982. 50,
  983. metric="measurements.lcp",
  984. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  985. timestamp=self.min_ago,
  986. )
  987. self.store_transaction_metric(
  988. 15,
  989. metric="measurements.fp",
  990. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  991. timestamp=self.min_ago,
  992. )
  993. self.store_transaction_metric(
  994. 1500,
  995. metric="measurements.fcp",
  996. tags={"measurement_rating": "meh", "transaction": "foo_transaction"},
  997. timestamp=self.min_ago,
  998. )
  999. self.store_transaction_metric(
  1000. 125,
  1001. metric="measurements.fid",
  1002. tags={"measurement_rating": "meh", "transaction": "foo_transaction"},
  1003. timestamp=self.min_ago,
  1004. )
  1005. self.store_transaction_metric(
  1006. 0.15,
  1007. metric="measurements.cls",
  1008. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  1009. timestamp=self.min_ago,
  1010. )
  1011. response = self.do_request(
  1012. {
  1013. "field": [
  1014. "transaction",
  1015. "count_web_vitals(measurements.lcp, good)",
  1016. "count_web_vitals(measurements.fp, good)",
  1017. "count_web_vitals(measurements.fcp, meh)",
  1018. "count_web_vitals(measurements.fid, meh)",
  1019. "count_web_vitals(measurements.cls, good)",
  1020. ],
  1021. "query": "event.type:transaction",
  1022. "dataset": "metricsEnhanced",
  1023. "per_page": 50,
  1024. }
  1025. )
  1026. assert response.status_code == 200, response.content
  1027. assert len(response.data["data"]) == 1
  1028. data = response.data["data"]
  1029. meta = response.data["meta"]
  1030. field_meta = meta["fields"]
  1031. assert data[0]["count_web_vitals(measurements.lcp, good)"] == 1
  1032. assert data[0]["count_web_vitals(measurements.fp, good)"] == 1
  1033. assert data[0]["count_web_vitals(measurements.fcp, meh)"] == 1
  1034. assert data[0]["count_web_vitals(measurements.fid, meh)"] == 1
  1035. assert data[0]["count_web_vitals(measurements.cls, good)"] == 1
  1036. assert meta["isMetricsData"]
  1037. assert field_meta["count_web_vitals(measurements.lcp, good)"] == "integer"
  1038. assert field_meta["count_web_vitals(measurements.fp, good)"] == "integer"
  1039. assert field_meta["count_web_vitals(measurements.fcp, meh)"] == "integer"
  1040. assert field_meta["count_web_vitals(measurements.fid, meh)"] == "integer"
  1041. assert field_meta["count_web_vitals(measurements.cls, good)"] == "integer"
  1042. def test_measurement_rating_that_does_not_exist(self):
  1043. self.store_transaction_metric(
  1044. 1,
  1045. metric="measurements.lcp",
  1046. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  1047. timestamp=self.min_ago,
  1048. )
  1049. response = self.do_request(
  1050. {
  1051. "field": ["transaction", "count_web_vitals(measurements.lcp, poor)"],
  1052. "query": "event.type:transaction",
  1053. "dataset": "metricsEnhanced",
  1054. "per_page": 50,
  1055. }
  1056. )
  1057. assert response.status_code == 200, response.content
  1058. assert len(response.data["data"]) == 1
  1059. data = response.data["data"]
  1060. meta = response.data["meta"]
  1061. assert data[0]["count_web_vitals(measurements.lcp, poor)"] == 0
  1062. assert meta["isMetricsData"]
  1063. assert meta["fields"]["count_web_vitals(measurements.lcp, poor)"] == "integer"
  1064. def test_count_web_vitals_invalid_vital(self):
  1065. query = {
  1066. "field": [
  1067. "count_web_vitals(measurements.foo, poor)",
  1068. ],
  1069. "project": [self.project.id],
  1070. "dataset": "metricsEnhanced",
  1071. }
  1072. response = self.do_request(query)
  1073. assert response.status_code == 400, response.content
  1074. query = {
  1075. "field": [
  1076. "count_web_vitals(tags[lcp], poor)",
  1077. ],
  1078. "project": [self.project.id],
  1079. "dataset": "metricsEnhanced",
  1080. }
  1081. response = self.do_request(query)
  1082. assert response.status_code == 400, response.content
  1083. query = {
  1084. "field": [
  1085. "count_web_vitals(transaction.duration, poor)",
  1086. ],
  1087. "project": [self.project.id],
  1088. "dataset": "metricsEnhanced",
  1089. }
  1090. response = self.do_request(query)
  1091. assert response.status_code == 400, response.content
  1092. query = {
  1093. "field": [
  1094. "count_web_vitals(measurements.lcp, bad)",
  1095. ],
  1096. "project": [self.project.id],
  1097. "dataset": "metricsEnhanced",
  1098. }
  1099. response = self.do_request(query)
  1100. assert response.status_code == 400, response.content
  1101. def test_count_unique_user_returns_zero(self):
  1102. self.store_transaction_metric(
  1103. 50,
  1104. metric="user",
  1105. tags={"transaction": "foo_transaction"},
  1106. timestamp=self.min_ago,
  1107. )
  1108. self.store_transaction_metric(
  1109. 50,
  1110. tags={"transaction": "foo_transaction"},
  1111. timestamp=self.min_ago,
  1112. )
  1113. self.store_transaction_metric(
  1114. 100,
  1115. tags={"transaction": "bar_transaction"},
  1116. timestamp=self.min_ago,
  1117. )
  1118. query = {
  1119. "project": [self.project.id],
  1120. "orderby": "p50()",
  1121. "field": [
  1122. "transaction",
  1123. "count_unique(user)",
  1124. "p50()",
  1125. ],
  1126. "dataset": "metricsEnhanced",
  1127. "per_page": 50,
  1128. }
  1129. response = self.do_request(query)
  1130. assert response.status_code == 200, response.content
  1131. assert len(response.data["data"]) == 2
  1132. data = response.data["data"]
  1133. meta = response.data["meta"]
  1134. assert data[0]["transaction"] == "foo_transaction"
  1135. assert data[0]["count_unique(user)"] == 1
  1136. assert data[1]["transaction"] == "bar_transaction"
  1137. assert data[1]["count_unique(user)"] == 0
  1138. assert meta["isMetricsData"]
  1139. def test_sum_transaction_duration(self):
  1140. self.store_transaction_metric(
  1141. 50,
  1142. tags={"transaction": "foo_transaction"},
  1143. timestamp=self.min_ago,
  1144. )
  1145. self.store_transaction_metric(
  1146. 100,
  1147. tags={"transaction": "foo_transaction"},
  1148. timestamp=self.min_ago,
  1149. )
  1150. self.store_transaction_metric(
  1151. 150,
  1152. tags={"transaction": "foo_transaction"},
  1153. timestamp=self.min_ago,
  1154. )
  1155. query = {
  1156. "project": [self.project.id],
  1157. "orderby": "sum(transaction.duration)",
  1158. "field": [
  1159. "transaction",
  1160. "sum(transaction.duration)",
  1161. ],
  1162. "dataset": "metricsEnhanced",
  1163. "per_page": 50,
  1164. }
  1165. response = self.do_request(query)
  1166. assert response.status_code == 200, response.content
  1167. assert len(response.data["data"]) == 1
  1168. data = response.data["data"]
  1169. meta = response.data["meta"]
  1170. assert data[0]["transaction"] == "foo_transaction"
  1171. assert data[0]["sum(transaction.duration)"] == 300
  1172. assert meta["isMetricsData"]
  1173. def test_custom_measurements_simple(self):
  1174. self.store_transaction_metric(
  1175. 1,
  1176. metric="measurements.something_custom",
  1177. internal_metric="d:transactions/measurements.something_custom@millisecond",
  1178. entity="metrics_distributions",
  1179. tags={"transaction": "foo_transaction"},
  1180. timestamp=self.min_ago,
  1181. )
  1182. query = {
  1183. "project": [self.project.id],
  1184. "orderby": "p50(measurements.something_custom)",
  1185. "field": [
  1186. "transaction",
  1187. "p50(measurements.something_custom)",
  1188. ],
  1189. "statsPeriod": "24h",
  1190. "dataset": "metricsEnhanced",
  1191. "per_page": 50,
  1192. }
  1193. self.wait_for_metric_count(
  1194. self.project,
  1195. 1,
  1196. metric="measurements.something_custom",
  1197. mri="d:transactions/measurements.something_custom@millisecond",
  1198. )
  1199. response = self.do_request(query)
  1200. assert response.status_code == 200, response.content
  1201. assert len(response.data["data"]) == 1
  1202. data = response.data["data"]
  1203. meta = response.data["meta"]
  1204. assert data[0]["transaction"] == "foo_transaction"
  1205. assert data[0]["p50(measurements.something_custom)"] == 1
  1206. assert meta["isMetricsData"]
  1207. assert meta["fields"]["p50(measurements.something_custom)"] == "duration"
  1208. assert meta["units"]["p50(measurements.something_custom)"] == "millisecond"
  1209. def test_custom_measurement_size_meta_type(self):
  1210. self.store_transaction_metric(
  1211. 100,
  1212. metric="measurements.custom_type",
  1213. internal_metric="d:transactions/measurements.custom_type@somethingcustom",
  1214. entity="metrics_distributions",
  1215. tags={"transaction": "foo_transaction"},
  1216. timestamp=self.min_ago,
  1217. )
  1218. self.store_transaction_metric(
  1219. 100,
  1220. metric="measurements.percent",
  1221. internal_metric="d:transactions/measurements.percent@ratio",
  1222. entity="metrics_distributions",
  1223. tags={"transaction": "foo_transaction"},
  1224. timestamp=self.min_ago,
  1225. )
  1226. self.store_transaction_metric(
  1227. 100,
  1228. metric="measurements.longtaskcount",
  1229. internal_metric="d:transactions/measurements.longtaskcount@none",
  1230. entity="metrics_distributions",
  1231. tags={"transaction": "foo_transaction"},
  1232. timestamp=self.min_ago,
  1233. )
  1234. query = {
  1235. "project": [self.project.id],
  1236. "orderby": "p50(measurements.longtaskcount)",
  1237. "field": [
  1238. "transaction",
  1239. "p50(measurements.longtaskcount)",
  1240. "p50(measurements.percent)",
  1241. "p50(measurements.custom_type)",
  1242. ],
  1243. "statsPeriod": "24h",
  1244. "dataset": "metricsEnhanced",
  1245. "per_page": 50,
  1246. }
  1247. response = self.do_request(query)
  1248. assert response.status_code == 200, response.content
  1249. assert len(response.data["data"]) == 1
  1250. data = response.data["data"]
  1251. meta = response.data["meta"]
  1252. assert data[0]["transaction"] == "foo_transaction"
  1253. assert data[0]["p50(measurements.longtaskcount)"] == 100
  1254. assert data[0]["p50(measurements.percent)"] == 100
  1255. assert data[0]["p50(measurements.custom_type)"] == 100
  1256. assert meta["isMetricsData"]
  1257. assert meta["fields"]["p50(measurements.longtaskcount)"] == "integer"
  1258. assert meta["units"]["p50(measurements.longtaskcount)"] is None
  1259. assert meta["fields"]["p50(measurements.percent)"] == "percentage"
  1260. assert meta["units"]["p50(measurements.percent)"] is None
  1261. assert meta["fields"]["p50(measurements.custom_type)"] == "number"
  1262. assert meta["units"]["p50(measurements.custom_type)"] is None
  1263. def test_custom_measurement_none_type(self):
  1264. self.store_transaction_metric(
  1265. 1,
  1266. metric="measurements.cls",
  1267. entity="metrics_distributions",
  1268. tags={"transaction": "foo_transaction"},
  1269. timestamp=self.min_ago,
  1270. )
  1271. query = {
  1272. "project": [self.project.id],
  1273. "orderby": "p75(measurements.cls)",
  1274. "field": [
  1275. "transaction",
  1276. "p75(measurements.cls)",
  1277. "p99(measurements.cls)",
  1278. "max(measurements.cls)",
  1279. ],
  1280. "statsPeriod": "24h",
  1281. "dataset": "metricsEnhanced",
  1282. "per_page": 50,
  1283. }
  1284. self.wait_for_metric_count(
  1285. self.project,
  1286. 1,
  1287. metric=TransactionMetricKey.MEASUREMENTS_CLS.value,
  1288. mri=TransactionMRI.MEASUREMENTS_CLS.value,
  1289. )
  1290. response = self.do_request(query)
  1291. assert response.status_code == 200, response.content
  1292. assert len(response.data["data"]) == 1
  1293. data = response.data["data"]
  1294. meta = response.data["meta"]
  1295. assert data[0]["transaction"] == "foo_transaction"
  1296. assert data[0]["p75(measurements.cls)"] == 1
  1297. assert data[0]["p99(measurements.cls)"] == 1
  1298. assert data[0]["max(measurements.cls)"] == 1
  1299. assert meta["isMetricsData"]
  1300. assert meta["fields"]["p75(measurements.cls)"] == "number"
  1301. assert meta["units"]["p75(measurements.cls)"] is None
  1302. assert meta["fields"]["p99(measurements.cls)"] == "number"
  1303. assert meta["units"]["p99(measurements.cls)"] is None
  1304. assert meta["fields"]["max(measurements.cls)"] == "number"
  1305. assert meta["units"]["max(measurements.cls)"] is None
  1306. def test_custom_measurement_duration_filtering(self):
  1307. self.store_transaction_metric(
  1308. 1,
  1309. metric="measurements.runtime",
  1310. internal_metric="d:transactions/measurements.runtime@hour",
  1311. entity="metrics_distributions",
  1312. tags={"transaction": "foo_transaction"},
  1313. timestamp=self.min_ago,
  1314. )
  1315. self.store_transaction_metric(
  1316. 180,
  1317. metric="measurements.runtime",
  1318. internal_metric="d:transactions/measurements.runtime@hour",
  1319. entity="metrics_distributions",
  1320. tags={"transaction": "bar_transaction"},
  1321. timestamp=self.min_ago,
  1322. )
  1323. query = {
  1324. "project": [self.project.id],
  1325. "field": [
  1326. "transaction",
  1327. "max(measurements.runtime)",
  1328. ],
  1329. "query": "p50(measurements.runtime):>1wk",
  1330. "statsPeriod": "24h",
  1331. "dataset": "metricsEnhanced",
  1332. "per_page": 50,
  1333. }
  1334. response = self.do_request(query)
  1335. assert response.status_code == 200, response.content
  1336. assert len(response.data["data"]) == 1
  1337. data = response.data["data"]
  1338. meta = response.data["meta"]
  1339. assert data[0]["transaction"] == "bar_transaction"
  1340. assert data[0]["max(measurements.runtime)"] == 180
  1341. assert meta["isMetricsData"]
  1342. def test_custom_measurement_size_filtering(self):
  1343. self.store_transaction_metric(
  1344. 1,
  1345. metric="measurements.datacenter_memory",
  1346. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1347. entity="metrics_distributions",
  1348. tags={"transaction": "foo_transaction"},
  1349. timestamp=self.min_ago,
  1350. )
  1351. self.store_transaction_metric(
  1352. 100,
  1353. metric="measurements.datacenter_memory",
  1354. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1355. entity="metrics_distributions",
  1356. tags={"transaction": "bar_transaction"},
  1357. timestamp=self.min_ago,
  1358. )
  1359. query = {
  1360. "project": [self.project.id],
  1361. "field": [
  1362. "transaction",
  1363. "max(measurements.datacenter_memory)",
  1364. ],
  1365. "query": "p50(measurements.datacenter_memory):>5pb",
  1366. "statsPeriod": "24h",
  1367. "dataset": "metricsEnhanced",
  1368. "per_page": 50,
  1369. }
  1370. response = self.do_request(query)
  1371. assert response.status_code == 200, response.content
  1372. assert len(response.data["data"]) == 1
  1373. data = response.data["data"]
  1374. meta = response.data["meta"]
  1375. assert data[0]["transaction"] == "bar_transaction"
  1376. assert data[0]["max(measurements.datacenter_memory)"] == 100
  1377. assert meta["units"]["max(measurements.datacenter_memory)"] == "petabyte"
  1378. assert meta["fields"]["max(measurements.datacenter_memory)"] == "size"
  1379. assert meta["isMetricsData"]
  1380. def test_has_custom_measurement(self):
  1381. self.store_transaction_metric(
  1382. 33,
  1383. metric="measurements.datacenter_memory",
  1384. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1385. entity="metrics_distributions",
  1386. tags={"transaction": "foo_transaction"},
  1387. timestamp=self.min_ago,
  1388. )
  1389. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1390. transaction_data["measurements"]["datacenter_memory"] = {
  1391. "value": 33,
  1392. "unit": "petabyte",
  1393. }
  1394. self.store_event(transaction_data, self.project.id)
  1395. measurement = "measurements.datacenter_memory"
  1396. response = self.do_request(
  1397. {
  1398. "field": ["transaction", measurement],
  1399. "query": "has:measurements.datacenter_memory",
  1400. "dataset": "discover",
  1401. }
  1402. )
  1403. assert response.status_code == 200, response.content
  1404. assert len(response.data["data"]) == 1
  1405. response = self.do_request(
  1406. {
  1407. "field": ["transaction", measurement],
  1408. "query": "!has:measurements.datacenter_memory",
  1409. "dataset": "discover",
  1410. }
  1411. )
  1412. assert response.status_code == 200, response.content
  1413. assert len(response.data["data"]) == 0
  1414. def test_environment_param(self):
  1415. self.create_environment(self.project, name="staging")
  1416. self.store_transaction_metric(
  1417. 1,
  1418. tags={"transaction": "foo_transaction", "environment": "staging"},
  1419. timestamp=self.min_ago,
  1420. )
  1421. self.store_transaction_metric(
  1422. 100,
  1423. tags={"transaction": "foo_transaction"},
  1424. timestamp=self.min_ago,
  1425. )
  1426. query = {
  1427. "project": [self.project.id],
  1428. "environment": "staging",
  1429. "orderby": "p50(transaction.duration)",
  1430. "field": [
  1431. "transaction",
  1432. "environment",
  1433. "p50(transaction.duration)",
  1434. ],
  1435. "statsPeriod": "24h",
  1436. "dataset": "metricsEnhanced",
  1437. "per_page": 50,
  1438. }
  1439. response = self.do_request(query)
  1440. assert response.status_code == 200, response.content
  1441. assert len(response.data["data"]) == 1
  1442. data = response.data["data"]
  1443. meta = response.data["meta"]
  1444. assert data[0]["transaction"] == "foo_transaction"
  1445. assert data[0]["environment"] == "staging"
  1446. assert data[0]["p50(transaction.duration)"] == 1
  1447. assert meta["isMetricsData"]
  1448. def test_environment_query(self):
  1449. self.create_environment(self.project, name="staging")
  1450. self.store_transaction_metric(
  1451. 1,
  1452. tags={"transaction": "foo_transaction", "environment": "staging"},
  1453. timestamp=self.min_ago,
  1454. )
  1455. self.store_transaction_metric(
  1456. 100,
  1457. tags={"transaction": "foo_transaction"},
  1458. timestamp=self.min_ago,
  1459. )
  1460. query = {
  1461. "project": [self.project.id],
  1462. "orderby": "p50(transaction.duration)",
  1463. "field": [
  1464. "transaction",
  1465. "environment",
  1466. "p50(transaction.duration)",
  1467. ],
  1468. "query": "!has:environment",
  1469. "statsPeriod": "24h",
  1470. "dataset": "metricsEnhanced",
  1471. "per_page": 50,
  1472. }
  1473. self.wait_for_metric_count(self.project, 2)
  1474. response = self.do_request(query)
  1475. assert response.status_code == 200, response.content
  1476. assert len(response.data["data"]) == 1
  1477. data = response.data["data"]
  1478. meta = response.data["meta"]
  1479. assert data[0]["transaction"] == "foo_transaction"
  1480. assert data[0]["environment"] is None or data[0]["environment"] == ""
  1481. assert data[0]["p50(transaction.duration)"] == 100
  1482. assert meta["isMetricsData"]
  1483. def test_has_transaction(self):
  1484. self.store_transaction_metric(
  1485. 1,
  1486. tags={},
  1487. timestamp=self.min_ago,
  1488. )
  1489. self.store_transaction_metric(
  1490. 100,
  1491. tags={"transaction": "foo_transaction"},
  1492. timestamp=self.min_ago,
  1493. )
  1494. query = {
  1495. "project": [self.project.id],
  1496. "orderby": "p50(transaction.duration)",
  1497. "field": [
  1498. "transaction",
  1499. "p50(transaction.duration)",
  1500. ],
  1501. "query": "has:transaction",
  1502. "statsPeriod": "24h",
  1503. "dataset": "metricsEnhanced",
  1504. "per_page": 50,
  1505. }
  1506. self.wait_for_metric_count(self.project, 2)
  1507. response = self.do_request(query)
  1508. assert response.status_code == 200, response.content
  1509. assert len(response.data["data"]) == 2
  1510. data = response.data["data"]
  1511. meta = response.data["meta"]
  1512. assert data[0]["transaction"] == "<< unparameterized >>"
  1513. assert data[0]["p50(transaction.duration)"] == 1
  1514. assert data[1]["transaction"] == "foo_transaction"
  1515. assert data[1]["p50(transaction.duration)"] == 100
  1516. assert meta["isMetricsData"]
  1517. query = {
  1518. "project": [self.project.id],
  1519. "orderby": "p50(transaction.duration)",
  1520. "field": [
  1521. "transaction",
  1522. "p50(transaction.duration)",
  1523. ],
  1524. "query": "!has:transaction",
  1525. "statsPeriod": "24h",
  1526. "dataset": "metricsEnhanced",
  1527. "per_page": 50,
  1528. }
  1529. response = self.do_request(query)
  1530. assert response.status_code == 400, response.content
  1531. def test_apdex_transaction_threshold(self):
  1532. ProjectTransactionThresholdOverride.objects.create(
  1533. transaction="foo_transaction",
  1534. project=self.project,
  1535. organization=self.project.organization,
  1536. threshold=600,
  1537. metric=TransactionMetric.LCP.value,
  1538. )
  1539. ProjectTransactionThresholdOverride.objects.create(
  1540. transaction="bar_transaction",
  1541. project=self.project,
  1542. organization=self.project.organization,
  1543. threshold=600,
  1544. metric=TransactionMetric.LCP.value,
  1545. )
  1546. self.store_transaction_metric(
  1547. 1,
  1548. tags={
  1549. "transaction": "foo_transaction",
  1550. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1551. },
  1552. timestamp=self.min_ago,
  1553. )
  1554. self.store_transaction_metric(
  1555. 1,
  1556. "measurements.lcp",
  1557. tags={
  1558. "transaction": "bar_transaction",
  1559. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1560. },
  1561. timestamp=self.min_ago,
  1562. )
  1563. response = self.do_request(
  1564. {
  1565. "field": [
  1566. "transaction",
  1567. "apdex()",
  1568. ],
  1569. "orderby": ["apdex()"],
  1570. "query": "event.type:transaction",
  1571. "dataset": "metrics",
  1572. "per_page": 50,
  1573. }
  1574. )
  1575. assert len(response.data["data"]) == 2
  1576. data = response.data["data"]
  1577. meta = response.data["meta"]
  1578. field_meta = meta["fields"]
  1579. assert data[0]["transaction"] == "bar_transaction"
  1580. # Threshold is lcp based
  1581. assert data[0]["apdex()"] == 1
  1582. assert data[1]["transaction"] == "foo_transaction"
  1583. # Threshold is lcp based
  1584. assert data[1]["apdex()"] == 0
  1585. assert meta["isMetricsData"]
  1586. assert field_meta["transaction"] == "string"
  1587. assert field_meta["apdex()"] == "number"
  1588. def test_apdex_project_threshold(self):
  1589. ProjectTransactionThreshold.objects.create(
  1590. project=self.project,
  1591. organization=self.project.organization,
  1592. threshold=600,
  1593. metric=TransactionMetric.LCP.value,
  1594. )
  1595. self.store_transaction_metric(
  1596. 1,
  1597. tags={
  1598. "transaction": "foo_transaction",
  1599. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1600. },
  1601. timestamp=self.min_ago,
  1602. )
  1603. self.store_transaction_metric(
  1604. 1,
  1605. "measurements.lcp",
  1606. tags={
  1607. "transaction": "bar_transaction",
  1608. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1609. },
  1610. timestamp=self.min_ago,
  1611. )
  1612. response = self.do_request(
  1613. {
  1614. "field": [
  1615. "transaction",
  1616. "apdex()",
  1617. ],
  1618. "orderby": ["apdex()"],
  1619. "query": "event.type:transaction",
  1620. "dataset": "metrics",
  1621. "per_page": 50,
  1622. }
  1623. )
  1624. assert response.status_code == 200, response.content
  1625. assert len(response.data["data"]) == 2
  1626. data = response.data["data"]
  1627. meta = response.data["meta"]
  1628. field_meta = meta["fields"]
  1629. assert data[0]["transaction"] == "bar_transaction"
  1630. # Threshold is lcp based
  1631. assert data[0]["apdex()"] == 1
  1632. assert data[1]["transaction"] == "foo_transaction"
  1633. # Threshold is lcp based
  1634. assert data[1]["apdex()"] == 0
  1635. assert meta["isMetricsData"]
  1636. assert field_meta["transaction"] == "string"
  1637. assert field_meta["apdex()"] == "number"
  1638. def test_apdex_satisfaction_param(self):
  1639. for function in ["apdex(300)", "user_misery(300)", "count_miserable(user, 300)"]:
  1640. query = {
  1641. "project": [self.project.id],
  1642. "field": [
  1643. "transaction",
  1644. function,
  1645. ],
  1646. "statsPeriod": "24h",
  1647. "dataset": "metricsEnhanced",
  1648. "per_page": 50,
  1649. }
  1650. response = self.do_request(query)
  1651. assert response.status_code == 200, response.content
  1652. assert len(response.data["data"]) == 0
  1653. meta = response.data["meta"]
  1654. assert not meta["isMetricsData"], function
  1655. query = {
  1656. "project": [self.project.id],
  1657. "field": [
  1658. "transaction",
  1659. function,
  1660. ],
  1661. "statsPeriod": "24h",
  1662. "dataset": "metrics",
  1663. "per_page": 50,
  1664. }
  1665. response = self.do_request(query)
  1666. assert response.status_code == 400, function
  1667. assert b"threshold parameter" in response.content, function
  1668. def test_mobile_metrics(self):
  1669. self.store_transaction_metric(
  1670. 0.4,
  1671. "measurements.frames_frozen_rate",
  1672. tags={
  1673. "transaction": "bar_transaction",
  1674. },
  1675. timestamp=self.min_ago,
  1676. )
  1677. query = {
  1678. "project": [self.project.id],
  1679. "field": [
  1680. "transaction",
  1681. "p50(measurements.frames_frozen_rate)",
  1682. ],
  1683. "statsPeriod": "24h",
  1684. "dataset": "metrics",
  1685. "per_page": 50,
  1686. }
  1687. response = self.do_request(query)
  1688. assert response.status_code == 200, response.content
  1689. assert len(response.data["data"]) == 1
  1690. assert response.data["data"][0]["p50(measurements.frames_frozen_rate)"] == 0.4
  1691. def test_merge_null_unparam(self):
  1692. self.store_transaction_metric(
  1693. 1,
  1694. # Transaction: unparam
  1695. tags={
  1696. "transaction": "<< unparameterized >>",
  1697. },
  1698. timestamp=self.min_ago,
  1699. )
  1700. self.store_transaction_metric(
  1701. 2,
  1702. # Transaction:null
  1703. tags={},
  1704. timestamp=self.min_ago,
  1705. )
  1706. query = {
  1707. "project": [self.project.id],
  1708. "field": [
  1709. "transaction",
  1710. "p50(transaction.duration)",
  1711. ],
  1712. "statsPeriod": "24h",
  1713. "dataset": "metrics",
  1714. "per_page": 50,
  1715. }
  1716. response = self.do_request(query)
  1717. assert response.status_code == 200, response.content
  1718. assert len(response.data["data"]) == 1
  1719. assert response.data["data"][0]["p50(transaction.duration)"] == 1.5
  1720. def test_unparam_filter(self):
  1721. self.store_transaction_metric(
  1722. 1,
  1723. # Transaction: unparam
  1724. tags={
  1725. "transaction": "<< unparameterized >>",
  1726. },
  1727. timestamp=self.min_ago,
  1728. )
  1729. self.store_transaction_metric(
  1730. 2,
  1731. # Transaction:null
  1732. tags={},
  1733. timestamp=self.min_ago,
  1734. )
  1735. self.store_transaction_metric(
  1736. 3,
  1737. tags={
  1738. "transaction": "foo_transaction",
  1739. },
  1740. timestamp=self.min_ago,
  1741. )
  1742. query = {
  1743. "project": [self.project.id],
  1744. "field": [
  1745. "transaction",
  1746. "count()",
  1747. ],
  1748. "query": 'transaction:"<< unparameterized >>"',
  1749. "statsPeriod": "24h",
  1750. "dataset": "metrics",
  1751. "per_page": 50,
  1752. }
  1753. self.wait_for_metric_count(self.project, 3)
  1754. response = self.do_request(query)
  1755. assert response.status_code == 200, response.content
  1756. assert len(response.data["data"]) == 1
  1757. assert response.data["data"][0]["transaction"] == "<< unparameterized >>"
  1758. assert response.data["data"][0]["count()"] == 2
  1759. def test_custom_measurements_without_function(self):
  1760. self.store_transaction_metric(
  1761. 33,
  1762. metric="measurements.datacenter_memory",
  1763. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1764. entity="metrics_distributions",
  1765. tags={"transaction": "foo_transaction"},
  1766. timestamp=self.min_ago,
  1767. )
  1768. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1769. transaction_data["measurements"]["datacenter_memory"] = {
  1770. "value": 33,
  1771. "unit": "petabyte",
  1772. }
  1773. self.store_event(transaction_data, self.project.id)
  1774. measurement = "measurements.datacenter_memory"
  1775. response = self.do_request(
  1776. {
  1777. "field": ["transaction", measurement],
  1778. "query": "measurements.datacenter_memory:33pb",
  1779. "dataset": "discover",
  1780. }
  1781. )
  1782. assert response.status_code == 200, response.content
  1783. data = response.data["data"]
  1784. assert len(data) == 1
  1785. assert data[0][measurement] == 33
  1786. meta = response.data["meta"]
  1787. field_meta = meta["fields"]
  1788. unit_meta = meta["units"]
  1789. assert field_meta[measurement] == "size"
  1790. assert unit_meta[measurement] == "petabyte"
  1791. assert not meta["isMetricsData"]
  1792. def test_custom_measurements_with_function(self):
  1793. self.store_transaction_metric(
  1794. 33,
  1795. metric="measurements.datacenter_memory",
  1796. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1797. entity="metrics_distributions",
  1798. tags={"transaction": "foo_transaction"},
  1799. timestamp=self.min_ago,
  1800. )
  1801. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1802. transaction_data["measurements"]["datacenter_memory"] = {
  1803. "value": 33,
  1804. "unit": "petabyte",
  1805. }
  1806. self.store_event(transaction_data, self.project.id)
  1807. measurement = "p50(measurements.datacenter_memory)"
  1808. response = self.do_request(
  1809. {
  1810. "field": ["transaction", measurement],
  1811. "query": "measurements.datacenter_memory:33pb",
  1812. "dataset": "discover",
  1813. }
  1814. )
  1815. assert response.status_code == 200, response.content
  1816. data = response.data["data"]
  1817. assert len(data) == 1
  1818. assert data[0][measurement] == 33
  1819. meta = response.data["meta"]
  1820. field_meta = meta["fields"]
  1821. unit_meta = meta["units"]
  1822. assert field_meta[measurement] == "size"
  1823. assert unit_meta[measurement] == "petabyte"
  1824. assert not meta["isMetricsData"]
  1825. def test_custom_measurements_equation(self):
  1826. self.store_transaction_metric(
  1827. 33,
  1828. metric="measurements.datacenter_memory",
  1829. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1830. entity="metrics_distributions",
  1831. tags={"transaction": "foo_transaction"},
  1832. timestamp=self.min_ago,
  1833. )
  1834. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1835. transaction_data["measurements"]["datacenter_memory"] = {
  1836. "value": 33,
  1837. "unit": "petabyte",
  1838. }
  1839. self.store_event(transaction_data, self.project.id)
  1840. response = self.do_request(
  1841. {
  1842. "field": [
  1843. "transaction",
  1844. "measurements.datacenter_memory",
  1845. "equation|measurements.datacenter_memory / 3",
  1846. ],
  1847. "query": "",
  1848. "dataset": "discover",
  1849. }
  1850. )
  1851. assert response.status_code == 200, response.content
  1852. data = response.data["data"]
  1853. assert len(data) == 1
  1854. assert data[0]["measurements.datacenter_memory"] == 33
  1855. assert data[0]["equation|measurements.datacenter_memory / 3"] == 11
  1856. meta = response.data["meta"]
  1857. assert not meta["isMetricsData"]
  1858. def test_transaction_wildcard(self):
  1859. self.store_transaction_metric(
  1860. 1,
  1861. tags={"transaction": "foo_transaction"},
  1862. timestamp=self.min_ago,
  1863. )
  1864. self.store_transaction_metric(
  1865. 1,
  1866. tags={"transaction": "bar_transaction"},
  1867. timestamp=self.min_ago,
  1868. )
  1869. response = self.do_request(
  1870. {
  1871. "field": [
  1872. "transaction",
  1873. "p90()",
  1874. ],
  1875. "query": "transaction:foo*",
  1876. "dataset": "metrics",
  1877. }
  1878. )
  1879. assert response.status_code == 200, response.content
  1880. data = response.data["data"]
  1881. assert len(data) == 1
  1882. assert data[0]["p90()"] == 1
  1883. meta = response.data["meta"]
  1884. assert meta["isMetricsData"]
  1885. assert data[0]["transaction"] == "foo_transaction"
  1886. def test_transaction_status_wildcard(self):
  1887. self.store_transaction_metric(
  1888. 1,
  1889. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1890. timestamp=self.min_ago,
  1891. )
  1892. response = self.do_request(
  1893. {
  1894. "field": [
  1895. "transaction",
  1896. "p90()",
  1897. ],
  1898. "query": "transaction.status:f*bar",
  1899. "dataset": "metrics",
  1900. }
  1901. )
  1902. assert response.status_code == 200, response.content
  1903. data = response.data["data"]
  1904. assert len(data) == 1
  1905. assert data[0]["p90()"] == 1
  1906. meta = response.data["meta"]
  1907. assert meta["isMetricsData"]
  1908. def test_http_error_rate(self):
  1909. self.store_transaction_metric(
  1910. 1,
  1911. tags={
  1912. "transaction": "foo_transaction",
  1913. "transaction.status": "foobar",
  1914. "http.status_code": "500",
  1915. },
  1916. timestamp=self.min_ago,
  1917. )
  1918. self.store_transaction_metric(
  1919. 1,
  1920. tags={"transaction": "bar_transaction", "http.status_code": "400"},
  1921. timestamp=self.min_ago,
  1922. )
  1923. response = self.do_request(
  1924. {
  1925. "field": [
  1926. "http_error_rate()",
  1927. ],
  1928. "dataset": "metrics",
  1929. }
  1930. )
  1931. assert response.status_code == 200, response.content
  1932. data = response.data["data"]
  1933. assert len(data) == 1
  1934. assert data[0]["http_error_rate()"] == 0.5
  1935. meta = response.data["meta"]
  1936. assert meta["isMetricsData"]
  1937. def test_time_spent(self):
  1938. self.store_transaction_metric(
  1939. 1,
  1940. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1941. timestamp=self.min_ago,
  1942. )
  1943. self.store_transaction_metric(
  1944. 1,
  1945. tags={"transaction": "bar_transaction"},
  1946. timestamp=self.min_ago,
  1947. )
  1948. response = self.do_request(
  1949. {
  1950. "field": [
  1951. "transaction",
  1952. "time_spent_percentage()",
  1953. ],
  1954. "dataset": "metrics",
  1955. }
  1956. )
  1957. assert response.status_code == 200, response.content
  1958. data = response.data["data"]
  1959. assert len(data) == 2
  1960. assert data[0]["time_spent_percentage()"] == 0.5
  1961. meta = response.data["meta"]
  1962. assert meta["isMetricsData"]
  1963. def test_has_filter(self):
  1964. self.store_transaction_metric(
  1965. 1,
  1966. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1967. timestamp=self.min_ago,
  1968. )
  1969. response = self.do_request(
  1970. {
  1971. "field": [
  1972. "transaction",
  1973. "p50()",
  1974. ],
  1975. # For the metrics dataset, has on metrics should be no-ops
  1976. "query": "has:measurements.frames_frozen_rate",
  1977. "dataset": "metrics",
  1978. }
  1979. )
  1980. assert response.status_code == 200, response.content
  1981. data = response.data["data"]
  1982. assert len(data) == 1
  1983. assert data[0]["p50()"] == 1
  1984. meta = response.data["meta"]
  1985. assert meta["isMetricsData"]
  1986. response = self.do_request(
  1987. {
  1988. "field": [
  1989. "transaction",
  1990. "p50()",
  1991. ],
  1992. "query": "has:transaction.status",
  1993. "dataset": "metrics",
  1994. }
  1995. )
  1996. assert response.status_code == 200, response.content
  1997. data = response.data["data"]
  1998. assert len(data) == 1
  1999. assert data[0]["p50()"] == 1
  2000. meta = response.data["meta"]
  2001. assert meta["isMetricsData"]
  2002. def test_not_has_filter(self):
  2003. self.store_transaction_metric(
  2004. 1,
  2005. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  2006. timestamp=self.min_ago,
  2007. )
  2008. response = self.do_request(
  2009. {
  2010. "field": [
  2011. "transaction",
  2012. "p50()",
  2013. ],
  2014. "query": "!has:transaction.status",
  2015. "dataset": "metrics",
  2016. }
  2017. )
  2018. assert response.status_code == 200, response.content
  2019. data = response.data["data"]
  2020. assert len(data) == 0
  2021. meta = response.data["meta"]
  2022. assert meta["isMetricsData"]
  2023. response = self.do_request(
  2024. {
  2025. "field": [
  2026. "transaction",
  2027. "p50()",
  2028. ],
  2029. # Doing !has on the metrics dataset doesn't really make sense
  2030. "query": "!has:measurements.frames_frozen_rate",
  2031. "dataset": "metrics",
  2032. }
  2033. )
  2034. assert response.status_code == 400, response.content
  2035. def test_p50_with_count(self):
  2036. """Implicitly test the fact that percentiles are their own 'dataset'"""
  2037. self.store_transaction_metric(
  2038. 1,
  2039. tags={"transaction": "foo_transaction"},
  2040. timestamp=self.min_ago,
  2041. )
  2042. response = self.do_request(
  2043. {
  2044. "field": ["title", "p50()", "count()"],
  2045. "query": "event.type:transaction",
  2046. "dataset": "metrics",
  2047. "project": self.project.id,
  2048. "per_page": 50,
  2049. }
  2050. )
  2051. assert response.status_code == 200, response.content
  2052. assert len(response.data["data"]) == 1
  2053. data = response.data["data"]
  2054. meta = response.data["meta"]
  2055. field_meta = meta["fields"]
  2056. assert data[0]["title"] == "foo_transaction"
  2057. assert data[0]["p50()"] == 1
  2058. assert data[0]["count()"] == 1
  2059. assert meta["isMetricsData"]
  2060. assert field_meta["title"] == "string"
  2061. assert field_meta["p50()"] == "duration"
  2062. assert field_meta["count()"] == "integer"
  2063. def test_p75_with_count_and_more_groupby(self):
  2064. """Implicitly test the fact that percentiles are their own 'dataset'"""
  2065. self.store_transaction_metric(
  2066. 1,
  2067. tags={"transaction": "foo_transaction"},
  2068. timestamp=self.min_ago,
  2069. )
  2070. self.store_transaction_metric(
  2071. 5,
  2072. tags={"transaction": "bar_transaction"},
  2073. timestamp=self.min_ago,
  2074. )
  2075. self.store_transaction_metric(
  2076. 5,
  2077. tags={"transaction": "bar_transaction"},
  2078. timestamp=self.min_ago,
  2079. )
  2080. response = self.do_request(
  2081. {
  2082. "field": [
  2083. "title",
  2084. "project",
  2085. "p75()",
  2086. "count()",
  2087. ],
  2088. "query": "event.type:transaction",
  2089. "orderby": "count()",
  2090. "dataset": "metrics",
  2091. "project": self.project.id,
  2092. "per_page": 50,
  2093. }
  2094. )
  2095. assert response.status_code == 200, response.content
  2096. assert len(response.data["data"]) == 2
  2097. data = response.data["data"]
  2098. meta = response.data["meta"]
  2099. field_meta = meta["fields"]
  2100. assert data[0]["title"] == "foo_transaction"
  2101. assert data[0]["p75()"] == 1
  2102. assert data[0]["count()"] == 1
  2103. assert data[1]["title"] == "bar_transaction"
  2104. assert data[1]["p75()"] == 5
  2105. assert data[1]["count()"] == 2
  2106. assert meta["isMetricsData"]
  2107. assert field_meta["title"] == "string"
  2108. assert field_meta["p75()"] == "duration"
  2109. assert field_meta["count()"] == "integer"
  2110. def test_title_and_transaction_alias(self):
  2111. # Title and transaction are aliases to the same column
  2112. self.store_transaction_metric(
  2113. 1,
  2114. tags={"transaction": "foo_transaction"},
  2115. timestamp=self.min_ago,
  2116. )
  2117. response = self.do_request(
  2118. {
  2119. "field": [
  2120. "title",
  2121. "transaction",
  2122. "p75()",
  2123. ],
  2124. "query": "event.type:transaction",
  2125. "orderby": "p75()",
  2126. "dataset": "metrics",
  2127. "project": self.project.id,
  2128. "per_page": 50,
  2129. }
  2130. )
  2131. assert response.status_code == 200, response.content
  2132. assert len(response.data["data"]) == 1
  2133. data = response.data["data"]
  2134. meta = response.data["meta"]
  2135. field_meta = meta["fields"]
  2136. assert data[0]["title"] == "foo_transaction"
  2137. assert data[0]["transaction"] == "foo_transaction"
  2138. assert data[0]["p75()"] == 1
  2139. assert meta["isMetricsData"]
  2140. assert field_meta["title"] == "string"
  2141. assert field_meta["transaction"] == "string"
  2142. assert field_meta["p75()"] == "duration"
  2143. def test_maintain_sort_order_across_datasets(self):
  2144. self.store_transaction_metric(
  2145. 1,
  2146. tags={"transaction": "foo_transaction"},
  2147. timestamp=self.min_ago,
  2148. )
  2149. self.store_transaction_metric(
  2150. 1,
  2151. metric="user",
  2152. tags={"transaction": "bar_transaction"},
  2153. timestamp=self.min_ago,
  2154. )
  2155. self.store_transaction_metric(
  2156. 5,
  2157. tags={"transaction": "bar_transaction"},
  2158. timestamp=self.min_ago,
  2159. )
  2160. self.store_transaction_metric(
  2161. 5,
  2162. tags={"transaction": "bar_transaction"},
  2163. timestamp=self.min_ago,
  2164. )
  2165. response = self.do_request(
  2166. {
  2167. "field": [
  2168. "title",
  2169. "project",
  2170. "count()",
  2171. "count_unique(user)",
  2172. ],
  2173. "query": "event.type:transaction",
  2174. "orderby": "count()",
  2175. "dataset": "metrics",
  2176. "project": self.project.id,
  2177. "per_page": 50,
  2178. }
  2179. )
  2180. assert response.status_code == 200, response.content
  2181. data = response.data["data"]
  2182. meta = response.data["meta"]
  2183. field_meta = meta["fields"]
  2184. assert len(data) == 2
  2185. assert data[0]["title"] == "foo_transaction"
  2186. assert data[0]["count()"] == 1
  2187. assert data[0]["count_unique(user)"] == 0
  2188. assert data[1]["title"] == "bar_transaction"
  2189. assert data[1]["count()"] == 2
  2190. assert data[1]["count_unique(user)"] == 1
  2191. assert meta["isMetricsData"]
  2192. assert field_meta["title"] == "string"
  2193. assert field_meta["count()"] == "integer"
  2194. assert field_meta["count_unique(user)"] == "integer"
  2195. class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithMetricLayer(
  2196. OrganizationEventsMetricsEnhancedPerformanceEndpointTest
  2197. ):
  2198. def setUp(self):
  2199. super().setUp()
  2200. self.features["organizations:use-metrics-layer"] = True
  2201. @pytest.mark.xfail(reason="Having not supported")
  2202. def test_custom_measurement_duration_filtering(self):
  2203. super().test_custom_measurement_size_filtering()
  2204. @pytest.mark.xfail(reason="Having not supported")
  2205. def test_having_condition_not_selected(self):
  2206. super().test_having_condition_not_selected()
  2207. @pytest.mark.xfail(reason="Having not supported")
  2208. def test_custom_measurement_size_filtering(self):
  2209. super().test_custom_measurement_size_filtering()
  2210. @pytest.mark.xfail(reason="Having not supported")
  2211. def test_having_condition(self):
  2212. super().test_having_condition()
  2213. @pytest.mark.xfail(reason="Not supported")
  2214. def test_time_spent(self):
  2215. super().test_time_spent()
  2216. @pytest.mark.xfail(reason="Not supported")
  2217. def test_http_error_rate(self):
  2218. super().test_http_error_rate()
  2219. @pytest.mark.xfail(reason="Multiple aliases to same column not supported")
  2220. def test_title_and_transaction_alias(self):
  2221. super().test_title_and_transaction_alias()
  2222. @pytest.mark.xfail(reason="Sort order is flaking when querying multiple datasets")
  2223. def test_maintain_sort_order_across_datasets(self):
  2224. """You may need to run this test a few times to get it to fail"""
  2225. super().test_maintain_sort_order_across_datasets()