test_organization_events_mep.py 75 KB

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