test_organization_events_mep.py 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218
  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 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 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()"] == "number"
  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()"] == "number"
  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()"] == "number"
  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. def test_user_misery_and_team_key_sort(self):
  453. self.store_transaction_metric(
  454. 1,
  455. tags={
  456. "transaction": "foo_transaction",
  457. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  458. },
  459. timestamp=self.min_ago,
  460. )
  461. self.store_transaction_metric(
  462. 1,
  463. "measurements.fcp",
  464. tags={"transaction": "foo_transaction"},
  465. timestamp=self.min_ago,
  466. )
  467. self.store_transaction_metric(
  468. 2,
  469. "measurements.lcp",
  470. tags={"transaction": "foo_transaction"},
  471. timestamp=self.min_ago,
  472. )
  473. self.store_transaction_metric(
  474. 3,
  475. "measurements.fid",
  476. tags={"transaction": "foo_transaction"},
  477. timestamp=self.min_ago,
  478. )
  479. self.store_transaction_metric(
  480. 4,
  481. "measurements.cls",
  482. tags={"transaction": "foo_transaction"},
  483. timestamp=self.min_ago,
  484. )
  485. self.store_transaction_metric(
  486. 1,
  487. "user",
  488. tags={
  489. "transaction": "foo_transaction",
  490. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_FRUSTRATED_TAG_VALUE,
  491. },
  492. timestamp=self.min_ago,
  493. )
  494. response = self.do_request(
  495. {
  496. "field": [
  497. "team_key_transaction",
  498. "transaction",
  499. "project",
  500. "tpm()",
  501. "p75(measurements.fcp)",
  502. "p75(measurements.lcp)",
  503. "p75(measurements.fid)",
  504. "p75(measurements.cls)",
  505. "count_unique(user)",
  506. "apdex()",
  507. "count_miserable(user)",
  508. "user_misery()",
  509. "failure_rate()",
  510. "failure_count()",
  511. ],
  512. "orderby": ["team_key_transaction", "user_misery()"],
  513. "query": "event.type:transaction",
  514. "dataset": "metrics",
  515. "per_page": 50,
  516. }
  517. )
  518. assert response.status_code == 200, response.content
  519. assert len(response.data["data"]) == 1
  520. data = response.data["data"][0]
  521. meta = response.data["meta"]
  522. field_meta = meta["fields"]
  523. assert data["transaction"] == "foo_transaction"
  524. assert data["project"] == self.project.slug
  525. assert data["p75(measurements.fcp)"] == 1.0
  526. assert data["p75(measurements.lcp)"] == 2.0
  527. assert data["p75(measurements.fid)"] == 3.0
  528. assert data["p75(measurements.cls)"] == 4.0
  529. assert data["apdex()"] == 1.0
  530. assert data["count_miserable(user)"] == 1.0
  531. assert data["user_misery()"] == 0.058
  532. assert data["failure_rate()"] == 1
  533. assert data["failure_count()"] == 1
  534. assert meta["isMetricsData"]
  535. assert field_meta["transaction"] == "string"
  536. assert field_meta["project"] == "string"
  537. assert field_meta["p75(measurements.fcp)"] == "duration"
  538. assert field_meta["p75(measurements.lcp)"] == "duration"
  539. assert field_meta["p75(measurements.fid)"] == "duration"
  540. assert field_meta["p75(measurements.cls)"] == "number"
  541. assert field_meta["apdex()"] == "number"
  542. assert field_meta["count_miserable(user)"] == "integer"
  543. assert field_meta["user_misery()"] == "number"
  544. assert field_meta["failure_rate()"] == "percentage"
  545. assert field_meta["failure_count()"] == "integer"
  546. def test_no_team_key_transactions(self):
  547. self.store_transaction_metric(
  548. 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago
  549. )
  550. self.store_transaction_metric(
  551. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  552. )
  553. query = {
  554. "team": "myteams",
  555. "project": [self.project.id],
  556. # TODO sort by transaction here once that's possible for order to match the same test without metrics
  557. "orderby": "p95()",
  558. "field": [
  559. "team_key_transaction",
  560. "transaction",
  561. "transaction.status",
  562. "project",
  563. "epm()",
  564. "failure_rate()",
  565. "p95()",
  566. ],
  567. "per_page": 50,
  568. "dataset": "metricsEnhanced",
  569. }
  570. response = self.do_request(query)
  571. assert response.status_code == 200, response.content
  572. assert len(response.data["data"]) == 2
  573. data = response.data["data"]
  574. meta = response.data["meta"]
  575. field_meta = meta["fields"]
  576. assert data[0]["team_key_transaction"] == 0
  577. assert data[0]["transaction"] == "foo_transaction"
  578. assert data[1]["team_key_transaction"] == 0
  579. assert data[1]["transaction"] == "bar_transaction"
  580. assert meta["isMetricsData"]
  581. assert field_meta["team_key_transaction"] == "boolean"
  582. assert field_meta["transaction"] == "string"
  583. def test_team_key_transactions_my_teams(self):
  584. team1 = self.create_team(organization=self.organization, name="Team A")
  585. self.create_team_membership(team1, user=self.user)
  586. self.project.add_team(team1)
  587. team2 = self.create_team(organization=self.organization, name="Team B")
  588. self.project.add_team(team2)
  589. key_transactions = [
  590. (team1, "foo_transaction"),
  591. (team2, "baz_transaction"),
  592. ]
  593. # Not a key transaction
  594. self.store_transaction_metric(
  595. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  596. )
  597. for team, transaction in key_transactions:
  598. self.store_transaction_metric(
  599. 1, tags={"transaction": transaction}, timestamp=self.min_ago
  600. )
  601. TeamKeyTransaction.objects.create(
  602. organization=self.organization,
  603. transaction=transaction,
  604. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  605. )
  606. query = {
  607. "team": "myteams",
  608. "project": [self.project.id],
  609. "field": [
  610. "team_key_transaction",
  611. "transaction",
  612. "transaction.status",
  613. "project",
  614. "epm()",
  615. "failure_rate()",
  616. "p95()",
  617. ],
  618. "per_page": 50,
  619. "dataset": "metricsEnhanced",
  620. }
  621. query["orderby"] = ["team_key_transaction", "p95()"]
  622. response = self.do_request(query)
  623. assert response.status_code == 200, response.content
  624. assert len(response.data["data"]) == 3
  625. data = response.data["data"]
  626. meta = response.data["meta"]
  627. field_meta = meta["fields"]
  628. assert data[0]["team_key_transaction"] == 0
  629. assert data[0]["transaction"] == "baz_transaction"
  630. assert data[1]["team_key_transaction"] == 0
  631. assert data[1]["transaction"] == "bar_transaction"
  632. assert data[2]["team_key_transaction"] == 1
  633. assert data[2]["transaction"] == "foo_transaction"
  634. assert meta["isMetricsData"]
  635. assert field_meta["team_key_transaction"] == "boolean"
  636. assert field_meta["transaction"] == "string"
  637. # not specifying any teams should use my teams
  638. query = {
  639. "project": [self.project.id],
  640. "field": [
  641. "team_key_transaction",
  642. "transaction",
  643. "transaction.status",
  644. "project",
  645. "epm()",
  646. "failure_rate()",
  647. "p95()",
  648. ],
  649. "per_page": 50,
  650. "dataset": "metricsEnhanced",
  651. }
  652. query["orderby"] = ["team_key_transaction", "p95()"]
  653. response = self.do_request(query)
  654. assert response.status_code == 200, response.content
  655. assert len(response.data["data"]) == 3
  656. data = response.data["data"]
  657. meta = response.data["meta"]
  658. field_meta = meta["fields"]
  659. assert data[0]["team_key_transaction"] == 0
  660. assert data[0]["transaction"] == "baz_transaction"
  661. assert data[1]["team_key_transaction"] == 0
  662. assert data[1]["transaction"] == "bar_transaction"
  663. assert data[2]["team_key_transaction"] == 1
  664. assert data[2]["transaction"] == "foo_transaction"
  665. assert meta["isMetricsData"]
  666. assert field_meta["team_key_transaction"] == "boolean"
  667. assert field_meta["transaction"] == "string"
  668. def test_team_key_transactions_orderby(self):
  669. team1 = self.create_team(organization=self.organization, name="Team A")
  670. team2 = self.create_team(organization=self.organization, name="Team B")
  671. key_transactions = [
  672. (team1, "foo_transaction", 1),
  673. (team2, "baz_transaction", 100),
  674. ]
  675. # Not a key transaction
  676. self.store_transaction_metric(
  677. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  678. )
  679. for team, transaction, value in key_transactions:
  680. self.store_transaction_metric(
  681. value, tags={"transaction": transaction}, timestamp=self.min_ago
  682. )
  683. self.create_team_membership(team, user=self.user)
  684. self.project.add_team(team)
  685. TeamKeyTransaction.objects.create(
  686. organization=self.organization,
  687. transaction=transaction,
  688. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  689. )
  690. query = {
  691. "team": "myteams",
  692. "project": [self.project.id],
  693. "field": [
  694. "team_key_transaction",
  695. "transaction",
  696. "transaction.status",
  697. "project",
  698. "epm()",
  699. "failure_rate()",
  700. "p95()",
  701. ],
  702. "per_page": 50,
  703. "dataset": "metricsEnhanced",
  704. }
  705. # test ascending order
  706. query["orderby"] = ["team_key_transaction", "p95()"]
  707. response = self.do_request(query)
  708. assert response.status_code == 200, response.content
  709. assert len(response.data["data"]) == 3
  710. data = response.data["data"]
  711. meta = response.data["meta"]
  712. field_meta = meta["fields"]
  713. assert data[0]["team_key_transaction"] == 0
  714. assert data[0]["transaction"] == "bar_transaction"
  715. assert data[1]["team_key_transaction"] == 1
  716. assert data[1]["transaction"] == "foo_transaction"
  717. assert data[2]["team_key_transaction"] == 1
  718. assert data[2]["transaction"] == "baz_transaction"
  719. assert meta["isMetricsData"]
  720. assert field_meta["team_key_transaction"] == "boolean"
  721. assert field_meta["transaction"] == "string"
  722. # test descending order
  723. query["orderby"] = ["-team_key_transaction", "p95()"]
  724. response = self.do_request(query)
  725. assert response.status_code == 200, response.content
  726. assert len(response.data["data"]) == 3
  727. data = response.data["data"]
  728. meta = response.data["meta"]
  729. field_meta = meta["fields"]
  730. assert data[0]["team_key_transaction"] == 1
  731. assert data[0]["transaction"] == "foo_transaction"
  732. assert data[1]["team_key_transaction"] == 1
  733. assert data[1]["transaction"] == "baz_transaction"
  734. assert data[2]["team_key_transaction"] == 0
  735. assert data[2]["transaction"] == "bar_transaction"
  736. assert meta["isMetricsData"]
  737. assert field_meta["team_key_transaction"] == "boolean"
  738. assert field_meta["transaction"] == "string"
  739. def test_team_key_transactions_query(self):
  740. team1 = self.create_team(organization=self.organization, name="Team A")
  741. team2 = self.create_team(organization=self.organization, name="Team B")
  742. key_transactions = [
  743. (team1, "foo_transaction", 1),
  744. (team2, "baz_transaction", 100),
  745. ]
  746. # Not a key transaction
  747. self.store_transaction_metric(
  748. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  749. )
  750. for team, transaction, value in key_transactions:
  751. self.store_transaction_metric(
  752. value, tags={"transaction": transaction}, timestamp=self.min_ago
  753. )
  754. self.create_team_membership(team, user=self.user)
  755. self.project.add_team(team)
  756. TeamKeyTransaction.objects.create(
  757. organization=self.organization,
  758. transaction=transaction,
  759. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  760. )
  761. query = {
  762. "team": "myteams",
  763. "project": [self.project.id],
  764. # use the order by to ensure the result order
  765. "orderby": "p95()",
  766. "field": [
  767. "team_key_transaction",
  768. "transaction",
  769. "transaction.status",
  770. "project",
  771. "epm()",
  772. "failure_rate()",
  773. "p95()",
  774. ],
  775. "per_page": 50,
  776. "dataset": "metricsEnhanced",
  777. }
  778. # key transactions
  779. query["query"] = "has:team_key_transaction"
  780. response = self.do_request(query)
  781. assert response.status_code == 200, response.content
  782. assert len(response.data["data"]) == 2
  783. data = response.data["data"]
  784. meta = response.data["meta"]
  785. field_meta = meta["fields"]
  786. assert data[0]["team_key_transaction"] == 1
  787. assert data[0]["transaction"] == "foo_transaction"
  788. assert data[1]["team_key_transaction"] == 1
  789. assert data[1]["transaction"] == "baz_transaction"
  790. assert meta["isMetricsData"]
  791. assert field_meta["team_key_transaction"] == "boolean"
  792. assert field_meta["transaction"] == "string"
  793. # key transactions
  794. query["query"] = "team_key_transaction:true"
  795. response = self.do_request(query)
  796. assert response.status_code == 200, response.content
  797. assert len(response.data["data"]) == 2
  798. data = response.data["data"]
  799. meta = response.data["meta"]
  800. field_meta = meta["fields"]
  801. assert data[0]["team_key_transaction"] == 1
  802. assert data[0]["transaction"] == "foo_transaction"
  803. assert data[1]["team_key_transaction"] == 1
  804. assert data[1]["transaction"] == "baz_transaction"
  805. assert meta["isMetricsData"]
  806. assert field_meta["team_key_transaction"] == "boolean"
  807. assert field_meta["transaction"] == "string"
  808. # not key transactions
  809. query["query"] = "!has:team_key_transaction"
  810. response = self.do_request(query)
  811. assert response.status_code == 200, response.content
  812. assert len(response.data["data"]) == 1
  813. data = response.data["data"]
  814. meta = response.data["meta"]
  815. field_meta = meta["fields"]
  816. assert data[0]["team_key_transaction"] == 0
  817. assert data[0]["transaction"] == "bar_transaction"
  818. assert meta["isMetricsData"]
  819. assert field_meta["team_key_transaction"] == "boolean"
  820. assert field_meta["transaction"] == "string"
  821. # not key transactions
  822. query["query"] = "team_key_transaction:false"
  823. response = self.do_request(query)
  824. assert response.status_code == 200, response.content
  825. assert len(response.data["data"]) == 1
  826. data = response.data["data"]
  827. meta = response.data["meta"]
  828. field_meta = meta["fields"]
  829. assert data[0]["team_key_transaction"] == 0
  830. assert data[0]["transaction"] == "bar_transaction"
  831. assert meta["isMetricsData"]
  832. assert field_meta["team_key_transaction"] == "boolean"
  833. assert field_meta["transaction"] == "string"
  834. def test_team_key_transaction_not_exists(self):
  835. team1 = self.create_team(organization=self.organization, name="Team A")
  836. team2 = self.create_team(organization=self.organization, name="Team B")
  837. key_transactions = [
  838. (team1, "foo_transaction", 1),
  839. (team2, "baz_transaction", 100),
  840. ]
  841. for team, transaction, value in key_transactions:
  842. self.store_transaction_metric(
  843. value, tags={"transaction": transaction}, timestamp=self.min_ago
  844. )
  845. self.create_team_membership(team, user=self.user)
  846. self.project.add_team(team)
  847. TeamKeyTransaction.objects.create(
  848. organization=self.organization,
  849. transaction=transaction,
  850. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  851. )
  852. # Don't create a metric for this one
  853. TeamKeyTransaction.objects.create(
  854. organization=self.organization,
  855. transaction="not_in_metrics",
  856. project_team=ProjectTeam.objects.get(project=self.project, team=team1),
  857. )
  858. query = {
  859. "team": "myteams",
  860. "project": [self.project.id],
  861. # use the order by to ensure the result order
  862. "orderby": "p95()",
  863. "field": [
  864. "team_key_transaction",
  865. "transaction",
  866. "transaction.status",
  867. "project",
  868. "epm()",
  869. "failure_rate()",
  870. "p95()",
  871. ],
  872. "per_page": 50,
  873. "dataset": "metricsEnhanced",
  874. }
  875. # key transactions
  876. query["query"] = "has:team_key_transaction"
  877. response = self.do_request(query)
  878. assert response.status_code == 200, response.content
  879. assert len(response.data["data"]) == 2
  880. data = response.data["data"]
  881. meta = response.data["meta"]
  882. field_meta = meta["fields"]
  883. assert data[0]["team_key_transaction"] == 1
  884. assert data[0]["transaction"] == "foo_transaction"
  885. assert data[1]["team_key_transaction"] == 1
  886. assert data[1]["transaction"] == "baz_transaction"
  887. assert meta["isMetricsData"]
  888. assert field_meta["team_key_transaction"] == "boolean"
  889. assert field_meta["transaction"] == "string"
  890. # key transactions
  891. query["query"] = "team_key_transaction:true"
  892. response = self.do_request(query)
  893. assert response.status_code == 200, response.content
  894. assert len(response.data["data"]) == 2
  895. data = response.data["data"]
  896. meta = response.data["meta"]
  897. field_meta = meta["fields"]
  898. assert data[0]["team_key_transaction"] == 1
  899. assert data[0]["transaction"] == "foo_transaction"
  900. assert data[1]["team_key_transaction"] == 1
  901. assert data[1]["transaction"] == "baz_transaction"
  902. assert meta["isMetricsData"]
  903. assert field_meta["team_key_transaction"] == "boolean"
  904. assert field_meta["transaction"] == "string"
  905. # not key transactions
  906. query["query"] = "!has:team_key_transaction"
  907. response = self.do_request(query)
  908. assert response.status_code == 200, response.content
  909. assert len(response.data["data"]) == 0
  910. data = response.data["data"]
  911. meta = response.data["meta"]
  912. field_meta = meta["fields"]
  913. assert meta["isMetricsData"]
  914. assert field_meta["team_key_transaction"] == "boolean"
  915. assert field_meta["transaction"] == "string"
  916. # not key transactions
  917. query["query"] = "team_key_transaction:false"
  918. response = self.do_request(query)
  919. assert response.status_code == 200, response.content
  920. assert len(response.data["data"]) == 0
  921. data = response.data["data"]
  922. meta = response.data["meta"]
  923. field_meta = meta["fields"]
  924. assert meta["isMetricsData"]
  925. assert field_meta["team_key_transaction"] == "boolean"
  926. assert field_meta["transaction"] == "string"
  927. def test_too_many_team_key_transactions(self):
  928. MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS = 1
  929. with mock.patch(
  930. "sentry.search.events.fields.MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS",
  931. MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS,
  932. ):
  933. team = self.create_team(organization=self.organization, name="Team A")
  934. self.create_team_membership(team, user=self.user)
  935. self.project.add_team(team)
  936. project_team = ProjectTeam.objects.get(project=self.project, team=team)
  937. transactions = ["foo_transaction", "bar_transaction", "baz_transaction"]
  938. for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1):
  939. self.store_transaction_metric(
  940. 100, tags={"transaction": transactions[i]}, timestamp=self.min_ago
  941. )
  942. TeamKeyTransaction.objects.bulk_create(
  943. [
  944. TeamKeyTransaction(
  945. organization=self.organization,
  946. project_team=project_team,
  947. transaction=transactions[i],
  948. )
  949. for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1)
  950. ]
  951. )
  952. query = {
  953. "team": "myteams",
  954. "project": [self.project.id],
  955. "orderby": "p95()",
  956. "field": [
  957. "team_key_transaction",
  958. "transaction",
  959. "transaction.status",
  960. "project",
  961. "epm()",
  962. "failure_rate()",
  963. "p95()",
  964. ],
  965. "dataset": "metricsEnhanced",
  966. "per_page": 50,
  967. }
  968. response = self.do_request(query)
  969. assert response.status_code == 200, response.content
  970. assert len(response.data["data"]) == 2
  971. data = response.data["data"]
  972. meta = response.data["meta"]
  973. assert (
  974. sum(row["team_key_transaction"] for row in data)
  975. == MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS
  976. )
  977. assert meta["isMetricsData"]
  978. def test_measurement_rating(self):
  979. self.store_transaction_metric(
  980. 50,
  981. metric="measurements.lcp",
  982. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  983. timestamp=self.min_ago,
  984. )
  985. self.store_transaction_metric(
  986. 15,
  987. metric="measurements.fp",
  988. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  989. timestamp=self.min_ago,
  990. )
  991. self.store_transaction_metric(
  992. 1500,
  993. metric="measurements.fcp",
  994. tags={"measurement_rating": "meh", "transaction": "foo_transaction"},
  995. timestamp=self.min_ago,
  996. )
  997. self.store_transaction_metric(
  998. 125,
  999. metric="measurements.fid",
  1000. tags={"measurement_rating": "meh", "transaction": "foo_transaction"},
  1001. timestamp=self.min_ago,
  1002. )
  1003. self.store_transaction_metric(
  1004. 0.15,
  1005. metric="measurements.cls",
  1006. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  1007. timestamp=self.min_ago,
  1008. )
  1009. response = self.do_request(
  1010. {
  1011. "field": [
  1012. "transaction",
  1013. "count_web_vitals(measurements.lcp, good)",
  1014. "count_web_vitals(measurements.fp, good)",
  1015. "count_web_vitals(measurements.fcp, meh)",
  1016. "count_web_vitals(measurements.fid, meh)",
  1017. "count_web_vitals(measurements.cls, good)",
  1018. ],
  1019. "query": "event.type:transaction",
  1020. "dataset": "metricsEnhanced",
  1021. "per_page": 50,
  1022. }
  1023. )
  1024. assert response.status_code == 200, response.content
  1025. assert len(response.data["data"]) == 1
  1026. data = response.data["data"]
  1027. meta = response.data["meta"]
  1028. field_meta = meta["fields"]
  1029. assert data[0]["count_web_vitals(measurements.lcp, good)"] == 1
  1030. assert data[0]["count_web_vitals(measurements.fp, good)"] == 1
  1031. assert data[0]["count_web_vitals(measurements.fcp, meh)"] == 1
  1032. assert data[0]["count_web_vitals(measurements.fid, meh)"] == 1
  1033. assert data[0]["count_web_vitals(measurements.cls, good)"] == 1
  1034. assert meta["isMetricsData"]
  1035. assert field_meta["count_web_vitals(measurements.lcp, good)"] == "integer"
  1036. assert field_meta["count_web_vitals(measurements.fp, good)"] == "integer"
  1037. assert field_meta["count_web_vitals(measurements.fcp, meh)"] == "integer"
  1038. assert field_meta["count_web_vitals(measurements.fid, meh)"] == "integer"
  1039. assert field_meta["count_web_vitals(measurements.cls, good)"] == "integer"
  1040. def test_measurement_rating_that_does_not_exist(self):
  1041. self.store_transaction_metric(
  1042. 1,
  1043. metric="measurements.lcp",
  1044. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  1045. timestamp=self.min_ago,
  1046. )
  1047. response = self.do_request(
  1048. {
  1049. "field": ["transaction", "count_web_vitals(measurements.lcp, poor)"],
  1050. "query": "event.type:transaction",
  1051. "dataset": "metricsEnhanced",
  1052. "per_page": 50,
  1053. }
  1054. )
  1055. assert response.status_code == 200, response.content
  1056. assert len(response.data["data"]) == 1
  1057. data = response.data["data"]
  1058. meta = response.data["meta"]
  1059. assert data[0]["count_web_vitals(measurements.lcp, poor)"] == 0
  1060. assert meta["isMetricsData"]
  1061. assert meta["fields"]["count_web_vitals(measurements.lcp, poor)"] == "integer"
  1062. def test_count_web_vitals_invalid_vital(self):
  1063. query = {
  1064. "field": [
  1065. "count_web_vitals(measurements.foo, poor)",
  1066. ],
  1067. "project": [self.project.id],
  1068. "dataset": "metricsEnhanced",
  1069. }
  1070. response = self.do_request(query)
  1071. assert response.status_code == 400, response.content
  1072. query = {
  1073. "field": [
  1074. "count_web_vitals(tags[lcp], poor)",
  1075. ],
  1076. "project": [self.project.id],
  1077. "dataset": "metricsEnhanced",
  1078. }
  1079. response = self.do_request(query)
  1080. assert response.status_code == 400, response.content
  1081. query = {
  1082. "field": [
  1083. "count_web_vitals(transaction.duration, poor)",
  1084. ],
  1085. "project": [self.project.id],
  1086. "dataset": "metricsEnhanced",
  1087. }
  1088. response = self.do_request(query)
  1089. assert response.status_code == 400, response.content
  1090. query = {
  1091. "field": [
  1092. "count_web_vitals(measurements.lcp, bad)",
  1093. ],
  1094. "project": [self.project.id],
  1095. "dataset": "metricsEnhanced",
  1096. }
  1097. response = self.do_request(query)
  1098. assert response.status_code == 400, response.content
  1099. def test_count_unique_user_returns_zero(self):
  1100. self.store_transaction_metric(
  1101. 50,
  1102. metric="user",
  1103. tags={"transaction": "foo_transaction"},
  1104. timestamp=self.min_ago,
  1105. )
  1106. self.store_transaction_metric(
  1107. 50,
  1108. tags={"transaction": "foo_transaction"},
  1109. timestamp=self.min_ago,
  1110. )
  1111. self.store_transaction_metric(
  1112. 100,
  1113. tags={"transaction": "bar_transaction"},
  1114. timestamp=self.min_ago,
  1115. )
  1116. query = {
  1117. "project": [self.project.id],
  1118. "orderby": "p50()",
  1119. "field": [
  1120. "transaction",
  1121. "count_unique(user)",
  1122. "p50()",
  1123. ],
  1124. "dataset": "metricsEnhanced",
  1125. "per_page": 50,
  1126. }
  1127. response = self.do_request(query)
  1128. assert response.status_code == 200, response.content
  1129. assert len(response.data["data"]) == 2
  1130. data = response.data["data"]
  1131. meta = response.data["meta"]
  1132. assert data[0]["transaction"] == "foo_transaction"
  1133. assert data[0]["count_unique(user)"] == 1
  1134. assert data[1]["transaction"] == "bar_transaction"
  1135. assert data[1]["count_unique(user)"] == 0
  1136. assert meta["isMetricsData"]
  1137. def test_sum_transaction_duration(self):
  1138. self.store_transaction_metric(
  1139. 50,
  1140. tags={"transaction": "foo_transaction"},
  1141. timestamp=self.min_ago,
  1142. )
  1143. self.store_transaction_metric(
  1144. 100,
  1145. tags={"transaction": "foo_transaction"},
  1146. timestamp=self.min_ago,
  1147. )
  1148. self.store_transaction_metric(
  1149. 150,
  1150. tags={"transaction": "foo_transaction"},
  1151. timestamp=self.min_ago,
  1152. )
  1153. query = {
  1154. "project": [self.project.id],
  1155. "orderby": "sum(transaction.duration)",
  1156. "field": [
  1157. "transaction",
  1158. "sum(transaction.duration)",
  1159. ],
  1160. "dataset": "metricsEnhanced",
  1161. "per_page": 50,
  1162. }
  1163. response = self.do_request(query)
  1164. assert response.status_code == 200, response.content
  1165. assert len(response.data["data"]) == 1
  1166. data = response.data["data"]
  1167. meta = response.data["meta"]
  1168. assert data[0]["transaction"] == "foo_transaction"
  1169. assert data[0]["sum(transaction.duration)"] == 300
  1170. assert meta["isMetricsData"]
  1171. def test_custom_measurements_simple(self):
  1172. self.store_transaction_metric(
  1173. 1,
  1174. metric="measurements.something_custom",
  1175. internal_metric="d:transactions/measurements.something_custom@millisecond",
  1176. entity="metrics_distributions",
  1177. tags={"transaction": "foo_transaction"},
  1178. timestamp=self.min_ago,
  1179. )
  1180. query = {
  1181. "project": [self.project.id],
  1182. "orderby": "p50(measurements.something_custom)",
  1183. "field": [
  1184. "transaction",
  1185. "p50(measurements.something_custom)",
  1186. ],
  1187. "statsPeriod": "24h",
  1188. "dataset": "metricsEnhanced",
  1189. "per_page": 50,
  1190. }
  1191. self.wait_for_metric_count(
  1192. self.project,
  1193. 1,
  1194. metric="measurements.something_custom",
  1195. mri="d:transactions/measurements.something_custom@millisecond",
  1196. )
  1197. response = self.do_request(query)
  1198. assert response.status_code == 200, response.content
  1199. assert len(response.data["data"]) == 1
  1200. data = response.data["data"]
  1201. meta = response.data["meta"]
  1202. assert data[0]["transaction"] == "foo_transaction"
  1203. assert data[0]["p50(measurements.something_custom)"] == 1
  1204. assert meta["isMetricsData"]
  1205. assert meta["fields"]["p50(measurements.something_custom)"] == "duration"
  1206. assert meta["units"]["p50(measurements.something_custom)"] == "millisecond"
  1207. def test_custom_measurement_size_meta_type(self):
  1208. self.store_transaction_metric(
  1209. 100,
  1210. metric="measurements.custom_type",
  1211. internal_metric="d:transactions/measurements.custom_type@somethingcustom",
  1212. entity="metrics_distributions",
  1213. tags={"transaction": "foo_transaction"},
  1214. timestamp=self.min_ago,
  1215. )
  1216. self.store_transaction_metric(
  1217. 100,
  1218. metric="measurements.percent",
  1219. internal_metric="d:transactions/measurements.percent@ratio",
  1220. entity="metrics_distributions",
  1221. tags={"transaction": "foo_transaction"},
  1222. timestamp=self.min_ago,
  1223. )
  1224. self.store_transaction_metric(
  1225. 100,
  1226. metric="measurements.longtaskcount",
  1227. internal_metric="d:transactions/measurements.longtaskcount@none",
  1228. entity="metrics_distributions",
  1229. tags={"transaction": "foo_transaction"},
  1230. timestamp=self.min_ago,
  1231. )
  1232. query = {
  1233. "project": [self.project.id],
  1234. "orderby": "p50(measurements.longtaskcount)",
  1235. "field": [
  1236. "transaction",
  1237. "p50(measurements.longtaskcount)",
  1238. "p50(measurements.percent)",
  1239. "p50(measurements.custom_type)",
  1240. ],
  1241. "statsPeriod": "24h",
  1242. "dataset": "metricsEnhanced",
  1243. "per_page": 50,
  1244. }
  1245. response = self.do_request(query)
  1246. assert response.status_code == 200, response.content
  1247. assert len(response.data["data"]) == 1
  1248. data = response.data["data"]
  1249. meta = response.data["meta"]
  1250. assert data[0]["transaction"] == "foo_transaction"
  1251. assert data[0]["p50(measurements.longtaskcount)"] == 100
  1252. assert data[0]["p50(measurements.percent)"] == 100
  1253. assert data[0]["p50(measurements.custom_type)"] == 100
  1254. assert meta["isMetricsData"]
  1255. assert meta["fields"]["p50(measurements.longtaskcount)"] == "integer"
  1256. assert meta["units"]["p50(measurements.longtaskcount)"] is None
  1257. assert meta["fields"]["p50(measurements.percent)"] == "percentage"
  1258. assert meta["units"]["p50(measurements.percent)"] is None
  1259. assert meta["fields"]["p50(measurements.custom_type)"] == "number"
  1260. assert meta["units"]["p50(measurements.custom_type)"] is None
  1261. def test_custom_measurement_none_type(self):
  1262. self.store_transaction_metric(
  1263. 1,
  1264. metric="measurements.cls",
  1265. entity="metrics_distributions",
  1266. tags={"transaction": "foo_transaction"},
  1267. timestamp=self.min_ago,
  1268. )
  1269. query = {
  1270. "project": [self.project.id],
  1271. "orderby": "p75(measurements.cls)",
  1272. "field": [
  1273. "transaction",
  1274. "p75(measurements.cls)",
  1275. "p99(measurements.cls)",
  1276. "max(measurements.cls)",
  1277. ],
  1278. "statsPeriod": "24h",
  1279. "dataset": "metricsEnhanced",
  1280. "per_page": 50,
  1281. }
  1282. self.wait_for_metric_count(
  1283. self.project,
  1284. 1,
  1285. metric=TransactionMetricKey.MEASUREMENTS_CLS.value,
  1286. mri=TransactionMRI.MEASUREMENTS_CLS.value,
  1287. )
  1288. response = self.do_request(query)
  1289. assert response.status_code == 200, response.content
  1290. assert len(response.data["data"]) == 1
  1291. data = response.data["data"]
  1292. meta = response.data["meta"]
  1293. assert data[0]["transaction"] == "foo_transaction"
  1294. assert data[0]["p75(measurements.cls)"] == 1
  1295. assert data[0]["p99(measurements.cls)"] == 1
  1296. assert data[0]["max(measurements.cls)"] == 1
  1297. assert meta["isMetricsData"]
  1298. assert meta["fields"]["p75(measurements.cls)"] == "number"
  1299. assert meta["units"]["p75(measurements.cls)"] is None
  1300. assert meta["fields"]["p99(measurements.cls)"] == "number"
  1301. assert meta["units"]["p99(measurements.cls)"] is None
  1302. assert meta["fields"]["max(measurements.cls)"] == "number"
  1303. assert meta["units"]["max(measurements.cls)"] is None
  1304. def test_custom_measurement_duration_filtering(self):
  1305. self.store_transaction_metric(
  1306. 1,
  1307. metric="measurements.runtime",
  1308. internal_metric="d:transactions/measurements.runtime@hour",
  1309. entity="metrics_distributions",
  1310. tags={"transaction": "foo_transaction"},
  1311. timestamp=self.min_ago,
  1312. )
  1313. self.store_transaction_metric(
  1314. 180,
  1315. metric="measurements.runtime",
  1316. internal_metric="d:transactions/measurements.runtime@hour",
  1317. entity="metrics_distributions",
  1318. tags={"transaction": "bar_transaction"},
  1319. timestamp=self.min_ago,
  1320. )
  1321. query = {
  1322. "project": [self.project.id],
  1323. "field": [
  1324. "transaction",
  1325. "max(measurements.runtime)",
  1326. ],
  1327. "query": "p50(measurements.runtime):>1wk",
  1328. "statsPeriod": "24h",
  1329. "dataset": "metricsEnhanced",
  1330. "per_page": 50,
  1331. }
  1332. response = self.do_request(query)
  1333. assert response.status_code == 200, response.content
  1334. assert len(response.data["data"]) == 1
  1335. data = response.data["data"]
  1336. meta = response.data["meta"]
  1337. assert data[0]["transaction"] == "bar_transaction"
  1338. assert data[0]["max(measurements.runtime)"] == 180
  1339. assert meta["isMetricsData"]
  1340. def test_custom_measurement_size_filtering(self):
  1341. self.store_transaction_metric(
  1342. 1,
  1343. metric="measurements.datacenter_memory",
  1344. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1345. entity="metrics_distributions",
  1346. tags={"transaction": "foo_transaction"},
  1347. timestamp=self.min_ago,
  1348. )
  1349. self.store_transaction_metric(
  1350. 100,
  1351. metric="measurements.datacenter_memory",
  1352. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1353. entity="metrics_distributions",
  1354. tags={"transaction": "bar_transaction"},
  1355. timestamp=self.min_ago,
  1356. )
  1357. query = {
  1358. "project": [self.project.id],
  1359. "field": [
  1360. "transaction",
  1361. "max(measurements.datacenter_memory)",
  1362. ],
  1363. "query": "p50(measurements.datacenter_memory):>5pb",
  1364. "statsPeriod": "24h",
  1365. "dataset": "metricsEnhanced",
  1366. "per_page": 50,
  1367. }
  1368. response = self.do_request(query)
  1369. assert response.status_code == 200, response.content
  1370. assert len(response.data["data"]) == 1
  1371. data = response.data["data"]
  1372. meta = response.data["meta"]
  1373. assert data[0]["transaction"] == "bar_transaction"
  1374. assert data[0]["max(measurements.datacenter_memory)"] == 100
  1375. assert meta["units"]["max(measurements.datacenter_memory)"] == "petabyte"
  1376. assert meta["fields"]["max(measurements.datacenter_memory)"] == "size"
  1377. assert meta["isMetricsData"]
  1378. def test_has_custom_measurement(self):
  1379. self.store_transaction_metric(
  1380. 33,
  1381. metric="measurements.datacenter_memory",
  1382. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1383. entity="metrics_distributions",
  1384. tags={"transaction": "foo_transaction"},
  1385. timestamp=self.min_ago,
  1386. )
  1387. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1388. transaction_data["measurements"]["datacenter_memory"] = {
  1389. "value": 33,
  1390. "unit": "petabyte",
  1391. }
  1392. self.store_event(transaction_data, self.project.id)
  1393. measurement = "measurements.datacenter_memory"
  1394. response = self.do_request(
  1395. {
  1396. "field": ["transaction", measurement],
  1397. "query": "has:measurements.datacenter_memory",
  1398. "dataset": "discover",
  1399. }
  1400. )
  1401. assert response.status_code == 200, response.content
  1402. assert len(response.data["data"]) == 1
  1403. response = self.do_request(
  1404. {
  1405. "field": ["transaction", measurement],
  1406. "query": "!has:measurements.datacenter_memory",
  1407. "dataset": "discover",
  1408. }
  1409. )
  1410. assert response.status_code == 200, response.content
  1411. assert len(response.data["data"]) == 0
  1412. def test_environment_param(self):
  1413. self.create_environment(self.project, name="staging")
  1414. self.store_transaction_metric(
  1415. 1,
  1416. tags={"transaction": "foo_transaction", "environment": "staging"},
  1417. timestamp=self.min_ago,
  1418. )
  1419. self.store_transaction_metric(
  1420. 100,
  1421. tags={"transaction": "foo_transaction"},
  1422. timestamp=self.min_ago,
  1423. )
  1424. query = {
  1425. "project": [self.project.id],
  1426. "environment": "staging",
  1427. "orderby": "p50(transaction.duration)",
  1428. "field": [
  1429. "transaction",
  1430. "environment",
  1431. "p50(transaction.duration)",
  1432. ],
  1433. "statsPeriod": "24h",
  1434. "dataset": "metricsEnhanced",
  1435. "per_page": 50,
  1436. }
  1437. response = self.do_request(query)
  1438. assert response.status_code == 200, response.content
  1439. assert len(response.data["data"]) == 1
  1440. data = response.data["data"]
  1441. meta = response.data["meta"]
  1442. assert data[0]["transaction"] == "foo_transaction"
  1443. assert data[0]["environment"] == "staging"
  1444. assert data[0]["p50(transaction.duration)"] == 1
  1445. assert meta["isMetricsData"]
  1446. def test_environment_query(self):
  1447. self.create_environment(self.project, name="staging")
  1448. self.store_transaction_metric(
  1449. 1,
  1450. tags={"transaction": "foo_transaction", "environment": "staging"},
  1451. timestamp=self.min_ago,
  1452. )
  1453. self.store_transaction_metric(
  1454. 100,
  1455. tags={"transaction": "foo_transaction"},
  1456. timestamp=self.min_ago,
  1457. )
  1458. query = {
  1459. "project": [self.project.id],
  1460. "orderby": "p50(transaction.duration)",
  1461. "field": [
  1462. "transaction",
  1463. "environment",
  1464. "p50(transaction.duration)",
  1465. ],
  1466. "query": "!has:environment",
  1467. "statsPeriod": "24h",
  1468. "dataset": "metricsEnhanced",
  1469. "per_page": 50,
  1470. }
  1471. self.wait_for_metric_count(self.project, 2)
  1472. response = self.do_request(query)
  1473. assert response.status_code == 200, response.content
  1474. assert len(response.data["data"]) == 1
  1475. data = response.data["data"]
  1476. meta = response.data["meta"]
  1477. assert data[0]["transaction"] == "foo_transaction"
  1478. assert data[0]["environment"] is None or data[0]["environment"] == ""
  1479. assert data[0]["p50(transaction.duration)"] == 100
  1480. assert meta["isMetricsData"]
  1481. def test_has_transaction(self):
  1482. self.store_transaction_metric(
  1483. 1,
  1484. tags={},
  1485. timestamp=self.min_ago,
  1486. )
  1487. self.store_transaction_metric(
  1488. 100,
  1489. tags={"transaction": "foo_transaction"},
  1490. timestamp=self.min_ago,
  1491. )
  1492. query = {
  1493. "project": [self.project.id],
  1494. "orderby": "p50(transaction.duration)",
  1495. "field": [
  1496. "transaction",
  1497. "p50(transaction.duration)",
  1498. ],
  1499. "query": "has:transaction",
  1500. "statsPeriod": "24h",
  1501. "dataset": "metricsEnhanced",
  1502. "per_page": 50,
  1503. }
  1504. self.wait_for_metric_count(self.project, 2)
  1505. response = self.do_request(query)
  1506. assert response.status_code == 200, response.content
  1507. assert len(response.data["data"]) == 2
  1508. data = response.data["data"]
  1509. meta = response.data["meta"]
  1510. assert data[0]["transaction"] == "<< unparameterized >>"
  1511. assert data[0]["p50(transaction.duration)"] == 1
  1512. assert data[1]["transaction"] == "foo_transaction"
  1513. assert data[1]["p50(transaction.duration)"] == 100
  1514. assert meta["isMetricsData"]
  1515. query = {
  1516. "project": [self.project.id],
  1517. "orderby": "p50(transaction.duration)",
  1518. "field": [
  1519. "transaction",
  1520. "p50(transaction.duration)",
  1521. ],
  1522. "query": "!has:transaction",
  1523. "statsPeriod": "24h",
  1524. "dataset": "metricsEnhanced",
  1525. "per_page": 50,
  1526. }
  1527. response = self.do_request(query)
  1528. assert response.status_code == 400, response.content
  1529. def test_apdex_transaction_threshold(self):
  1530. ProjectTransactionThresholdOverride.objects.create(
  1531. transaction="foo_transaction",
  1532. project=self.project,
  1533. organization=self.project.organization,
  1534. threshold=600,
  1535. metric=TransactionMetric.LCP.value,
  1536. )
  1537. ProjectTransactionThresholdOverride.objects.create(
  1538. transaction="bar_transaction",
  1539. project=self.project,
  1540. organization=self.project.organization,
  1541. threshold=600,
  1542. metric=TransactionMetric.LCP.value,
  1543. )
  1544. self.store_transaction_metric(
  1545. 1,
  1546. tags={
  1547. "transaction": "foo_transaction",
  1548. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1549. },
  1550. timestamp=self.min_ago,
  1551. )
  1552. self.store_transaction_metric(
  1553. 1,
  1554. "measurements.lcp",
  1555. tags={
  1556. "transaction": "bar_transaction",
  1557. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1558. },
  1559. timestamp=self.min_ago,
  1560. )
  1561. response = self.do_request(
  1562. {
  1563. "field": [
  1564. "transaction",
  1565. "apdex()",
  1566. ],
  1567. "orderby": ["apdex()"],
  1568. "query": "event.type:transaction",
  1569. "dataset": "metrics",
  1570. "per_page": 50,
  1571. }
  1572. )
  1573. assert len(response.data["data"]) == 2
  1574. data = response.data["data"]
  1575. meta = response.data["meta"]
  1576. field_meta = meta["fields"]
  1577. assert data[0]["transaction"] == "bar_transaction"
  1578. # Threshold is lcp based
  1579. assert data[0]["apdex()"] == 1
  1580. assert data[1]["transaction"] == "foo_transaction"
  1581. # Threshold is lcp based
  1582. assert data[1]["apdex()"] == 0
  1583. assert meta["isMetricsData"]
  1584. assert field_meta["transaction"] == "string"
  1585. assert field_meta["apdex()"] == "number"
  1586. def test_apdex_project_threshold(self):
  1587. ProjectTransactionThreshold.objects.create(
  1588. project=self.project,
  1589. organization=self.project.organization,
  1590. threshold=600,
  1591. metric=TransactionMetric.LCP.value,
  1592. )
  1593. self.store_transaction_metric(
  1594. 1,
  1595. tags={
  1596. "transaction": "foo_transaction",
  1597. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1598. },
  1599. timestamp=self.min_ago,
  1600. )
  1601. self.store_transaction_metric(
  1602. 1,
  1603. "measurements.lcp",
  1604. tags={
  1605. "transaction": "bar_transaction",
  1606. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1607. },
  1608. timestamp=self.min_ago,
  1609. )
  1610. response = self.do_request(
  1611. {
  1612. "field": [
  1613. "transaction",
  1614. "apdex()",
  1615. ],
  1616. "orderby": ["apdex()"],
  1617. "query": "event.type:transaction",
  1618. "dataset": "metrics",
  1619. "per_page": 50,
  1620. }
  1621. )
  1622. assert response.status_code == 200, response.content
  1623. assert len(response.data["data"]) == 2
  1624. data = response.data["data"]
  1625. meta = response.data["meta"]
  1626. field_meta = meta["fields"]
  1627. assert data[0]["transaction"] == "bar_transaction"
  1628. # Threshold is lcp based
  1629. assert data[0]["apdex()"] == 1
  1630. assert data[1]["transaction"] == "foo_transaction"
  1631. # Threshold is lcp based
  1632. assert data[1]["apdex()"] == 0
  1633. assert meta["isMetricsData"]
  1634. assert field_meta["transaction"] == "string"
  1635. assert field_meta["apdex()"] == "number"
  1636. def test_apdex_satisfaction_param(self):
  1637. for function in ["apdex(300)", "user_misery(300)", "count_miserable(user, 300)"]:
  1638. query = {
  1639. "project": [self.project.id],
  1640. "field": [
  1641. "transaction",
  1642. function,
  1643. ],
  1644. "statsPeriod": "24h",
  1645. "dataset": "metricsEnhanced",
  1646. "per_page": 50,
  1647. }
  1648. response = self.do_request(query)
  1649. assert response.status_code == 200, response.content
  1650. assert len(response.data["data"]) == 0
  1651. meta = response.data["meta"]
  1652. assert not meta["isMetricsData"], function
  1653. query = {
  1654. "project": [self.project.id],
  1655. "field": [
  1656. "transaction",
  1657. function,
  1658. ],
  1659. "statsPeriod": "24h",
  1660. "dataset": "metrics",
  1661. "per_page": 50,
  1662. }
  1663. response = self.do_request(query)
  1664. assert response.status_code == 400, function
  1665. assert b"threshold parameter" in response.content, function
  1666. def test_mobile_metrics(self):
  1667. self.store_transaction_metric(
  1668. 0.4,
  1669. "measurements.frames_frozen_rate",
  1670. tags={
  1671. "transaction": "bar_transaction",
  1672. },
  1673. timestamp=self.min_ago,
  1674. )
  1675. query = {
  1676. "project": [self.project.id],
  1677. "field": [
  1678. "transaction",
  1679. "p50(measurements.frames_frozen_rate)",
  1680. ],
  1681. "statsPeriod": "24h",
  1682. "dataset": "metrics",
  1683. "per_page": 50,
  1684. }
  1685. response = self.do_request(query)
  1686. assert response.status_code == 200, response.content
  1687. assert len(response.data["data"]) == 1
  1688. assert response.data["data"][0]["p50(measurements.frames_frozen_rate)"] == 0.4
  1689. def test_merge_null_unparam(self):
  1690. self.store_transaction_metric(
  1691. 1,
  1692. # Transaction: unparam
  1693. tags={
  1694. "transaction": "<< unparameterized >>",
  1695. },
  1696. timestamp=self.min_ago,
  1697. )
  1698. self.store_transaction_metric(
  1699. 2,
  1700. # Transaction:null
  1701. tags={},
  1702. timestamp=self.min_ago,
  1703. )
  1704. query = {
  1705. "project": [self.project.id],
  1706. "field": [
  1707. "transaction",
  1708. "p50(transaction.duration)",
  1709. ],
  1710. "statsPeriod": "24h",
  1711. "dataset": "metrics",
  1712. "per_page": 50,
  1713. }
  1714. response = self.do_request(query)
  1715. assert response.status_code == 200, response.content
  1716. assert len(response.data["data"]) == 1
  1717. assert response.data["data"][0]["p50(transaction.duration)"] == 1.5
  1718. def test_unparam_filter(self):
  1719. self.store_transaction_metric(
  1720. 1,
  1721. # Transaction: unparam
  1722. tags={
  1723. "transaction": "<< unparameterized >>",
  1724. },
  1725. timestamp=self.min_ago,
  1726. )
  1727. self.store_transaction_metric(
  1728. 2,
  1729. # Transaction:null
  1730. tags={},
  1731. timestamp=self.min_ago,
  1732. )
  1733. self.store_transaction_metric(
  1734. 3,
  1735. tags={
  1736. "transaction": "foo_transaction",
  1737. },
  1738. timestamp=self.min_ago,
  1739. )
  1740. query = {
  1741. "project": [self.project.id],
  1742. "field": [
  1743. "transaction",
  1744. "count()",
  1745. ],
  1746. "query": 'transaction:"<< unparameterized >>"',
  1747. "statsPeriod": "24h",
  1748. "dataset": "metrics",
  1749. "per_page": 50,
  1750. }
  1751. self.wait_for_metric_count(self.project, 3)
  1752. response = self.do_request(query)
  1753. assert response.status_code == 200, response.content
  1754. assert len(response.data["data"]) == 1
  1755. assert response.data["data"][0]["transaction"] == "<< unparameterized >>"
  1756. assert response.data["data"][0]["count()"] == 2
  1757. def test_custom_measurements_without_function(self):
  1758. self.store_transaction_metric(
  1759. 33,
  1760. metric="measurements.datacenter_memory",
  1761. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1762. entity="metrics_distributions",
  1763. tags={"transaction": "foo_transaction"},
  1764. timestamp=self.min_ago,
  1765. )
  1766. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1767. transaction_data["measurements"]["datacenter_memory"] = {
  1768. "value": 33,
  1769. "unit": "petabyte",
  1770. }
  1771. self.store_event(transaction_data, self.project.id)
  1772. measurement = "measurements.datacenter_memory"
  1773. response = self.do_request(
  1774. {
  1775. "field": ["transaction", measurement],
  1776. "query": "measurements.datacenter_memory:33pb",
  1777. "dataset": "discover",
  1778. }
  1779. )
  1780. assert response.status_code == 200, response.content
  1781. data = response.data["data"]
  1782. assert len(data) == 1
  1783. assert data[0][measurement] == 33
  1784. meta = response.data["meta"]
  1785. field_meta = meta["fields"]
  1786. unit_meta = meta["units"]
  1787. assert field_meta[measurement] == "size"
  1788. assert unit_meta[measurement] == "petabyte"
  1789. assert not meta["isMetricsData"]
  1790. def test_custom_measurements_with_function(self):
  1791. self.store_transaction_metric(
  1792. 33,
  1793. metric="measurements.datacenter_memory",
  1794. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1795. entity="metrics_distributions",
  1796. tags={"transaction": "foo_transaction"},
  1797. timestamp=self.min_ago,
  1798. )
  1799. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1800. transaction_data["measurements"]["datacenter_memory"] = {
  1801. "value": 33,
  1802. "unit": "petabyte",
  1803. }
  1804. self.store_event(transaction_data, self.project.id)
  1805. measurement = "p50(measurements.datacenter_memory)"
  1806. response = self.do_request(
  1807. {
  1808. "field": ["transaction", measurement],
  1809. "query": "measurements.datacenter_memory:33pb",
  1810. "dataset": "discover",
  1811. }
  1812. )
  1813. assert response.status_code == 200, response.content
  1814. data = response.data["data"]
  1815. assert len(data) == 1
  1816. assert data[0][measurement] == 33
  1817. meta = response.data["meta"]
  1818. field_meta = meta["fields"]
  1819. unit_meta = meta["units"]
  1820. assert field_meta[measurement] == "size"
  1821. assert unit_meta[measurement] == "petabyte"
  1822. assert not meta["isMetricsData"]
  1823. def test_custom_measurements_equation(self):
  1824. self.store_transaction_metric(
  1825. 33,
  1826. metric="measurements.datacenter_memory",
  1827. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1828. entity="metrics_distributions",
  1829. tags={"transaction": "foo_transaction"},
  1830. timestamp=self.min_ago,
  1831. )
  1832. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1833. transaction_data["measurements"]["datacenter_memory"] = {
  1834. "value": 33,
  1835. "unit": "petabyte",
  1836. }
  1837. self.store_event(transaction_data, self.project.id)
  1838. response = self.do_request(
  1839. {
  1840. "field": [
  1841. "transaction",
  1842. "measurements.datacenter_memory",
  1843. "equation|measurements.datacenter_memory / 3",
  1844. ],
  1845. "query": "",
  1846. "dataset": "discover",
  1847. }
  1848. )
  1849. assert response.status_code == 200, response.content
  1850. data = response.data["data"]
  1851. assert len(data) == 1
  1852. assert data[0]["measurements.datacenter_memory"] == 33
  1853. assert data[0]["equation|measurements.datacenter_memory / 3"] == 11
  1854. meta = response.data["meta"]
  1855. assert not meta["isMetricsData"]
  1856. def test_transaction_wildcard(self):
  1857. self.store_transaction_metric(
  1858. 1,
  1859. tags={"transaction": "foo_transaction"},
  1860. timestamp=self.min_ago,
  1861. )
  1862. self.store_transaction_metric(
  1863. 1,
  1864. tags={"transaction": "bar_transaction"},
  1865. timestamp=self.min_ago,
  1866. )
  1867. response = self.do_request(
  1868. {
  1869. "field": [
  1870. "transaction",
  1871. "p90()",
  1872. ],
  1873. "query": "transaction:foo*",
  1874. "dataset": "metrics",
  1875. }
  1876. )
  1877. assert response.status_code == 200, response.content
  1878. data = response.data["data"]
  1879. assert len(data) == 1
  1880. assert data[0]["p90()"] == 1
  1881. meta = response.data["meta"]
  1882. assert meta["isMetricsData"]
  1883. assert data[0]["transaction"] == "foo_transaction"
  1884. def test_transaction_status_wildcard(self):
  1885. self.store_transaction_metric(
  1886. 1,
  1887. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1888. timestamp=self.min_ago,
  1889. )
  1890. response = self.do_request(
  1891. {
  1892. "field": [
  1893. "transaction",
  1894. "p90()",
  1895. ],
  1896. "query": "transaction.status:f*bar",
  1897. "dataset": "metrics",
  1898. }
  1899. )
  1900. assert response.status_code == 200, response.content
  1901. data = response.data["data"]
  1902. assert len(data) == 1
  1903. assert data[0]["p90()"] == 1
  1904. meta = response.data["meta"]
  1905. assert meta["isMetricsData"]
  1906. def test_http_error_rate(self):
  1907. self.store_transaction_metric(
  1908. 1,
  1909. tags={
  1910. "transaction": "foo_transaction",
  1911. "transaction.status": "foobar",
  1912. "http.status_code": "500",
  1913. },
  1914. timestamp=self.min_ago,
  1915. )
  1916. self.store_transaction_metric(
  1917. 1,
  1918. tags={"transaction": "bar_transaction", "http.status_code": "400"},
  1919. timestamp=self.min_ago,
  1920. )
  1921. response = self.do_request(
  1922. {
  1923. "field": [
  1924. "http_error_rate()",
  1925. ],
  1926. "dataset": "metrics",
  1927. }
  1928. )
  1929. assert response.status_code == 200, response.content
  1930. data = response.data["data"]
  1931. assert len(data) == 1
  1932. assert data[0]["http_error_rate()"] == 0.5
  1933. meta = response.data["meta"]
  1934. assert meta["isMetricsData"]
  1935. def test_time_spent(self):
  1936. self.store_transaction_metric(
  1937. 1,
  1938. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1939. timestamp=self.min_ago,
  1940. )
  1941. self.store_transaction_metric(
  1942. 1,
  1943. tags={"transaction": "bar_transaction"},
  1944. timestamp=self.min_ago,
  1945. )
  1946. response = self.do_request(
  1947. {
  1948. "field": [
  1949. "transaction",
  1950. "time_spent_percentage()",
  1951. ],
  1952. "dataset": "metrics",
  1953. }
  1954. )
  1955. assert response.status_code == 200, response.content
  1956. data = response.data["data"]
  1957. assert len(data) == 2
  1958. assert data[0]["time_spent_percentage()"] == 0.5
  1959. meta = response.data["meta"]
  1960. assert meta["isMetricsData"]
  1961. class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithMetricLayer(
  1962. OrganizationEventsMetricsEnhancedPerformanceEndpointTest
  1963. ):
  1964. def setUp(self):
  1965. super().setUp()
  1966. self.features["organizations:use-metrics-layer"] = True
  1967. @pytest.mark.xfail(reason="Having not supported")
  1968. def test_custom_measurement_duration_filtering(self):
  1969. super().test_custom_measurement_size_filtering()
  1970. @pytest.mark.xfail(reason="Having not supported")
  1971. def test_having_condition_not_selected(self):
  1972. super().test_having_condition_not_selected()
  1973. @pytest.mark.xfail(reason="Having not supported")
  1974. def test_custom_measurement_size_filtering(self):
  1975. super().test_custom_measurement_size_filtering()
  1976. @pytest.mark.xfail(reason="Having not supported")
  1977. def test_having_condition(self):
  1978. super().test_having_condition()
  1979. @pytest.mark.xfail(reason="Not supported")
  1980. def test_time_spent(self):
  1981. super().test_custom_measurement_size_filtering()
  1982. @pytest.mark.xfail(reason="Not supported")
  1983. def test_http_error_rate(self):
  1984. super().test_having_condition()