test_organization_events_mep.py 124 KB

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