test_organization_events_mep.py 147 KB

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