test_organization_events_mep.py 148 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231
  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 DatasetSourcesTypes, 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
  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": before_now(days=20),
  95. "end": 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_invalid_performance_score_column(self):
  2536. self.store_transaction_metric(
  2537. 0.03,
  2538. metric="measurements.score.total",
  2539. tags={"transaction": "foo_transaction"},
  2540. timestamp=self.min_ago,
  2541. )
  2542. response = self.do_request(
  2543. {
  2544. "field": [
  2545. "transaction",
  2546. "performance_score(measurements.score.fp)",
  2547. ],
  2548. "query": "event.type:transaction",
  2549. "dataset": "metrics",
  2550. "per_page": 50,
  2551. }
  2552. )
  2553. assert response.status_code == 400, response.content
  2554. def test_opportunity_score(self):
  2555. self.store_transaction_metric(
  2556. 0.03,
  2557. metric="measurements.score.ttfb",
  2558. tags={"transaction": "foo_transaction"},
  2559. timestamp=self.min_ago,
  2560. )
  2561. self.store_transaction_metric(
  2562. 0.30,
  2563. metric="measurements.score.weight.ttfb",
  2564. tags={"transaction": "foo_transaction"},
  2565. timestamp=self.min_ago,
  2566. )
  2567. self.store_transaction_metric(
  2568. 0.40,
  2569. metric="measurements.score.fcp",
  2570. tags={"transaction": "foo_transaction"},
  2571. timestamp=self.min_ago,
  2572. )
  2573. self.store_transaction_metric(
  2574. 0.70,
  2575. metric="measurements.score.weight.fcp",
  2576. tags={"transaction": "foo_transaction"},
  2577. timestamp=self.min_ago,
  2578. )
  2579. self.store_transaction_metric(
  2580. 0.43,
  2581. metric="measurements.score.total",
  2582. tags={"transaction": "foo_transaction"},
  2583. timestamp=self.min_ago,
  2584. )
  2585. self.store_transaction_metric(
  2586. 1.0,
  2587. metric="measurements.score.ttfb",
  2588. tags={"transaction": "foo_transaction"},
  2589. timestamp=self.min_ago,
  2590. )
  2591. self.store_transaction_metric(
  2592. 1.0,
  2593. metric="measurements.score.weight.ttfb",
  2594. tags={"transaction": "foo_transaction"},
  2595. timestamp=self.min_ago,
  2596. )
  2597. self.store_transaction_metric(
  2598. 1.0,
  2599. metric="measurements.score.total",
  2600. tags={"transaction": "foo_transaction"},
  2601. timestamp=self.min_ago,
  2602. )
  2603. self.store_transaction_metric(
  2604. 0.0,
  2605. metric="measurements.score.total",
  2606. tags={"transaction": "foo_transaction"},
  2607. timestamp=self.min_ago,
  2608. )
  2609. self.store_transaction_metric(
  2610. 0.80,
  2611. metric="measurements.score.inp",
  2612. tags={"transaction": "foo_transaction"},
  2613. timestamp=self.min_ago,
  2614. )
  2615. self.store_transaction_metric(
  2616. 1.00,
  2617. metric="measurements.score.weight.inp",
  2618. tags={"transaction": "foo_transaction"},
  2619. timestamp=self.min_ago,
  2620. )
  2621. self.store_transaction_metric(
  2622. 0.80,
  2623. metric="measurements.score.total",
  2624. tags={"transaction": "foo_transaction"},
  2625. timestamp=self.min_ago,
  2626. )
  2627. response = self.do_request(
  2628. {
  2629. "field": [
  2630. "transaction",
  2631. "opportunity_score(measurements.score.ttfb)",
  2632. "opportunity_score(measurements.score.inp)",
  2633. "opportunity_score(measurements.score.total)",
  2634. ],
  2635. "query": "event.type:transaction",
  2636. "dataset": "metrics",
  2637. "per_page": 50,
  2638. }
  2639. )
  2640. assert response.status_code == 200, response.content
  2641. assert len(response.data["data"]) == 1
  2642. data = response.data["data"]
  2643. meta = response.data["meta"]
  2644. assert data[0]["opportunity_score(measurements.score.ttfb)"] == 0.27
  2645. # Should be 0.2. Precision issue?
  2646. assert data[0]["opportunity_score(measurements.score.inp)"] == 0.19999999999999996
  2647. assert data[0]["opportunity_score(measurements.score.total)"] == 1.77
  2648. assert meta["isMetricsData"]
  2649. def test_opportunity_score_with_fixed_weights(self):
  2650. self.store_transaction_metric(
  2651. 0.5,
  2652. metric="measurements.score.inp",
  2653. tags={"transaction": "foo_transaction"},
  2654. timestamp=self.min_ago,
  2655. )
  2656. self.store_transaction_metric(
  2657. 1.0,
  2658. metric="measurements.score.weight.inp",
  2659. tags={"transaction": "foo_transaction"},
  2660. timestamp=self.min_ago,
  2661. )
  2662. self.store_transaction_metric(
  2663. 0.2,
  2664. metric="measurements.score.inp",
  2665. tags={"transaction": "foo_transaction"},
  2666. timestamp=self.min_ago,
  2667. )
  2668. self.store_transaction_metric(
  2669. 1.0,
  2670. metric="measurements.score.weight.inp",
  2671. tags={"transaction": "foo_transaction"},
  2672. timestamp=self.min_ago,
  2673. )
  2674. self.store_transaction_metric(
  2675. 0.2,
  2676. metric="measurements.score.inp",
  2677. tags={"transaction": "foo_transaction"},
  2678. timestamp=self.min_ago,
  2679. )
  2680. self.store_transaction_metric(
  2681. 0.5,
  2682. metric="measurements.score.weight.inp",
  2683. tags={"transaction": "foo_transaction"},
  2684. timestamp=self.min_ago,
  2685. )
  2686. self.store_transaction_metric(
  2687. 0.1,
  2688. metric="measurements.score.lcp",
  2689. tags={"transaction": "foo_transaction"},
  2690. timestamp=self.min_ago,
  2691. )
  2692. self.store_transaction_metric(
  2693. 0.3,
  2694. metric="measurements.score.weight.lcp",
  2695. tags={"transaction": "foo_transaction"},
  2696. timestamp=self.min_ago,
  2697. )
  2698. self.store_transaction_metric(
  2699. 0.2,
  2700. metric="measurements.score.inp",
  2701. tags={"transaction": "bar_transaction"},
  2702. timestamp=self.min_ago,
  2703. )
  2704. self.store_transaction_metric(
  2705. 0.5,
  2706. metric="measurements.score.weight.inp",
  2707. tags={"transaction": "bar_transaction"},
  2708. timestamp=self.min_ago,
  2709. )
  2710. response = self.do_request(
  2711. {
  2712. "field": [
  2713. "transaction",
  2714. "total_opportunity_score()",
  2715. ],
  2716. "query": "event.type:transaction",
  2717. "orderby": "transaction",
  2718. "dataset": "metrics",
  2719. "per_page": 50,
  2720. }
  2721. )
  2722. assert response.status_code == 200, response.content
  2723. assert len(response.data["data"]) == 2
  2724. data = response.data["data"]
  2725. meta = response.data["meta"]
  2726. assert data[0]["total_opportunity_score()"] == 0.029999999999999995
  2727. assert data[1]["total_opportunity_score()"] == 0.36
  2728. assert meta["isMetricsData"]
  2729. def test_opportunity_score_with_fixed_weights_and_missing_vitals(self):
  2730. self.store_transaction_metric(
  2731. 0.5,
  2732. metric="measurements.score.inp",
  2733. tags={"transaction": "foo_transaction", "browser.name": "Safari"},
  2734. timestamp=self.min_ago,
  2735. )
  2736. self.store_transaction_metric(
  2737. 1.0,
  2738. metric="measurements.score.weight.inp",
  2739. tags={"transaction": "foo_transaction", "browser.name": "Safari"},
  2740. timestamp=self.min_ago,
  2741. )
  2742. self.store_transaction_metric(
  2743. 0.2,
  2744. metric="measurements.score.inp",
  2745. tags={"transaction": "foo_transaction", "browser.name": "Safari"},
  2746. timestamp=self.min_ago,
  2747. )
  2748. self.store_transaction_metric(
  2749. 1.0,
  2750. metric="measurements.score.weight.inp",
  2751. tags={"transaction": "foo_transaction", "browser.name": "Safari"},
  2752. timestamp=self.min_ago,
  2753. )
  2754. self.store_transaction_metric(
  2755. 0.2,
  2756. metric="measurements.score.inp",
  2757. tags={"transaction": "foo_transaction", "browser.name": "Safari"},
  2758. timestamp=self.min_ago,
  2759. )
  2760. self.store_transaction_metric(
  2761. 0.5,
  2762. metric="measurements.score.weight.inp",
  2763. tags={"transaction": "foo_transaction", "browser.name": "Safari"},
  2764. timestamp=self.min_ago,
  2765. )
  2766. self.store_transaction_metric(
  2767. 0.1,
  2768. metric="measurements.score.lcp",
  2769. tags={"transaction": "foo_transaction", "browser.name": "Firefox"},
  2770. timestamp=self.min_ago,
  2771. )
  2772. self.store_transaction_metric(
  2773. 0.3,
  2774. metric="measurements.score.weight.lcp",
  2775. tags={"transaction": "foo_transaction", "browser.name": "Firefox"},
  2776. timestamp=self.min_ago,
  2777. )
  2778. self.store_transaction_metric(
  2779. 0.2,
  2780. metric="measurements.score.inp",
  2781. tags={"transaction": "bar_transaction", "browser.name": "Safari"},
  2782. timestamp=self.min_ago,
  2783. )
  2784. self.store_transaction_metric(
  2785. 0.5,
  2786. metric="measurements.score.weight.inp",
  2787. tags={"transaction": "bar_transaction", "browser.name": "Safari"},
  2788. timestamp=self.min_ago,
  2789. )
  2790. self.store_transaction_metric(
  2791. 0.5,
  2792. metric="measurements.score.total",
  2793. tags={"transaction": "foo_transaction", "browser.name": "Safari"},
  2794. timestamp=self.min_ago,
  2795. )
  2796. self.store_transaction_metric(
  2797. 0.5,
  2798. metric="measurements.score.total",
  2799. tags={"transaction": "bar_transaction", "browser.name": "Safari"},
  2800. timestamp=self.min_ago,
  2801. )
  2802. with self.feature({"organizations:performance-vitals-handle-missing-webvitals": True}):
  2803. response = self.do_request(
  2804. {
  2805. "field": [
  2806. "transaction",
  2807. "total_opportunity_score()",
  2808. ],
  2809. "query": 'event.type:transaction transaction.op:[pageload,""] (browser.name:Safari OR browser.name:Firefox) avg(measurements.score.total):>0',
  2810. "orderby": "transaction",
  2811. "dataset": "metrics",
  2812. "per_page": 50,
  2813. }
  2814. )
  2815. assert response.status_code == 200, response.content
  2816. assert len(response.data["data"]) == 2
  2817. data = response.data["data"]
  2818. meta = response.data["meta"]
  2819. assert data[0]["total_opportunity_score()"] == 0.09999999999999999
  2820. assert data[1]["total_opportunity_score()"] == 0.6
  2821. assert meta["isMetricsData"]
  2822. def test_total_performance_score(self):
  2823. self.store_transaction_metric(
  2824. 0.03,
  2825. metric="measurements.score.lcp",
  2826. tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
  2827. timestamp=self.min_ago,
  2828. )
  2829. self.store_transaction_metric(
  2830. 0.30,
  2831. metric="measurements.score.weight.lcp",
  2832. tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
  2833. timestamp=self.min_ago,
  2834. )
  2835. self.store_transaction_metric(
  2836. 0.15,
  2837. metric="measurements.score.fcp",
  2838. tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
  2839. timestamp=self.min_ago,
  2840. )
  2841. self.store_transaction_metric(
  2842. 0.15,
  2843. metric="measurements.score.weight.fcp",
  2844. tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
  2845. timestamp=self.min_ago,
  2846. )
  2847. self.store_transaction_metric(
  2848. 0.10,
  2849. metric="measurements.score.cls",
  2850. tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
  2851. timestamp=self.min_ago,
  2852. )
  2853. self.store_transaction_metric(
  2854. 0.15,
  2855. metric="measurements.score.weight.cls",
  2856. tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
  2857. timestamp=self.min_ago,
  2858. )
  2859. self.store_transaction_metric(
  2860. 0.05,
  2861. metric="measurements.score.ttfb",
  2862. tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
  2863. timestamp=self.min_ago,
  2864. )
  2865. self.store_transaction_metric(
  2866. 0.10,
  2867. metric="measurements.score.weight.ttfb",
  2868. tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
  2869. timestamp=self.min_ago,
  2870. )
  2871. self.store_transaction_metric(
  2872. 0.05,
  2873. metric="measurements.score.inp",
  2874. tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
  2875. timestamp=self.min_ago,
  2876. )
  2877. self.store_transaction_metric(
  2878. 0.10,
  2879. metric="measurements.score.weight.inp",
  2880. tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
  2881. timestamp=self.min_ago,
  2882. )
  2883. response = self.do_request(
  2884. {
  2885. "field": [
  2886. "transaction",
  2887. "performance_score(measurements.score.total)",
  2888. ],
  2889. "query": "",
  2890. "dataset": "metrics",
  2891. "per_page": 50,
  2892. }
  2893. )
  2894. assert response.status_code == 200, response.content
  2895. assert len(response.data["data"]) == 1
  2896. data = response.data["data"]
  2897. meta = response.data["meta"]
  2898. assert data[0]["performance_score(measurements.score.total)"] == 0.48
  2899. assert meta["isMetricsData"]
  2900. def test_total_performance_score_with_missing_vitals(self):
  2901. self.store_transaction_metric(
  2902. 0.03,
  2903. metric="measurements.score.lcp",
  2904. tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
  2905. timestamp=self.min_ago,
  2906. )
  2907. self.store_transaction_metric(
  2908. 0.30,
  2909. metric="measurements.score.weight.lcp",
  2910. tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
  2911. timestamp=self.min_ago,
  2912. )
  2913. self.store_transaction_metric(
  2914. 0.15,
  2915. metric="measurements.score.fcp",
  2916. tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
  2917. timestamp=self.min_ago,
  2918. )
  2919. self.store_transaction_metric(
  2920. 0.15,
  2921. metric="measurements.score.weight.fcp",
  2922. tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
  2923. timestamp=self.min_ago,
  2924. )
  2925. with self.feature({"organizations:performance-vitals-handle-missing-webvitals": True}):
  2926. response = self.do_request(
  2927. {
  2928. "field": [
  2929. "transaction",
  2930. "performance_score(measurements.score.total)",
  2931. ],
  2932. "query": "",
  2933. "dataset": "metrics",
  2934. "per_page": 50,
  2935. }
  2936. )
  2937. assert response.status_code == 200, response.content
  2938. assert len(response.data["data"]) == 1
  2939. data = response.data["data"]
  2940. meta = response.data["meta"]
  2941. assert data[0]["performance_score(measurements.score.total)"] == 0.4
  2942. assert meta["isMetricsData"]
  2943. def test_count_scores(self):
  2944. self.store_transaction_metric(
  2945. 0.1,
  2946. metric="measurements.score.total",
  2947. tags={"transaction": "foo_transaction"},
  2948. timestamp=self.min_ago,
  2949. )
  2950. self.store_transaction_metric(
  2951. 0.2,
  2952. metric="measurements.score.total",
  2953. tags={"transaction": "foo_transaction"},
  2954. timestamp=self.min_ago,
  2955. )
  2956. self.store_transaction_metric(
  2957. 0.3,
  2958. metric="measurements.score.total",
  2959. tags={"transaction": "foo_transaction"},
  2960. timestamp=self.min_ago,
  2961. )
  2962. self.store_transaction_metric(
  2963. 0.4,
  2964. metric="measurements.score.total",
  2965. tags={"transaction": "foo_transaction"},
  2966. timestamp=self.min_ago,
  2967. )
  2968. self.store_transaction_metric(
  2969. 0.5,
  2970. metric="measurements.score.ttfb",
  2971. tags={"transaction": "foo_transaction"},
  2972. timestamp=self.min_ago,
  2973. )
  2974. self.store_transaction_metric(
  2975. 0.8,
  2976. metric="measurements.score.inp",
  2977. tags={"transaction": "foo_transaction"},
  2978. timestamp=self.min_ago,
  2979. )
  2980. response = self.do_request(
  2981. {
  2982. "field": [
  2983. "transaction",
  2984. "count_scores(measurements.score.total)",
  2985. "count_scores(measurements.score.ttfb)",
  2986. "count_scores(measurements.score.inp)",
  2987. ],
  2988. "query": "event.type:transaction",
  2989. "dataset": "metrics",
  2990. "per_page": 50,
  2991. }
  2992. )
  2993. assert response.status_code == 200, response.content
  2994. assert len(response.data["data"]) == 1
  2995. data = response.data["data"]
  2996. meta = response.data["meta"]
  2997. assert data[0]["count_scores(measurements.score.total)"] == 4
  2998. assert data[0]["count_scores(measurements.score.ttfb)"] == 1
  2999. assert data[0]["count_scores(measurements.score.inp)"] == 1
  3000. assert meta["isMetricsData"]
  3001. def test_count_starts(self):
  3002. self.store_transaction_metric(
  3003. 200,
  3004. metric="measurements.app_start_warm",
  3005. tags={"transaction": "foo_transaction"},
  3006. timestamp=self.min_ago,
  3007. )
  3008. self.store_transaction_metric(
  3009. 100,
  3010. metric="measurements.app_start_warm",
  3011. tags={"transaction": "foo_transaction"},
  3012. timestamp=self.min_ago,
  3013. )
  3014. self.store_transaction_metric(
  3015. 10,
  3016. metric="measurements.app_start_cold",
  3017. tags={"transaction": "foo_transaction"},
  3018. timestamp=self.min_ago,
  3019. )
  3020. response = self.do_request(
  3021. {
  3022. "field": [
  3023. "transaction",
  3024. "count_starts(measurements.app_start_warm)",
  3025. "count_starts(measurements.app_start_cold)",
  3026. ],
  3027. "query": "event.type:transaction",
  3028. "dataset": "metrics",
  3029. "per_page": 50,
  3030. }
  3031. )
  3032. assert response.status_code == 200, response.content
  3033. assert len(response.data["data"]) == 1
  3034. data = response.data["data"]
  3035. meta = response.data["meta"]
  3036. assert data[0]["count_starts(measurements.app_start_warm)"] == 2
  3037. assert data[0]["count_starts(measurements.app_start_cold)"] == 1
  3038. assert meta["isMetricsData"]
  3039. def test_count_starts_returns_all_counts_when_no_arg_is_passed(self):
  3040. self.store_transaction_metric(
  3041. 200,
  3042. metric="measurements.app_start_warm",
  3043. tags={"transaction": "foo_transaction"},
  3044. timestamp=self.min_ago,
  3045. )
  3046. self.store_transaction_metric(
  3047. 100,
  3048. metric="measurements.app_start_warm",
  3049. tags={"transaction": "foo_transaction"},
  3050. timestamp=self.min_ago,
  3051. )
  3052. self.store_transaction_metric(
  3053. 10,
  3054. metric="measurements.app_start_cold",
  3055. tags={"transaction": "foo_transaction"},
  3056. timestamp=self.min_ago,
  3057. )
  3058. response = self.do_request(
  3059. {
  3060. "field": [
  3061. "transaction",
  3062. "count_total_starts()",
  3063. ],
  3064. "query": "event.type:transaction",
  3065. "dataset": "metrics",
  3066. "per_page": 50,
  3067. }
  3068. )
  3069. assert response.status_code == 200, response.content
  3070. assert len(response.data["data"]) == 1
  3071. data = response.data["data"]
  3072. meta = response.data["meta"]
  3073. assert data[0]["count_total_starts()"] == 3
  3074. assert meta["isMetricsData"]
  3075. def test_timestamp_groupby(self):
  3076. self.store_transaction_metric(
  3077. 0.03,
  3078. tags={"transaction": "foo_transaction", "user": "foo"},
  3079. timestamp=self.min_ago,
  3080. )
  3081. response = self.do_request(
  3082. {
  3083. "field": [
  3084. "transaction",
  3085. "timestamp",
  3086. "count()",
  3087. "count_unique(user)",
  3088. ],
  3089. "query": "event.type:transaction",
  3090. "dataset": "metricsEnhanced",
  3091. "per_page": 50,
  3092. }
  3093. )
  3094. assert response.status_code == 200, response.content
  3095. assert len(response.data["data"]) == 1
  3096. data = response.data["data"]
  3097. meta = response.data["meta"]
  3098. assert data[0]["transaction"] == "foo_transaction"
  3099. assert meta["dataset"] == "metricsEnhanced"
  3100. def test_on_demand_with_mep(self):
  3101. # Store faketag as an OnDemandMetricSpec, which will put faketag into the metrics indexer
  3102. spec = OnDemandMetricSpec(
  3103. field="count()",
  3104. query="user.email:blah@example.com",
  3105. environment="prod",
  3106. groupbys=["faketag"],
  3107. spec_type=MetricSpecType.DYNAMIC_QUERY,
  3108. )
  3109. self.store_on_demand_metric(123, spec=spec)
  3110. # This is the event that we should actually return
  3111. transaction_data = load_data("transaction", timestamp=self.min_ago)
  3112. transaction_data["tags"].append(("faketag", "foo"))
  3113. self.store_event(transaction_data, self.project.id)
  3114. with self.feature({"organizations:mep-use-default-tags": True}):
  3115. response = self.do_request(
  3116. {
  3117. "field": [
  3118. "faketag",
  3119. "count()",
  3120. ],
  3121. "query": "event.type:transaction",
  3122. "dataset": "metricsEnhanced",
  3123. "per_page": 50,
  3124. }
  3125. )
  3126. assert response.status_code == 200, response.content
  3127. assert len(response.data["data"]) == 1
  3128. data = response.data["data"]
  3129. meta = response.data["meta"]
  3130. assert data[0]["faketag"] == "foo"
  3131. assert not meta["isMetricsData"]
  3132. def test_filtering_by_org_id_is_not_compatible(self):
  3133. """Implicitly test the fact that percentiles are their own 'dataset'"""
  3134. self.store_transaction_metric(
  3135. 1,
  3136. tags={"transaction": "foo_transaction"},
  3137. timestamp=self.min_ago,
  3138. )
  3139. response = self.do_request(
  3140. {
  3141. "field": ["title", "p50()", "count()"],
  3142. "query": "event.type:transaction organization_id:2",
  3143. "dataset": "metrics",
  3144. "project": self.project.id,
  3145. "per_page": 50,
  3146. }
  3147. )
  3148. assert response.status_code == 400, response.content
  3149. def test_cache_miss_rate(self):
  3150. self.store_span_metric(
  3151. 1,
  3152. timestamp=self.min_ago,
  3153. tags={"cache.hit": "true"},
  3154. )
  3155. self.store_span_metric(
  3156. 1,
  3157. timestamp=self.min_ago,
  3158. tags={"cache.hit": "false"},
  3159. )
  3160. self.store_span_metric(
  3161. 1,
  3162. timestamp=self.min_ago,
  3163. tags={"cache.hit": "false"},
  3164. )
  3165. self.store_span_metric(
  3166. 1,
  3167. timestamp=self.min_ago,
  3168. tags={"cache.hit": "false"},
  3169. )
  3170. response = self.do_request(
  3171. {
  3172. "field": ["cache_miss_rate()"],
  3173. "query": "",
  3174. "project": self.project.id,
  3175. "dataset": "metrics",
  3176. }
  3177. )
  3178. assert response.status_code == 200, response.content
  3179. data = response.data["data"]
  3180. meta = response.data["meta"]
  3181. assert len(data) == 1
  3182. assert data[0]["cache_miss_rate()"] == 0.75
  3183. assert meta["dataset"] == "metrics"
  3184. assert meta["fields"]["cache_miss_rate()"] == "percentage"
  3185. def test_http_response_rate(self):
  3186. self.store_span_metric(
  3187. 1,
  3188. timestamp=self.min_ago,
  3189. tags={"span.status_code": "200"},
  3190. )
  3191. self.store_span_metric(
  3192. 3,
  3193. timestamp=self.min_ago,
  3194. tags={"span.status_code": "301"},
  3195. )
  3196. self.store_span_metric(
  3197. 3,
  3198. timestamp=self.min_ago,
  3199. tags={"span.status_code": "404"},
  3200. )
  3201. self.store_span_metric(
  3202. 4,
  3203. timestamp=self.min_ago,
  3204. tags={"span.status_code": "503"},
  3205. )
  3206. self.store_span_metric(
  3207. 5,
  3208. timestamp=self.min_ago,
  3209. tags={"span.status_code": "501"},
  3210. )
  3211. response = self.do_request(
  3212. {
  3213. "field": [
  3214. "http_response_rate(200)", # By exact code
  3215. "http_response_rate(3)", # By code class
  3216. "http_response_rate(4)",
  3217. "http_response_rate(5)",
  3218. ],
  3219. "query": "",
  3220. "project": self.project.id,
  3221. "dataset": "metrics",
  3222. }
  3223. )
  3224. assert response.status_code == 200, response.content
  3225. data = response.data["data"]
  3226. assert len(data) == 1
  3227. assert data[0]["http_response_rate(200)"] == 0.2
  3228. assert data[0]["http_response_rate(3)"] == 0.2
  3229. assert data[0]["http_response_rate(4)"] == 0.2
  3230. assert data[0]["http_response_rate(5)"] == 0.4
  3231. meta = response.data["meta"]
  3232. assert meta["dataset"] == "metrics"
  3233. assert meta["fields"]["http_response_rate(200)"] == "percentage"
  3234. def test_avg_span_self_time(self):
  3235. self.store_span_metric(
  3236. 1,
  3237. timestamp=self.min_ago,
  3238. )
  3239. self.store_span_metric(
  3240. 3,
  3241. timestamp=self.min_ago,
  3242. )
  3243. self.store_span_metric(
  3244. 3,
  3245. timestamp=self.min_ago,
  3246. )
  3247. self.store_span_metric(
  3248. 4,
  3249. timestamp=self.min_ago,
  3250. )
  3251. self.store_span_metric(
  3252. 5,
  3253. timestamp=self.min_ago,
  3254. )
  3255. response = self.do_request(
  3256. {
  3257. "field": [
  3258. "avg(span.self_time)",
  3259. ],
  3260. "query": "",
  3261. "project": self.project.id,
  3262. "dataset": "metrics",
  3263. }
  3264. )
  3265. assert response.status_code == 200, response.content
  3266. data = response.data["data"]
  3267. assert len(data) == 1
  3268. assert data[0]["avg(span.self_time)"] == 3.2
  3269. def test_avg_message_receive_latency_gauge_functions(self):
  3270. self.store_span_metric(
  3271. {
  3272. "min": 5,
  3273. "max": 5,
  3274. "sum": 5,
  3275. "count": 1,
  3276. "last": 5,
  3277. },
  3278. internal_metric=constants.SPAN_MESSAGING_LATENCY,
  3279. timestamp=self.min_ago,
  3280. entity="metrics_gauges",
  3281. )
  3282. self.store_span_metric(
  3283. {
  3284. "min": 15,
  3285. "max": 15,
  3286. "sum": 15,
  3287. "count": 1,
  3288. "last": 15,
  3289. },
  3290. internal_metric=constants.SPAN_MESSAGING_LATENCY,
  3291. timestamp=self.min_ago,
  3292. entity="metrics_gauges",
  3293. )
  3294. response = self.do_request(
  3295. {
  3296. "field": [
  3297. "avg(messaging.message.receive.latency)",
  3298. "sum(messaging.message.receive.latency)",
  3299. "min(messaging.message.receive.latency)",
  3300. "max(messaging.message.receive.latency)",
  3301. ],
  3302. "query": "",
  3303. "project": self.project.id,
  3304. "dataset": "metrics",
  3305. }
  3306. )
  3307. assert response.status_code == 200, response.content
  3308. data = response.data["data"]
  3309. assert len(data) == 1
  3310. assert data[0]["avg(messaging.message.receive.latency)"] == 10.0
  3311. assert data[0]["sum(messaging.message.receive.latency)"] == 20.0
  3312. assert data[0]["min(messaging.message.receive.latency)"] == 5.0
  3313. assert data[0]["max(messaging.message.receive.latency)"] == 15.0
  3314. response = self.do_request(
  3315. {
  3316. "field": [
  3317. "avg(g:spans/messaging.message.receive.latency@millisecond)",
  3318. "sum(g:spans/messaging.message.receive.latency@millisecond)",
  3319. "min(g:spans/messaging.message.receive.latency@millisecond)",
  3320. "max(g:spans/messaging.message.receive.latency@millisecond)",
  3321. ],
  3322. "query": "",
  3323. "project": self.project.id,
  3324. "dataset": "metrics",
  3325. }
  3326. )
  3327. assert response.status_code == 200, response.content
  3328. data = response.data["data"]
  3329. assert len(data) == 1
  3330. assert data[0]["avg(g:spans/messaging.message.receive.latency@millisecond)"] == 10.0
  3331. assert data[0]["sum(g:spans/messaging.message.receive.latency@millisecond)"] == 20.0
  3332. assert data[0]["min(g:spans/messaging.message.receive.latency@millisecond)"] == 5.0
  3333. assert data[0]["max(g:spans/messaging.message.receive.latency@millisecond)"] == 15.0
  3334. def test_span_module_filter(self):
  3335. self.store_span_metric(
  3336. 1,
  3337. timestamp=self.min_ago,
  3338. tags={"span.category": "db", "span.op": "db.redis"},
  3339. )
  3340. self.store_span_metric(
  3341. 4,
  3342. timestamp=self.min_ago,
  3343. tags={"span.category": "cache"},
  3344. )
  3345. self.store_span_metric(
  3346. 4,
  3347. timestamp=self.min_ago,
  3348. tags={"span.category": "db", "span.op": "db.sql.room"},
  3349. )
  3350. self.store_span_metric(
  3351. 2,
  3352. timestamp=self.min_ago,
  3353. tags={"span.category": "db"},
  3354. )
  3355. self.store_span_metric(
  3356. 3,
  3357. timestamp=self.min_ago,
  3358. tags={"span.category": "http"},
  3359. )
  3360. response = self.do_request(
  3361. {
  3362. "field": [
  3363. "span.description",
  3364. "span.module",
  3365. "avg(span.self_time)",
  3366. ],
  3367. "orderby": "avg(span.self_time)",
  3368. "query": "span.module:[db, cache, other]",
  3369. "project": self.project.id,
  3370. "dataset": "metrics",
  3371. }
  3372. )
  3373. assert response.status_code == 200, response.content
  3374. data = response.data["data"]
  3375. assert len(data) == 3
  3376. assert data[0]["span.module"] == "db"
  3377. assert data[0]["avg(span.self_time)"] == 2
  3378. assert data[1]["span.module"] == "cache"
  3379. assert data[1]["avg(span.self_time)"] == 2.5
  3380. assert data[2]["span.module"] == "other"
  3381. assert data[2]["avg(span.self_time)"] == 4
  3382. class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithOnDemandMetrics(
  3383. MetricsEnhancedPerformanceTestCase
  3384. ):
  3385. viewname = "sentry-api-0-organization-events"
  3386. def setUp(self) -> None:
  3387. super().setUp()
  3388. self.url = reverse(
  3389. self.viewname, kwargs={"organization_id_or_slug": self.organization.slug}
  3390. )
  3391. self.features = {"organizations:on-demand-metrics-extraction-widgets": True}
  3392. def _create_specs(
  3393. self, params: dict[str, Any], groupbys: list[str] | None = None
  3394. ) -> list[OnDemandMetricSpec]:
  3395. """Creates all specs based on the parameters that would be passed to the endpoint."""
  3396. specs = []
  3397. for field in params["field"]:
  3398. spec = OnDemandMetricSpec(
  3399. field=field,
  3400. query=params["query"],
  3401. environment=params.get("environment"),
  3402. groupbys=groupbys,
  3403. spec_type=MetricSpecType.DYNAMIC_QUERY,
  3404. )
  3405. specs.append(spec)
  3406. return specs
  3407. def _make_on_demand_request(
  3408. self, params: dict[str, Any], extra_features: dict[str, bool] | None = None
  3409. ) -> Response:
  3410. """Ensures that the required parameters for an on-demand request are included."""
  3411. # Expected parameters for this helper function
  3412. params["dataset"] = "metricsEnhanced"
  3413. params["useOnDemandMetrics"] = "true"
  3414. params["onDemandType"] = "dynamic_query"
  3415. _features = {**self.features, **(extra_features or {})}
  3416. return self.do_request(params, features=_features)
  3417. def _assert_on_demand_response(
  3418. self,
  3419. response: Response,
  3420. expected_on_demand_query: bool | None = True,
  3421. expected_dataset: str | None = "metricsEnhanced",
  3422. ) -> None:
  3423. """Basic assertions for an on-demand request."""
  3424. assert response.status_code == 200, response.content
  3425. meta = response.data["meta"]
  3426. assert meta.get("isMetricsExtractedData", False) is expected_on_demand_query
  3427. assert meta["dataset"] == expected_dataset
  3428. def test_is_metrics_extracted_data_is_included(self) -> None:
  3429. params = {"field": ["count()"], "query": "transaction.duration:>=91", "yAxis": "count()"}
  3430. specs = self._create_specs(params)
  3431. for spec in specs:
  3432. self.store_on_demand_metric(1, spec=spec)
  3433. response = self._make_on_demand_request(params)
  3434. self._assert_on_demand_response(response)
  3435. def test_on_demand_user_misery(self) -> None:
  3436. user_misery_field = "user_misery(300)"
  3437. query = "transaction.duration:>=100"
  3438. # We store data for both specs, however, when the query builders try to query
  3439. # for the data it will not query on-demand data
  3440. for spec_version in OnDemandMetricSpecVersioning.get_spec_versions():
  3441. spec = OnDemandMetricSpec(
  3442. field=user_misery_field,
  3443. query=query,
  3444. spec_type=MetricSpecType.DYNAMIC_QUERY,
  3445. # We only allow querying the function in the latest spec version,
  3446. # otherwise, the data returned by the endpoint would be 0.05
  3447. spec_version=spec_version,
  3448. )
  3449. tags = {"satisfaction": "miserable"}
  3450. self.store_on_demand_metric(1, spec=spec, additional_tags=tags, timestamp=self.min_ago)
  3451. self.store_on_demand_metric(2, spec=spec, timestamp=self.min_ago)
  3452. params = {"field": [user_misery_field], "project": self.project.id, "query": query}
  3453. self._create_specs(params)
  3454. # We expect it to be False because we're not using the extra feature flag
  3455. response = self._make_on_demand_request(params)
  3456. self._assert_on_demand_response(response, expected_on_demand_query=False)
  3457. # Since we're using the extra feature flag we expect user_misery to be an on-demand metric
  3458. response = self._make_on_demand_request(params, {SPEC_VERSION_TWO_FLAG: True})
  3459. self._assert_on_demand_response(response, expected_on_demand_query=True)
  3460. assert response.data["data"] == [{user_misery_field: user_misery_formula(1, 2)}]
  3461. def test_on_demand_user_misery_discover_split_with_widget_id_unsaved(self) -> None:
  3462. user_misery_field = "user_misery(300)"
  3463. query = "transaction.duration:>=100"
  3464. _, widget, __ = create_widget(["count()"], "", self.project, discover_widget_split=None)
  3465. # We store data for both specs, however, when the query builders try to query
  3466. # for the data it will not query on-demand data
  3467. for spec_version in OnDemandMetricSpecVersioning.get_spec_versions():
  3468. spec = OnDemandMetricSpec(
  3469. field=user_misery_field,
  3470. query=query,
  3471. spec_type=MetricSpecType.DYNAMIC_QUERY,
  3472. # We only allow querying the function in the latest spec version,
  3473. # otherwise, the data returned by the endpoint would be 0.05
  3474. spec_version=spec_version,
  3475. )
  3476. tags = {"satisfaction": "miserable"}
  3477. self.store_on_demand_metric(1, spec=spec, additional_tags=tags, timestamp=self.min_ago)
  3478. self.store_on_demand_metric(2, spec=spec, timestamp=self.min_ago)
  3479. params = {"field": [user_misery_field], "project": self.project.id, "query": query}
  3480. self._create_specs(params)
  3481. params["dashboardWidgetId"] = widget.id
  3482. # Since we're using the extra feature flag we expect user_misery to be an on-demand metric
  3483. with mock.patch.object(widget, "save") as mock_widget_save:
  3484. response = self._make_on_demand_request(params, {SPEC_VERSION_TWO_FLAG: True})
  3485. assert bool(mock_widget_save.assert_called_once)
  3486. self._assert_on_demand_response(response, expected_on_demand_query=True)
  3487. assert response.data["data"] == [{user_misery_field: user_misery_formula(1, 2)}]
  3488. def test_on_demand_user_misery_discover_split_with_widget_id_saved(self) -> None:
  3489. user_misery_field = "user_misery(300)"
  3490. query = "transaction.duration:>=100"
  3491. _, widget, __ = create_widget(
  3492. ["count()"],
  3493. "",
  3494. self.project,
  3495. discover_widget_split=DashboardWidgetTypes.TRANSACTION_LIKE, # Transactions like uses on-demand
  3496. )
  3497. # We store data for both specs, however, when the query builders try to query
  3498. # for the data it will not query on-demand data
  3499. for spec_version in OnDemandMetricSpecVersioning.get_spec_versions():
  3500. spec = OnDemandMetricSpec(
  3501. field=user_misery_field,
  3502. query=query,
  3503. spec_type=MetricSpecType.DYNAMIC_QUERY,
  3504. # We only allow querying the function in the latest spec version,
  3505. # otherwise, the data returned by the endpoint would be 0.05
  3506. spec_version=spec_version,
  3507. )
  3508. tags = {"satisfaction": "miserable"}
  3509. self.store_on_demand_metric(1, spec=spec, additional_tags=tags, timestamp=self.min_ago)
  3510. self.store_on_demand_metric(2, spec=spec, timestamp=self.min_ago)
  3511. params = {"field": [user_misery_field], "project": self.project.id, "query": query}
  3512. self._create_specs(params)
  3513. params["dashboardWidgetId"] = widget.id
  3514. # Since we're using the extra feature flag we expect user_misery to be an on-demand metric
  3515. with mock.patch.object(widget, "save") as mock_widget_save:
  3516. response = self._make_on_demand_request(params, {SPEC_VERSION_TWO_FLAG: True})
  3517. assert bool(mock_widget_save.assert_not_called)
  3518. self._assert_on_demand_response(response, expected_on_demand_query=True)
  3519. assert response.data["data"] == [{user_misery_field: user_misery_formula(1, 2)}]
  3520. def test_on_demand_count_unique(self):
  3521. field = "count_unique(user)"
  3522. query = "transaction.duration:>0"
  3523. params = {"field": [field], "query": query}
  3524. # We do not really have to create the metrics for both specs since
  3525. # the first API call will not query any on-demand metric
  3526. for spec_version in OnDemandMetricSpecVersioning.get_spec_versions():
  3527. spec = OnDemandMetricSpec(
  3528. field=field,
  3529. query=query,
  3530. spec_type=MetricSpecType.DYNAMIC_QUERY,
  3531. spec_version=spec_version,
  3532. )
  3533. self.store_on_demand_metric(1, spec=spec, timestamp=self.min_ago)
  3534. self.store_on_demand_metric(2, spec=spec, timestamp=self.min_ago)
  3535. # The first call will not be on-demand
  3536. response = self._make_on_demand_request(params)
  3537. self._assert_on_demand_response(response, expected_on_demand_query=False)
  3538. # This second call will be on-demand
  3539. response = self._make_on_demand_request(
  3540. params, extra_features={SPEC_VERSION_TWO_FLAG: True}
  3541. )
  3542. self._assert_on_demand_response(response, expected_on_demand_query=True)
  3543. assert response.data["data"] == [{"count_unique(user)": 2}]
  3544. def test_split_decision_for_errors_widget(self):
  3545. error_data = load_data("python", timestamp=before_now(minutes=1))
  3546. self.store_event(
  3547. data={
  3548. **error_data,
  3549. "exception": {"values": [{"type": "blah", "data": {"values": []}}]},
  3550. },
  3551. project_id=self.project.id,
  3552. )
  3553. _, widget, __ = create_widget(
  3554. ["count()", "error.type"], "error.type:blah", self.project, discover_widget_split=None
  3555. )
  3556. response = self.do_request(
  3557. {
  3558. "field": ["count()", "error.type"],
  3559. "query": "error.type:blah",
  3560. "dataset": "metricsEnhanced",
  3561. "per_page": 50,
  3562. "dashboardWidgetId": widget.id,
  3563. }
  3564. )
  3565. assert response.status_code == 200, response.content
  3566. assert response.data.get("meta").get(
  3567. "discoverSplitDecision"
  3568. ) is DashboardWidgetTypes.get_type_name(DashboardWidgetTypes.ERROR_EVENTS)
  3569. widget.refresh_from_db()
  3570. assert widget.discover_widget_split == DashboardWidgetTypes.ERROR_EVENTS
  3571. assert widget.dataset_source == DatasetSourcesTypes.INFERRED.value
  3572. def test_split_decision_for_transactions_widget(self):
  3573. transaction_data = load_data("transaction", timestamp=before_now(minutes=1))
  3574. self.store_event(
  3575. data={
  3576. **transaction_data,
  3577. },
  3578. project_id=self.project.id,
  3579. )
  3580. _, widget, __ = create_widget(
  3581. ["count()", "transaction.name"], "", self.project, discover_widget_split=None
  3582. )
  3583. assert widget.discover_widget_split is None
  3584. response = self.do_request(
  3585. {
  3586. "field": ["count()", "transaction.name"],
  3587. "query": "",
  3588. "dataset": "metricsEnhanced",
  3589. "per_page": 50,
  3590. "dashboardWidgetId": widget.id,
  3591. }
  3592. )
  3593. assert response.status_code == 200, response.content
  3594. assert response.data.get("meta").get(
  3595. "discoverSplitDecision"
  3596. ) is DashboardWidgetTypes.get_type_name(DashboardWidgetTypes.TRANSACTION_LIKE)
  3597. widget.refresh_from_db()
  3598. assert widget.discover_widget_split == DashboardWidgetTypes.TRANSACTION_LIKE
  3599. assert widget.dataset_source == DatasetSourcesTypes.INFERRED.value
  3600. def test_split_decision_for_ambiguous_widget_without_data(self):
  3601. _, widget, __ = create_widget(
  3602. ["count()", "transaction.name", "error.type"],
  3603. "",
  3604. self.project,
  3605. discover_widget_split=None,
  3606. )
  3607. assert widget.discover_widget_split is None
  3608. response = self.do_request(
  3609. {
  3610. "field": ["count()", "transaction.op", "error.type"],
  3611. "query": "",
  3612. "dataset": "metricsEnhanced",
  3613. "per_page": 50,
  3614. "dashboardWidgetId": widget.id,
  3615. },
  3616. features={"organizations:performance-discover-dataset-selector": True},
  3617. )
  3618. assert response.status_code == 200, response.content
  3619. assert response.data.get("meta").get(
  3620. "discoverSplitDecision"
  3621. ) == DashboardWidgetTypes.get_type_name(DashboardWidgetTypes.ERROR_EVENTS)
  3622. widget.refresh_from_db()
  3623. assert widget.discover_widget_split == DashboardWidgetTypes.ERROR_EVENTS
  3624. assert widget.dataset_source == DatasetSourcesTypes.FORCED.value
  3625. def test_split_decision_for_ambiguous_widget_with_data(self):
  3626. # Store a transaction
  3627. transaction_data = load_data("transaction", timestamp=before_now(minutes=1))
  3628. self.store_event(
  3629. data={
  3630. **transaction_data,
  3631. },
  3632. project_id=self.project.id,
  3633. )
  3634. # Store an event
  3635. error_data = load_data("python", timestamp=before_now(minutes=1))
  3636. self.store_event(
  3637. data={
  3638. **error_data,
  3639. "exception": {"values": [{"type": "blah", "data": {"values": []}}]},
  3640. },
  3641. project_id=self.project.id,
  3642. )
  3643. _, widget, __ = create_widget(
  3644. ["count()"],
  3645. "",
  3646. self.project,
  3647. discover_widget_split=None,
  3648. )
  3649. assert widget.discover_widget_split is None
  3650. response = self.do_request(
  3651. {
  3652. "field": ["count()"],
  3653. "query": "",
  3654. "dataset": "metricsEnhanced",
  3655. "per_page": 50,
  3656. "dashboardWidgetId": widget.id,
  3657. },
  3658. features={"organizations:performance-discover-dataset-selector": True},
  3659. )
  3660. assert response.status_code == 200, response.content
  3661. assert response.data.get("meta").get(
  3662. "discoverSplitDecision"
  3663. ) == DashboardWidgetTypes.get_type_name(DashboardWidgetTypes.ERROR_EVENTS)
  3664. widget.refresh_from_db()
  3665. assert widget.discover_widget_split == DashboardWidgetTypes.ERROR_EVENTS
  3666. assert widget.dataset_source == DatasetSourcesTypes.FORCED.value
  3667. @mock.patch("sentry.snuba.errors.query")
  3668. def test_errors_request_made_for_saved_error_dashboard_widget_type(self, mock_errors_query):
  3669. mock_errors_query.return_value = {
  3670. "data": [],
  3671. "meta": {},
  3672. }
  3673. _, widget, __ = create_widget(
  3674. ["count()"], "", self.project, discover_widget_split=DashboardWidgetTypes.ERROR_EVENTS
  3675. )
  3676. response = self.do_request(
  3677. {
  3678. "field": [
  3679. "count()",
  3680. ],
  3681. "query": "",
  3682. "dataset": "metricsEnhanced",
  3683. "per_page": 50,
  3684. "dashboardWidgetId": widget.id,
  3685. }
  3686. )
  3687. assert response.status_code == 200, response.content
  3688. mock_errors_query.assert_called_once()
  3689. @mock.patch("sentry.snuba.metrics_enhanced_performance.query")
  3690. def test_metrics_enhanced_request_made_for_saved_transaction_like_dashboard_widget_type(
  3691. self, mock_mep_query
  3692. ):
  3693. mock_mep_query.return_value = {
  3694. "data": [],
  3695. "meta": {},
  3696. }
  3697. _, widget, __ = create_widget(
  3698. ["count()"],
  3699. "",
  3700. self.project,
  3701. discover_widget_split=DashboardWidgetTypes.TRANSACTION_LIKE,
  3702. )
  3703. response = self.do_request(
  3704. {
  3705. "field": [
  3706. "count()",
  3707. ],
  3708. "query": "",
  3709. "dataset": "metricsEnhanced",
  3710. "per_page": 50,
  3711. "dashboardWidgetId": widget.id,
  3712. }
  3713. )
  3714. assert response.status_code == 200, response.content
  3715. mock_mep_query.assert_called_once()
  3716. class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithMetricLayer(
  3717. OrganizationEventsMetricsEnhancedPerformanceEndpointTest
  3718. ):
  3719. def setUp(self):
  3720. super().setUp()
  3721. self.features["organizations:use-metrics-layer"] = True
  3722. @pytest.mark.xfail(reason="Not supported")
  3723. def test_time_spent(self):
  3724. super().test_time_spent()
  3725. @pytest.mark.xfail(reason="Not supported")
  3726. def test_http_error_rate(self):
  3727. super().test_http_error_rate()
  3728. @pytest.mark.xfail(reason="Multiple aliases to same column not supported")
  3729. def test_title_and_transaction_alias(self):
  3730. super().test_title_and_transaction_alias()
  3731. @pytest.mark.xfail(reason="Sort order is flaking when querying multiple datasets")
  3732. def test_maintain_sort_order_across_datasets(self):
  3733. """You may need to run this test a few times to get it to fail"""
  3734. super().test_maintain_sort_order_across_datasets()
  3735. @pytest.mark.xfail(reason="Not implemented")
  3736. def test_avg_compare(self):
  3737. super().test_avg_compare()
  3738. @pytest.mark.xfail(reason="Not implemented")
  3739. def test_avg_if(self):
  3740. super().test_avg_if()
  3741. @pytest.mark.xfail(reason="Not implemented")
  3742. def test_count_if(self):
  3743. super().test_count_if()
  3744. @pytest.mark.xfail(reason="Not implemented")
  3745. def test_device_class(self):
  3746. super().test_device_class()
  3747. @pytest.mark.xfail(reason="Not implemented")
  3748. def test_device_class_filter(self):
  3749. super().test_device_class_filter()
  3750. @pytest.mark.xfail(reason="Not implemented")
  3751. def test_performance_score(self):
  3752. super().test_performance_score()
  3753. @pytest.mark.xfail(reason="Not implemented")
  3754. def test_performance_score_boundaries(self):
  3755. super().test_performance_score_boundaries()
  3756. @pytest.mark.xfail(reason="Not implemented")
  3757. def test_total_performance_score(self):
  3758. super().test_total_performance_score()
  3759. @pytest.mark.xfail(reason="Not implemented")
  3760. def test_total_performance_score_with_missing_vitals(self):
  3761. super().test_total_performance_score_with_missing_vitals()
  3762. @pytest.mark.xfail(reason="Not implemented")
  3763. def test_invalid_performance_score_column(self):
  3764. super().test_invalid_performance_score_column()
  3765. @pytest.mark.xfail(reason="Not implemented")
  3766. def test_opportunity_score(self):
  3767. super().test_opportunity_score()
  3768. @pytest.mark.xfail(reason="Not implemented")
  3769. def test_opportunity_score_with_fixed_weights(self):
  3770. super().test_opportunity_score_with_fixed_weights()
  3771. @pytest.mark.xfail(reason="Not implemented")
  3772. def test_opportunity_score_with_fixed_weights_and_missing_vitals(self):
  3773. super().test_opportunity_score_with_fixed_weights_and_missing_vitals()
  3774. @pytest.mark.xfail(reason="Not implemented")
  3775. def test_count_scores(self):
  3776. super().test_count_scores()
  3777. @pytest.mark.xfail(reason="Not implemented")
  3778. def test_count_starts(self):
  3779. super().test_count_starts()
  3780. @pytest.mark.xfail(reason="Not implemented")
  3781. def test_count_starts_returns_all_counts_when_no_arg_is_passed(self):
  3782. super().test_count_starts_returns_all_counts_when_no_arg_is_passed()
  3783. @pytest.mark.xfail(reason="Not implemented")
  3784. def test_timestamp_groupby(self):
  3785. super().test_timestamp_groupby()
  3786. @pytest.mark.xfail(reason="Not implemented")
  3787. def test_on_demand_with_mep(self):
  3788. super().test_on_demand_with_mep()
  3789. @pytest.mark.xfail(reason="Not implemented")
  3790. def test_cache_miss_rate(self):
  3791. super().test_cache_miss_rate()
  3792. @pytest.mark.xfail(reason="Not implemented")
  3793. def test_http_response_rate(self):
  3794. super().test_http_response_rate()
  3795. @pytest.mark.xfail(reason="Not implemented")
  3796. def test_avg_span_self_time(self):
  3797. super().test_avg_span_self_time()
  3798. @pytest.mark.xfail(reason="Not implemented")
  3799. def test_avg_message_receive_latency_gauge_functions(self):
  3800. super().test_avg_message_receive_latency_gauge_functions()
  3801. @pytest.mark.xfail(reason="Not implemented")
  3802. def test_span_module_filter(self):
  3803. super().test_span_module_filter()