test_organization_events_mep.py 126 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589
  1. from typing import Any
  2. from unittest import mock
  3. import pytest
  4. from django.urls import reverse
  5. from rest_framework.response import Response
  6. from sentry.discover.models import TeamKeyTransaction
  7. from sentry.models.dashboard_widget import DashboardWidgetTypes
  8. from sentry.models.projectteam import ProjectTeam
  9. from sentry.models.transaction_threshold import (
  10. ProjectTransactionThreshold,
  11. ProjectTransactionThresholdOverride,
  12. TransactionMetric,
  13. )
  14. from sentry.search.events import constants
  15. from sentry.search.utils import map_device_class_level
  16. from sentry.snuba.metrics.extraction import (
  17. SPEC_VERSION_TWO_FLAG,
  18. MetricSpecType,
  19. OnDemandMetricSpec,
  20. OnDemandMetricSpecVersioning,
  21. )
  22. from sentry.snuba.metrics.naming_layer.mri import TransactionMRI
  23. from sentry.snuba.metrics.naming_layer.public import TransactionMetricKey
  24. from sentry.snuba.utils import DATASET_OPTIONS
  25. from sentry.testutils.cases import MetricsEnhancedPerformanceTestCase
  26. from sentry.testutils.helpers.datetime import before_now, iso_format
  27. from sentry.testutils.helpers.discover import user_misery_formula
  28. from sentry.testutils.helpers.on_demand import create_widget
  29. from sentry.utils.samples import load_data
  30. pytestmark = pytest.mark.sentry_metrics
  31. class OrganizationEventsMetricsEnhancedPerformanceEndpointTest(MetricsEnhancedPerformanceTestCase):
  32. viewname = "sentry-api-0-organization-events"
  33. # Poor intentionally omitted for test_measurement_rating_that_does_not_exist
  34. METRIC_STRINGS = [
  35. "foo_transaction",
  36. "bar_transaction",
  37. "baz_transaction",
  38. "staging",
  39. "measurement_rating",
  40. "good",
  41. "meh",
  42. "d:transactions/measurements.something_custom@millisecond",
  43. "d:transactions/measurements.runtime@hour",
  44. "d:transactions/measurements.bytes_transfered@byte",
  45. "d:transactions/measurements.datacenter_memory@petabyte",
  46. "d:transactions/measurements.custom.kilobyte@kilobyte",
  47. "d:transactions/measurements.longtaskcount@none",
  48. "d:transactions/measurements.percent@ratio",
  49. "d:transactions/measurements.custom_type@somethingcustom",
  50. ]
  51. def setUp(self):
  52. super().setUp()
  53. self.transaction_data = load_data("transaction", timestamp=before_now(minutes=1))
  54. self.features = {
  55. "organizations:performance-use-metrics": True,
  56. }
  57. def do_request(self, query, features=None):
  58. if features is None:
  59. features = {"organizations:discover-basic": True}
  60. features.update(self.features)
  61. self.login_as(user=self.user)
  62. url = reverse(
  63. self.viewname,
  64. kwargs={"organization_id_or_slug": self.organization.slug},
  65. )
  66. with self.feature(features):
  67. return self.client.get(url, query, format="json")
  68. def test_no_projects(self):
  69. response = self.do_request(
  70. {
  71. "dataset": "metricsEnhanced",
  72. }
  73. )
  74. assert response.status_code == 200, response.content
  75. def test_invalid_dataset(self):
  76. response = self.do_request(
  77. {
  78. "dataset": "aFakeDataset",
  79. "project": self.project.id,
  80. }
  81. )
  82. assert response.status_code == 400, response.content
  83. assert (
  84. response.data["detail"]
  85. == f"dataset must be one of: {', '.join([key for key in DATASET_OPTIONS.keys()])}"
  86. )
  87. def test_out_of_retention(self):
  88. self.create_project()
  89. with self.options({"system.event-retention-days": 10}):
  90. query = {
  91. "field": ["id", "timestamp"],
  92. "orderby": ["-timestamp", "-id"],
  93. "query": "event.type:transaction",
  94. "start": iso_format(before_now(days=20)),
  95. "end": iso_format(before_now(days=15)),
  96. "dataset": "metricsEnhanced",
  97. }
  98. response = self.do_request(query)
  99. assert response.status_code == 400, response.content
  100. assert response.data["detail"] == "Invalid date range. Please try a more recent date range."
  101. def test_invalid_search_terms(self):
  102. response = self.do_request(
  103. {
  104. "field": ["epm()"],
  105. "query": "hi \n there",
  106. "project": self.project.id,
  107. "dataset": "metricsEnhanced",
  108. }
  109. )
  110. assert response.status_code == 400, response.content
  111. assert (
  112. response.data["detail"]
  113. == "Parse error at 'hi \n ther' (column 4). This is commonly caused by unmatched parentheses. Enclose any text in double quotes."
  114. )
  115. def test_percentile_with_no_data(self):
  116. response = self.do_request(
  117. {
  118. "field": ["p50()"],
  119. "query": "",
  120. "project": self.project.id,
  121. "dataset": "metricsEnhanced",
  122. }
  123. )
  124. assert response.status_code == 200, response.content
  125. data = response.data["data"]
  126. assert len(data) == 1
  127. assert data[0]["p50()"] == 0
  128. def test_project_name(self):
  129. self.store_transaction_metric(
  130. 1,
  131. tags={"environment": "staging"},
  132. timestamp=self.min_ago,
  133. )
  134. response = self.do_request(
  135. {
  136. "field": ["project.name", "environment", "epm()"],
  137. "query": "event.type:transaction",
  138. "dataset": "metricsEnhanced",
  139. "per_page": 50,
  140. }
  141. )
  142. assert response.status_code == 200, response.content
  143. assert len(response.data["data"]) == 1
  144. data = response.data["data"]
  145. meta = response.data["meta"]
  146. field_meta = meta["fields"]
  147. assert data[0]["project.name"] == self.project.slug
  148. assert "project.id" not in data[0]
  149. assert data[0]["environment"] == "staging"
  150. assert meta["isMetricsData"]
  151. assert field_meta["project.name"] == "string"
  152. assert field_meta["environment"] == "string"
  153. assert field_meta["epm()"] == "rate"
  154. def test_project_id(self):
  155. self.store_transaction_metric(
  156. 1,
  157. tags={"environment": "staging"},
  158. timestamp=self.min_ago,
  159. )
  160. response = self.do_request(
  161. {
  162. "field": ["project_id", "environment", "epm()"],
  163. "query": "event.type:transaction",
  164. "dataset": "metrics",
  165. "per_page": 50,
  166. }
  167. )
  168. assert response.status_code == 200, response.content
  169. assert len(response.data["data"]) == 1
  170. data = response.data["data"]
  171. meta = response.data["meta"]
  172. field_meta = meta["fields"]
  173. assert data[0]["project_id"] == self.project.id
  174. assert data[0]["environment"] == "staging"
  175. assert meta["isMetricsData"]
  176. assert field_meta["project_id"] == "integer"
  177. assert field_meta["environment"] == "string"
  178. assert field_meta["epm()"] == "rate"
  179. def test_project_dot_id(self):
  180. self.store_transaction_metric(
  181. 1,
  182. tags={"environment": "staging"},
  183. timestamp=self.min_ago,
  184. )
  185. response = self.do_request(
  186. {
  187. "field": ["project.id", "environment", "epm()"],
  188. "query": "event.type:transaction",
  189. "dataset": "metrics",
  190. "per_page": 50,
  191. }
  192. )
  193. assert response.status_code == 200, response.content
  194. assert len(response.data["data"]) == 1
  195. data = response.data["data"]
  196. meta = response.data["meta"]
  197. field_meta = meta["fields"]
  198. assert data[0]["project.id"] == self.project.id
  199. assert data[0]["environment"] == "staging"
  200. assert meta["isMetricsData"]
  201. assert field_meta["project.id"] == "integer"
  202. assert field_meta["environment"] == "string"
  203. assert field_meta["epm()"] == "rate"
  204. def test_title_alias(self):
  205. """title is an alias to transaction name"""
  206. self.store_transaction_metric(
  207. 1,
  208. tags={"transaction": "foo_transaction"},
  209. timestamp=self.min_ago,
  210. )
  211. response = self.do_request(
  212. {
  213. "field": ["title", "p50()"],
  214. "query": "event.type:transaction",
  215. "dataset": "metricsEnhanced",
  216. "per_page": 50,
  217. }
  218. )
  219. assert response.status_code == 200, response.content
  220. assert len(response.data["data"]) == 1
  221. data = response.data["data"]
  222. meta = response.data["meta"]
  223. field_meta = meta["fields"]
  224. assert data[0]["title"] == "foo_transaction"
  225. assert data[0]["p50()"] == 1
  226. assert meta["isMetricsData"]
  227. assert field_meta["title"] == "string"
  228. assert field_meta["p50()"] == "duration"
  229. def test_having_condition(self):
  230. self.store_transaction_metric(
  231. 1,
  232. tags={"environment": "staging", "transaction": "foo_transaction"},
  233. timestamp=self.min_ago,
  234. )
  235. self.store_transaction_metric(
  236. # shouldn't show up
  237. 100,
  238. tags={"environment": "staging", "transaction": "bar_transaction"},
  239. timestamp=self.min_ago,
  240. )
  241. response = self.do_request(
  242. {
  243. "field": ["transaction", "project", "p50(transaction.duration)"],
  244. "query": "event.type:transaction p50(transaction.duration):<50",
  245. "dataset": "metricsEnhanced",
  246. "per_page": 50,
  247. }
  248. )
  249. assert response.status_code == 200, response.content
  250. assert len(response.data["data"]) == 1
  251. data = response.data["data"]
  252. meta = response.data["meta"]
  253. field_meta = meta["fields"]
  254. assert data[0]["transaction"] == "foo_transaction"
  255. assert data[0]["project"] == self.project.slug
  256. assert data[0]["p50(transaction.duration)"] == 1
  257. assert meta["isMetricsData"]
  258. assert field_meta["transaction"] == "string"
  259. assert field_meta["project"] == "string"
  260. assert field_meta["p50(transaction.duration)"] == "duration"
  261. def test_having_condition_with_preventing_aggregates(self):
  262. self.store_transaction_metric(
  263. 1,
  264. tags={"environment": "staging", "transaction": "foo_transaction"},
  265. timestamp=self.min_ago,
  266. )
  267. self.store_transaction_metric(
  268. 100,
  269. tags={"environment": "staging", "transaction": "bar_transaction"},
  270. timestamp=self.min_ago,
  271. )
  272. response = self.do_request(
  273. {
  274. "field": ["transaction", "project", "p50(transaction.duration)"],
  275. "query": "event.type:transaction p50(transaction.duration):<50",
  276. "dataset": "metricsEnhanced",
  277. "preventMetricAggregates": "1",
  278. "per_page": 50,
  279. }
  280. )
  281. assert response.status_code == 200, response.content
  282. assert len(response.data["data"]) == 0
  283. meta = response.data["meta"]
  284. field_meta = meta["fields"]
  285. assert not meta["isMetricsData"]
  286. assert field_meta["transaction"] == "string"
  287. assert field_meta["project"] == "string"
  288. assert field_meta["p50(transaction.duration)"] == "duration"
  289. def test_having_condition_with_preventing_aggregate_metrics_only(self):
  290. """same as the previous test, but with the dataset on explicit metrics
  291. which should throw a 400 error instead"""
  292. response = self.do_request(
  293. {
  294. "field": ["transaction", "project", "p50(transaction.duration)"],
  295. "query": "event.type:transaction p50(transaction.duration):<50",
  296. "dataset": "metrics",
  297. "preventMetricAggregates": "1",
  298. "per_page": 50,
  299. "project": self.project.id,
  300. }
  301. )
  302. assert response.status_code == 400, response.content
  303. def test_having_condition_not_selected(self):
  304. self.store_transaction_metric(
  305. 1,
  306. tags={"environment": "staging", "transaction": "foo_transaction"},
  307. timestamp=self.min_ago,
  308. )
  309. self.store_transaction_metric(
  310. # shouldn't show up
  311. 100,
  312. tags={"environment": "staging", "transaction": "bar_transaction"},
  313. timestamp=self.min_ago,
  314. )
  315. response = self.do_request(
  316. {
  317. "field": ["transaction", "project", "p50(transaction.duration)"],
  318. "query": "event.type:transaction p75(transaction.duration):<50",
  319. "dataset": "metricsEnhanced",
  320. "per_page": 50,
  321. }
  322. )
  323. assert response.status_code == 200, response.content
  324. assert len(response.data["data"]) == 1
  325. data = response.data["data"]
  326. meta = response.data["meta"]
  327. field_meta = meta["fields"]
  328. assert data[0]["transaction"] == "foo_transaction"
  329. assert data[0]["project"] == self.project.slug
  330. assert data[0]["p50(transaction.duration)"] == 1
  331. assert meta["isMetricsData"]
  332. assert field_meta["transaction"] == "string"
  333. assert field_meta["project"] == "string"
  334. assert field_meta["p50(transaction.duration)"] == "duration"
  335. def test_non_metrics_tag_with_implicit_format(self):
  336. self.store_transaction_metric(
  337. 1,
  338. tags={"environment": "staging", "transaction": "foo_transaction"},
  339. timestamp=self.min_ago,
  340. )
  341. response = self.do_request(
  342. {
  343. "field": ["test", "p50(transaction.duration)"],
  344. "query": "event.type:transaction",
  345. "dataset": "metricsEnhanced",
  346. "per_page": 50,
  347. }
  348. )
  349. assert response.status_code == 200, response.content
  350. assert len(response.data["data"]) == 0
  351. assert not response.data["meta"]["isMetricsData"]
  352. def test_non_metrics_tag_with_implicit_format_metrics_dataset(self):
  353. self.store_transaction_metric(
  354. 1,
  355. tags={"environment": "staging", "transaction": "foo_transaction"},
  356. timestamp=self.min_ago,
  357. )
  358. response = self.do_request(
  359. {
  360. "field": ["test", "p50(transaction.duration)"],
  361. "query": "event.type:transaction",
  362. "dataset": "metrics",
  363. "per_page": 50,
  364. }
  365. )
  366. assert response.status_code == 400, response.content
  367. @pytest.mark.querybuilder
  368. def test_performance_homepage_query(self):
  369. self.store_transaction_metric(
  370. 1,
  371. tags={
  372. "transaction": "foo_transaction",
  373. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  374. },
  375. timestamp=self.min_ago,
  376. )
  377. self.store_transaction_metric(
  378. 1,
  379. "measurements.fcp",
  380. tags={"transaction": "foo_transaction"},
  381. timestamp=self.min_ago,
  382. )
  383. self.store_transaction_metric(
  384. 2,
  385. "measurements.lcp",
  386. tags={"transaction": "foo_transaction"},
  387. timestamp=self.min_ago,
  388. )
  389. self.store_transaction_metric(
  390. 3,
  391. "measurements.fid",
  392. tags={"transaction": "foo_transaction"},
  393. timestamp=self.min_ago,
  394. )
  395. self.store_transaction_metric(
  396. 4,
  397. "measurements.cls",
  398. tags={"transaction": "foo_transaction"},
  399. timestamp=self.min_ago,
  400. )
  401. self.store_transaction_metric(
  402. 1,
  403. "user",
  404. tags={
  405. "transaction": "foo_transaction",
  406. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_FRUSTRATED_TAG_VALUE,
  407. },
  408. timestamp=self.min_ago,
  409. )
  410. for dataset in ["metrics", "metricsEnhanced"]:
  411. response = self.do_request(
  412. {
  413. "field": [
  414. "transaction",
  415. "project",
  416. "tpm()",
  417. "p75(measurements.fcp)",
  418. "p75(measurements.lcp)",
  419. "p75(measurements.fid)",
  420. "p75(measurements.cls)",
  421. "count_unique(user)",
  422. "apdex()",
  423. "count_miserable(user)",
  424. "user_misery()",
  425. "failure_rate()",
  426. "failure_count()",
  427. ],
  428. "orderby": "tpm()",
  429. "query": "event.type:transaction",
  430. "dataset": dataset,
  431. "per_page": 50,
  432. }
  433. )
  434. assert len(response.data["data"]) == 1
  435. data = response.data["data"][0]
  436. meta = response.data["meta"]
  437. field_meta = meta["fields"]
  438. assert data["transaction"] == "foo_transaction"
  439. assert data["project"] == self.project.slug
  440. assert data["p75(measurements.fcp)"] == 1.0
  441. assert data["p75(measurements.lcp)"] == 2.0
  442. assert data["p75(measurements.fid)"] == 3.0
  443. assert data["p75(measurements.cls)"] == 4.0
  444. assert data["apdex()"] == 1.0
  445. assert data["count_miserable(user)"] == 1.0
  446. assert data["user_misery()"] == 0.058
  447. assert data["failure_rate()"] == 1
  448. assert data["failure_count()"] == 1
  449. assert meta["isMetricsData"]
  450. assert field_meta["transaction"] == "string"
  451. assert field_meta["project"] == "string"
  452. assert field_meta["p75(measurements.fcp)"] == "duration"
  453. assert field_meta["p75(measurements.lcp)"] == "duration"
  454. assert field_meta["p75(measurements.fid)"] == "duration"
  455. assert field_meta["p75(measurements.cls)"] == "number"
  456. assert field_meta["apdex()"] == "number"
  457. assert field_meta["count_miserable(user)"] == "integer"
  458. assert field_meta["user_misery()"] == "number"
  459. assert field_meta["failure_rate()"] == "percentage"
  460. assert field_meta["failure_count()"] == "integer"
  461. assert field_meta["tpm()"] == "rate"
  462. assert meta["units"]["tpm()"] == "1/minute"
  463. def test_user_misery_and_team_key_sort(self):
  464. self.store_transaction_metric(
  465. 1,
  466. tags={
  467. "transaction": "foo_transaction",
  468. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  469. },
  470. timestamp=self.min_ago,
  471. )
  472. self.store_transaction_metric(
  473. 1,
  474. "measurements.fcp",
  475. tags={"transaction": "foo_transaction"},
  476. timestamp=self.min_ago,
  477. )
  478. self.store_transaction_metric(
  479. 2,
  480. "measurements.lcp",
  481. tags={"transaction": "foo_transaction"},
  482. timestamp=self.min_ago,
  483. )
  484. self.store_transaction_metric(
  485. 3,
  486. "measurements.fid",
  487. tags={"transaction": "foo_transaction"},
  488. timestamp=self.min_ago,
  489. )
  490. self.store_transaction_metric(
  491. 4,
  492. "measurements.cls",
  493. tags={"transaction": "foo_transaction"},
  494. timestamp=self.min_ago,
  495. )
  496. self.store_transaction_metric(
  497. 1,
  498. "user",
  499. tags={
  500. "transaction": "foo_transaction",
  501. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_FRUSTRATED_TAG_VALUE,
  502. },
  503. timestamp=self.min_ago,
  504. )
  505. response = self.do_request(
  506. {
  507. "field": [
  508. "team_key_transaction",
  509. "transaction",
  510. "project",
  511. "tpm()",
  512. "p75(measurements.fcp)",
  513. "p75(measurements.lcp)",
  514. "p75(measurements.fid)",
  515. "p75(measurements.cls)",
  516. "count_unique(user)",
  517. "apdex()",
  518. "count_miserable(user)",
  519. "user_misery()",
  520. "failure_rate()",
  521. "failure_count()",
  522. ],
  523. "orderby": ["team_key_transaction", "user_misery()"],
  524. "query": "event.type:transaction",
  525. "dataset": "metrics",
  526. "per_page": 50,
  527. }
  528. )
  529. assert response.status_code == 200, response.content
  530. assert len(response.data["data"]) == 1
  531. data = response.data["data"][0]
  532. meta = response.data["meta"]
  533. field_meta = meta["fields"]
  534. assert data["transaction"] == "foo_transaction"
  535. assert data["project"] == self.project.slug
  536. assert data["p75(measurements.fcp)"] == 1.0
  537. assert data["p75(measurements.lcp)"] == 2.0
  538. assert data["p75(measurements.fid)"] == 3.0
  539. assert data["p75(measurements.cls)"] == 4.0
  540. assert data["apdex()"] == 1.0
  541. assert data["count_miserable(user)"] == 1.0
  542. assert data["user_misery()"] == 0.058
  543. assert data["failure_rate()"] == 1
  544. assert data["failure_count()"] == 1
  545. assert meta["isMetricsData"]
  546. assert field_meta["transaction"] == "string"
  547. assert field_meta["project"] == "string"
  548. assert field_meta["p75(measurements.fcp)"] == "duration"
  549. assert field_meta["p75(measurements.lcp)"] == "duration"
  550. assert field_meta["p75(measurements.fid)"] == "duration"
  551. assert field_meta["p75(measurements.cls)"] == "number"
  552. assert field_meta["apdex()"] == "number"
  553. assert field_meta["count_miserable(user)"] == "integer"
  554. assert field_meta["user_misery()"] == "number"
  555. assert field_meta["failure_rate()"] == "percentage"
  556. assert field_meta["failure_count()"] == "integer"
  557. def test_no_team_key_transactions(self):
  558. self.store_transaction_metric(
  559. 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago
  560. )
  561. self.store_transaction_metric(
  562. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  563. )
  564. query = {
  565. "team": "myteams",
  566. "project": [self.project.id],
  567. # TODO sort by transaction here once that's possible for order to match the same test without metrics
  568. "orderby": "p95()",
  569. "field": [
  570. "team_key_transaction",
  571. "transaction",
  572. "transaction.status",
  573. "project",
  574. "epm()",
  575. "failure_rate()",
  576. "p95()",
  577. ],
  578. "per_page": 50,
  579. "dataset": "metricsEnhanced",
  580. }
  581. response = self.do_request(query)
  582. assert response.status_code == 200, response.content
  583. assert len(response.data["data"]) == 2
  584. data = response.data["data"]
  585. meta = response.data["meta"]
  586. field_meta = meta["fields"]
  587. assert data[0]["team_key_transaction"] == 0
  588. assert data[0]["transaction"] == "foo_transaction"
  589. assert data[1]["team_key_transaction"] == 0
  590. assert data[1]["transaction"] == "bar_transaction"
  591. assert meta["isMetricsData"]
  592. assert field_meta["team_key_transaction"] == "boolean"
  593. assert field_meta["transaction"] == "string"
  594. def test_team_key_transactions_my_teams(self):
  595. team1 = self.create_team(organization=self.organization, name="Team A")
  596. self.create_team_membership(team1, user=self.user)
  597. self.project.add_team(team1)
  598. team2 = self.create_team(organization=self.organization, name="Team B")
  599. self.project.add_team(team2)
  600. key_transactions = [
  601. (team1, "foo_transaction"),
  602. (team2, "baz_transaction"),
  603. ]
  604. # Not a key transaction
  605. self.store_transaction_metric(
  606. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  607. )
  608. for team, transaction in key_transactions:
  609. self.store_transaction_metric(
  610. 1, tags={"transaction": transaction}, timestamp=self.min_ago
  611. )
  612. TeamKeyTransaction.objects.create(
  613. organization=self.organization,
  614. transaction=transaction,
  615. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  616. )
  617. query = {
  618. "team": "myteams",
  619. "project": [self.project.id],
  620. "field": [
  621. "team_key_transaction",
  622. "transaction",
  623. "transaction.status",
  624. "project",
  625. "epm()",
  626. "failure_rate()",
  627. "p95()",
  628. ],
  629. "per_page": 50,
  630. "dataset": "metricsEnhanced",
  631. }
  632. query["orderby"] = ["team_key_transaction", "p95()"]
  633. response = self.do_request(query)
  634. assert response.status_code == 200, response.content
  635. assert len(response.data["data"]) == 3
  636. data = response.data["data"]
  637. meta = response.data["meta"]
  638. field_meta = meta["fields"]
  639. assert data[0]["team_key_transaction"] == 0
  640. assert data[0]["transaction"] == "baz_transaction"
  641. assert data[1]["team_key_transaction"] == 0
  642. assert data[1]["transaction"] == "bar_transaction"
  643. assert data[2]["team_key_transaction"] == 1
  644. assert data[2]["transaction"] == "foo_transaction"
  645. assert meta["isMetricsData"]
  646. assert field_meta["team_key_transaction"] == "boolean"
  647. assert field_meta["transaction"] == "string"
  648. # not specifying any teams should use my teams
  649. query = {
  650. "project": [self.project.id],
  651. "field": [
  652. "team_key_transaction",
  653. "transaction",
  654. "transaction.status",
  655. "project",
  656. "epm()",
  657. "failure_rate()",
  658. "p95()",
  659. ],
  660. "per_page": 50,
  661. "dataset": "metricsEnhanced",
  662. }
  663. query["orderby"] = ["team_key_transaction", "p95()"]
  664. response = self.do_request(query)
  665. assert response.status_code == 200, response.content
  666. assert len(response.data["data"]) == 3
  667. data = response.data["data"]
  668. meta = response.data["meta"]
  669. field_meta = meta["fields"]
  670. assert data[0]["team_key_transaction"] == 0
  671. assert data[0]["transaction"] == "baz_transaction"
  672. assert data[1]["team_key_transaction"] == 0
  673. assert data[1]["transaction"] == "bar_transaction"
  674. assert data[2]["team_key_transaction"] == 1
  675. assert data[2]["transaction"] == "foo_transaction"
  676. assert meta["isMetricsData"]
  677. assert field_meta["team_key_transaction"] == "boolean"
  678. assert field_meta["transaction"] == "string"
  679. def test_team_key_transactions_orderby(self):
  680. team1 = self.create_team(organization=self.organization, name="Team A")
  681. team2 = self.create_team(organization=self.organization, name="Team B")
  682. key_transactions = [
  683. (team1, "foo_transaction", 1),
  684. (team2, "baz_transaction", 100),
  685. ]
  686. # Not a key transaction
  687. self.store_transaction_metric(
  688. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  689. )
  690. for team, transaction, value in key_transactions:
  691. self.store_transaction_metric(
  692. value, tags={"transaction": transaction}, timestamp=self.min_ago
  693. )
  694. self.create_team_membership(team, user=self.user)
  695. self.project.add_team(team)
  696. TeamKeyTransaction.objects.create(
  697. organization=self.organization,
  698. transaction=transaction,
  699. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  700. )
  701. query = {
  702. "team": "myteams",
  703. "project": [self.project.id],
  704. "field": [
  705. "team_key_transaction",
  706. "transaction",
  707. "transaction.status",
  708. "project",
  709. "epm()",
  710. "failure_rate()",
  711. "p95()",
  712. ],
  713. "per_page": 50,
  714. "dataset": "metricsEnhanced",
  715. }
  716. # test ascending order
  717. query["orderby"] = ["team_key_transaction", "p95()"]
  718. response = self.do_request(query)
  719. assert response.status_code == 200, response.content
  720. assert len(response.data["data"]) == 3
  721. data = response.data["data"]
  722. meta = response.data["meta"]
  723. field_meta = meta["fields"]
  724. assert data[0]["team_key_transaction"] == 0
  725. assert data[0]["transaction"] == "bar_transaction"
  726. assert data[1]["team_key_transaction"] == 1
  727. assert data[1]["transaction"] == "foo_transaction"
  728. assert data[2]["team_key_transaction"] == 1
  729. assert data[2]["transaction"] == "baz_transaction"
  730. assert meta["isMetricsData"]
  731. assert field_meta["team_key_transaction"] == "boolean"
  732. assert field_meta["transaction"] == "string"
  733. # test descending order
  734. query["orderby"] = ["-team_key_transaction", "p95()"]
  735. response = self.do_request(query)
  736. assert response.status_code == 200, response.content
  737. assert len(response.data["data"]) == 3
  738. data = response.data["data"]
  739. meta = response.data["meta"]
  740. field_meta = meta["fields"]
  741. assert data[0]["team_key_transaction"] == 1
  742. assert data[0]["transaction"] == "foo_transaction"
  743. assert data[1]["team_key_transaction"] == 1
  744. assert data[1]["transaction"] == "baz_transaction"
  745. assert data[2]["team_key_transaction"] == 0
  746. assert data[2]["transaction"] == "bar_transaction"
  747. assert meta["isMetricsData"]
  748. assert field_meta["team_key_transaction"] == "boolean"
  749. assert field_meta["transaction"] == "string"
  750. def test_team_key_transactions_query(self):
  751. team1 = self.create_team(organization=self.organization, name="Team A")
  752. team2 = self.create_team(organization=self.organization, name="Team B")
  753. key_transactions = [
  754. (team1, "foo_transaction", 1),
  755. (team2, "baz_transaction", 100),
  756. ]
  757. # Not a key transaction
  758. self.store_transaction_metric(
  759. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  760. )
  761. for team, transaction, value in key_transactions:
  762. self.store_transaction_metric(
  763. value, tags={"transaction": transaction}, timestamp=self.min_ago
  764. )
  765. self.create_team_membership(team, user=self.user)
  766. self.project.add_team(team)
  767. TeamKeyTransaction.objects.create(
  768. organization=self.organization,
  769. transaction=transaction,
  770. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  771. )
  772. query = {
  773. "team": "myteams",
  774. "project": [self.project.id],
  775. # use the order by to ensure the result order
  776. "orderby": "p95()",
  777. "field": [
  778. "team_key_transaction",
  779. "transaction",
  780. "transaction.status",
  781. "project",
  782. "epm()",
  783. "failure_rate()",
  784. "p95()",
  785. ],
  786. "per_page": 50,
  787. "dataset": "metricsEnhanced",
  788. }
  789. # key transactions
  790. query["query"] = "has:team_key_transaction"
  791. response = self.do_request(query)
  792. assert response.status_code == 200, response.content
  793. assert len(response.data["data"]) == 2
  794. data = response.data["data"]
  795. meta = response.data["meta"]
  796. field_meta = meta["fields"]
  797. assert data[0]["team_key_transaction"] == 1
  798. assert data[0]["transaction"] == "foo_transaction"
  799. assert data[1]["team_key_transaction"] == 1
  800. assert data[1]["transaction"] == "baz_transaction"
  801. assert meta["isMetricsData"]
  802. assert field_meta["team_key_transaction"] == "boolean"
  803. assert field_meta["transaction"] == "string"
  804. # key transactions
  805. query["query"] = "team_key_transaction:true"
  806. response = self.do_request(query)
  807. assert response.status_code == 200, response.content
  808. assert len(response.data["data"]) == 2
  809. data = response.data["data"]
  810. meta = response.data["meta"]
  811. field_meta = meta["fields"]
  812. assert data[0]["team_key_transaction"] == 1
  813. assert data[0]["transaction"] == "foo_transaction"
  814. assert data[1]["team_key_transaction"] == 1
  815. assert data[1]["transaction"] == "baz_transaction"
  816. assert meta["isMetricsData"]
  817. assert field_meta["team_key_transaction"] == "boolean"
  818. assert field_meta["transaction"] == "string"
  819. # not key transactions
  820. query["query"] = "!has:team_key_transaction"
  821. response = self.do_request(query)
  822. assert response.status_code == 200, response.content
  823. assert len(response.data["data"]) == 1
  824. data = response.data["data"]
  825. meta = response.data["meta"]
  826. field_meta = meta["fields"]
  827. assert data[0]["team_key_transaction"] == 0
  828. assert data[0]["transaction"] == "bar_transaction"
  829. assert meta["isMetricsData"]
  830. assert field_meta["team_key_transaction"] == "boolean"
  831. assert field_meta["transaction"] == "string"
  832. # not key transactions
  833. query["query"] = "team_key_transaction:false"
  834. response = self.do_request(query)
  835. assert response.status_code == 200, response.content
  836. assert len(response.data["data"]) == 1
  837. data = response.data["data"]
  838. meta = response.data["meta"]
  839. field_meta = meta["fields"]
  840. assert data[0]["team_key_transaction"] == 0
  841. assert data[0]["transaction"] == "bar_transaction"
  842. assert meta["isMetricsData"]
  843. assert field_meta["team_key_transaction"] == "boolean"
  844. assert field_meta["transaction"] == "string"
  845. def test_team_key_transaction_not_exists(self):
  846. team1 = self.create_team(organization=self.organization, name="Team A")
  847. team2 = self.create_team(organization=self.organization, name="Team B")
  848. key_transactions = [
  849. (team1, "foo_transaction", 1),
  850. (team2, "baz_transaction", 100),
  851. ]
  852. for team, transaction, value in key_transactions:
  853. self.store_transaction_metric(
  854. value, tags={"transaction": transaction}, timestamp=self.min_ago
  855. )
  856. self.create_team_membership(team, user=self.user)
  857. self.project.add_team(team)
  858. TeamKeyTransaction.objects.create(
  859. organization=self.organization,
  860. transaction=transaction,
  861. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  862. )
  863. # Don't create a metric for this one
  864. TeamKeyTransaction.objects.create(
  865. organization=self.organization,
  866. transaction="not_in_metrics",
  867. project_team=ProjectTeam.objects.get(project=self.project, team=team1),
  868. )
  869. query = {
  870. "team": "myteams",
  871. "project": [self.project.id],
  872. # use the order by to ensure the result order
  873. "orderby": "p95()",
  874. "field": [
  875. "team_key_transaction",
  876. "transaction",
  877. "transaction.status",
  878. "project",
  879. "epm()",
  880. "failure_rate()",
  881. "p95()",
  882. ],
  883. "per_page": 50,
  884. "dataset": "metricsEnhanced",
  885. }
  886. # key transactions
  887. query["query"] = "has:team_key_transaction"
  888. response = self.do_request(query)
  889. assert response.status_code == 200, response.content
  890. assert len(response.data["data"]) == 2
  891. data = response.data["data"]
  892. meta = response.data["meta"]
  893. field_meta = meta["fields"]
  894. assert data[0]["team_key_transaction"] == 1
  895. assert data[0]["transaction"] == "foo_transaction"
  896. assert data[1]["team_key_transaction"] == 1
  897. assert data[1]["transaction"] == "baz_transaction"
  898. assert meta["isMetricsData"]
  899. assert field_meta["team_key_transaction"] == "boolean"
  900. assert field_meta["transaction"] == "string"
  901. # key transactions
  902. query["query"] = "team_key_transaction:true"
  903. response = self.do_request(query)
  904. assert response.status_code == 200, response.content
  905. assert len(response.data["data"]) == 2
  906. data = response.data["data"]
  907. meta = response.data["meta"]
  908. field_meta = meta["fields"]
  909. assert data[0]["team_key_transaction"] == 1
  910. assert data[0]["transaction"] == "foo_transaction"
  911. assert data[1]["team_key_transaction"] == 1
  912. assert data[1]["transaction"] == "baz_transaction"
  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"] = "!has:team_key_transaction"
  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. # not key transactions
  928. query["query"] = "team_key_transaction:false"
  929. response = self.do_request(query)
  930. assert response.status_code == 200, response.content
  931. assert len(response.data["data"]) == 0
  932. data = response.data["data"]
  933. meta = response.data["meta"]
  934. field_meta = meta["fields"]
  935. assert meta["isMetricsData"]
  936. assert field_meta["team_key_transaction"] == "boolean"
  937. assert field_meta["transaction"] == "string"
  938. def test_too_many_team_key_transactions(self):
  939. MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS = 1
  940. with mock.patch(
  941. "sentry.search.events.fields.MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS",
  942. MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS,
  943. ):
  944. team = self.create_team(organization=self.organization, name="Team A")
  945. self.create_team_membership(team, user=self.user)
  946. self.project.add_team(team)
  947. project_team = ProjectTeam.objects.get(project=self.project, team=team)
  948. transactions = ["foo_transaction", "bar_transaction", "baz_transaction"]
  949. for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1):
  950. self.store_transaction_metric(
  951. 100, tags={"transaction": transactions[i]}, timestamp=self.min_ago
  952. )
  953. TeamKeyTransaction.objects.bulk_create(
  954. [
  955. TeamKeyTransaction(
  956. organization=self.organization,
  957. project_team=project_team,
  958. transaction=transactions[i],
  959. )
  960. for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1)
  961. ]
  962. )
  963. query = {
  964. "team": "myteams",
  965. "project": [self.project.id],
  966. "orderby": "p95()",
  967. "field": [
  968. "team_key_transaction",
  969. "transaction",
  970. "transaction.status",
  971. "project",
  972. "epm()",
  973. "failure_rate()",
  974. "p95()",
  975. ],
  976. "dataset": "metricsEnhanced",
  977. "per_page": 50,
  978. }
  979. response = self.do_request(query)
  980. assert response.status_code == 200, response.content
  981. assert len(response.data["data"]) == 2
  982. data = response.data["data"]
  983. meta = response.data["meta"]
  984. assert (
  985. sum(row["team_key_transaction"] for row in data)
  986. == MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS
  987. )
  988. assert meta["isMetricsData"]
  989. def test_measurement_rating(self):
  990. self.store_transaction_metric(
  991. 50,
  992. metric="measurements.lcp",
  993. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  994. timestamp=self.min_ago,
  995. )
  996. self.store_transaction_metric(
  997. 15,
  998. metric="measurements.fp",
  999. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  1000. timestamp=self.min_ago,
  1001. )
  1002. self.store_transaction_metric(
  1003. 1500,
  1004. metric="measurements.fcp",
  1005. tags={"measurement_rating": "meh", "transaction": "foo_transaction"},
  1006. timestamp=self.min_ago,
  1007. )
  1008. self.store_transaction_metric(
  1009. 125,
  1010. metric="measurements.fid",
  1011. tags={"measurement_rating": "meh", "transaction": "foo_transaction"},
  1012. timestamp=self.min_ago,
  1013. )
  1014. self.store_transaction_metric(
  1015. 0.15,
  1016. metric="measurements.cls",
  1017. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  1018. timestamp=self.min_ago,
  1019. )
  1020. response = self.do_request(
  1021. {
  1022. "field": [
  1023. "transaction",
  1024. "count_web_vitals(measurements.lcp, good)",
  1025. "count_web_vitals(measurements.fp, good)",
  1026. "count_web_vitals(measurements.fcp, meh)",
  1027. "count_web_vitals(measurements.fid, meh)",
  1028. "count_web_vitals(measurements.cls, good)",
  1029. "count_web_vitals(measurements.lcp, any)",
  1030. ],
  1031. "query": "event.type:transaction",
  1032. "dataset": "metricsEnhanced",
  1033. "per_page": 50,
  1034. }
  1035. )
  1036. assert response.status_code == 200, response.content
  1037. assert len(response.data["data"]) == 1
  1038. data = response.data["data"]
  1039. meta = response.data["meta"]
  1040. field_meta = meta["fields"]
  1041. assert data[0]["count_web_vitals(measurements.lcp, good)"] == 1
  1042. assert data[0]["count_web_vitals(measurements.fp, good)"] == 1
  1043. assert data[0]["count_web_vitals(measurements.fcp, meh)"] == 1
  1044. assert data[0]["count_web_vitals(measurements.fid, meh)"] == 1
  1045. assert data[0]["count_web_vitals(measurements.cls, good)"] == 1
  1046. assert data[0]["count_web_vitals(measurements.lcp, any)"] == 1
  1047. assert meta["isMetricsData"]
  1048. assert field_meta["count_web_vitals(measurements.lcp, good)"] == "integer"
  1049. assert field_meta["count_web_vitals(measurements.fp, good)"] == "integer"
  1050. assert field_meta["count_web_vitals(measurements.fcp, meh)"] == "integer"
  1051. assert field_meta["count_web_vitals(measurements.fid, meh)"] == "integer"
  1052. assert field_meta["count_web_vitals(measurements.cls, good)"] == "integer"
  1053. assert field_meta["count_web_vitals(measurements.lcp, any)"] == "integer"
  1054. def test_measurement_rating_that_does_not_exist(self):
  1055. self.store_transaction_metric(
  1056. 1,
  1057. metric="measurements.lcp",
  1058. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  1059. timestamp=self.min_ago,
  1060. )
  1061. response = self.do_request(
  1062. {
  1063. "field": ["transaction", "count_web_vitals(measurements.lcp, poor)"],
  1064. "query": "event.type:transaction",
  1065. "dataset": "metricsEnhanced",
  1066. "per_page": 50,
  1067. }
  1068. )
  1069. assert response.status_code == 200, response.content
  1070. assert len(response.data["data"]) == 1
  1071. data = response.data["data"]
  1072. meta = response.data["meta"]
  1073. assert data[0]["count_web_vitals(measurements.lcp, poor)"] == 0
  1074. assert meta["isMetricsData"]
  1075. assert meta["fields"]["count_web_vitals(measurements.lcp, poor)"] == "integer"
  1076. def test_count_web_vitals_invalid_vital(self):
  1077. query = {
  1078. "field": [
  1079. "count_web_vitals(measurements.foo, poor)",
  1080. ],
  1081. "project": [self.project.id],
  1082. "dataset": "metricsEnhanced",
  1083. }
  1084. response = self.do_request(query)
  1085. assert response.status_code == 400, response.content
  1086. query = {
  1087. "field": [
  1088. "count_web_vitals(tags[lcp], poor)",
  1089. ],
  1090. "project": [self.project.id],
  1091. "dataset": "metricsEnhanced",
  1092. }
  1093. response = self.do_request(query)
  1094. assert response.status_code == 400, response.content
  1095. query = {
  1096. "field": [
  1097. "count_web_vitals(transaction.duration, poor)",
  1098. ],
  1099. "project": [self.project.id],
  1100. "dataset": "metricsEnhanced",
  1101. }
  1102. response = self.do_request(query)
  1103. assert response.status_code == 400, response.content
  1104. query = {
  1105. "field": [
  1106. "count_web_vitals(measurements.lcp, bad)",
  1107. ],
  1108. "project": [self.project.id],
  1109. "dataset": "metricsEnhanced",
  1110. }
  1111. response = self.do_request(query)
  1112. assert response.status_code == 400, response.content
  1113. def test_count_unique_user_returns_zero(self):
  1114. self.store_transaction_metric(
  1115. 50,
  1116. metric="user",
  1117. tags={"transaction": "foo_transaction"},
  1118. timestamp=self.min_ago,
  1119. )
  1120. self.store_transaction_metric(
  1121. 50,
  1122. tags={"transaction": "foo_transaction"},
  1123. timestamp=self.min_ago,
  1124. )
  1125. self.store_transaction_metric(
  1126. 100,
  1127. tags={"transaction": "bar_transaction"},
  1128. timestamp=self.min_ago,
  1129. )
  1130. query = {
  1131. "project": [self.project.id],
  1132. "orderby": "p50()",
  1133. "field": [
  1134. "transaction",
  1135. "count_unique(user)",
  1136. "p50()",
  1137. ],
  1138. "dataset": "metricsEnhanced",
  1139. "per_page": 50,
  1140. }
  1141. response = self.do_request(query)
  1142. assert response.status_code == 200, response.content
  1143. assert len(response.data["data"]) == 2
  1144. data = response.data["data"]
  1145. meta = response.data["meta"]
  1146. assert data[0]["transaction"] == "foo_transaction"
  1147. assert data[0]["count_unique(user)"] == 1
  1148. assert data[1]["transaction"] == "bar_transaction"
  1149. assert data[1]["count_unique(user)"] == 0
  1150. assert meta["isMetricsData"]
  1151. def test_sum_transaction_duration(self):
  1152. self.store_transaction_metric(
  1153. 50,
  1154. tags={"transaction": "foo_transaction"},
  1155. timestamp=self.min_ago,
  1156. )
  1157. self.store_transaction_metric(
  1158. 100,
  1159. tags={"transaction": "foo_transaction"},
  1160. timestamp=self.min_ago,
  1161. )
  1162. self.store_transaction_metric(
  1163. 150,
  1164. tags={"transaction": "foo_transaction"},
  1165. timestamp=self.min_ago,
  1166. )
  1167. query = {
  1168. "project": [self.project.id],
  1169. "orderby": "sum(transaction.duration)",
  1170. "field": [
  1171. "transaction",
  1172. "sum(transaction.duration)",
  1173. ],
  1174. "dataset": "metricsEnhanced",
  1175. "per_page": 50,
  1176. }
  1177. response = self.do_request(query)
  1178. assert response.status_code == 200, response.content
  1179. assert len(response.data["data"]) == 1
  1180. data = response.data["data"]
  1181. meta = response.data["meta"]
  1182. assert data[0]["transaction"] == "foo_transaction"
  1183. assert data[0]["sum(transaction.duration)"] == 300
  1184. assert meta["isMetricsData"]
  1185. def test_custom_measurements_simple(self):
  1186. self.store_transaction_metric(
  1187. 1,
  1188. metric="measurements.something_custom",
  1189. internal_metric="d:transactions/measurements.something_custom@millisecond",
  1190. entity="metrics_distributions",
  1191. tags={"transaction": "foo_transaction"},
  1192. timestamp=self.min_ago,
  1193. )
  1194. query = {
  1195. "project": [self.project.id],
  1196. "orderby": "p50(measurements.something_custom)",
  1197. "field": [
  1198. "transaction",
  1199. "p50(measurements.something_custom)",
  1200. ],
  1201. "statsPeriod": "24h",
  1202. "dataset": "metricsEnhanced",
  1203. "per_page": 50,
  1204. }
  1205. self.wait_for_metric_count(
  1206. self.project,
  1207. 1,
  1208. metric="measurements.something_custom",
  1209. mri="d:transactions/measurements.something_custom@millisecond",
  1210. )
  1211. response = self.do_request(query)
  1212. assert response.status_code == 200, response.content
  1213. assert len(response.data["data"]) == 1
  1214. data = response.data["data"]
  1215. meta = response.data["meta"]
  1216. assert data[0]["transaction"] == "foo_transaction"
  1217. assert data[0]["p50(measurements.something_custom)"] == 1
  1218. assert meta["isMetricsData"]
  1219. assert meta["fields"]["p50(measurements.something_custom)"] == "duration"
  1220. assert meta["units"]["p50(measurements.something_custom)"] == "millisecond"
  1221. def test_custom_measurement_size_meta_type(self):
  1222. self.store_transaction_metric(
  1223. 100,
  1224. metric="measurements.custom_type",
  1225. internal_metric="d:transactions/measurements.custom_type@somethingcustom",
  1226. entity="metrics_distributions",
  1227. tags={"transaction": "foo_transaction"},
  1228. timestamp=self.min_ago,
  1229. )
  1230. self.store_transaction_metric(
  1231. 100,
  1232. metric="measurements.percent",
  1233. internal_metric="d:transactions/measurements.percent@ratio",
  1234. entity="metrics_distributions",
  1235. tags={"transaction": "foo_transaction"},
  1236. timestamp=self.min_ago,
  1237. )
  1238. self.store_transaction_metric(
  1239. 100,
  1240. metric="measurements.longtaskcount",
  1241. internal_metric="d:transactions/measurements.longtaskcount@none",
  1242. entity="metrics_distributions",
  1243. tags={"transaction": "foo_transaction"},
  1244. timestamp=self.min_ago,
  1245. )
  1246. query = {
  1247. "project": [self.project.id],
  1248. "orderby": "p50(measurements.longtaskcount)",
  1249. "field": [
  1250. "transaction",
  1251. "p50(measurements.longtaskcount)",
  1252. "p50(measurements.percent)",
  1253. "p50(measurements.custom_type)",
  1254. ],
  1255. "statsPeriod": "24h",
  1256. "dataset": "metricsEnhanced",
  1257. "per_page": 50,
  1258. }
  1259. response = self.do_request(query)
  1260. assert response.status_code == 200, response.content
  1261. assert len(response.data["data"]) == 1
  1262. data = response.data["data"]
  1263. meta = response.data["meta"]
  1264. assert data[0]["transaction"] == "foo_transaction"
  1265. assert data[0]["p50(measurements.longtaskcount)"] == 100
  1266. assert data[0]["p50(measurements.percent)"] == 100
  1267. assert data[0]["p50(measurements.custom_type)"] == 100
  1268. assert meta["isMetricsData"]
  1269. assert meta["fields"]["p50(measurements.longtaskcount)"] == "integer"
  1270. assert meta["units"]["p50(measurements.longtaskcount)"] is None
  1271. assert meta["fields"]["p50(measurements.percent)"] == "percentage"
  1272. assert meta["units"]["p50(measurements.percent)"] is None
  1273. assert meta["fields"]["p50(measurements.custom_type)"] == "number"
  1274. assert meta["units"]["p50(measurements.custom_type)"] is None
  1275. def test_custom_measurement_none_type(self):
  1276. self.store_transaction_metric(
  1277. 1,
  1278. metric="measurements.cls",
  1279. entity="metrics_distributions",
  1280. tags={"transaction": "foo_transaction"},
  1281. timestamp=self.min_ago,
  1282. )
  1283. query = {
  1284. "project": [self.project.id],
  1285. "orderby": "p75(measurements.cls)",
  1286. "field": [
  1287. "transaction",
  1288. "p75(measurements.cls)",
  1289. "p99(measurements.cls)",
  1290. "max(measurements.cls)",
  1291. ],
  1292. "statsPeriod": "24h",
  1293. "dataset": "metricsEnhanced",
  1294. "per_page": 50,
  1295. }
  1296. self.wait_for_metric_count(
  1297. self.project,
  1298. 1,
  1299. metric=TransactionMetricKey.MEASUREMENTS_CLS.value,
  1300. mri=TransactionMRI.MEASUREMENTS_CLS.value,
  1301. )
  1302. response = self.do_request(query)
  1303. assert response.status_code == 200, response.content
  1304. assert len(response.data["data"]) == 1
  1305. data = response.data["data"]
  1306. meta = response.data["meta"]
  1307. assert data[0]["transaction"] == "foo_transaction"
  1308. assert data[0]["p75(measurements.cls)"] == 1
  1309. assert data[0]["p99(measurements.cls)"] == 1
  1310. assert data[0]["max(measurements.cls)"] == 1
  1311. assert meta["isMetricsData"]
  1312. assert meta["fields"]["p75(measurements.cls)"] == "number"
  1313. assert meta["units"]["p75(measurements.cls)"] is None
  1314. assert meta["fields"]["p99(measurements.cls)"] == "number"
  1315. assert meta["units"]["p99(measurements.cls)"] is None
  1316. assert meta["fields"]["max(measurements.cls)"] == "number"
  1317. assert meta["units"]["max(measurements.cls)"] is None
  1318. def test_custom_measurement_duration_filtering(self):
  1319. self.store_transaction_metric(
  1320. 1,
  1321. metric="measurements.runtime",
  1322. internal_metric="d:transactions/measurements.runtime@hour",
  1323. entity="metrics_distributions",
  1324. tags={"transaction": "foo_transaction"},
  1325. timestamp=self.min_ago,
  1326. )
  1327. self.store_transaction_metric(
  1328. 180,
  1329. metric="measurements.runtime",
  1330. internal_metric="d:transactions/measurements.runtime@hour",
  1331. entity="metrics_distributions",
  1332. tags={"transaction": "bar_transaction"},
  1333. timestamp=self.min_ago,
  1334. )
  1335. query = {
  1336. "project": [self.project.id],
  1337. "field": [
  1338. "transaction",
  1339. "max(measurements.runtime)",
  1340. ],
  1341. "query": "p50(measurements.runtime):>1wk",
  1342. "statsPeriod": "24h",
  1343. "dataset": "metricsEnhanced",
  1344. "per_page": 50,
  1345. }
  1346. response = self.do_request(query)
  1347. assert response.status_code == 200, response.content
  1348. assert len(response.data["data"]) == 1
  1349. data = response.data["data"]
  1350. meta = response.data["meta"]
  1351. assert data[0]["transaction"] == "bar_transaction"
  1352. assert data[0]["max(measurements.runtime)"] == 180
  1353. assert meta["isMetricsData"]
  1354. def test_custom_measurement_size_filtering(self):
  1355. self.store_transaction_metric(
  1356. 1,
  1357. metric="measurements.datacenter_memory",
  1358. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1359. entity="metrics_distributions",
  1360. tags={"transaction": "foo_transaction"},
  1361. timestamp=self.min_ago,
  1362. )
  1363. self.store_transaction_metric(
  1364. 100,
  1365. metric="measurements.datacenter_memory",
  1366. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1367. entity="metrics_distributions",
  1368. tags={"transaction": "bar_transaction"},
  1369. timestamp=self.min_ago,
  1370. )
  1371. query = {
  1372. "project": [self.project.id],
  1373. "field": [
  1374. "transaction",
  1375. "max(measurements.datacenter_memory)",
  1376. ],
  1377. "query": "p50(measurements.datacenter_memory):>5pb",
  1378. "statsPeriod": "24h",
  1379. "dataset": "metricsEnhanced",
  1380. "per_page": 50,
  1381. }
  1382. response = self.do_request(query)
  1383. assert response.status_code == 200, response.content
  1384. assert len(response.data["data"]) == 1
  1385. data = response.data["data"]
  1386. meta = response.data["meta"]
  1387. assert data[0]["transaction"] == "bar_transaction"
  1388. assert data[0]["max(measurements.datacenter_memory)"] == 100
  1389. assert meta["units"]["max(measurements.datacenter_memory)"] == "petabyte"
  1390. assert meta["fields"]["max(measurements.datacenter_memory)"] == "size"
  1391. assert meta["isMetricsData"]
  1392. def test_has_custom_measurement(self):
  1393. self.store_transaction_metric(
  1394. 33,
  1395. metric="measurements.datacenter_memory",
  1396. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1397. entity="metrics_distributions",
  1398. tags={"transaction": "foo_transaction"},
  1399. timestamp=self.min_ago,
  1400. )
  1401. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1402. transaction_data["measurements"]["datacenter_memory"] = {
  1403. "value": 33,
  1404. "unit": "petabyte",
  1405. }
  1406. self.store_event(transaction_data, self.project.id)
  1407. measurement = "measurements.datacenter_memory"
  1408. response = self.do_request(
  1409. {
  1410. "field": ["transaction", measurement],
  1411. "query": "has:measurements.datacenter_memory",
  1412. "dataset": "discover",
  1413. }
  1414. )
  1415. assert response.status_code == 200, response.content
  1416. assert len(response.data["data"]) == 1
  1417. response = self.do_request(
  1418. {
  1419. "field": ["transaction", measurement],
  1420. "query": "!has:measurements.datacenter_memory",
  1421. "dataset": "discover",
  1422. }
  1423. )
  1424. assert response.status_code == 200, response.content
  1425. assert len(response.data["data"]) == 0
  1426. def test_environment_param(self):
  1427. self.create_environment(self.project, name="staging")
  1428. self.store_transaction_metric(
  1429. 1,
  1430. tags={"transaction": "foo_transaction", "environment": "staging"},
  1431. timestamp=self.min_ago,
  1432. )
  1433. self.store_transaction_metric(
  1434. 100,
  1435. tags={"transaction": "foo_transaction"},
  1436. timestamp=self.min_ago,
  1437. )
  1438. query = {
  1439. "project": [self.project.id],
  1440. "environment": "staging",
  1441. "orderby": "p50(transaction.duration)",
  1442. "field": [
  1443. "transaction",
  1444. "environment",
  1445. "p50(transaction.duration)",
  1446. ],
  1447. "statsPeriod": "24h",
  1448. "dataset": "metricsEnhanced",
  1449. "per_page": 50,
  1450. }
  1451. response = self.do_request(query)
  1452. assert response.status_code == 200, response.content
  1453. assert len(response.data["data"]) == 1
  1454. data = response.data["data"]
  1455. meta = response.data["meta"]
  1456. assert data[0]["transaction"] == "foo_transaction"
  1457. assert data[0]["environment"] == "staging"
  1458. assert data[0]["p50(transaction.duration)"] == 1
  1459. assert meta["isMetricsData"]
  1460. @pytest.mark.xfail(reason="Started failing on ClickHouse 21.8")
  1461. def test_environment_query(self):
  1462. self.create_environment(self.project, name="staging")
  1463. self.store_transaction_metric(
  1464. 1,
  1465. tags={"transaction": "foo_transaction", "environment": "staging"},
  1466. timestamp=self.min_ago,
  1467. )
  1468. self.store_transaction_metric(
  1469. 100,
  1470. tags={"transaction": "foo_transaction"},
  1471. timestamp=self.min_ago,
  1472. )
  1473. query = {
  1474. "project": [self.project.id],
  1475. "orderby": "p50(transaction.duration)",
  1476. "field": [
  1477. "transaction",
  1478. "environment",
  1479. "p50(transaction.duration)",
  1480. ],
  1481. "query": "!has:environment",
  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"]) == 1
  1490. data = response.data["data"]
  1491. meta = response.data["meta"]
  1492. assert data[0]["transaction"] == "foo_transaction"
  1493. assert data[0]["environment"] is None or data[0]["environment"] == ""
  1494. assert data[0]["p50(transaction.duration)"] == 100
  1495. assert meta["isMetricsData"]
  1496. def test_has_transaction(self):
  1497. self.store_transaction_metric(
  1498. 1,
  1499. tags={},
  1500. timestamp=self.min_ago,
  1501. )
  1502. self.store_transaction_metric(
  1503. 100,
  1504. tags={"transaction": "foo_transaction"},
  1505. timestamp=self.min_ago,
  1506. )
  1507. query = {
  1508. "project": [self.project.id],
  1509. "orderby": "p50(transaction.duration)",
  1510. "field": [
  1511. "transaction",
  1512. "p50(transaction.duration)",
  1513. ],
  1514. "query": "has:transaction",
  1515. "statsPeriod": "24h",
  1516. "dataset": "metricsEnhanced",
  1517. "per_page": 50,
  1518. }
  1519. self.wait_for_metric_count(self.project, 2)
  1520. response = self.do_request(query)
  1521. assert response.status_code == 200, response.content
  1522. assert len(response.data["data"]) == 2
  1523. data = response.data["data"]
  1524. meta = response.data["meta"]
  1525. assert data[0]["transaction"] == "<< unparameterized >>"
  1526. assert data[0]["p50(transaction.duration)"] == 1
  1527. assert data[1]["transaction"] == "foo_transaction"
  1528. assert data[1]["p50(transaction.duration)"] == 100
  1529. assert meta["isMetricsData"]
  1530. query = {
  1531. "project": [self.project.id],
  1532. "orderby": "p50(transaction.duration)",
  1533. "field": [
  1534. "transaction",
  1535. "p50(transaction.duration)",
  1536. ],
  1537. "query": "!has:transaction",
  1538. "statsPeriod": "24h",
  1539. "dataset": "metricsEnhanced",
  1540. "per_page": 50,
  1541. }
  1542. response = self.do_request(query)
  1543. assert response.status_code == 400, response.content
  1544. def test_apdex_transaction_threshold(self):
  1545. ProjectTransactionThresholdOverride.objects.create(
  1546. transaction="foo_transaction",
  1547. project=self.project,
  1548. organization=self.project.organization,
  1549. threshold=600,
  1550. metric=TransactionMetric.LCP.value,
  1551. )
  1552. ProjectTransactionThresholdOverride.objects.create(
  1553. transaction="bar_transaction",
  1554. project=self.project,
  1555. organization=self.project.organization,
  1556. threshold=600,
  1557. metric=TransactionMetric.LCP.value,
  1558. )
  1559. self.store_transaction_metric(
  1560. 1,
  1561. tags={
  1562. "transaction": "foo_transaction",
  1563. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1564. },
  1565. timestamp=self.min_ago,
  1566. )
  1567. self.store_transaction_metric(
  1568. 1,
  1569. "measurements.lcp",
  1570. tags={
  1571. "transaction": "bar_transaction",
  1572. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1573. },
  1574. timestamp=self.min_ago,
  1575. )
  1576. response = self.do_request(
  1577. {
  1578. "field": [
  1579. "transaction",
  1580. "apdex()",
  1581. ],
  1582. "orderby": ["apdex()"],
  1583. "query": "event.type:transaction",
  1584. "dataset": "metrics",
  1585. "per_page": 50,
  1586. }
  1587. )
  1588. assert len(response.data["data"]) == 2
  1589. data = response.data["data"]
  1590. meta = response.data["meta"]
  1591. field_meta = meta["fields"]
  1592. assert data[0]["transaction"] == "bar_transaction"
  1593. # Threshold is lcp based
  1594. assert data[0]["apdex()"] == 1
  1595. assert data[1]["transaction"] == "foo_transaction"
  1596. # Threshold is lcp based
  1597. assert data[1]["apdex()"] == 0
  1598. assert meta["isMetricsData"]
  1599. assert field_meta["transaction"] == "string"
  1600. assert field_meta["apdex()"] == "number"
  1601. def test_apdex_project_threshold(self):
  1602. ProjectTransactionThreshold.objects.create(
  1603. project=self.project,
  1604. organization=self.project.organization,
  1605. threshold=600,
  1606. metric=TransactionMetric.LCP.value,
  1607. )
  1608. self.store_transaction_metric(
  1609. 1,
  1610. tags={
  1611. "transaction": "foo_transaction",
  1612. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1613. },
  1614. timestamp=self.min_ago,
  1615. )
  1616. self.store_transaction_metric(
  1617. 1,
  1618. "measurements.lcp",
  1619. tags={
  1620. "transaction": "bar_transaction",
  1621. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1622. },
  1623. timestamp=self.min_ago,
  1624. )
  1625. response = self.do_request(
  1626. {
  1627. "field": [
  1628. "transaction",
  1629. "apdex()",
  1630. ],
  1631. "orderby": ["apdex()"],
  1632. "query": "event.type:transaction",
  1633. "dataset": "metrics",
  1634. "per_page": 50,
  1635. }
  1636. )
  1637. assert response.status_code == 200, response.content
  1638. assert len(response.data["data"]) == 2
  1639. data = response.data["data"]
  1640. meta = response.data["meta"]
  1641. field_meta = meta["fields"]
  1642. assert data[0]["transaction"] == "bar_transaction"
  1643. # Threshold is lcp based
  1644. assert data[0]["apdex()"] == 1
  1645. assert data[1]["transaction"] == "foo_transaction"
  1646. # Threshold is lcp based
  1647. assert data[1]["apdex()"] == 0
  1648. assert meta["isMetricsData"]
  1649. assert field_meta["transaction"] == "string"
  1650. assert field_meta["apdex()"] == "number"
  1651. def test_apdex_satisfaction_param(self):
  1652. for function in ["apdex(300)", "user_misery(300)", "count_miserable(user, 300)"]:
  1653. query = {
  1654. "project": [self.project.id],
  1655. "field": [
  1656. "transaction",
  1657. function,
  1658. ],
  1659. "statsPeriod": "24h",
  1660. "dataset": "metricsEnhanced",
  1661. "per_page": 50,
  1662. }
  1663. response = self.do_request(query)
  1664. assert response.status_code == 200, response.content
  1665. assert len(response.data["data"]) == 0
  1666. meta = response.data["meta"]
  1667. assert not meta["isMetricsData"], function
  1668. query = {
  1669. "project": [self.project.id],
  1670. "field": [
  1671. "transaction",
  1672. function,
  1673. ],
  1674. "statsPeriod": "24h",
  1675. "dataset": "metrics",
  1676. "per_page": 50,
  1677. }
  1678. response = self.do_request(query)
  1679. assert response.status_code == 400, function
  1680. assert b"threshold parameter" in response.content, function
  1681. def test_mobile_metrics(self):
  1682. self.store_transaction_metric(
  1683. 0.4,
  1684. "measurements.frames_frozen_rate",
  1685. tags={
  1686. "transaction": "bar_transaction",
  1687. },
  1688. timestamp=self.min_ago,
  1689. )
  1690. query = {
  1691. "project": [self.project.id],
  1692. "field": [
  1693. "transaction",
  1694. "p50(measurements.frames_frozen_rate)",
  1695. ],
  1696. "statsPeriod": "24h",
  1697. "dataset": "metrics",
  1698. "per_page": 50,
  1699. }
  1700. response = self.do_request(query)
  1701. assert response.status_code == 200, response.content
  1702. assert len(response.data["data"]) == 1
  1703. assert response.data["data"][0]["p50(measurements.frames_frozen_rate)"] == 0.4
  1704. def test_merge_null_unparam(self):
  1705. self.store_transaction_metric(
  1706. 1,
  1707. # Transaction: unparam
  1708. tags={
  1709. "transaction": "<< unparameterized >>",
  1710. },
  1711. timestamp=self.min_ago,
  1712. )
  1713. self.store_transaction_metric(
  1714. 2,
  1715. # Transaction:null
  1716. tags={},
  1717. timestamp=self.min_ago,
  1718. )
  1719. query = {
  1720. "project": [self.project.id],
  1721. "field": [
  1722. "transaction",
  1723. "p50(transaction.duration)",
  1724. ],
  1725. "statsPeriod": "24h",
  1726. "dataset": "metrics",
  1727. "per_page": 50,
  1728. }
  1729. response = self.do_request(query)
  1730. assert response.status_code == 200, response.content
  1731. assert len(response.data["data"]) == 1
  1732. assert response.data["data"][0]["p50(transaction.duration)"] == 1.5
  1733. def test_unparam_filter(self):
  1734. self.store_transaction_metric(
  1735. 1,
  1736. # Transaction: unparam
  1737. tags={
  1738. "transaction": "<< unparameterized >>",
  1739. },
  1740. timestamp=self.min_ago,
  1741. )
  1742. self.store_transaction_metric(
  1743. 2,
  1744. # Transaction:null
  1745. tags={},
  1746. timestamp=self.min_ago,
  1747. )
  1748. self.store_transaction_metric(
  1749. 3,
  1750. tags={
  1751. "transaction": "foo_transaction",
  1752. },
  1753. timestamp=self.min_ago,
  1754. )
  1755. query = {
  1756. "project": [self.project.id],
  1757. "field": [
  1758. "transaction",
  1759. "count()",
  1760. ],
  1761. "query": 'transaction:"<< unparameterized >>"',
  1762. "statsPeriod": "24h",
  1763. "dataset": "metrics",
  1764. "per_page": 50,
  1765. }
  1766. self.wait_for_metric_count(self.project, 3)
  1767. response = self.do_request(query)
  1768. assert response.status_code == 200, response.content
  1769. assert len(response.data["data"]) == 1
  1770. assert response.data["data"][0]["transaction"] == "<< unparameterized >>"
  1771. assert response.data["data"][0]["count()"] == 2
  1772. def test_custom_measurements_without_function(self):
  1773. self.store_transaction_metric(
  1774. 33,
  1775. metric="measurements.datacenter_memory",
  1776. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1777. entity="metrics_distributions",
  1778. tags={"transaction": "foo_transaction"},
  1779. timestamp=self.min_ago,
  1780. )
  1781. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1782. transaction_data["measurements"]["datacenter_memory"] = {
  1783. "value": 33,
  1784. "unit": "petabyte",
  1785. }
  1786. self.store_event(transaction_data, self.project.id)
  1787. measurement = "measurements.datacenter_memory"
  1788. response = self.do_request(
  1789. {
  1790. "field": ["transaction", measurement],
  1791. "query": "measurements.datacenter_memory:33pb",
  1792. "dataset": "discover",
  1793. }
  1794. )
  1795. assert response.status_code == 200, response.content
  1796. data = response.data["data"]
  1797. assert len(data) == 1
  1798. assert data[0][measurement] == 33
  1799. meta = response.data["meta"]
  1800. field_meta = meta["fields"]
  1801. unit_meta = meta["units"]
  1802. assert field_meta[measurement] == "size"
  1803. assert unit_meta[measurement] == "petabyte"
  1804. assert not meta["isMetricsData"]
  1805. def test_custom_measurements_with_function(self):
  1806. self.store_transaction_metric(
  1807. 33,
  1808. metric="measurements.datacenter_memory",
  1809. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1810. entity="metrics_distributions",
  1811. tags={"transaction": "foo_transaction"},
  1812. timestamp=self.min_ago,
  1813. )
  1814. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1815. transaction_data["measurements"]["datacenter_memory"] = {
  1816. "value": 33,
  1817. "unit": "petabyte",
  1818. }
  1819. self.store_event(transaction_data, self.project.id)
  1820. measurement = "p50(measurements.datacenter_memory)"
  1821. response = self.do_request(
  1822. {
  1823. "field": ["transaction", measurement],
  1824. "query": "measurements.datacenter_memory:33pb",
  1825. "dataset": "discover",
  1826. }
  1827. )
  1828. assert response.status_code == 200, response.content
  1829. data = response.data["data"]
  1830. assert len(data) == 1
  1831. assert data[0][measurement] == 33
  1832. meta = response.data["meta"]
  1833. field_meta = meta["fields"]
  1834. unit_meta = meta["units"]
  1835. assert field_meta[measurement] == "size"
  1836. assert unit_meta[measurement] == "petabyte"
  1837. assert not meta["isMetricsData"]
  1838. def test_custom_measurements_equation(self):
  1839. self.store_transaction_metric(
  1840. 33,
  1841. metric="measurements.datacenter_memory",
  1842. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1843. entity="metrics_distributions",
  1844. tags={"transaction": "foo_transaction"},
  1845. timestamp=self.min_ago,
  1846. )
  1847. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1848. transaction_data["measurements"]["datacenter_memory"] = {
  1849. "value": 33,
  1850. "unit": "petabyte",
  1851. }
  1852. self.store_event(transaction_data, self.project.id)
  1853. response = self.do_request(
  1854. {
  1855. "field": [
  1856. "transaction",
  1857. "measurements.datacenter_memory",
  1858. "equation|measurements.datacenter_memory / 3",
  1859. ],
  1860. "query": "",
  1861. "dataset": "discover",
  1862. }
  1863. )
  1864. assert response.status_code == 200, response.content
  1865. data = response.data["data"]
  1866. assert len(data) == 1
  1867. assert data[0]["measurements.datacenter_memory"] == 33
  1868. assert data[0]["equation|measurements.datacenter_memory / 3"] == 11
  1869. meta = response.data["meta"]
  1870. assert not meta["isMetricsData"]
  1871. def test_transaction_wildcard(self):
  1872. self.store_transaction_metric(
  1873. 1,
  1874. tags={"transaction": "foo_transaction"},
  1875. timestamp=self.min_ago,
  1876. )
  1877. self.store_transaction_metric(
  1878. 1,
  1879. tags={"transaction": "bar_transaction"},
  1880. timestamp=self.min_ago,
  1881. )
  1882. response = self.do_request(
  1883. {
  1884. "field": [
  1885. "transaction",
  1886. "p90()",
  1887. ],
  1888. "query": "transaction:foo*",
  1889. "dataset": "metrics",
  1890. }
  1891. )
  1892. assert response.status_code == 200, response.content
  1893. data = response.data["data"]
  1894. assert len(data) == 1
  1895. assert data[0]["p90()"] == 1
  1896. meta = response.data["meta"]
  1897. assert meta["isMetricsData"]
  1898. assert data[0]["transaction"] == "foo_transaction"
  1899. def test_transaction_status_wildcard(self):
  1900. self.store_transaction_metric(
  1901. 1,
  1902. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1903. timestamp=self.min_ago,
  1904. )
  1905. response = self.do_request(
  1906. {
  1907. "field": [
  1908. "transaction",
  1909. "p90()",
  1910. ],
  1911. "query": "transaction.status:f*bar",
  1912. "dataset": "metrics",
  1913. }
  1914. )
  1915. assert response.status_code == 200, response.content
  1916. data = response.data["data"]
  1917. assert len(data) == 1
  1918. assert data[0]["p90()"] == 1
  1919. meta = response.data["meta"]
  1920. assert meta["isMetricsData"]
  1921. def test_http_error_rate(self):
  1922. self.store_transaction_metric(
  1923. 1,
  1924. tags={
  1925. "transaction": "foo_transaction",
  1926. "transaction.status": "foobar",
  1927. "http.status_code": "500",
  1928. },
  1929. timestamp=self.min_ago,
  1930. )
  1931. self.store_transaction_metric(
  1932. 1,
  1933. tags={"transaction": "bar_transaction", "http.status_code": "400"},
  1934. timestamp=self.min_ago,
  1935. )
  1936. response = self.do_request(
  1937. {
  1938. "field": [
  1939. "http_error_rate()",
  1940. ],
  1941. "dataset": "metrics",
  1942. }
  1943. )
  1944. assert response.status_code == 200, response.content
  1945. data = response.data["data"]
  1946. assert len(data) == 1
  1947. assert data[0]["http_error_rate()"] == 0.5
  1948. meta = response.data["meta"]
  1949. assert meta["isMetricsData"]
  1950. def test_time_spent(self):
  1951. self.store_transaction_metric(
  1952. 1,
  1953. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1954. timestamp=self.min_ago,
  1955. )
  1956. self.store_transaction_metric(
  1957. 1,
  1958. tags={"transaction": "bar_transaction"},
  1959. timestamp=self.min_ago,
  1960. )
  1961. response = self.do_request(
  1962. {
  1963. "field": [
  1964. "transaction",
  1965. "time_spent_percentage()",
  1966. ],
  1967. "dataset": "metrics",
  1968. }
  1969. )
  1970. assert response.status_code == 200, response.content
  1971. data = response.data["data"]
  1972. assert len(data) == 2
  1973. assert data[0]["time_spent_percentage()"] == 0.5
  1974. meta = response.data["meta"]
  1975. assert meta["isMetricsData"]
  1976. def test_has_filter(self):
  1977. self.store_transaction_metric(
  1978. 1,
  1979. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1980. timestamp=self.min_ago,
  1981. )
  1982. response = self.do_request(
  1983. {
  1984. "field": [
  1985. "transaction",
  1986. "p50()",
  1987. ],
  1988. # For the metrics dataset, has on metrics should be no-ops
  1989. "query": "has:measurements.frames_frozen_rate",
  1990. "dataset": "metrics",
  1991. }
  1992. )
  1993. assert response.status_code == 200, response.content
  1994. data = response.data["data"]
  1995. assert len(data) == 1
  1996. assert data[0]["p50()"] == 1
  1997. meta = response.data["meta"]
  1998. assert meta["isMetricsData"]
  1999. response = self.do_request(
  2000. {
  2001. "field": [
  2002. "transaction",
  2003. "p50()",
  2004. ],
  2005. "query": "has:transaction.status",
  2006. "dataset": "metrics",
  2007. }
  2008. )
  2009. assert response.status_code == 200, response.content
  2010. data = response.data["data"]
  2011. assert len(data) == 1
  2012. assert data[0]["p50()"] == 1
  2013. meta = response.data["meta"]
  2014. assert meta["isMetricsData"]
  2015. def test_not_has_filter(self):
  2016. self.store_transaction_metric(
  2017. 1,
  2018. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  2019. timestamp=self.min_ago,
  2020. )
  2021. response = self.do_request(
  2022. {
  2023. "field": [
  2024. "transaction",
  2025. "p50()",
  2026. ],
  2027. "query": "!has:transaction.status",
  2028. "dataset": "metrics",
  2029. }
  2030. )
  2031. assert response.status_code == 200, response.content
  2032. data = response.data["data"]
  2033. assert len(data) == 0
  2034. meta = response.data["meta"]
  2035. assert meta["isMetricsData"]
  2036. response = self.do_request(
  2037. {
  2038. "field": [
  2039. "transaction",
  2040. "p50()",
  2041. ],
  2042. # Doing !has on the metrics dataset doesn't really make sense
  2043. "query": "!has:measurements.frames_frozen_rate",
  2044. "dataset": "metrics",
  2045. }
  2046. )
  2047. assert response.status_code == 400, response.content
  2048. def test_p50_with_count(self):
  2049. """Implicitly test the fact that percentiles are their own 'dataset'"""
  2050. self.store_transaction_metric(
  2051. 1,
  2052. tags={"transaction": "foo_transaction"},
  2053. timestamp=self.min_ago,
  2054. )
  2055. response = self.do_request(
  2056. {
  2057. "field": ["title", "p50()", "count()"],
  2058. "query": "event.type:transaction",
  2059. "dataset": "metrics",
  2060. "project": self.project.id,
  2061. "per_page": 50,
  2062. }
  2063. )
  2064. assert response.status_code == 200, response.content
  2065. assert len(response.data["data"]) == 1
  2066. data = response.data["data"]
  2067. meta = response.data["meta"]
  2068. field_meta = meta["fields"]
  2069. assert data[0]["title"] == "foo_transaction"
  2070. assert data[0]["p50()"] == 1
  2071. assert data[0]["count()"] == 1
  2072. assert meta["isMetricsData"]
  2073. assert field_meta["title"] == "string"
  2074. assert field_meta["p50()"] == "duration"
  2075. assert field_meta["count()"] == "integer"
  2076. def test_p75_with_count_and_more_groupby(self):
  2077. """Implicitly test the fact that percentiles are their own 'dataset'"""
  2078. self.store_transaction_metric(
  2079. 1,
  2080. tags={"transaction": "foo_transaction"},
  2081. timestamp=self.min_ago,
  2082. )
  2083. self.store_transaction_metric(
  2084. 5,
  2085. tags={"transaction": "bar_transaction"},
  2086. timestamp=self.min_ago,
  2087. )
  2088. self.store_transaction_metric(
  2089. 5,
  2090. tags={"transaction": "bar_transaction"},
  2091. timestamp=self.min_ago,
  2092. )
  2093. response = self.do_request(
  2094. {
  2095. "field": [
  2096. "title",
  2097. "project",
  2098. "p75()",
  2099. "count()",
  2100. ],
  2101. "query": "event.type:transaction",
  2102. "orderby": "count()",
  2103. "dataset": "metrics",
  2104. "project": self.project.id,
  2105. "per_page": 50,
  2106. }
  2107. )
  2108. assert response.status_code == 200, response.content
  2109. assert len(response.data["data"]) == 2
  2110. data = response.data["data"]
  2111. meta = response.data["meta"]
  2112. field_meta = meta["fields"]
  2113. assert data[0]["title"] == "foo_transaction"
  2114. assert data[0]["p75()"] == 1
  2115. assert data[0]["count()"] == 1
  2116. assert data[1]["title"] == "bar_transaction"
  2117. assert data[1]["p75()"] == 5
  2118. assert data[1]["count()"] == 2
  2119. assert meta["isMetricsData"]
  2120. assert field_meta["title"] == "string"
  2121. assert field_meta["p75()"] == "duration"
  2122. assert field_meta["count()"] == "integer"
  2123. def test_title_and_transaction_alias(self):
  2124. # Title and transaction are aliases to the same column
  2125. self.store_transaction_metric(
  2126. 1,
  2127. tags={"transaction": "foo_transaction"},
  2128. timestamp=self.min_ago,
  2129. )
  2130. response = self.do_request(
  2131. {
  2132. "field": [
  2133. "title",
  2134. "transaction",
  2135. "p75()",
  2136. ],
  2137. "query": "event.type:transaction",
  2138. "orderby": "p75()",
  2139. "dataset": "metrics",
  2140. "project": self.project.id,
  2141. "per_page": 50,
  2142. }
  2143. )
  2144. assert response.status_code == 200, response.content
  2145. assert len(response.data["data"]) == 1
  2146. data = response.data["data"]
  2147. meta = response.data["meta"]
  2148. field_meta = meta["fields"]
  2149. assert data[0]["title"] == "foo_transaction"
  2150. assert data[0]["transaction"] == "foo_transaction"
  2151. assert data[0]["p75()"] == 1
  2152. assert meta["isMetricsData"]
  2153. assert field_meta["title"] == "string"
  2154. assert field_meta["transaction"] == "string"
  2155. assert field_meta["p75()"] == "duration"
  2156. def test_maintain_sort_order_across_datasets(self):
  2157. self.store_transaction_metric(
  2158. 1,
  2159. tags={"transaction": "foo_transaction"},
  2160. timestamp=self.min_ago,
  2161. )
  2162. self.store_transaction_metric(
  2163. 1,
  2164. metric="user",
  2165. tags={"transaction": "bar_transaction"},
  2166. timestamp=self.min_ago,
  2167. )
  2168. self.store_transaction_metric(
  2169. 5,
  2170. tags={"transaction": "bar_transaction"},
  2171. timestamp=self.min_ago,
  2172. )
  2173. self.store_transaction_metric(
  2174. 5,
  2175. tags={"transaction": "bar_transaction"},
  2176. timestamp=self.min_ago,
  2177. )
  2178. response = self.do_request(
  2179. {
  2180. "field": [
  2181. "title",
  2182. "project",
  2183. "count()",
  2184. "count_unique(user)",
  2185. ],
  2186. "query": "event.type:transaction",
  2187. "orderby": "count()",
  2188. "dataset": "metrics",
  2189. "project": self.project.id,
  2190. "per_page": 50,
  2191. }
  2192. )
  2193. assert response.status_code == 200, response.content
  2194. data = response.data["data"]
  2195. meta = response.data["meta"]
  2196. field_meta = meta["fields"]
  2197. assert len(data) == 2
  2198. assert data[0]["title"] == "foo_transaction"
  2199. assert data[0]["count()"] == 1
  2200. assert data[0]["count_unique(user)"] == 0
  2201. assert data[1]["title"] == "bar_transaction"
  2202. assert data[1]["count()"] == 2
  2203. assert data[1]["count_unique(user)"] == 1
  2204. assert meta["isMetricsData"]
  2205. assert field_meta["title"] == "string"
  2206. assert field_meta["count()"] == "integer"
  2207. assert field_meta["count_unique(user)"] == "integer"
  2208. def test_avg_compare(self):
  2209. self.store_transaction_metric(
  2210. 100,
  2211. timestamp=self.min_ago,
  2212. tags={"release": "foo"},
  2213. )
  2214. self.store_transaction_metric(
  2215. 10,
  2216. timestamp=self.min_ago,
  2217. tags={"release": "bar"},
  2218. )
  2219. for function_name in [
  2220. "avg_compare(transaction.duration, release, foo, bar)",
  2221. 'avg_compare(transaction.duration, release, "foo", "bar")',
  2222. ]:
  2223. response = self.do_request(
  2224. {
  2225. "field": [function_name],
  2226. "query": "",
  2227. "project": self.project.id,
  2228. "dataset": "metrics",
  2229. }
  2230. )
  2231. assert response.status_code == 200, response.content
  2232. data = response.data["data"]
  2233. meta = response.data["meta"]
  2234. assert len(data) == 1
  2235. assert data[0][function_name] == -0.9
  2236. assert meta["dataset"] == "metrics"
  2237. assert meta["fields"][function_name] == "percent_change"
  2238. def test_avg_if(self):
  2239. self.store_transaction_metric(
  2240. 100,
  2241. timestamp=self.min_ago,
  2242. tags={"release": "foo"},
  2243. )
  2244. self.store_transaction_metric(
  2245. 200,
  2246. timestamp=self.min_ago,
  2247. tags={"release": "foo"},
  2248. )
  2249. self.store_transaction_metric(
  2250. 10,
  2251. timestamp=self.min_ago,
  2252. tags={"release": "bar"},
  2253. )
  2254. response = self.do_request(
  2255. {
  2256. "field": ["avg_if(transaction.duration, release, foo)"],
  2257. "query": "",
  2258. "project": self.project.id,
  2259. "dataset": "metrics",
  2260. }
  2261. )
  2262. assert response.status_code == 200, response.content
  2263. data = response.data["data"]
  2264. meta = response.data["meta"]
  2265. assert len(data) == 1
  2266. assert data[0]["avg_if(transaction.duration, release, foo)"] == 150
  2267. assert meta["dataset"] == "metrics"
  2268. assert meta["fields"]["avg_if(transaction.duration, release, foo)"] == "duration"
  2269. def test_count_if(self):
  2270. self.store_transaction_metric(
  2271. 100,
  2272. timestamp=self.min_ago,
  2273. tags={"release": "1.0.0"},
  2274. )
  2275. self.store_transaction_metric(
  2276. 200,
  2277. timestamp=self.min_ago,
  2278. tags={"release": "1.0.0"},
  2279. )
  2280. self.store_transaction_metric(
  2281. 10,
  2282. timestamp=self.min_ago,
  2283. tags={"release": "2.0.0"},
  2284. )
  2285. countIfRelease1 = "count_if(transaction.duration,release,1.0.0)"
  2286. countIfRelease2 = "count_if(transaction.duration,release,2.0.0)"
  2287. response = self.do_request(
  2288. {
  2289. "field": [
  2290. countIfRelease1,
  2291. countIfRelease2,
  2292. ],
  2293. "query": "",
  2294. "project": self.project.id,
  2295. "dataset": "metrics",
  2296. }
  2297. )
  2298. assert response.status_code == 200, response.content
  2299. data = response.data["data"]
  2300. meta = response.data["meta"]
  2301. assert len(data) == 1
  2302. assert data[0][countIfRelease1] == 2
  2303. assert data[0][countIfRelease2] == 1
  2304. assert meta["dataset"] == "metrics"
  2305. assert meta["fields"][countIfRelease1] == "integer"
  2306. assert meta["fields"][countIfRelease2] == "integer"
  2307. def test_device_class(self):
  2308. self.store_transaction_metric(
  2309. 100,
  2310. timestamp=self.min_ago,
  2311. tags={"device.class": "1"},
  2312. )
  2313. self.store_transaction_metric(
  2314. 200,
  2315. timestamp=self.min_ago,
  2316. tags={"device.class": "2"},
  2317. )
  2318. self.store_transaction_metric(
  2319. 300,
  2320. timestamp=self.min_ago,
  2321. tags={"device.class": ""},
  2322. )
  2323. response = self.do_request(
  2324. {
  2325. "field": ["device.class", "p95()"],
  2326. "query": "",
  2327. "orderby": "p95()",
  2328. "project": self.project.id,
  2329. "dataset": "metrics",
  2330. }
  2331. )
  2332. assert response.status_code == 200, response.content
  2333. data = response.data["data"]
  2334. meta = response.data["meta"]
  2335. assert len(data) == 3
  2336. # Need to actually check the dict since the level for 1 isn't guaranteed to stay `low` or `medium`
  2337. assert data[0]["device.class"] == map_device_class_level("1")
  2338. assert data[1]["device.class"] == map_device_class_level("2")
  2339. assert data[2]["device.class"] == "Unknown"
  2340. assert meta["fields"]["device.class"] == "string"
  2341. def test_device_class_filter(self):
  2342. self.store_transaction_metric(
  2343. 300,
  2344. timestamp=self.min_ago,
  2345. tags={"device.class": "1"},
  2346. )
  2347. # Need to actually check the dict since the level for 1 isn't guaranteed to stay `low`
  2348. level = map_device_class_level("1")
  2349. response = self.do_request(
  2350. {
  2351. "field": ["device.class", "count()"],
  2352. "query": f"device.class:{level}",
  2353. "orderby": "count()",
  2354. "project": self.project.id,
  2355. "dataset": "metrics",
  2356. }
  2357. )
  2358. assert response.status_code == 200, response.content
  2359. data = response.data["data"]
  2360. meta = response.data["meta"]
  2361. assert len(data) == 1
  2362. assert data[0]["device.class"] == level
  2363. assert meta["fields"]["device.class"] == "string"
  2364. def test_performance_score(self):
  2365. self.store_transaction_metric(
  2366. 0.03,
  2367. metric="measurements.score.ttfb",
  2368. tags={"transaction": "foo_transaction"},
  2369. timestamp=self.min_ago,
  2370. )
  2371. self.store_transaction_metric(
  2372. 0.30,
  2373. metric="measurements.score.weight.ttfb",
  2374. tags={"transaction": "foo_transaction"},
  2375. timestamp=self.min_ago,
  2376. )
  2377. self.store_transaction_metric(
  2378. 0.35,
  2379. metric="measurements.score.fcp",
  2380. tags={"transaction": "foo_transaction"},
  2381. timestamp=self.min_ago,
  2382. )
  2383. self.store_transaction_metric(
  2384. 0.70,
  2385. metric="measurements.score.weight.fcp",
  2386. tags={"transaction": "foo_transaction"},
  2387. timestamp=self.min_ago,
  2388. )
  2389. self.store_transaction_metric(
  2390. 0.38,
  2391. metric="measurements.score.total",
  2392. tags={"transaction": "foo_transaction"},
  2393. timestamp=self.min_ago,
  2394. )
  2395. self.store_transaction_metric(
  2396. 1.00,
  2397. metric="measurements.score.ttfb",
  2398. tags={"transaction": "foo_transaction"},
  2399. timestamp=self.min_ago,
  2400. )
  2401. self.store_transaction_metric(
  2402. 1.00,
  2403. metric="measurements.score.weight.ttfb",
  2404. tags={"transaction": "foo_transaction"},
  2405. timestamp=self.min_ago,
  2406. )
  2407. self.store_transaction_metric(
  2408. 1.00,
  2409. metric="measurements.score.fid",
  2410. tags={"transaction": "foo_transaction"},
  2411. timestamp=self.min_ago,
  2412. )
  2413. # These fid and ttfb scenarios shouldn't really be happening, but we can test them anyways
  2414. self.store_transaction_metric(
  2415. 0.00,
  2416. metric="measurements.score.weight.fid",
  2417. tags={"transaction": "foo_transaction"},
  2418. timestamp=self.min_ago,
  2419. )
  2420. self.store_transaction_metric(
  2421. 0.00,
  2422. metric="measurements.score.ttfb",
  2423. tags={"transaction": "foo_transaction"},
  2424. timestamp=self.min_ago,
  2425. )
  2426. self.store_transaction_metric(
  2427. 0.00,
  2428. metric="measurements.score.weight.ttfb",
  2429. tags={"transaction": "foo_transaction"},
  2430. timestamp=self.min_ago,
  2431. )
  2432. self.store_transaction_metric(
  2433. 1.00,
  2434. metric="measurements.score.total",
  2435. tags={"transaction": "foo_transaction"},
  2436. timestamp=self.min_ago,
  2437. )
  2438. # INP metrics
  2439. self.store_transaction_metric(
  2440. 0.80,
  2441. metric="measurements.score.inp",
  2442. tags={"transaction": "foo_transaction"},
  2443. timestamp=self.min_ago,
  2444. )
  2445. self.store_transaction_metric(
  2446. 1.00,
  2447. metric="measurements.score.weight.inp",
  2448. tags={"transaction": "foo_transaction"},
  2449. timestamp=self.min_ago,
  2450. )
  2451. self.store_transaction_metric(
  2452. 0.80,
  2453. metric="measurements.score.total",
  2454. tags={"transaction": "foo_transaction"},
  2455. timestamp=self.min_ago,
  2456. )
  2457. response = self.do_request(
  2458. {
  2459. "field": [
  2460. "transaction",
  2461. "performance_score(measurements.score.fcp)",
  2462. "performance_score(measurements.score.fid)",
  2463. "performance_score(measurements.score.ttfb)",
  2464. "performance_score(measurements.score.inp)",
  2465. ],
  2466. "query": "event.type:transaction",
  2467. "dataset": "metrics",
  2468. "per_page": 50,
  2469. }
  2470. )
  2471. assert response.status_code == 200, response.content
  2472. assert len(response.data["data"]) == 1
  2473. data = response.data["data"]
  2474. meta = response.data["meta"]
  2475. field_meta = meta["fields"]
  2476. assert data[0]["performance_score(measurements.score.fcp)"] == 0.5
  2477. assert data[0]["performance_score(measurements.score.fid)"] == 0
  2478. assert data[0]["performance_score(measurements.score.ttfb)"] == 0.7923076923076923
  2479. assert data[0]["performance_score(measurements.score.inp)"] == 0.8
  2480. assert meta["isMetricsData"]
  2481. assert field_meta["performance_score(measurements.score.ttfb)"] == "number"
  2482. def test_performance_score_boundaries(self):
  2483. # Scores shouldn't exceed 1 or go below 0, but we can test these boundaries anyways
  2484. self.store_transaction_metric(
  2485. 0.65,
  2486. metric="measurements.score.ttfb",
  2487. tags={"transaction": "foo_transaction"},
  2488. timestamp=self.min_ago,
  2489. )
  2490. self.store_transaction_metric(
  2491. 0.30,
  2492. metric="measurements.score.weight.ttfb",
  2493. tags={"transaction": "foo_transaction"},
  2494. timestamp=self.min_ago,
  2495. )
  2496. self.store_transaction_metric(
  2497. -0.35,
  2498. metric="measurements.score.fcp",
  2499. tags={"transaction": "foo_transaction"},
  2500. timestamp=self.min_ago,
  2501. )
  2502. self.store_transaction_metric(
  2503. 0.70,
  2504. metric="measurements.score.weight.fcp",
  2505. tags={"transaction": "foo_transaction"},
  2506. timestamp=self.min_ago,
  2507. )
  2508. self.store_transaction_metric(
  2509. 0.3,
  2510. metric="measurements.score.total",
  2511. tags={"transaction": "foo_transaction"},
  2512. timestamp=self.min_ago,
  2513. )
  2514. response = self.do_request(
  2515. {
  2516. "field": [
  2517. "transaction",
  2518. "performance_score(measurements.score.ttfb)",
  2519. "performance_score(measurements.score.fcp)",
  2520. ],
  2521. "query": "event.type:transaction",
  2522. "dataset": "metrics",
  2523. "per_page": 50,
  2524. }
  2525. )
  2526. assert response.status_code == 200, response.content
  2527. assert len(response.data["data"]) == 1
  2528. data = response.data["data"]
  2529. meta = response.data["meta"]
  2530. field_meta = meta["fields"]
  2531. assert data[0]["performance_score(measurements.score.ttfb)"] == 1.0
  2532. assert data[0]["performance_score(measurements.score.fcp)"] == 0.0
  2533. assert meta["isMetricsData"]
  2534. assert field_meta["performance_score(measurements.score.ttfb)"] == "number"
  2535. def test_weighted_performance_score(self):
  2536. self.store_transaction_metric(
  2537. 0.03,
  2538. metric="measurements.score.ttfb",
  2539. tags={"transaction": "foo_transaction"},
  2540. timestamp=self.min_ago,
  2541. )
  2542. self.store_transaction_metric(
  2543. 0.30,
  2544. metric="measurements.score.weight.ttfb",
  2545. tags={"transaction": "foo_transaction"},
  2546. timestamp=self.min_ago,
  2547. )
  2548. self.store_transaction_metric(
  2549. 0.03,
  2550. metric="measurements.score.total",
  2551. tags={"transaction": "foo_transaction"},
  2552. timestamp=self.min_ago,
  2553. )
  2554. self.store_transaction_metric(
  2555. 1.00,
  2556. metric="measurements.score.ttfb",
  2557. tags={"transaction": "foo_transaction"},
  2558. timestamp=self.min_ago,
  2559. )
  2560. self.store_transaction_metric(
  2561. 1.00,
  2562. metric="measurements.score.weight.ttfb",
  2563. tags={"transaction": "foo_transaction"},
  2564. timestamp=self.min_ago,
  2565. )
  2566. self.store_transaction_metric(
  2567. 1.00,
  2568. metric="measurements.score.total",
  2569. tags={"transaction": "foo_transaction"},
  2570. timestamp=self.min_ago,
  2571. )
  2572. self.store_transaction_metric(
  2573. 0.00,
  2574. metric="measurements.score.total",
  2575. tags={"transaction": "foo_transaction"},
  2576. timestamp=self.min_ago,
  2577. )
  2578. self.store_transaction_metric(
  2579. 0.80,
  2580. metric="measurements.score.inp",
  2581. tags={"transaction": "foo_transaction"},
  2582. timestamp=self.min_ago,
  2583. )
  2584. self.store_transaction_metric(
  2585. 1.00,
  2586. metric="measurements.score.weight.inp",
  2587. tags={"transaction": "foo_transaction"},
  2588. timestamp=self.min_ago,
  2589. )
  2590. self.store_transaction_metric(
  2591. 0.80,
  2592. metric="measurements.score.total",
  2593. tags={"transaction": "foo_transaction"},
  2594. timestamp=self.min_ago,
  2595. )
  2596. with self.feature({"organizations:starfish-browser-webvitals-score-computed-total": True}):
  2597. response = self.do_request(
  2598. {
  2599. "field": [
  2600. "transaction",
  2601. "weighted_performance_score(measurements.score.ttfb)",
  2602. "weighted_performance_score(measurements.score.inp)",
  2603. ],
  2604. "query": "event.type:transaction",
  2605. "dataset": "metrics",
  2606. "per_page": 50,
  2607. }
  2608. )
  2609. assert response.status_code == 200, response.content
  2610. assert len(response.data["data"]) == 1
  2611. data = response.data["data"]
  2612. meta = response.data["meta"]
  2613. field_meta = meta["fields"]
  2614. assert data[0]["weighted_performance_score(measurements.score.ttfb)"] == 0.3433333333333333
  2615. assert data[0]["weighted_performance_score(measurements.score.inp)"] == 0.26666666666666666
  2616. assert meta["isMetricsData"]
  2617. assert field_meta["weighted_performance_score(measurements.score.ttfb)"] == "number"
  2618. def test_invalid_performance_score_column(self):
  2619. self.store_transaction_metric(
  2620. 0.03,
  2621. metric="measurements.score.total",
  2622. tags={"transaction": "foo_transaction"},
  2623. timestamp=self.min_ago,
  2624. )
  2625. response = self.do_request(
  2626. {
  2627. "field": [
  2628. "transaction",
  2629. "performance_score(measurements.score.fp)",
  2630. ],
  2631. "query": "event.type:transaction",
  2632. "dataset": "metrics",
  2633. "per_page": 50,
  2634. }
  2635. )
  2636. assert response.status_code == 400, response.content
  2637. def test_invalid_weighted_performance_score_column(self):
  2638. self.store_transaction_metric(
  2639. 0.03,
  2640. metric="measurements.score.total",
  2641. tags={"transaction": "foo_transaction"},
  2642. timestamp=self.min_ago,
  2643. )
  2644. response = self.do_request(
  2645. {
  2646. "field": [
  2647. "transaction",
  2648. "weighted_performance_score(measurements.score.fp)",
  2649. ],
  2650. "query": "event.type:transaction",
  2651. "dataset": "metrics",
  2652. "per_page": 50,
  2653. }
  2654. )
  2655. assert response.status_code == 400, response.content
  2656. def test_no_weighted_performance_score_column(self):
  2657. self.store_transaction_metric(
  2658. 0.0,
  2659. metric="measurements.score.ttfb",
  2660. tags={"transaction": "foo_transaction"},
  2661. timestamp=self.min_ago,
  2662. )
  2663. response = self.do_request(
  2664. {
  2665. "field": [
  2666. "transaction",
  2667. "weighted_performance_score(measurements.score.ttfb)",
  2668. ],
  2669. "query": "event.type:transaction",
  2670. "dataset": "metrics",
  2671. "per_page": 50,
  2672. }
  2673. )
  2674. assert response.status_code == 200, response.content
  2675. assert len(response.data["data"]) == 1
  2676. data = response.data["data"]
  2677. meta = response.data["meta"]
  2678. field_meta = meta["fields"]
  2679. assert data[0]["weighted_performance_score(measurements.score.ttfb)"] == 0.0
  2680. assert meta["isMetricsData"]
  2681. assert field_meta["weighted_performance_score(measurements.score.ttfb)"] == "number"
  2682. def test_opportunity_score(self):
  2683. self.store_transaction_metric(
  2684. 0.03,
  2685. metric="measurements.score.ttfb",
  2686. tags={"transaction": "foo_transaction"},
  2687. timestamp=self.min_ago,
  2688. )
  2689. self.store_transaction_metric(
  2690. 0.30,
  2691. metric="measurements.score.weight.ttfb",
  2692. tags={"transaction": "foo_transaction"},
  2693. timestamp=self.min_ago,
  2694. )
  2695. self.store_transaction_metric(
  2696. 0.40,
  2697. metric="measurements.score.fcp",
  2698. tags={"transaction": "foo_transaction"},
  2699. timestamp=self.min_ago,
  2700. )
  2701. self.store_transaction_metric(
  2702. 0.70,
  2703. metric="measurements.score.weight.fcp",
  2704. tags={"transaction": "foo_transaction"},
  2705. timestamp=self.min_ago,
  2706. )
  2707. self.store_transaction_metric(
  2708. 0.43,
  2709. metric="measurements.score.total",
  2710. tags={"transaction": "foo_transaction"},
  2711. timestamp=self.min_ago,
  2712. )
  2713. self.store_transaction_metric(
  2714. 1.0,
  2715. metric="measurements.score.ttfb",
  2716. tags={"transaction": "foo_transaction"},
  2717. timestamp=self.min_ago,
  2718. )
  2719. self.store_transaction_metric(
  2720. 1.0,
  2721. metric="measurements.score.weight.ttfb",
  2722. tags={"transaction": "foo_transaction"},
  2723. timestamp=self.min_ago,
  2724. )
  2725. self.store_transaction_metric(
  2726. 1.0,
  2727. metric="measurements.score.total",
  2728. tags={"transaction": "foo_transaction"},
  2729. timestamp=self.min_ago,
  2730. )
  2731. self.store_transaction_metric(
  2732. 0.0,
  2733. metric="measurements.score.total",
  2734. tags={"transaction": "foo_transaction"},
  2735. timestamp=self.min_ago,
  2736. )
  2737. self.store_transaction_metric(
  2738. 0.80,
  2739. metric="measurements.score.inp",
  2740. tags={"transaction": "foo_transaction"},
  2741. timestamp=self.min_ago,
  2742. )
  2743. self.store_transaction_metric(
  2744. 1.00,
  2745. metric="measurements.score.weight.inp",
  2746. tags={"transaction": "foo_transaction"},
  2747. timestamp=self.min_ago,
  2748. )
  2749. self.store_transaction_metric(
  2750. 0.80,
  2751. metric="measurements.score.total",
  2752. tags={"transaction": "foo_transaction"},
  2753. timestamp=self.min_ago,
  2754. )
  2755. response = self.do_request(
  2756. {
  2757. "field": [
  2758. "transaction",
  2759. "opportunity_score(measurements.score.ttfb)",
  2760. "opportunity_score(measurements.score.inp)",
  2761. "opportunity_score(measurements.score.total)",
  2762. ],
  2763. "query": "event.type:transaction",
  2764. "dataset": "metrics",
  2765. "per_page": 50,
  2766. }
  2767. )
  2768. assert response.status_code == 200, response.content
  2769. assert len(response.data["data"]) == 1
  2770. data = response.data["data"]
  2771. meta = response.data["meta"]
  2772. assert data[0]["opportunity_score(measurements.score.ttfb)"] == 0.27
  2773. # Should be 0.2. Precision issue?
  2774. assert data[0]["opportunity_score(measurements.score.inp)"] == 0.19999999999999996
  2775. assert data[0]["opportunity_score(measurements.score.total)"] == 1.77
  2776. assert meta["isMetricsData"]
  2777. def test_count_scores(self):
  2778. self.store_transaction_metric(
  2779. 0.1,
  2780. metric="measurements.score.total",
  2781. tags={"transaction": "foo_transaction"},
  2782. timestamp=self.min_ago,
  2783. )
  2784. self.store_transaction_metric(
  2785. 0.2,
  2786. metric="measurements.score.total",
  2787. tags={"transaction": "foo_transaction"},
  2788. timestamp=self.min_ago,
  2789. )
  2790. self.store_transaction_metric(
  2791. 0.3,
  2792. metric="measurements.score.total",
  2793. tags={"transaction": "foo_transaction"},
  2794. timestamp=self.min_ago,
  2795. )
  2796. self.store_transaction_metric(
  2797. 0.4,
  2798. metric="measurements.score.total",
  2799. tags={"transaction": "foo_transaction"},
  2800. timestamp=self.min_ago,
  2801. )
  2802. self.store_transaction_metric(
  2803. 0.5,
  2804. metric="measurements.score.ttfb",
  2805. tags={"transaction": "foo_transaction"},
  2806. timestamp=self.min_ago,
  2807. )
  2808. self.store_transaction_metric(
  2809. 0.8,
  2810. metric="measurements.score.inp",
  2811. tags={"transaction": "foo_transaction"},
  2812. timestamp=self.min_ago,
  2813. )
  2814. response = self.do_request(
  2815. {
  2816. "field": [
  2817. "transaction",
  2818. "count_scores(measurements.score.total)",
  2819. "count_scores(measurements.score.ttfb)",
  2820. "count_scores(measurements.score.inp)",
  2821. ],
  2822. "query": "event.type:transaction",
  2823. "dataset": "metrics",
  2824. "per_page": 50,
  2825. }
  2826. )
  2827. assert response.status_code == 200, response.content
  2828. assert len(response.data["data"]) == 1
  2829. data = response.data["data"]
  2830. meta = response.data["meta"]
  2831. assert data[0]["count_scores(measurements.score.total)"] == 4
  2832. assert data[0]["count_scores(measurements.score.ttfb)"] == 1
  2833. assert data[0]["count_scores(measurements.score.inp)"] == 1
  2834. assert meta["isMetricsData"]
  2835. def test_count_starts(self):
  2836. self.store_transaction_metric(
  2837. 200,
  2838. metric="measurements.app_start_warm",
  2839. tags={"transaction": "foo_transaction"},
  2840. timestamp=self.min_ago,
  2841. )
  2842. self.store_transaction_metric(
  2843. 100,
  2844. metric="measurements.app_start_warm",
  2845. tags={"transaction": "foo_transaction"},
  2846. timestamp=self.min_ago,
  2847. )
  2848. self.store_transaction_metric(
  2849. 10,
  2850. metric="measurements.app_start_cold",
  2851. tags={"transaction": "foo_transaction"},
  2852. timestamp=self.min_ago,
  2853. )
  2854. response = self.do_request(
  2855. {
  2856. "field": [
  2857. "transaction",
  2858. "count_starts(measurements.app_start_warm)",
  2859. "count_starts(measurements.app_start_cold)",
  2860. ],
  2861. "query": "event.type:transaction",
  2862. "dataset": "metrics",
  2863. "per_page": 50,
  2864. }
  2865. )
  2866. assert response.status_code == 200, response.content
  2867. assert len(response.data["data"]) == 1
  2868. data = response.data["data"]
  2869. meta = response.data["meta"]
  2870. assert data[0]["count_starts(measurements.app_start_warm)"] == 2
  2871. assert data[0]["count_starts(measurements.app_start_cold)"] == 1
  2872. assert meta["isMetricsData"]
  2873. def test_count_starts_returns_all_counts_when_no_arg_is_passed(self):
  2874. self.store_transaction_metric(
  2875. 200,
  2876. metric="measurements.app_start_warm",
  2877. tags={"transaction": "foo_transaction"},
  2878. timestamp=self.min_ago,
  2879. )
  2880. self.store_transaction_metric(
  2881. 100,
  2882. metric="measurements.app_start_warm",
  2883. tags={"transaction": "foo_transaction"},
  2884. timestamp=self.min_ago,
  2885. )
  2886. self.store_transaction_metric(
  2887. 10,
  2888. metric="measurements.app_start_cold",
  2889. tags={"transaction": "foo_transaction"},
  2890. timestamp=self.min_ago,
  2891. )
  2892. response = self.do_request(
  2893. {
  2894. "field": [
  2895. "transaction",
  2896. "count_total_starts()",
  2897. ],
  2898. "query": "event.type:transaction",
  2899. "dataset": "metrics",
  2900. "per_page": 50,
  2901. }
  2902. )
  2903. assert response.status_code == 200, response.content
  2904. assert len(response.data["data"]) == 1
  2905. data = response.data["data"]
  2906. meta = response.data["meta"]
  2907. assert data[0]["count_total_starts()"] == 3
  2908. assert meta["isMetricsData"]
  2909. def test_timestamp_groupby(self):
  2910. self.store_transaction_metric(
  2911. 0.03,
  2912. tags={"transaction": "foo_transaction", "user": "foo"},
  2913. timestamp=self.min_ago,
  2914. )
  2915. response = self.do_request(
  2916. {
  2917. "field": [
  2918. "transaction",
  2919. "timestamp",
  2920. "count()",
  2921. "count_unique(user)",
  2922. ],
  2923. "query": "event.type:transaction",
  2924. "dataset": "metricsEnhanced",
  2925. "per_page": 50,
  2926. }
  2927. )
  2928. assert response.status_code == 200, response.content
  2929. assert len(response.data["data"]) == 1
  2930. data = response.data["data"]
  2931. meta = response.data["meta"]
  2932. assert data[0]["transaction"] == "foo_transaction"
  2933. assert meta["dataset"] == "metricsEnhanced"
  2934. def test_on_demand_with_mep(self):
  2935. # Store faketag as an OnDemandMetricSpec, which will put faketag into the metrics indexer
  2936. spec = OnDemandMetricSpec(
  2937. field="count()",
  2938. query="user.email:blah@example.com",
  2939. environment="prod",
  2940. groupbys=["faketag"],
  2941. spec_type=MetricSpecType.DYNAMIC_QUERY,
  2942. )
  2943. self.store_on_demand_metric(123, spec=spec)
  2944. # This is the event that we should actually return
  2945. transaction_data = load_data("transaction", timestamp=self.min_ago)
  2946. transaction_data["tags"].append(("faketag", "foo"))
  2947. self.store_event(transaction_data, self.project.id)
  2948. with self.feature({"organizations:mep-use-default-tags": True}):
  2949. response = self.do_request(
  2950. {
  2951. "field": [
  2952. "faketag",
  2953. "count()",
  2954. ],
  2955. "query": "event.type:transaction",
  2956. "dataset": "metricsEnhanced",
  2957. "per_page": 50,
  2958. }
  2959. )
  2960. assert response.status_code == 200, response.content
  2961. assert len(response.data["data"]) == 1
  2962. data = response.data["data"]
  2963. meta = response.data["meta"]
  2964. assert data[0]["faketag"] == "foo"
  2965. assert not meta["isMetricsData"]
  2966. def test_filtering_by_org_id_is_not_compatible(self):
  2967. """Implicitly test the fact that percentiles are their own 'dataset'"""
  2968. self.store_transaction_metric(
  2969. 1,
  2970. tags={"transaction": "foo_transaction"},
  2971. timestamp=self.min_ago,
  2972. )
  2973. response = self.do_request(
  2974. {
  2975. "field": ["title", "p50()", "count()"],
  2976. "query": "event.type:transaction organization_id:2",
  2977. "dataset": "metrics",
  2978. "project": self.project.id,
  2979. "per_page": 50,
  2980. }
  2981. )
  2982. assert response.status_code == 400, response.content
  2983. class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithOnDemandMetrics(
  2984. MetricsEnhancedPerformanceTestCase
  2985. ):
  2986. viewname = "sentry-api-0-organization-events"
  2987. def setUp(self) -> None:
  2988. super().setUp()
  2989. self.url = reverse(
  2990. self.viewname, kwargs={"organization_id_or_slug": self.organization.slug}
  2991. )
  2992. self.features = {"organizations:on-demand-metrics-extraction-widgets": True}
  2993. def _create_specs(
  2994. self, params: dict[str, Any], groupbys: list[str] | None = None
  2995. ) -> list[OnDemandMetricSpec]:
  2996. """Creates all specs based on the parameters that would be passed to the endpoint."""
  2997. specs = []
  2998. for field in params["field"]:
  2999. spec = OnDemandMetricSpec(
  3000. field=field,
  3001. query=params["query"],
  3002. environment=params.get("environment"),
  3003. groupbys=groupbys,
  3004. spec_type=MetricSpecType.DYNAMIC_QUERY,
  3005. )
  3006. specs.append(spec)
  3007. return specs
  3008. def _make_on_demand_request(
  3009. self, params: dict[str, Any], extra_features: dict[str, bool] | None = None
  3010. ) -> Response:
  3011. """Ensures that the required parameters for an on-demand request are included."""
  3012. # Expected parameters for this helper function
  3013. params["dataset"] = "metricsEnhanced"
  3014. params["useOnDemandMetrics"] = "true"
  3015. params["onDemandType"] = "dynamic_query"
  3016. _features = {**self.features, **(extra_features or {})}
  3017. return self.do_request(params, features=_features)
  3018. def _assert_on_demand_response(
  3019. self,
  3020. response: Response,
  3021. expected_on_demand_query: bool | None = True,
  3022. expected_dataset: str | None = "metricsEnhanced",
  3023. ) -> None:
  3024. """Basic assertions for an on-demand request."""
  3025. assert response.status_code == 200, response.content
  3026. meta = response.data["meta"]
  3027. assert meta.get("isMetricsExtractedData", False) is expected_on_demand_query
  3028. assert meta["dataset"] == expected_dataset
  3029. def test_is_metrics_extracted_data_is_included(self) -> None:
  3030. params = {"field": ["count()"], "query": "transaction.duration:>=91", "yAxis": "count()"}
  3031. specs = self._create_specs(params)
  3032. for spec in specs:
  3033. self.store_on_demand_metric(1, spec=spec)
  3034. response = self._make_on_demand_request(params)
  3035. self._assert_on_demand_response(response)
  3036. def test_on_demand_user_misery(self) -> None:
  3037. user_misery_field = "user_misery(300)"
  3038. query = "transaction.duration:>=100"
  3039. # We store data for both specs, however, when the query builders try to query
  3040. # for the data it will not query on-demand data
  3041. for spec_version in OnDemandMetricSpecVersioning.get_spec_versions():
  3042. spec = OnDemandMetricSpec(
  3043. field=user_misery_field,
  3044. query=query,
  3045. spec_type=MetricSpecType.DYNAMIC_QUERY,
  3046. # We only allow querying the function in the latest spec version,
  3047. # otherwise, the data returned by the endpoint would be 0.05
  3048. spec_version=spec_version,
  3049. )
  3050. tags = {"satisfaction": "miserable"}
  3051. self.store_on_demand_metric(1, spec=spec, additional_tags=tags, timestamp=self.min_ago)
  3052. self.store_on_demand_metric(2, spec=spec, timestamp=self.min_ago)
  3053. params = {"field": [user_misery_field], "project": self.project.id, "query": query}
  3054. self._create_specs(params)
  3055. # We expect it to be False because we're not using the extra feature flag
  3056. response = self._make_on_demand_request(params)
  3057. self._assert_on_demand_response(response, expected_on_demand_query=False)
  3058. # Since we're using the extra feature flag we expect user_misery to be an on-demand metric
  3059. response = self._make_on_demand_request(params, {SPEC_VERSION_TWO_FLAG: True})
  3060. self._assert_on_demand_response(response, expected_on_demand_query=True)
  3061. assert response.data["data"] == [{user_misery_field: user_misery_formula(1, 2)}]
  3062. def test_on_demand_user_misery_discover_split_with_widget_id_unsaved(self) -> None:
  3063. user_misery_field = "user_misery(300)"
  3064. query = "transaction.duration:>=100"
  3065. _, widget, __ = create_widget(["count()"], "", self.project, discover_widget_split=None)
  3066. # We store data for both specs, however, when the query builders try to query
  3067. # for the data it will not query on-demand data
  3068. for spec_version in OnDemandMetricSpecVersioning.get_spec_versions():
  3069. spec = OnDemandMetricSpec(
  3070. field=user_misery_field,
  3071. query=query,
  3072. spec_type=MetricSpecType.DYNAMIC_QUERY,
  3073. # We only allow querying the function in the latest spec version,
  3074. # otherwise, the data returned by the endpoint would be 0.05
  3075. spec_version=spec_version,
  3076. )
  3077. tags = {"satisfaction": "miserable"}
  3078. self.store_on_demand_metric(1, spec=spec, additional_tags=tags, timestamp=self.min_ago)
  3079. self.store_on_demand_metric(2, spec=spec, timestamp=self.min_ago)
  3080. params = {"field": [user_misery_field], "project": self.project.id, "query": query}
  3081. self._create_specs(params)
  3082. params["dashboardWidgetId"] = widget.id
  3083. # Since we're using the extra feature flag we expect user_misery to be an on-demand metric
  3084. with mock.patch.object(widget, "save") as mock_widget_save:
  3085. response = self._make_on_demand_request(params, {SPEC_VERSION_TWO_FLAG: True})
  3086. assert bool(mock_widget_save.assert_called_once)
  3087. self._assert_on_demand_response(response, expected_on_demand_query=True)
  3088. assert response.data["data"] == [{user_misery_field: user_misery_formula(1, 2)}]
  3089. def test_on_demand_user_misery_discover_split_with_widget_id_saved(self) -> None:
  3090. user_misery_field = "user_misery(300)"
  3091. query = "transaction.duration:>=100"
  3092. _, widget, __ = create_widget(
  3093. ["count()"],
  3094. "",
  3095. self.project,
  3096. discover_widget_split=DashboardWidgetTypes.TRANSACTION_LIKE, # Transactions like uses on-demand
  3097. )
  3098. # We store data for both specs, however, when the query builders try to query
  3099. # for the data it will not query on-demand data
  3100. for spec_version in OnDemandMetricSpecVersioning.get_spec_versions():
  3101. spec = OnDemandMetricSpec(
  3102. field=user_misery_field,
  3103. query=query,
  3104. spec_type=MetricSpecType.DYNAMIC_QUERY,
  3105. # We only allow querying the function in the latest spec version,
  3106. # otherwise, the data returned by the endpoint would be 0.05
  3107. spec_version=spec_version,
  3108. )
  3109. tags = {"satisfaction": "miserable"}
  3110. self.store_on_demand_metric(1, spec=spec, additional_tags=tags, timestamp=self.min_ago)
  3111. self.store_on_demand_metric(2, spec=spec, timestamp=self.min_ago)
  3112. params = {"field": [user_misery_field], "project": self.project.id, "query": query}
  3113. self._create_specs(params)
  3114. params["dashboardWidgetId"] = widget.id
  3115. # Since we're using the extra feature flag we expect user_misery to be an on-demand metric
  3116. with mock.patch.object(widget, "save") as mock_widget_save:
  3117. response = self._make_on_demand_request(params, {SPEC_VERSION_TWO_FLAG: True})
  3118. assert bool(mock_widget_save.assert_not_called)
  3119. self._assert_on_demand_response(response, expected_on_demand_query=True)
  3120. assert response.data["data"] == [{user_misery_field: user_misery_formula(1, 2)}]
  3121. def test_on_demand_count_unique(self):
  3122. field = "count_unique(user)"
  3123. query = "transaction.duration:>0"
  3124. params = {"field": [field], "query": query}
  3125. # We do not really have to create the metrics for both specs since
  3126. # the first API call will not query any on-demand metric
  3127. for spec_version in OnDemandMetricSpecVersioning.get_spec_versions():
  3128. spec = OnDemandMetricSpec(
  3129. field=field,
  3130. query=query,
  3131. spec_type=MetricSpecType.DYNAMIC_QUERY,
  3132. spec_version=spec_version,
  3133. )
  3134. self.store_on_demand_metric(1, spec=spec, timestamp=self.min_ago)
  3135. self.store_on_demand_metric(2, spec=spec, timestamp=self.min_ago)
  3136. # The first call will not be on-demand
  3137. response = self._make_on_demand_request(params)
  3138. self._assert_on_demand_response(response, expected_on_demand_query=False)
  3139. # This second call will be on-demand
  3140. response = self._make_on_demand_request(
  3141. params, extra_features={SPEC_VERSION_TWO_FLAG: True}
  3142. )
  3143. self._assert_on_demand_response(response, expected_on_demand_query=True)
  3144. assert response.data["data"] == [{"count_unique(user)": 2}]
  3145. class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithMetricLayer(
  3146. OrganizationEventsMetricsEnhancedPerformanceEndpointTest
  3147. ):
  3148. def setUp(self):
  3149. super().setUp()
  3150. self.features["organizations:use-metrics-layer"] = True
  3151. @pytest.mark.xfail(reason="Not supported")
  3152. def test_time_spent(self):
  3153. super().test_time_spent()
  3154. @pytest.mark.xfail(reason="Not supported")
  3155. def test_http_error_rate(self):
  3156. super().test_http_error_rate()
  3157. @pytest.mark.xfail(reason="Multiple aliases to same column not supported")
  3158. def test_title_and_transaction_alias(self):
  3159. super().test_title_and_transaction_alias()
  3160. @pytest.mark.xfail(reason="Sort order is flaking when querying multiple datasets")
  3161. def test_maintain_sort_order_across_datasets(self):
  3162. """You may need to run this test a few times to get it to fail"""
  3163. super().test_maintain_sort_order_across_datasets()
  3164. @pytest.mark.xfail(reason="Not implemented")
  3165. def test_avg_compare(self):
  3166. super().test_avg_compare()
  3167. @pytest.mark.xfail(reason="Not implemented")
  3168. def test_avg_if(self):
  3169. super().test_avg_if()
  3170. @pytest.mark.xfail(reason="Not implemented")
  3171. def test_count_if(self):
  3172. super().test_count_if()
  3173. @pytest.mark.xfail(reason="Not implemented")
  3174. def test_device_class(self):
  3175. super().test_device_class()
  3176. @pytest.mark.xfail(reason="Not implemented")
  3177. def test_device_class_filter(self):
  3178. super().test_device_class_filter()
  3179. @pytest.mark.xfail(reason="Not implemented")
  3180. def test_performance_score(self):
  3181. super().test_performance_score()
  3182. @pytest.mark.xfail(reason="Not implemented")
  3183. def test_performance_score_boundaries(self):
  3184. super().test_performance_score()
  3185. @pytest.mark.xfail(reason="Not implemented")
  3186. def test_weighted_performance_score(self):
  3187. super().test_weighted_performance_score()
  3188. @pytest.mark.xfail(reason="Not implemented")
  3189. def test_invalid_performance_score_column(self):
  3190. super().test_invalid_performance_score_column()
  3191. @pytest.mark.xfail(reason="Not implemented")
  3192. def test_invalid_weighted_performance_score_column(self):
  3193. super().test_invalid_weighted_performance_score_column()
  3194. @pytest.mark.xfail(reason="Not implemented")
  3195. def test_no_weighted_performance_score_column(self):
  3196. super().test_invalid_weighted_performance_score_column()
  3197. @pytest.mark.xfail(reason="Not implemented")
  3198. def test_opportunity_score(self):
  3199. super().test_opportunity_score()
  3200. @pytest.mark.xfail(reason="Not implemented")
  3201. def test_count_scores(self):
  3202. super().test_count_scores()
  3203. @pytest.mark.xfail(reason="Not implemented")
  3204. def test_count_starts(self):
  3205. super().test_count_starts()
  3206. @pytest.mark.xfail(reason="Not implemented")
  3207. def test_count_starts_returns_all_counts_when_no_arg_is_passed(self):
  3208. super().test_count_starts_returns_all_counts_when_no_arg_is_passed()
  3209. @pytest.mark.xfail(reason="Not implemented")
  3210. def test_timestamp_groupby(self):
  3211. super().test_timestamp_groupby()
  3212. @pytest.mark.xfail(reason="Not implemented")
  3213. def test_on_demand_with_mep(self):
  3214. super().test_on_demand_with_mep()