test_organization_events.py 232 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141
  1. import math
  2. from base64 import b64encode
  3. from datetime import timedelta
  4. from unittest import mock
  5. import pytest
  6. from django.test import override_settings
  7. from django.urls import reverse
  8. from django.utils import timezone
  9. from freezegun import freeze_time
  10. from pytz import utc
  11. from snuba_sdk.column import Column
  12. from snuba_sdk.function import Function
  13. from sentry.discover.models import TeamKeyTransaction
  14. from sentry.issues.grouptype import ProfileFileIOGroupType
  15. from sentry.models import ApiKey, ProjectTeam, ProjectTransactionThreshold, ReleaseStages
  16. from sentry.models.transaction_threshold import (
  17. ProjectTransactionThresholdOverride,
  18. TransactionMetric,
  19. )
  20. from sentry.search.events import constants
  21. from sentry.testutils import APITestCase, SnubaTestCase
  22. from sentry.testutils.cases import PerformanceIssueTestCase
  23. from sentry.testutils.helpers import parse_link_header
  24. from sentry.testutils.helpers.datetime import before_now, iso_format
  25. from sentry.testutils.silo import region_silo_test
  26. from sentry.testutils.skips import requires_not_arm64
  27. from sentry.utils import json
  28. from sentry.utils.samples import load_data
  29. from sentry.utils.snuba import QueryExecutionError, QueryIllegalTypeOfArgument, RateLimitExceeded
  30. from tests.sentry.issues.test_utils import SearchIssueTestMixin
  31. MAX_QUERYABLE_TRANSACTION_THRESHOLDS = 1
  32. class OrganizationEventsEndpointTestBase(APITestCase, SnubaTestCase):
  33. viewname = "sentry-api-0-organization-events"
  34. referrer = "api.organization-events"
  35. def setUp(self):
  36. super().setUp()
  37. self.nine_mins_ago = before_now(minutes=9)
  38. self.ten_mins_ago = before_now(minutes=10)
  39. self.ten_mins_ago_iso = iso_format(self.ten_mins_ago)
  40. self.eleven_mins_ago = before_now(minutes=11)
  41. self.eleven_mins_ago_iso = iso_format(self.eleven_mins_ago)
  42. self.transaction_data = load_data("transaction", timestamp=self.ten_mins_ago)
  43. self.features = {}
  44. def client_get(self, *args, **kwargs):
  45. return self.client.get(*args, **kwargs)
  46. def reverse_url(self):
  47. return reverse(
  48. self.viewname,
  49. kwargs={"organization_slug": self.organization.slug},
  50. )
  51. def do_request(self, query, features=None, **kwargs):
  52. if features is None:
  53. features = {"organizations:discover-basic": True}
  54. features.update(self.features)
  55. self.login_as(user=self.user)
  56. with self.feature(features):
  57. return self.client_get(self.reverse_url(), query, format="json", **kwargs)
  58. def load_data(self, platform="transaction", timestamp=None, duration=None, **kwargs):
  59. if timestamp is None:
  60. timestamp = self.ten_mins_ago
  61. min_age = before_now(minutes=10)
  62. if timestamp > min_age:
  63. # Sentry does some rounding of timestamps to improve cache hits in snuba.
  64. # This can result in events not being returns if the timestamps
  65. # are too recent.
  66. raise Exception(
  67. f"Please define a timestamp older than 10 minutes to avoid flakey tests. Want a timestamp before {min_age}, got: {timestamp} "
  68. )
  69. start_timestamp = None
  70. if duration is not None:
  71. start_timestamp = timestamp - duration
  72. start_timestamp = start_timestamp - timedelta(
  73. microseconds=start_timestamp.microsecond % 1000
  74. )
  75. return load_data(platform, timestamp=timestamp, start_timestamp=start_timestamp, **kwargs)
  76. @region_silo_test
  77. class OrganizationEventsEndpointTest(OrganizationEventsEndpointTestBase, PerformanceIssueTestCase):
  78. def test_no_projects(self):
  79. response = self.do_request({})
  80. assert response.status_code == 200, response.content
  81. assert response.data["data"] == []
  82. assert response.data["meta"] == {
  83. "tips": {"query": "Need at least one valid project to query."}
  84. }
  85. def test_api_key_request(self):
  86. self.store_event(
  87. data={
  88. "event_id": "a" * 32,
  89. "environment": "staging",
  90. "timestamp": self.ten_mins_ago_iso,
  91. },
  92. project_id=self.project.id,
  93. )
  94. # Project ID cannot be inferred when using an org API key, so that must
  95. # be passed in the parameters
  96. api_key = ApiKey.objects.create(
  97. organization_id=self.organization.id, scope_list=["org:read"]
  98. )
  99. query = {"field": ["project.name", "environment"], "project": [self.project.id]}
  100. url = self.reverse_url()
  101. response = self.client_get(
  102. url,
  103. query,
  104. format="json",
  105. HTTP_AUTHORIZATION=b"Basic " + b64encode(f"{api_key.key}:".encode()),
  106. )
  107. assert response.status_code == 200, response.content
  108. assert len(response.data["data"]) == 1
  109. assert response.data["data"][0]["project.name"] == self.project.slug
  110. def test_environment_filter(self):
  111. self.create_environment(self.project, name="production")
  112. self.store_event(
  113. data={
  114. "event_id": "a" * 32,
  115. "environment": "staging",
  116. "timestamp": self.ten_mins_ago_iso,
  117. },
  118. project_id=self.project.id,
  119. )
  120. self.store_event(
  121. data={
  122. "event_id": "b" * 32,
  123. "environment": "production",
  124. "timestamp": self.ten_mins_ago_iso,
  125. },
  126. project_id=self.project.id,
  127. )
  128. query = {
  129. "field": ["id", "project.id"],
  130. "project": [self.project.id],
  131. "environment": ["staging", "production"],
  132. }
  133. response = self.do_request(query)
  134. assert response.status_code == 200
  135. assert len(response.data["data"]) == 2
  136. def test_performance_view_feature(self):
  137. self.store_event(
  138. data={
  139. "event_id": "a" * 32,
  140. "timestamp": self.ten_mins_ago_iso,
  141. "fingerprint": ["group1"],
  142. },
  143. project_id=self.project.id,
  144. )
  145. query = {"field": ["id", "project.id"], "project": [self.project.id]}
  146. response = self.do_request(query)
  147. assert response.status_code == 200
  148. assert len(response.data["data"]) == 1
  149. def test_multi_project_feature_gate_rejection(self):
  150. team = self.create_team(organization=self.organization, members=[self.user])
  151. project = self.create_project(organization=self.organization, teams=[team])
  152. project2 = self.create_project(organization=self.organization, teams=[team])
  153. query = {"field": ["id", "project.id"], "project": [project.id, project2.id]}
  154. response = self.do_request(query)
  155. assert response.status_code == 400
  156. assert "events from multiple projects" in response.data["detail"]
  157. def test_multi_project_feature_gate_replays(self):
  158. team = self.create_team(organization=self.organization, members=[self.user])
  159. project = self.create_project(organization=self.organization, teams=[team])
  160. project2 = self.create_project(organization=self.organization, teams=[team])
  161. query = {"field": ["id", "project.id"], "project": [project.id, project2.id]}
  162. response = self.do_request(query, **{"HTTP_X-Sentry-Replay-Request": "1"})
  163. assert response.status_code == 200
  164. def test_invalid_search_terms(self):
  165. self.create_project()
  166. query = {"field": ["id"], "query": "hi \n there"}
  167. response = self.do_request(query)
  168. assert response.status_code == 400, response.content
  169. assert (
  170. response.data["detail"]
  171. == "Parse error at 'hi \n ther' (column 4). This is commonly caused by unmatched parentheses. Enclose any text in double quotes."
  172. )
  173. def test_invalid_trace_span(self):
  174. self.create_project()
  175. query = {"field": ["id"], "query": "trace.span:invalid"}
  176. response = self.do_request(query)
  177. assert response.status_code == 400, response.content
  178. assert (
  179. response.data["detail"]
  180. == "trace.span must be a valid 16 character hex (containing only digits, or a-f characters)"
  181. )
  182. query = {"field": ["id"], "query": "trace.parent_span:invalid"}
  183. response = self.do_request(query)
  184. assert response.status_code == 400, response.content
  185. assert (
  186. response.data["detail"]
  187. == "trace.parent_span must be a valid 16 character hex (containing only digits, or a-f characters)"
  188. )
  189. query = {"field": ["id"], "query": "trace.span:*"}
  190. response = self.do_request(query)
  191. assert response.status_code == 400, response.content
  192. assert (
  193. response.data["detail"] == "Wildcard conditions are not permitted on `trace.span` field"
  194. )
  195. query = {"field": ["id"], "query": "trace.parent_span:*"}
  196. response = self.do_request(query)
  197. assert response.status_code == 400, response.content
  198. assert (
  199. response.data["detail"]
  200. == "Wildcard conditions are not permitted on `trace.parent_span` field"
  201. )
  202. def test_has_trace_context(self):
  203. self.store_event(
  204. data={
  205. "event_id": "a" * 32,
  206. "message": "how to make fast",
  207. "timestamp": self.ten_mins_ago_iso,
  208. "contexts": {
  209. "trace": {
  210. "span_id": "a" * 16,
  211. "trace_id": "b" * 32,
  212. },
  213. },
  214. },
  215. project_id=self.project.id,
  216. )
  217. query = {"field": ["id", "trace.parent_span"], "query": "has:trace.span"}
  218. response = self.do_request(query)
  219. assert response.status_code == 200, response.content
  220. assert len(response.data["data"]) == 1
  221. assert response.data["data"][0]["id"] == "a" * 32
  222. query = {"field": ["id"], "query": "has:trace.parent_span"}
  223. response = self.do_request(query)
  224. assert response.status_code == 200, response.content
  225. assert len(response.data["data"]) == 0
  226. def test_not_has_trace_context(self):
  227. self.store_event(
  228. data={
  229. "event_id": "a" * 32,
  230. "message": "how to make fast",
  231. "timestamp": self.ten_mins_ago_iso,
  232. "contexts": {
  233. "trace": {
  234. "span_id": "a" * 16,
  235. "trace_id": "b" * 32,
  236. },
  237. },
  238. },
  239. project_id=self.project.id,
  240. )
  241. query = {"field": ["id", "trace.parent_span"], "query": "!has:trace.span"}
  242. response = self.do_request(query)
  243. assert response.status_code == 200, response.content
  244. assert len(response.data["data"]) == 0
  245. query = {"field": ["id"], "query": "!has:trace.parent_span"}
  246. response = self.do_request(query)
  247. assert response.status_code == 200, response.content
  248. assert len(response.data["data"]) == 1
  249. assert response.data["data"][0]["id"] == "a" * 32
  250. def test_parent_span_id_in_context(self):
  251. self.store_event(
  252. data={
  253. "event_id": "a" * 32,
  254. "message": "how to make fast",
  255. "timestamp": self.ten_mins_ago_iso,
  256. "contexts": {
  257. "trace": {
  258. "span_id": "a" * 16,
  259. "trace_id": "b" * 32,
  260. "parent_span_id": "c" * 16,
  261. },
  262. },
  263. },
  264. project_id=self.project.id,
  265. )
  266. query = {"field": ["id"], "query": f"trace.parent_span:{'c' * 16}"}
  267. response = self.do_request(query)
  268. assert response.status_code == 200, response.content
  269. assert len(response.data["data"]) == 1
  270. assert response.data["data"][0]["id"] == "a" * 32
  271. @mock.patch("sentry.search.events.builder.discover.raw_snql_query")
  272. def test_handling_snuba_errors(self, mock_snql_query):
  273. self.create_project()
  274. mock_snql_query.side_effect = RateLimitExceeded("test")
  275. query = {"field": ["id", "timestamp"], "orderby": ["-timestamp", "-id"]}
  276. response = self.do_request(query)
  277. assert response.status_code == 400, response.content
  278. assert response.data["detail"] == constants.TIMEOUT_ERROR_MESSAGE
  279. mock_snql_query.side_effect = QueryExecutionError("test")
  280. query = {"field": ["id", "timestamp"], "orderby": ["-timestamp", "-id"]}
  281. response = self.do_request(query)
  282. assert response.status_code == 500, response.content
  283. assert response.data["detail"] == "Internal error. Your query failed to run."
  284. mock_snql_query.side_effect = QueryIllegalTypeOfArgument("test")
  285. query = {"field": ["id", "timestamp"], "orderby": ["-timestamp", "-id"]}
  286. response = self.do_request(query)
  287. assert response.status_code == 400, response.content
  288. assert response.data["detail"] == "Invalid query. Argument to function is wrong type."
  289. def test_out_of_retention(self):
  290. self.create_project()
  291. with self.options({"system.event-retention-days": 10}):
  292. query = {
  293. "field": ["id", "timestamp"],
  294. "orderby": ["-timestamp", "-id"],
  295. "start": iso_format(before_now(days=20)),
  296. "end": iso_format(before_now(days=15)),
  297. }
  298. response = self.do_request(query)
  299. assert response.status_code == 400, response.content
  300. assert response.data["detail"] == "Invalid date range. Please try a more recent date range."
  301. def test_raw_data(self):
  302. self.store_event(
  303. data={
  304. "event_id": "a" * 32,
  305. "environment": "staging",
  306. "timestamp": self.eleven_mins_ago_iso,
  307. "user": {"ip_address": "127.0.0.1", "email": "foo@example.com"},
  308. },
  309. project_id=self.project.id,
  310. )
  311. self.store_event(
  312. data={
  313. "event_id": "b" * 32,
  314. "environment": "staging",
  315. "timestamp": self.ten_mins_ago_iso,
  316. "user": {"ip_address": "127.0.0.1", "email": "foo@example.com"},
  317. },
  318. project_id=self.project.id,
  319. )
  320. query = {
  321. "field": ["id", "project.id", "user.email", "user.ip", "timestamp"],
  322. "orderby": "-timestamp",
  323. }
  324. response = self.do_request(query)
  325. assert response.status_code == 200, response.content
  326. data = response.data["data"]
  327. assert len(data) == 2
  328. assert data[0]["id"] == "b" * 32
  329. assert data[0]["project.id"] == self.project.id
  330. assert data[0]["user.email"] == "foo@example.com"
  331. assert "project.name" not in data[0], "project.id does not auto select name"
  332. assert "project" not in data[0]
  333. meta = response.data["meta"]
  334. field_meta = meta["fields"]
  335. assert field_meta["id"] == "string"
  336. assert field_meta["user.email"] == "string"
  337. assert field_meta["user.ip"] == "string"
  338. assert field_meta["timestamp"] == "date"
  339. def test_project_name(self):
  340. self.store_event(
  341. data={
  342. "event_id": "a" * 32,
  343. "environment": "staging",
  344. "timestamp": self.ten_mins_ago_iso,
  345. },
  346. project_id=self.project.id,
  347. )
  348. query = {"field": ["project.name", "environment"]}
  349. response = self.do_request(query)
  350. assert response.status_code == 200, response.content
  351. assert len(response.data["data"]) == 1
  352. assert response.data["data"][0]["project.name"] == self.project.slug
  353. assert "project.id" not in response.data["data"][0]
  354. assert response.data["data"][0]["environment"] == "staging"
  355. def test_project_without_name(self):
  356. self.store_event(
  357. data={
  358. "event_id": "a" * 32,
  359. "environment": "staging",
  360. "timestamp": self.ten_mins_ago_iso,
  361. },
  362. project_id=self.project.id,
  363. )
  364. query = {"field": ["project", "environment"]}
  365. response = self.do_request(query)
  366. assert response.status_code == 200, response.content
  367. assert len(response.data["data"]) == 1
  368. assert response.data["data"][0]["project"] == self.project.slug
  369. assert response.data["meta"]["fields"]["project"] == "string"
  370. assert "project.id" not in response.data["data"][0]
  371. assert response.data["data"][0]["environment"] == "staging"
  372. def test_project_in_query(self):
  373. self.store_event(
  374. data={
  375. "event_id": "a" * 32,
  376. "environment": "staging",
  377. "timestamp": self.ten_mins_ago_iso,
  378. },
  379. project_id=self.project.id,
  380. )
  381. query = {
  382. "field": ["project", "count()"],
  383. "query": f'project:"{self.project.slug}"',
  384. "statsPeriod": "14d",
  385. }
  386. response = self.do_request(query)
  387. assert response.status_code == 200, response.content
  388. assert len(response.data["data"]) == 1
  389. assert response.data["data"][0]["project"] == self.project.slug
  390. assert "project.id" not in response.data["data"][0]
  391. def test_project_in_query_not_in_header(self):
  392. project = self.create_project()
  393. other_project = self.create_project()
  394. self.store_event(
  395. data={
  396. "event_id": "a" * 32,
  397. "environment": "staging",
  398. "timestamp": self.ten_mins_ago_iso,
  399. },
  400. project_id=project.id,
  401. )
  402. query = {
  403. "field": ["project", "count()"],
  404. "query": 'project:"%s"' % project.slug,
  405. "statsPeriod": "14d",
  406. "project": other_project.id,
  407. }
  408. response = self.do_request(query)
  409. assert response.status_code == 400, response.content
  410. assert (
  411. response.data["detail"]
  412. == f"Invalid query. Project(s) {project.slug} do not exist or are not actively selected."
  413. )
  414. def test_project_in_query_does_not_exist(self):
  415. self.create_project()
  416. query = {"field": ["project", "count()"], "query": "project:morty", "statsPeriod": "14d"}
  417. response = self.do_request(query)
  418. assert response.status_code == 400, response.content
  419. assert (
  420. response.data["detail"]
  421. == "Invalid query. Project(s) morty do not exist or are not actively selected."
  422. )
  423. def test_not_project_in_query_but_in_header(self):
  424. team = self.create_team(organization=self.organization, members=[self.user])
  425. project = self.create_project(organization=self.organization, teams=[team])
  426. project2 = self.create_project(organization=self.organization, teams=[team])
  427. self.store_event(
  428. data={
  429. "event_id": "a" * 32,
  430. "timestamp": self.ten_mins_ago_iso,
  431. "fingerprint": ["group1"],
  432. },
  433. project_id=project.id,
  434. )
  435. self.store_event(
  436. data={
  437. "event_id": "b" * 32,
  438. "timestamp": self.ten_mins_ago_iso,
  439. "fingerprint": ["group2"],
  440. },
  441. project_id=project2.id,
  442. )
  443. query = {
  444. "field": ["id", "project.id"],
  445. "project": [project.id],
  446. "query": f"!project:{project2.slug}",
  447. }
  448. response = self.do_request(query)
  449. assert response.status_code == 200
  450. assert response.data["data"] == [{"id": "a" * 32, "project.id": project.id}]
  451. def test_not_project_in_query_with_all_projects(self):
  452. team = self.create_team(organization=self.organization, members=[self.user])
  453. project = self.create_project(organization=self.organization, teams=[team])
  454. project2 = self.create_project(organization=self.organization, teams=[team])
  455. self.store_event(
  456. data={
  457. "event_id": "a" * 32,
  458. "timestamp": self.ten_mins_ago_iso,
  459. "fingerprint": ["group1"],
  460. },
  461. project_id=project.id,
  462. )
  463. self.store_event(
  464. data={
  465. "event_id": "b" * 32,
  466. "timestamp": self.ten_mins_ago_iso,
  467. "fingerprint": ["group2"],
  468. },
  469. project_id=project2.id,
  470. )
  471. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  472. query = {
  473. "field": ["id", "project.id"],
  474. "project": [-1],
  475. "query": f"!project:{project2.slug}",
  476. }
  477. response = self.do_request(query, features=features)
  478. assert response.status_code == 200
  479. assert response.data["data"] == [{"id": "a" * 32, "project.id": project.id}]
  480. def test_project_condition_used_for_automatic_filters(self):
  481. self.store_event(
  482. data={
  483. "event_id": "a" * 32,
  484. "environment": "staging",
  485. "timestamp": self.ten_mins_ago_iso,
  486. },
  487. project_id=self.project.id,
  488. )
  489. query = {
  490. "field": ["project", "count()"],
  491. "query": f'project:"{self.project.slug}"',
  492. "statsPeriod": "14d",
  493. }
  494. response = self.do_request(query)
  495. assert response.status_code == 200, response.content
  496. assert len(response.data["data"]) == 1
  497. assert response.data["data"][0]["project"] == self.project.slug
  498. assert "project.id" not in response.data["data"][0]
  499. def test_auto_insert_project_name_when_event_id_present(self):
  500. self.store_event(
  501. data={
  502. "event_id": "a" * 32,
  503. "environment": "staging",
  504. "timestamp": self.ten_mins_ago_iso,
  505. },
  506. project_id=self.project.id,
  507. )
  508. query = {"field": ["id"], "statsPeriod": "1h"}
  509. response = self.do_request(query)
  510. assert response.status_code == 200, response.content
  511. assert response.data["data"] == [{"project.name": self.project.slug, "id": "a" * 32}]
  512. def test_auto_insert_project_name_when_event_id_present_with_aggregate(self):
  513. self.store_event(
  514. data={
  515. "event_id": "a" * 32,
  516. "environment": "staging",
  517. "timestamp": self.ten_mins_ago_iso,
  518. },
  519. project_id=self.project.id,
  520. )
  521. query = {"field": ["id", "count()"], "statsPeriod": "1h"}
  522. response = self.do_request(query)
  523. assert response.status_code == 200, response.content
  524. assert response.data["data"] == [
  525. {"project.name": self.project.slug, "id": "a" * 32, "count()": 1}
  526. ]
  527. def test_performance_short_group_id(self):
  528. event = self.create_performance_issue()
  529. query = {
  530. "field": ["count()"],
  531. "statsPeriod": "1h",
  532. "query": f"project:{event.group.project.slug} issue:{event.group.qualified_short_id}",
  533. "dataset": "issuePlatform",
  534. }
  535. response = self.do_request(query)
  536. assert response.status_code == 200, response.content
  537. assert response.data["data"][0]["count()"] == 1
  538. def test_multiple_performance_short_group_ids_filter(self):
  539. event1 = self.create_performance_issue()
  540. event2 = self.create_performance_issue()
  541. query = {
  542. "field": ["count()"],
  543. "statsPeriod": "1h",
  544. "query": f"project:{event1.group.project.slug} issue:[{event1.group.qualified_short_id},{event2.group.qualified_short_id}]",
  545. "dataset": "issuePlatform",
  546. }
  547. response = self.do_request(query)
  548. assert response.status_code == 200, response.content
  549. assert response.data["data"][0]["count()"] == 2
  550. def test_event_id_with_in_search(self):
  551. self.store_event(
  552. data={
  553. "event_id": "a" * 32,
  554. "environment": "staging1",
  555. "timestamp": self.ten_mins_ago_iso,
  556. },
  557. project_id=self.project.id,
  558. )
  559. self.store_event(
  560. data={
  561. "event_id": "b" * 32,
  562. "environment": "staging2",
  563. "timestamp": self.ten_mins_ago_iso,
  564. },
  565. project_id=self.project.id,
  566. )
  567. # Should not show up
  568. self.store_event(
  569. data={
  570. "event_id": "c" * 32,
  571. "environment": "staging3",
  572. "timestamp": self.ten_mins_ago_iso,
  573. },
  574. project_id=self.project.id,
  575. )
  576. query = {
  577. "field": ["id", "environment"],
  578. "statsPeriod": "1h",
  579. "query": f"id:[{'a' * 32}, {'b' * 32}]",
  580. "orderby": "environment",
  581. }
  582. response = self.do_request(query)
  583. assert response.status_code == 200, response.content
  584. assert len(response.data["data"]) == 2
  585. assert response.data["data"][0]["id"] == "a" * 32
  586. assert response.data["data"][1]["id"] == "b" * 32
  587. def test_user_search(self):
  588. self.transaction_data["user"] = {
  589. "email": "foo@example.com",
  590. "id": "123",
  591. "ip_address": "127.0.0.1",
  592. "username": "foo",
  593. }
  594. self.store_event(self.transaction_data, project_id=self.project.id)
  595. fields = {
  596. "email": "user.email",
  597. "id": "user.id",
  598. "ip_address": "user.ip",
  599. "username": "user.username",
  600. }
  601. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  602. for key, value in self.transaction_data["user"].items():
  603. field = fields[key]
  604. query = {
  605. "field": ["project", "user"],
  606. "query": f"{field}:{value}",
  607. "statsPeriod": "14d",
  608. }
  609. response = self.do_request(query, features=features)
  610. assert response.status_code == 200, response.content
  611. assert len(response.data["data"]) == 1
  612. assert response.data["data"][0]["project"] == self.project.slug
  613. assert response.data["data"][0]["user"] == "id:123"
  614. def test_has_user(self):
  615. self.store_event(self.transaction_data, project_id=self.project.id)
  616. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  617. for value in self.transaction_data["user"].values():
  618. query = {"field": ["project", "user"], "query": "has:user", "statsPeriod": "14d"}
  619. response = self.do_request(query, features=features)
  620. assert response.status_code == 200, response.content
  621. assert len(response.data["data"]) == 1
  622. assert response.data["data"][0]["user"] == "ip:{}".format(
  623. self.transaction_data["user"]["ip_address"]
  624. )
  625. def test_team_param_no_access(self):
  626. org = self.create_organization(
  627. owner=self.user, # use other user as owner
  628. name="foo",
  629. flags=0, # disable default allow_joinleave
  630. )
  631. project = self.create_project(name="baz", organization=org)
  632. user = self.create_user()
  633. self.login_as(user=user, superuser=False)
  634. team = self.create_team(organization=org, name="Team Bar")
  635. project.add_team(team)
  636. self.store_event(
  637. data={
  638. "event_id": "a" * 32,
  639. "timestamp": self.ten_mins_ago_iso,
  640. "fingerprint": ["group1"],
  641. },
  642. project_id=project.id,
  643. )
  644. query = {"field": ["id", "project.id"], "project": [project.id], "team": [team.id]}
  645. response = self.do_request(query)
  646. assert response.status_code == 403, response.content
  647. assert response.data["detail"] == "You do not have permission to perform this action."
  648. def test_team_is_nan(self):
  649. query = {"field": ["id"], "project": [self.project.id], "team": [math.nan]}
  650. response = self.do_request(query)
  651. assert response.status_code == 400, response.content
  652. assert response.data["detail"] == "Invalid Team ID: nan"
  653. def test_comparison_operators_on_numeric_field(self):
  654. event = self.store_event(
  655. {"timestamp": iso_format(before_now(minutes=1))}, project_id=self.project.id
  656. )
  657. query = {"field": ["issue"], "query": f"issue.id:>{event.group.id - 1}"}
  658. response = self.do_request(query)
  659. assert response.status_code == 200, response.content
  660. assert len(response.data["data"]) == 1
  661. assert response.data["data"][0]["issue"] == event.group.qualified_short_id
  662. query = {"field": ["issue"], "query": f"issue.id:>{event.group.id}"}
  663. response = self.do_request(query)
  664. assert response.status_code == 200, response.content
  665. assert len(response.data["data"]) == 0
  666. def test_negation_on_numeric_field_excludes_issue(self):
  667. event = self.store_event({"timestamp": self.ten_mins_ago_iso}, project_id=self.project.id)
  668. query = {"field": ["issue"], "query": f"issue.id:{event.group.id}"}
  669. response = self.do_request(query)
  670. assert response.status_code == 200, response.content
  671. assert len(response.data["data"]) == 1
  672. assert response.data["data"][0]["issue"] == event.group.qualified_short_id
  673. query = {"field": ["issue"], "query": f"!issue.id:{event.group.id}"}
  674. response = self.do_request(query)
  675. assert response.status_code == 200, response.content
  676. assert len(response.data["data"]) == 0
  677. def test_negation_on_numeric_in_filter_excludes_issue(self):
  678. event = self.store_event({"timestamp": self.ten_mins_ago_iso}, project_id=self.project.id)
  679. query = {"field": ["issue"], "query": f"issue.id:[{event.group.id}]"}
  680. response = self.do_request(query)
  681. assert response.status_code == 200, response.content
  682. assert len(response.data["data"]) == 1
  683. assert response.data["data"][0]["issue"] == event.group.qualified_short_id
  684. query = {"field": ["issue"], "query": f"!issue.id:[{event.group.id}]"}
  685. response = self.do_request(query)
  686. assert response.status_code == 200, response.content
  687. assert len(response.data["data"]) == 0
  688. def test_negation_on_duration_filter_excludes_transaction(self):
  689. event = self.store_event(self.transaction_data, project_id=self.project.id)
  690. duration = int(event.data.get("timestamp") - event.data.get("start_timestamp")) * 1000
  691. query = {"field": ["transaction"], "query": f"transaction.duration:{duration}"}
  692. response = self.do_request(query)
  693. assert response.status_code == 200, response.content
  694. assert len(response.data["data"]) == 1
  695. assert response.data["data"][0]["id"] == event.event_id
  696. query = {"field": ["transaction"], "query": f"!transaction.duration:{duration}"}
  697. response = self.do_request(query)
  698. assert response.status_code == 200, response.content
  699. assert len(response.data["data"]) == 0
  700. def test_has_issue(self):
  701. event = self.store_event({"timestamp": self.ten_mins_ago_iso}, project_id=self.project.id)
  702. self.store_event(self.transaction_data, project_id=self.project.id)
  703. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  704. # should only show 1 event of type default
  705. query = {"field": ["project", "issue"], "query": "has:issue", "statsPeriod": "14d"}
  706. response = self.do_request(query, features=features)
  707. assert response.status_code == 200, response.content
  708. assert len(response.data["data"]) == 1
  709. assert response.data["data"][0]["issue"] == event.group.qualified_short_id
  710. # should only show 1 event of type default
  711. query = {
  712. "field": ["project", "issue"],
  713. "query": "event.type:default has:issue",
  714. "statsPeriod": "14d",
  715. }
  716. response = self.do_request(query, features=features)
  717. assert response.status_code == 200, response.content
  718. assert len(response.data["data"]) == 1
  719. assert response.data["data"][0]["issue"] == event.group.qualified_short_id
  720. # should show no results because no the default event has an issue
  721. query = {
  722. "field": ["project", "issue"],
  723. "query": "event.type:default !has:issue",
  724. "statsPeriod": "14d",
  725. }
  726. response = self.do_request(query, features=features)
  727. assert response.status_code == 200, response.content
  728. assert len(response.data["data"]) == 0
  729. # should show no results because no transactions have issues
  730. query = {
  731. "field": ["project", "issue"],
  732. "query": "event.type:transaction has:issue",
  733. "statsPeriod": "14d",
  734. }
  735. response = self.do_request(query, features=features)
  736. assert response.status_code == 200, response.content
  737. assert len(response.data["data"]) == 0
  738. # should only show 1 event of type transaction since they don't have issues
  739. query = {
  740. "field": ["project", "issue"],
  741. "query": "event.type:transaction !has:issue",
  742. "statsPeriod": "14d",
  743. }
  744. response = self.do_request(query, features=features)
  745. assert response.status_code == 200, response.content
  746. assert len(response.data["data"]) == 1
  747. assert response.data["data"][0]["issue"] == "unknown"
  748. @pytest.mark.skip("Cannot look up group_id of transaction events")
  749. def test_unknown_issue(self):
  750. event = self.store_event({"timestamp": self.ten_mins_ago_iso}, project_id=self.project.id)
  751. self.store_event(self.transaction_data, project_id=self.project.id)
  752. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  753. query = {"field": ["project", "issue"], "query": "issue:unknown", "statsPeriod": "14d"}
  754. response = self.do_request(query, features=features)
  755. assert response.status_code == 200, response.content
  756. assert len(response.data["data"]) == 1
  757. assert response.data["data"][0]["issue"] == "unknown"
  758. query = {"field": ["project", "issue"], "query": "!issue:unknown", "statsPeriod": "14d"}
  759. response = self.do_request(query, features=features)
  760. assert response.status_code == 200, response.content
  761. assert len(response.data["data"]) == 1
  762. assert response.data["data"][0]["issue"] == event.group.qualified_short_id
  763. def test_negative_user_search(self):
  764. user_data = {"email": "foo@example.com", "id": "123", "username": "foo"}
  765. # Load an event with data that shouldn't match
  766. data = self.transaction_data.copy()
  767. data["transaction"] = "/transactions/nomatch"
  768. event_user = user_data.copy()
  769. event_user["id"] = "undefined"
  770. data["user"] = event_user
  771. self.store_event(data, project_id=self.project.id)
  772. # Load a matching event
  773. data = self.transaction_data.copy()
  774. data["transaction"] = "/transactions/matching"
  775. data["user"] = user_data
  776. self.store_event(data, project_id=self.project.id)
  777. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  778. query = {
  779. "field": ["project", "user"],
  780. "query": '!user:"id:undefined"',
  781. "statsPeriod": "14d",
  782. }
  783. response = self.do_request(query, features=features)
  784. assert response.status_code == 200, response.content
  785. assert len(response.data["data"]) == 1
  786. assert response.data["data"][0]["user"] == "id:{}".format(user_data["id"])
  787. assert "user.email" not in response.data["data"][0]
  788. assert "user.id" not in response.data["data"][0]
  789. def test_not_project_in_query(self):
  790. project1 = self.create_project()
  791. project2 = self.create_project()
  792. self.store_event(
  793. data={
  794. "event_id": "a" * 32,
  795. "environment": "staging",
  796. "timestamp": self.ten_mins_ago_iso,
  797. },
  798. project_id=project1.id,
  799. )
  800. self.store_event(
  801. data={
  802. "event_id": "b" * 32,
  803. "environment": "staging",
  804. "timestamp": self.ten_mins_ago_iso,
  805. },
  806. project_id=project2.id,
  807. )
  808. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  809. query = {
  810. "field": ["project", "count()"],
  811. "query": '!project:"%s"' % project1.slug,
  812. "statsPeriod": "14d",
  813. }
  814. response = self.do_request(query, features=features)
  815. assert response.status_code == 200, response.content
  816. assert len(response.data["data"]) == 1
  817. assert response.data["data"][0]["project"] == project2.slug
  818. assert "project.id" not in response.data["data"][0]
  819. def test_error_handled_condition(self):
  820. prototype = self.load_data(platform="android-ndk")
  821. events = (
  822. ("a" * 32, "not handled", False),
  823. ("b" * 32, "was handled", True),
  824. ("c" * 32, "undefined", None),
  825. )
  826. for event in events:
  827. prototype["event_id"] = event[0]
  828. prototype["message"] = event[1]
  829. prototype["exception"]["values"][0]["value"] = event[1]
  830. prototype["exception"]["values"][0]["mechanism"]["handled"] = event[2]
  831. prototype["timestamp"] = self.ten_mins_ago_iso
  832. self.store_event(data=prototype, project_id=self.project.id)
  833. with self.feature("organizations:discover-basic"):
  834. query = {
  835. "field": ["message", "error.handled"],
  836. "query": "error.handled:0",
  837. "orderby": "message",
  838. }
  839. response = self.do_request(query)
  840. assert response.status_code == 200, response.data
  841. assert 1 == len(response.data["data"])
  842. assert 0 == response.data["data"][0]["error.handled"]
  843. with self.feature("organizations:discover-basic"):
  844. query = {
  845. "field": ["message", "error.handled"],
  846. "query": "error.handled:1",
  847. "orderby": "message",
  848. }
  849. response = self.do_request(query)
  850. assert response.status_code == 200, response.data
  851. assert 2 == len(response.data["data"])
  852. assert 1 == response.data["data"][0]["error.handled"]
  853. assert 1 == response.data["data"][1]["error.handled"]
  854. def test_error_unhandled_condition(self):
  855. prototype = self.load_data(platform="android-ndk")
  856. events = (
  857. ("a" * 32, "not handled", False),
  858. ("b" * 32, "was handled", True),
  859. ("c" * 32, "undefined", None),
  860. )
  861. for event in events:
  862. prototype["event_id"] = event[0]
  863. prototype["message"] = event[1]
  864. prototype["exception"]["values"][0]["value"] = event[1]
  865. prototype["exception"]["values"][0]["mechanism"]["handled"] = event[2]
  866. prototype["timestamp"] = self.ten_mins_ago_iso
  867. self.store_event(data=prototype, project_id=self.project.id)
  868. with self.feature("organizations:discover-basic"):
  869. query = {
  870. "field": ["message", "error.unhandled", "error.handled"],
  871. "query": "error.unhandled:true",
  872. "orderby": "message",
  873. }
  874. response = self.do_request(query)
  875. assert response.status_code == 200, response.data
  876. assert 1 == len(response.data["data"])
  877. assert 0 == response.data["data"][0]["error.handled"]
  878. assert 1 == response.data["data"][0]["error.unhandled"]
  879. with self.feature("organizations:discover-basic"):
  880. query = {
  881. "field": ["message", "error.handled", "error.unhandled"],
  882. "query": "error.unhandled:false",
  883. "orderby": "message",
  884. }
  885. response = self.do_request(query)
  886. assert response.status_code == 200, response.data
  887. assert 2 == len(response.data["data"])
  888. assert 1 == response.data["data"][0]["error.handled"]
  889. assert 0 == response.data["data"][0]["error.unhandled"]
  890. assert 1 == response.data["data"][1]["error.handled"]
  891. assert 0 == response.data["data"][1]["error.unhandled"]
  892. def test_groupby_error_handled_and_unhandled(self):
  893. prototype = self.load_data(platform="android-ndk")
  894. events = (
  895. ("a" * 32, "not handled", False),
  896. ("b" * 32, "was handled", True),
  897. ("c" * 32, "undefined", None),
  898. )
  899. for event in events:
  900. prototype["event_id"] = event[0]
  901. prototype["message"] = event[1]
  902. prototype["exception"]["values"][0]["value"] = event[1]
  903. prototype["exception"]["values"][0]["mechanism"]["handled"] = event[2]
  904. prototype["timestamp"] = self.ten_mins_ago_iso
  905. self.store_event(data=prototype, project_id=self.project.id)
  906. with self.feature("organizations:discover-basic"):
  907. query = {
  908. "field": ["error.handled", "count()"],
  909. "query": "event.type:error",
  910. }
  911. response = self.do_request(query)
  912. assert response.status_code == 200, response.data
  913. assert 2 == len(response.data["data"])
  914. assert 0 == response.data["data"][0]["error.handled"]
  915. assert 1 == response.data["data"][0]["count()"]
  916. assert 1 == response.data["data"][1]["error.handled"]
  917. assert 2 == response.data["data"][1]["count()"]
  918. with self.feature("organizations:discover-basic"):
  919. query = {
  920. "field": ["error.unhandled", "count()"],
  921. "query": "event.type:error",
  922. }
  923. response = self.do_request(query)
  924. assert response.status_code == 200, response.data
  925. assert 2 == len(response.data["data"])
  926. assert 0 == response.data["data"][0]["error.unhandled"]
  927. assert 2 == response.data["data"][0]["count()"]
  928. assert 1 == response.data["data"][1]["error.unhandled"]
  929. assert 1 == response.data["data"][1]["count()"]
  930. def test_error_main_thread_condition(self):
  931. prototype = self.load_data(platform="android-ndk")
  932. prototype["timestamp"] = self.ten_mins_ago_iso
  933. self.store_event(data=prototype, project_id=self.project.id)
  934. with self.feature("organizations:discover-basic"):
  935. query = {
  936. "field": ["id", "project.id"],
  937. "query": "error.main_thread:true",
  938. "project": [self.project.id],
  939. }
  940. response = self.do_request(query)
  941. assert response.status_code == 200, response.data
  942. assert 1 == len(response.data["data"])
  943. with self.feature("organizations:discover-basic"):
  944. query = {
  945. "field": ["id", "project.id"],
  946. "query": "error.main_thread:false",
  947. "project": [self.project.id],
  948. }
  949. response = self.do_request(query)
  950. assert response.status_code == 200, response.data
  951. assert 0 == len(response.data["data"])
  952. def test_implicit_groupby(self):
  953. self.store_event(
  954. data={
  955. "event_id": "a" * 32,
  956. "timestamp": self.eleven_mins_ago_iso,
  957. "fingerprint": ["group_1"],
  958. },
  959. project_id=self.project.id,
  960. )
  961. event1 = self.store_event(
  962. data={
  963. "event_id": "b" * 32,
  964. "timestamp": self.ten_mins_ago_iso,
  965. "fingerprint": ["group_1"],
  966. },
  967. project_id=self.project.id,
  968. )
  969. event2 = self.store_event(
  970. data={
  971. "event_id": "c" * 32,
  972. "timestamp": self.ten_mins_ago_iso,
  973. "fingerprint": ["group_2"],
  974. },
  975. project_id=self.project.id,
  976. )
  977. query = {"field": ["count(id)", "project.id", "issue.id"], "orderby": "issue.id"}
  978. response = self.do_request(query)
  979. assert response.status_code == 200, response.content
  980. assert len(response.data["data"]) == 2
  981. data = response.data["data"]
  982. assert data[0] == {
  983. "project.id": self.project.id,
  984. "issue.id": event1.group_id,
  985. "count(id)": 2,
  986. }
  987. assert data[1] == {
  988. "project.id": self.project.id,
  989. "issue.id": event2.group_id,
  990. "count(id)": 1,
  991. }
  992. meta = response.data["meta"]["fields"]
  993. assert meta["count(id)"] == "integer"
  994. def test_orderby(self):
  995. self.store_event(
  996. data={"event_id": "a" * 32, "timestamp": self.eleven_mins_ago_iso},
  997. project_id=self.project.id,
  998. )
  999. self.store_event(
  1000. data={"event_id": "b" * 32, "timestamp": self.ten_mins_ago_iso},
  1001. project_id=self.project.id,
  1002. )
  1003. self.store_event(
  1004. data={"event_id": "c" * 32, "timestamp": self.ten_mins_ago_iso},
  1005. project_id=self.project.id,
  1006. )
  1007. query = {"field": ["id", "timestamp"], "orderby": ["-timestamp", "-id"]}
  1008. response = self.do_request(query)
  1009. assert response.status_code == 200, response.content
  1010. data = response.data["data"]
  1011. assert data[0]["id"] == "c" * 32
  1012. assert data[1]["id"] == "b" * 32
  1013. assert data[2]["id"] == "a" * 32
  1014. def test_sort_title(self):
  1015. self.store_event(
  1016. data={"event_id": "a" * 32, "message": "zlast", "timestamp": self.eleven_mins_ago_iso},
  1017. project_id=self.project.id,
  1018. )
  1019. self.store_event(
  1020. data={"event_id": "b" * 32, "message": "second", "timestamp": self.ten_mins_ago_iso},
  1021. project_id=self.project.id,
  1022. )
  1023. self.store_event(
  1024. data={"event_id": "c" * 32, "message": "first", "timestamp": self.ten_mins_ago_iso},
  1025. project_id=self.project.id,
  1026. )
  1027. query = {"field": ["id", "title"], "sort": "title"}
  1028. response = self.do_request(query)
  1029. assert response.status_code == 200, response.content
  1030. data = response.data["data"]
  1031. assert data[0]["id"] == "c" * 32
  1032. assert data[1]["id"] == "b" * 32
  1033. assert data[2]["id"] == "a" * 32
  1034. def test_sort_invalid(self):
  1035. self.create_project()
  1036. query = {"field": ["id"], "sort": "garbage"}
  1037. response = self.do_request(query)
  1038. assert response.status_code == 400
  1039. assert "sort by" in response.data["detail"]
  1040. def test_latest_release_alias(self):
  1041. event1 = self.store_event(
  1042. data={"event_id": "a" * 32, "timestamp": self.eleven_mins_ago_iso, "release": "0.8"},
  1043. project_id=self.project.id,
  1044. )
  1045. query = {"field": ["issue.id", "release"], "query": "release:latest"}
  1046. response = self.do_request(query)
  1047. assert response.status_code == 200, response.content
  1048. data = response.data["data"]
  1049. assert data[0]["issue.id"] == event1.group_id
  1050. assert data[0]["release"] == "0.8"
  1051. event2 = self.store_event(
  1052. data={"event_id": "a" * 32, "timestamp": self.ten_mins_ago_iso, "release": "0.9"},
  1053. project_id=self.project.id,
  1054. )
  1055. query = {"field": ["issue.id", "release"], "query": "release:latest"}
  1056. response = self.do_request(query)
  1057. assert response.status_code == 200, response.content
  1058. data = response.data["data"]
  1059. assert data[0]["issue.id"] == event2.group_id
  1060. assert data[0]["release"] == "0.9"
  1061. def test_semver(self):
  1062. release_1 = self.create_release(version="test@1.2.3")
  1063. release_2 = self.create_release(version="test@1.2.4")
  1064. release_3 = self.create_release(version="test@1.2.5")
  1065. release_1_e_1 = self.store_event(
  1066. data={"release": release_1.version, "timestamp": self.ten_mins_ago_iso},
  1067. project_id=self.project.id,
  1068. ).event_id
  1069. release_1_e_2 = self.store_event(
  1070. data={"release": release_1.version, "timestamp": self.ten_mins_ago_iso},
  1071. project_id=self.project.id,
  1072. ).event_id
  1073. release_2_e_1 = self.store_event(
  1074. data={"release": release_2.version, "timestamp": self.ten_mins_ago_iso},
  1075. project_id=self.project.id,
  1076. ).event_id
  1077. release_2_e_2 = self.store_event(
  1078. data={"release": release_2.version, "timestamp": self.ten_mins_ago_iso},
  1079. project_id=self.project.id,
  1080. ).event_id
  1081. release_3_e_1 = self.store_event(
  1082. data={"release": release_3.version, "timestamp": self.ten_mins_ago_iso},
  1083. project_id=self.project.id,
  1084. ).event_id
  1085. release_3_e_2 = self.store_event(
  1086. data={"release": release_3.version, "timestamp": self.ten_mins_ago_iso},
  1087. project_id=self.project.id,
  1088. ).event_id
  1089. query = {"field": ["id"], "query": f"{constants.SEMVER_ALIAS}:>1.2.3"}
  1090. response = self.do_request(query)
  1091. assert response.status_code == 200, response.content
  1092. assert {r["id"] for r in response.data["data"]} == {
  1093. release_2_e_1,
  1094. release_2_e_2,
  1095. release_3_e_1,
  1096. release_3_e_2,
  1097. }
  1098. query = {"field": ["id"], "query": f"{constants.SEMVER_ALIAS}:>=1.2.3"}
  1099. response = self.do_request(query)
  1100. assert response.status_code == 200, response.content
  1101. assert {r["id"] for r in response.data["data"]} == {
  1102. release_1_e_1,
  1103. release_1_e_2,
  1104. release_2_e_1,
  1105. release_2_e_2,
  1106. release_3_e_1,
  1107. release_3_e_2,
  1108. }
  1109. query = {"field": ["id"], "query": f"{constants.SEMVER_ALIAS}:<1.2.4"}
  1110. response = self.do_request(query)
  1111. assert response.status_code == 200, response.content
  1112. assert {r["id"] for r in response.data["data"]} == {
  1113. release_1_e_1,
  1114. release_1_e_2,
  1115. }
  1116. query = {"field": ["id"], "query": f"{constants.SEMVER_ALIAS}:1.2.3"}
  1117. response = self.do_request(query)
  1118. assert response.status_code == 200, response.content
  1119. assert {r["id"] for r in response.data["data"]} == {
  1120. release_1_e_1,
  1121. release_1_e_2,
  1122. }
  1123. query = {"field": ["id"], "query": f"!{constants.SEMVER_ALIAS}:1.2.3"}
  1124. response = self.do_request(query)
  1125. assert response.status_code == 200, response.content
  1126. assert {r["id"] for r in response.data["data"]} == {
  1127. release_2_e_1,
  1128. release_2_e_2,
  1129. release_3_e_1,
  1130. release_3_e_2,
  1131. }
  1132. def test_release_stage(self):
  1133. replaced_release = self.create_release(
  1134. version="replaced_release",
  1135. environments=[self.environment],
  1136. adopted=timezone.now(),
  1137. unadopted=timezone.now(),
  1138. )
  1139. adopted_release = self.create_release(
  1140. version="adopted_release",
  1141. environments=[self.environment],
  1142. adopted=timezone.now(),
  1143. )
  1144. self.create_release(version="not_adopted_release", environments=[self.environment])
  1145. adopted_release_e_1 = self.store_event(
  1146. data={
  1147. "release": adopted_release.version,
  1148. "timestamp": self.ten_mins_ago_iso,
  1149. "environment": self.environment.name,
  1150. },
  1151. project_id=self.project.id,
  1152. ).event_id
  1153. adopted_release_e_2 = self.store_event(
  1154. data={
  1155. "release": adopted_release.version,
  1156. "timestamp": self.ten_mins_ago_iso,
  1157. "environment": self.environment.name,
  1158. },
  1159. project_id=self.project.id,
  1160. ).event_id
  1161. replaced_release_e_1 = self.store_event(
  1162. data={
  1163. "release": replaced_release.version,
  1164. "timestamp": self.ten_mins_ago_iso,
  1165. "environment": self.environment.name,
  1166. },
  1167. project_id=self.project.id,
  1168. ).event_id
  1169. replaced_release_e_2 = self.store_event(
  1170. data={
  1171. "release": replaced_release.version,
  1172. "timestamp": self.ten_mins_ago_iso,
  1173. "environment": self.environment.name,
  1174. },
  1175. project_id=self.project.id,
  1176. ).event_id
  1177. query = {
  1178. "field": ["id"],
  1179. "query": f"{constants.RELEASE_STAGE_ALIAS}:{ReleaseStages.ADOPTED}",
  1180. "environment": [self.environment.name],
  1181. }
  1182. response = self.do_request(query)
  1183. assert response.status_code == 200, response.content
  1184. assert {r["id"] for r in response.data["data"]} == {
  1185. adopted_release_e_1,
  1186. adopted_release_e_2,
  1187. }
  1188. query = {
  1189. "field": ["id"],
  1190. "query": f"!{constants.RELEASE_STAGE_ALIAS}:{ReleaseStages.LOW_ADOPTION}",
  1191. "environment": [self.environment.name],
  1192. }
  1193. response = self.do_request(query)
  1194. assert response.status_code == 200, response.content
  1195. assert {r["id"] for r in response.data["data"]} == {
  1196. adopted_release_e_1,
  1197. adopted_release_e_2,
  1198. replaced_release_e_1,
  1199. replaced_release_e_2,
  1200. }
  1201. query = {
  1202. "field": ["id"],
  1203. "query": f"{constants.RELEASE_STAGE_ALIAS}:[{ReleaseStages.ADOPTED}, {ReleaseStages.REPLACED}]",
  1204. "environment": [self.environment.name],
  1205. }
  1206. response = self.do_request(query)
  1207. assert response.status_code == 200, response.content
  1208. assert {r["id"] for r in response.data["data"]} == {
  1209. adopted_release_e_1,
  1210. adopted_release_e_2,
  1211. replaced_release_e_1,
  1212. replaced_release_e_2,
  1213. }
  1214. def test_semver_package(self):
  1215. release_1 = self.create_release(version="test@1.2.3")
  1216. release_2 = self.create_release(version="test2@1.2.4")
  1217. release_1_e_1 = self.store_event(
  1218. data={"release": release_1.version, "timestamp": self.ten_mins_ago_iso},
  1219. project_id=self.project.id,
  1220. ).event_id
  1221. release_1_e_2 = self.store_event(
  1222. data={"release": release_1.version, "timestamp": self.ten_mins_ago_iso},
  1223. project_id=self.project.id,
  1224. ).event_id
  1225. release_2_e_1 = self.store_event(
  1226. data={"release": release_2.version, "timestamp": self.ten_mins_ago_iso},
  1227. project_id=self.project.id,
  1228. ).event_id
  1229. query = {"field": ["id"], "query": f"{constants.SEMVER_PACKAGE_ALIAS}:test"}
  1230. response = self.do_request(query)
  1231. assert response.status_code == 200, response.content
  1232. assert {r["id"] for r in response.data["data"]} == {
  1233. release_1_e_1,
  1234. release_1_e_2,
  1235. }
  1236. query = {"field": ["id"], "query": f"{constants.SEMVER_PACKAGE_ALIAS}:test2"}
  1237. response = self.do_request(query)
  1238. assert response.status_code == 200, response.content
  1239. assert {r["id"] for r in response.data["data"]} == {
  1240. release_2_e_1,
  1241. }
  1242. def test_semver_build(self):
  1243. release_1 = self.create_release(version="test@1.2.3+123")
  1244. release_2 = self.create_release(version="test2@1.2.4+124")
  1245. release_1_e_1 = self.store_event(
  1246. data={"release": release_1.version, "timestamp": self.ten_mins_ago_iso},
  1247. project_id=self.project.id,
  1248. ).event_id
  1249. release_1_e_2 = self.store_event(
  1250. data={"release": release_1.version, "timestamp": self.ten_mins_ago_iso},
  1251. project_id=self.project.id,
  1252. ).event_id
  1253. release_2_e_1 = self.store_event(
  1254. data={"release": release_2.version, "timestamp": self.ten_mins_ago_iso},
  1255. project_id=self.project.id,
  1256. ).event_id
  1257. query = {"field": ["id"], "query": f"{constants.SEMVER_BUILD_ALIAS}:123"}
  1258. response = self.do_request(query)
  1259. assert response.status_code == 200, response.content
  1260. assert {r["id"] for r in response.data["data"]} == {
  1261. release_1_e_1,
  1262. release_1_e_2,
  1263. }
  1264. query = {"field": ["id"], "query": f"{constants.SEMVER_BUILD_ALIAS}:124"}
  1265. response = self.do_request(query)
  1266. assert response.status_code == 200, response.content
  1267. assert {r["id"] for r in response.data["data"]} == {
  1268. release_2_e_1,
  1269. }
  1270. query = {"field": ["id"], "query": f"!{constants.SEMVER_BUILD_ALIAS}:124"}
  1271. response = self.do_request(query)
  1272. assert response.status_code == 200, response.content
  1273. assert {r["id"] for r in response.data["data"]} == {
  1274. release_1_e_1,
  1275. release_1_e_2,
  1276. }
  1277. def test_aliased_fields(self):
  1278. event1 = self.store_event(
  1279. data={
  1280. "event_id": "a" * 32,
  1281. "timestamp": self.ten_mins_ago_iso,
  1282. "fingerprint": ["group_1"],
  1283. "user": {"email": "foo@example.com"},
  1284. },
  1285. project_id=self.project.id,
  1286. )
  1287. event2 = self.store_event(
  1288. data={
  1289. "event_id": "b" * 32,
  1290. "timestamp": self.ten_mins_ago_iso,
  1291. "fingerprint": ["group_2"],
  1292. "user": {"email": "foo@example.com"},
  1293. },
  1294. project_id=self.project.id,
  1295. )
  1296. self.store_event(
  1297. data={
  1298. "event_id": "c" * 32,
  1299. "timestamp": self.ten_mins_ago_iso,
  1300. "fingerprint": ["group_2"],
  1301. "user": {"email": "bar@example.com"},
  1302. },
  1303. project_id=self.project.id,
  1304. )
  1305. query = {"field": ["issue.id", "count(id)", "count_unique(user)"], "orderby": "issue.id"}
  1306. response = self.do_request(query)
  1307. assert response.status_code == 200, response.content
  1308. assert len(response.data["data"]) == 2
  1309. data = response.data["data"]
  1310. assert data[0]["issue.id"] == event1.group_id
  1311. assert data[0]["count(id)"] == 1
  1312. assert data[0]["count_unique(user)"] == 1
  1313. assert "projectid" not in data[0]
  1314. assert "project.id" not in data[0]
  1315. assert data[1]["issue.id"] == event2.group_id
  1316. assert data[1]["count(id)"] == 2
  1317. assert data[1]["count_unique(user)"] == 2
  1318. def test_aggregate_field_with_dotted_param(self):
  1319. event1 = self.store_event(
  1320. data={
  1321. "event_id": "a" * 32,
  1322. "timestamp": self.ten_mins_ago_iso,
  1323. "fingerprint": ["group_1"],
  1324. "user": {"id": "123", "email": "foo@example.com"},
  1325. },
  1326. project_id=self.project.id,
  1327. )
  1328. event2 = self.store_event(
  1329. data={
  1330. "event_id": "b" * 32,
  1331. "timestamp": self.ten_mins_ago_iso,
  1332. "fingerprint": ["group_2"],
  1333. "user": {"id": "123", "email": "foo@example.com"},
  1334. },
  1335. project_id=self.project.id,
  1336. )
  1337. self.store_event(
  1338. data={
  1339. "event_id": "c" * 32,
  1340. "timestamp": self.ten_mins_ago_iso,
  1341. "fingerprint": ["group_2"],
  1342. "user": {"id": "456", "email": "bar@example.com"},
  1343. },
  1344. project_id=self.project.id,
  1345. )
  1346. query = {
  1347. "field": ["issue.id", "issue_title", "count(id)", "count_unique(user.email)"],
  1348. "orderby": "issue.id",
  1349. }
  1350. response = self.do_request(query)
  1351. assert response.status_code == 200, response.content
  1352. assert len(response.data["data"]) == 2
  1353. data = response.data["data"]
  1354. assert data[0]["issue.id"] == event1.group_id
  1355. assert data[0]["count(id)"] == 1
  1356. assert data[0]["count_unique(user.email)"] == 1
  1357. assert "projectid" not in data[0]
  1358. assert "project.id" not in data[0]
  1359. assert data[1]["issue.id"] == event2.group_id
  1360. assert data[1]["count(id)"] == 2
  1361. assert data[1]["count_unique(user.email)"] == 2
  1362. def test_failure_rate_alias_field(self):
  1363. data = self.transaction_data.copy()
  1364. data["transaction"] = "/failure_rate/success"
  1365. self.store_event(data, project_id=self.project.id)
  1366. data = self.transaction_data.copy()
  1367. data["transaction"] = "/failure_rate/unknown"
  1368. data["contexts"]["trace"]["status"] = "unknown_error"
  1369. self.store_event(data, project_id=self.project.id)
  1370. for i in range(6):
  1371. data = self.transaction_data.copy()
  1372. data["transaction"] = f"/failure_rate/{i}"
  1373. data["contexts"]["trace"]["status"] = "unauthenticated"
  1374. self.store_event(data, project_id=self.project.id)
  1375. query = {"field": ["failure_rate()"], "query": "event.type:transaction"}
  1376. response = self.do_request(query)
  1377. assert response.status_code == 200, response.content
  1378. assert len(response.data["data"]) == 1
  1379. data = response.data["data"]
  1380. assert data[0]["failure_rate()"] == 0.75
  1381. def test_count_miserable_alias_field(self):
  1382. events = [
  1383. ("one", 300),
  1384. ("one", 300),
  1385. ("two", 3000),
  1386. ("two", 3000),
  1387. ("three", 300),
  1388. ("three", 3000),
  1389. ]
  1390. for idx, event in enumerate(events):
  1391. data = self.load_data(
  1392. timestamp=before_now(minutes=(10 + idx)),
  1393. duration=timedelta(milliseconds=event[1]),
  1394. )
  1395. data["event_id"] = f"{idx}" * 32
  1396. data["transaction"] = f"/count_miserable/horribilis/{idx}"
  1397. data["user"] = {"email": f"{event[0]}@example.com"}
  1398. self.store_event(data, project_id=self.project.id)
  1399. query = {"field": ["count_miserable(user, 300)"], "query": "event.type:transaction"}
  1400. response = self.do_request(query)
  1401. assert response.status_code == 200, response.content
  1402. assert len(response.data["data"]) == 1
  1403. data = response.data["data"]
  1404. assert data[0]["count_miserable(user, 300)"] == 2
  1405. @mock.patch(
  1406. "sentry.search.events.fields.MAX_QUERYABLE_TRANSACTION_THRESHOLDS",
  1407. MAX_QUERYABLE_TRANSACTION_THRESHOLDS,
  1408. )
  1409. @mock.patch(
  1410. "sentry.search.events.datasets.discover.MAX_QUERYABLE_TRANSACTION_THRESHOLDS",
  1411. MAX_QUERYABLE_TRANSACTION_THRESHOLDS,
  1412. )
  1413. def test_too_many_transaction_thresholds(self):
  1414. project_transaction_thresholds = []
  1415. project_ids = []
  1416. for i in range(MAX_QUERYABLE_TRANSACTION_THRESHOLDS + 1):
  1417. project = self.create_project(name=f"bulk_txn_{i}")
  1418. project_ids.append(project.id)
  1419. project_transaction_thresholds.append(
  1420. ProjectTransactionThreshold(
  1421. organization=self.organization,
  1422. project=project,
  1423. threshold=400,
  1424. metric=TransactionMetric.LCP.value,
  1425. )
  1426. )
  1427. ProjectTransactionThreshold.objects.bulk_create(project_transaction_thresholds)
  1428. query = {
  1429. "field": [
  1430. "transaction",
  1431. "count_miserable(user)",
  1432. ],
  1433. "query": "event.type:transaction",
  1434. "project": project_ids,
  1435. }
  1436. response = self.do_request(
  1437. query,
  1438. features={
  1439. "organizations:discover-basic": True,
  1440. "organizations:global-views": True,
  1441. },
  1442. )
  1443. assert response.status_code == 400
  1444. assert (
  1445. response.data["detail"]
  1446. == "Exceeded 1 configured transaction thresholds limit, try with fewer Projects."
  1447. )
  1448. def test_count_miserable_new_alias_field(self):
  1449. ProjectTransactionThreshold.objects.create(
  1450. project=self.project,
  1451. organization=self.project.organization,
  1452. threshold=400,
  1453. metric=TransactionMetric.DURATION.value,
  1454. )
  1455. events = [
  1456. ("one", 400),
  1457. ("one", 400),
  1458. ("two", 3000),
  1459. ("two", 3000),
  1460. ("three", 300),
  1461. ("three", 3000),
  1462. ]
  1463. for idx, event in enumerate(events):
  1464. data = self.load_data(
  1465. timestamp=before_now(minutes=(10 + idx)),
  1466. duration=timedelta(milliseconds=event[1]),
  1467. )
  1468. data["event_id"] = f"{idx}" * 32
  1469. data["transaction"] = f"/count_miserable/horribilis/{event[0]}"
  1470. data["user"] = {"email": f"{idx}@example.com"}
  1471. self.store_event(data, project_id=self.project.id)
  1472. query = {
  1473. "field": [
  1474. "transaction",
  1475. "count_miserable(user)",
  1476. ],
  1477. "query": "event.type:transaction",
  1478. "project": [self.project.id],
  1479. "sort": "count_miserable_user",
  1480. }
  1481. response = self.do_request(
  1482. query,
  1483. )
  1484. assert response.status_code == 200, response.content
  1485. assert len(response.data["data"]) == 3
  1486. data = response.data["data"]
  1487. assert data[0]["count_miserable(user)"] == 0
  1488. assert data[1]["count_miserable(user)"] == 1
  1489. assert data[2]["count_miserable(user)"] == 2
  1490. query["query"] = "event.type:transaction count_miserable(user):>0"
  1491. response = self.do_request(
  1492. query,
  1493. )
  1494. assert response.status_code == 200, response.content
  1495. assert len(response.data["data"]) == 2
  1496. data = response.data["data"]
  1497. assert abs(data[0]["count_miserable(user)"]) == 1
  1498. assert abs(data[1]["count_miserable(user)"]) == 2
  1499. def test_user_misery_denominator(self):
  1500. """This is to test against a bug where the denominator of misery(total unique users) was wrong
  1501. This is because the total unique users for a LCP misery should only count users that have had a txn with lcp,
  1502. and not count all transactions (ie. uniq_if(transaction has lcp) not just uniq())
  1503. """
  1504. ProjectTransactionThreshold.objects.create(
  1505. project=self.project,
  1506. organization=self.project.organization,
  1507. threshold=600,
  1508. metric=TransactionMetric.LCP.value,
  1509. )
  1510. lcps = [
  1511. 400,
  1512. 400,
  1513. 300,
  1514. 3000,
  1515. 3000,
  1516. 3000,
  1517. ]
  1518. for idx, lcp in enumerate(lcps):
  1519. data = self.load_data(
  1520. timestamp=before_now(minutes=(10 + idx)),
  1521. )
  1522. data["event_id"] = f"{idx}" * 32
  1523. data["transaction"] = "/misery/new/"
  1524. data["user"] = {"email": f"{idx}@example.com"}
  1525. data["measurements"] = {
  1526. "lcp": {"value": lcp},
  1527. }
  1528. self.store_event(data, project_id=self.project.id)
  1529. # Shouldn't count towards misery
  1530. data = self.load_data(timestamp=self.ten_mins_ago, duration=timedelta(milliseconds=0))
  1531. data["transaction"] = "/misery/new/"
  1532. data["user"] = {"email": "7@example.com"}
  1533. data["measurements"] = {}
  1534. self.store_event(data, project_id=self.project.id)
  1535. query = {
  1536. "field": [
  1537. "transaction",
  1538. "user_misery()",
  1539. ],
  1540. "query": "event.type:transaction",
  1541. "project": [self.project.id],
  1542. "sort": "-user_misery",
  1543. }
  1544. response = self.do_request(
  1545. query,
  1546. )
  1547. assert response.status_code == 200, response.content
  1548. assert len(response.data["data"]) == 1
  1549. data = response.data["data"]
  1550. # (3 frustrated + 5.8875) / (6 + 117.75)
  1551. assert abs(data[0]["user_misery()"] - 0.071818) < 0.0001
  1552. def test_user_misery_alias_field(self):
  1553. events = [
  1554. ("one", 300),
  1555. ("one", 300),
  1556. ("two", 3000),
  1557. ("two", 3000),
  1558. ("three", 300),
  1559. ("three", 3000),
  1560. ]
  1561. for idx, event in enumerate(events):
  1562. data = self.load_data(
  1563. timestamp=before_now(minutes=(10 + idx)),
  1564. duration=timedelta(milliseconds=event[1]),
  1565. )
  1566. data["event_id"] = f"{idx}" * 32
  1567. data["transaction"] = f"/user_misery/{idx}"
  1568. data["user"] = {"email": f"{event[0]}@example.com"}
  1569. self.store_event(data, project_id=self.project.id)
  1570. query = {"field": ["user_misery(300)"], "query": "event.type:transaction"}
  1571. response = self.do_request(query)
  1572. assert response.status_code == 200, response.content
  1573. assert len(response.data["data"]) == 1
  1574. data = response.data["data"]
  1575. assert abs(data[0]["user_misery(300)"] - 0.0653) < 0.0001
  1576. def test_apdex_denominator_correct(self):
  1577. """This is to test against a bug where the denominator of apdex(total count) was wrong
  1578. This is because the total_count for a LCP apdex should only count transactions that have lcp, and not count
  1579. all transactions (ie. count_if(transaction has lcp) not just count())
  1580. """
  1581. ProjectTransactionThreshold.objects.create(
  1582. project=self.project,
  1583. organization=self.project.organization,
  1584. threshold=600,
  1585. metric=TransactionMetric.LCP.value,
  1586. )
  1587. lcps = [
  1588. 400,
  1589. 400,
  1590. 300,
  1591. 800,
  1592. 3000,
  1593. 3000,
  1594. 3000,
  1595. ]
  1596. for idx, lcp in enumerate(lcps):
  1597. data = self.load_data(
  1598. timestamp=before_now(minutes=(10 + idx)),
  1599. )
  1600. data["event_id"] = f"{idx}" * 32
  1601. data["transaction"] = "/apdex/new/"
  1602. data["user"] = {"email": f"{idx}@example.com"}
  1603. data["measurements"] = {
  1604. "lcp": {"value": lcp},
  1605. }
  1606. self.store_event(data, project_id=self.project.id)
  1607. # Shouldn't count towards apdex
  1608. data = self.load_data(
  1609. timestamp=self.ten_mins_ago,
  1610. duration=timedelta(milliseconds=0),
  1611. )
  1612. data["transaction"] = "/apdex/new/"
  1613. data["user"] = {"email": "7@example.com"}
  1614. data["measurements"] = {}
  1615. self.store_event(data, project_id=self.project.id)
  1616. query = {
  1617. "field": [
  1618. "transaction",
  1619. "apdex()",
  1620. ],
  1621. "query": "event.type:transaction",
  1622. "project": [self.project.id],
  1623. "sort": "-apdex",
  1624. }
  1625. response = self.do_request(
  1626. query,
  1627. )
  1628. assert response.status_code == 200, response.content
  1629. assert len(response.data["data"]) == 1
  1630. data = response.data["data"]
  1631. # 3 satisfied + 1 tolerated => 3.5/7
  1632. assert data[0]["apdex()"] == 0.5
  1633. def test_apdex_new_alias_field(self):
  1634. ProjectTransactionThreshold.objects.create(
  1635. project=self.project,
  1636. organization=self.project.organization,
  1637. threshold=400,
  1638. metric=TransactionMetric.DURATION.value,
  1639. )
  1640. events = [
  1641. ("one", 400),
  1642. ("one", 400),
  1643. ("two", 3000),
  1644. ("two", 3000),
  1645. ("three", 300),
  1646. ("three", 3000),
  1647. ]
  1648. for idx, event in enumerate(events):
  1649. data = self.load_data(
  1650. timestamp=before_now(minutes=(10 + idx)),
  1651. duration=timedelta(milliseconds=event[1]),
  1652. )
  1653. data["event_id"] = f"{idx}" * 32
  1654. data["transaction"] = f"/apdex/new/{event[0]}"
  1655. data["user"] = {"email": f"{idx}@example.com"}
  1656. self.store_event(data, project_id=self.project.id)
  1657. query = {
  1658. "field": [
  1659. "transaction",
  1660. "apdex()",
  1661. ],
  1662. "query": "event.type:transaction",
  1663. "project": [self.project.id],
  1664. "sort": "-apdex",
  1665. }
  1666. response = self.do_request(
  1667. query,
  1668. )
  1669. assert response.status_code == 200, response.content
  1670. assert len(response.data["data"]) == 3
  1671. data = response.data["data"]
  1672. assert data[0]["apdex()"] == 1.0
  1673. assert data[1]["apdex()"] == 0.5
  1674. assert data[2]["apdex()"] == 0.0
  1675. query["query"] = "event.type:transaction apdex():>0.50"
  1676. response = self.do_request(
  1677. query,
  1678. )
  1679. assert response.status_code == 200, response.content
  1680. assert len(response.data["data"]) == 1
  1681. data = response.data["data"]
  1682. assert data[0]["apdex()"] == 1.0
  1683. def test_user_misery_alias_field_with_project_threshold(self):
  1684. ProjectTransactionThreshold.objects.create(
  1685. project=self.project,
  1686. organization=self.project.organization,
  1687. threshold=400,
  1688. metric=TransactionMetric.DURATION.value,
  1689. )
  1690. events = [
  1691. ("one", 400),
  1692. ("one", 400),
  1693. ("two", 3000),
  1694. ("two", 3000),
  1695. ("three", 300),
  1696. ("three", 3000),
  1697. ]
  1698. for idx, event in enumerate(events):
  1699. data = self.load_data(
  1700. timestamp=before_now(minutes=(10 + idx)),
  1701. duration=timedelta(milliseconds=event[1]),
  1702. )
  1703. data["event_id"] = f"{idx}" * 32
  1704. data["transaction"] = f"/count_miserable/horribilis/{event[0]}"
  1705. data["user"] = {"email": f"{idx}@example.com"}
  1706. self.store_event(data, project_id=self.project.id)
  1707. query = {
  1708. "field": [
  1709. "transaction",
  1710. "user_misery()",
  1711. ],
  1712. "orderby": "user_misery()",
  1713. "query": "event.type:transaction",
  1714. "project": [self.project.id],
  1715. }
  1716. response = self.do_request(query)
  1717. assert response.status_code == 200, response.content
  1718. assert len(response.data["data"]) == 3
  1719. data = response.data["data"]
  1720. assert data[0]["user_misery()"] == pytest.approx(0.04916, rel=1e-3)
  1721. assert data[1]["user_misery()"] == pytest.approx(0.05751, rel=1e-3)
  1722. assert data[2]["user_misery()"] == pytest.approx(0.06586, rel=1e-3)
  1723. query["query"] = "event.type:transaction user_misery():>0.050"
  1724. response = self.do_request(
  1725. query,
  1726. )
  1727. assert response.status_code == 200, response.content
  1728. assert len(response.data["data"]) == 2
  1729. data = response.data["data"]
  1730. assert data[0]["user_misery()"] == pytest.approx(0.05751, rel=1e-3)
  1731. assert data[1]["user_misery()"] == pytest.approx(0.06586, rel=1e-3)
  1732. def test_user_misery_alias_field_with_transaction_threshold(self):
  1733. events = [
  1734. ("one", 300),
  1735. ("two", 300),
  1736. ("one", 3000),
  1737. ("two", 3000),
  1738. ("three", 400),
  1739. ("four", 4000),
  1740. ]
  1741. for idx, event in enumerate(events):
  1742. data = self.load_data(
  1743. timestamp=before_now(minutes=(10 + idx)),
  1744. duration=timedelta(milliseconds=event[1]),
  1745. )
  1746. data["event_id"] = f"{idx}" * 32
  1747. data["transaction"] = f"/count_miserable/horribilis/{idx}"
  1748. data["user"] = {"email": f"{event[0]}@example.com"}
  1749. self.store_event(data, project_id=self.project.id)
  1750. if idx % 2:
  1751. ProjectTransactionThresholdOverride.objects.create(
  1752. transaction=f"/count_miserable/horribilis/{idx}",
  1753. project=self.project,
  1754. organization=self.project.organization,
  1755. threshold=100 * idx,
  1756. metric=TransactionMetric.DURATION.value,
  1757. )
  1758. query = {
  1759. "field": [
  1760. "transaction",
  1761. "user_misery()",
  1762. ],
  1763. "query": "event.type:transaction",
  1764. "orderby": "transaction",
  1765. "project": [self.project.id],
  1766. }
  1767. response = self.do_request(
  1768. query,
  1769. )
  1770. assert response.status_code == 200, response.content
  1771. expected = [
  1772. ("/count_miserable/horribilis/0", ["duration", 300], 0.049578),
  1773. ("/count_miserable/horribilis/1", ["duration", 100], 0.049578),
  1774. ("/count_miserable/horribilis/2", ["duration", 300], 0.058),
  1775. ("/count_miserable/horribilis/3", ["duration", 300], 0.058),
  1776. ("/count_miserable/horribilis/4", ["duration", 300], 0.049578),
  1777. ("/count_miserable/horribilis/5", ["duration", 500], 0.058),
  1778. ]
  1779. assert len(response.data["data"]) == 6
  1780. data = response.data["data"]
  1781. for i, record in enumerate(expected):
  1782. name, threshold_config, misery = record
  1783. assert data[i]["transaction"] == name
  1784. assert data[i]["project_threshold_config"] == threshold_config
  1785. assert data[i]["user_misery()"] == pytest.approx(misery, rel=1e-3)
  1786. query["query"] = "event.type:transaction user_misery():>0.050"
  1787. response = self.do_request(
  1788. query,
  1789. )
  1790. assert response.status_code == 200, response.content
  1791. assert len(response.data["data"]) == 3
  1792. data = response.data["data"]
  1793. assert data[0]["user_misery()"] == pytest.approx(0.058, rel=1e-3)
  1794. assert data[1]["user_misery()"] == pytest.approx(0.058, rel=1e-3)
  1795. assert data[2]["user_misery()"] == pytest.approx(0.058, rel=1e-3)
  1796. def test_user_misery_alias_field_with_transaction_threshold_and_project_threshold(self):
  1797. project = self.create_project()
  1798. ProjectTransactionThreshold.objects.create(
  1799. project=project,
  1800. organization=project.organization,
  1801. threshold=100,
  1802. metric=TransactionMetric.DURATION.value,
  1803. )
  1804. events = [
  1805. ("one", 300),
  1806. ("two", 300),
  1807. ("one", 3000),
  1808. ("two", 3000),
  1809. ("three", 400),
  1810. ("four", 4000),
  1811. ]
  1812. for idx, event in enumerate(events):
  1813. data = self.load_data(
  1814. timestamp=before_now(minutes=(10 + idx)),
  1815. duration=timedelta(milliseconds=event[1]),
  1816. )
  1817. data["event_id"] = f"{idx}" * 32
  1818. data["transaction"] = f"/count_miserable/horribilis/{idx}"
  1819. data["user"] = {"email": f"{event[0]}@example.com"}
  1820. self.store_event(data, project_id=project.id)
  1821. if idx % 2:
  1822. ProjectTransactionThresholdOverride.objects.create(
  1823. transaction=f"/count_miserable/horribilis/{idx}",
  1824. project=project,
  1825. organization=project.organization,
  1826. threshold=100 * idx,
  1827. metric=TransactionMetric.DURATION.value,
  1828. )
  1829. project2 = self.create_project()
  1830. data = self.load_data()
  1831. data["transaction"] = "/count_miserable/horribilis/project2"
  1832. data["user"] = {"email": "project2@example.com"}
  1833. self.store_event(data, project_id=project2.id)
  1834. query = {
  1835. "field": [
  1836. "transaction",
  1837. "user_misery()",
  1838. ],
  1839. "query": "event.type:transaction",
  1840. "orderby": "transaction",
  1841. "project": [project.id, project2.id],
  1842. }
  1843. response = self.do_request(
  1844. query,
  1845. features={
  1846. "organizations:discover-basic": True,
  1847. "organizations:global-views": True,
  1848. },
  1849. )
  1850. assert response.status_code == 200, response.content
  1851. expected = [
  1852. (
  1853. "/count_miserable/horribilis/0",
  1854. ["duration", 100],
  1855. 0.049578,
  1856. ), # Uses project threshold
  1857. ("/count_miserable/horribilis/1", ["duration", 100], 0.049578), # Uses txn threshold
  1858. ("/count_miserable/horribilis/2", ["duration", 100], 0.058), # Uses project threshold
  1859. ("/count_miserable/horribilis/3", ["duration", 300], 0.058), # Uses txn threshold
  1860. (
  1861. "/count_miserable/horribilis/4",
  1862. ["duration", 100],
  1863. 0.049578,
  1864. ), # Uses project threshold
  1865. ("/count_miserable/horribilis/5", ["duration", 500], 0.058), # Uses txn threshold
  1866. ("/count_miserable/horribilis/project2", ["duration", 300], 0.058), # Uses fallback
  1867. ]
  1868. assert len(response.data["data"]) == 7
  1869. data = response.data["data"]
  1870. for i, record in enumerate(expected):
  1871. name, threshold_config, misery = record
  1872. assert data[i]["transaction"] == name
  1873. assert data[i]["project_threshold_config"] == threshold_config
  1874. assert data[i]["user_misery()"] == pytest.approx(misery, rel=1e-3)
  1875. query["query"] = "event.type:transaction user_misery():>0.050"
  1876. response = self.do_request(
  1877. query,
  1878. features={
  1879. "organizations:discover-basic": True,
  1880. "organizations:global-views": True,
  1881. },
  1882. )
  1883. assert response.status_code == 200, response.content
  1884. assert len(response.data["data"]) == 4
  1885. def test_aggregation(self):
  1886. self.store_event(
  1887. data={
  1888. "event_id": "a" * 32,
  1889. "timestamp": self.ten_mins_ago_iso,
  1890. "fingerprint": ["group_1"],
  1891. "user": {"email": "foo@example.com"},
  1892. "environment": "prod",
  1893. "tags": {"sub_customer.is-Enterprise-42": "1"},
  1894. },
  1895. project_id=self.project.id,
  1896. )
  1897. self.store_event(
  1898. data={
  1899. "event_id": "b" * 32,
  1900. "timestamp": self.ten_mins_ago_iso,
  1901. "fingerprint": ["group_2"],
  1902. "user": {"email": "foo@example.com"},
  1903. "environment": "staging",
  1904. "tags": {"sub_customer.is-Enterprise-42": "1"},
  1905. },
  1906. project_id=self.project.id,
  1907. )
  1908. self.store_event(
  1909. data={
  1910. "event_id": "c" * 32,
  1911. "timestamp": self.ten_mins_ago_iso,
  1912. "fingerprint": ["group_2"],
  1913. "user": {"email": "foo@example.com"},
  1914. "environment": "prod",
  1915. "tags": {"sub_customer.is-Enterprise-42": "0"},
  1916. },
  1917. project_id=self.project.id,
  1918. )
  1919. self.store_event(
  1920. data={
  1921. "event_id": "d" * 32,
  1922. "timestamp": self.ten_mins_ago_iso,
  1923. "fingerprint": ["group_2"],
  1924. "user": {"email": "foo@example.com"},
  1925. "environment": "prod",
  1926. "tags": {"sub_customer.is-Enterprise-42": "1"},
  1927. },
  1928. project_id=self.project.id,
  1929. )
  1930. query = {
  1931. "field": ["sub_customer.is-Enterprise-42", "count(sub_customer.is-Enterprise-42)"],
  1932. "orderby": "sub_customer.is-Enterprise-42",
  1933. }
  1934. response = self.do_request(query)
  1935. assert response.status_code == 200, response.content
  1936. assert len(response.data["data"]) == 2
  1937. data = response.data["data"]
  1938. assert data[0]["count(sub_customer.is-Enterprise-42)"] == 1
  1939. assert data[1]["count(sub_customer.is-Enterprise-42)"] == 3
  1940. def test_aggregation_comparison(self):
  1941. self.store_event(
  1942. data={
  1943. "event_id": "a" * 32,
  1944. "timestamp": self.ten_mins_ago_iso,
  1945. "fingerprint": ["group_1"],
  1946. "user": {"email": "foo@example.com"},
  1947. },
  1948. project_id=self.project.id,
  1949. )
  1950. event = self.store_event(
  1951. data={
  1952. "event_id": "b" * 32,
  1953. "timestamp": self.ten_mins_ago_iso,
  1954. "fingerprint": ["group_2"],
  1955. "user": {"email": "foo@example.com"},
  1956. },
  1957. project_id=self.project.id,
  1958. )
  1959. self.store_event(
  1960. data={
  1961. "event_id": "c" * 32,
  1962. "timestamp": self.ten_mins_ago_iso,
  1963. "fingerprint": ["group_2"],
  1964. "user": {"email": "bar@example.com"},
  1965. },
  1966. project_id=self.project.id,
  1967. )
  1968. self.store_event(
  1969. data={
  1970. "event_id": "d" * 32,
  1971. "timestamp": self.ten_mins_ago_iso,
  1972. "fingerprint": ["group_3"],
  1973. "user": {"email": "bar@example.com"},
  1974. },
  1975. project_id=self.project.id,
  1976. )
  1977. self.store_event(
  1978. data={
  1979. "event_id": "e" * 32,
  1980. "timestamp": self.ten_mins_ago_iso,
  1981. "fingerprint": ["group_3"],
  1982. "user": {"email": "bar@example.com"},
  1983. },
  1984. project_id=self.project.id,
  1985. )
  1986. query = {
  1987. "field": ["issue.id", "count(id)", "count_unique(user)"],
  1988. "query": "count(id):>1 count_unique(user):>1",
  1989. "orderby": "issue.id",
  1990. }
  1991. response = self.do_request(query)
  1992. assert response.status_code == 200, response.content
  1993. assert len(response.data["data"]) == 1
  1994. data = response.data["data"]
  1995. assert data[0]["issue.id"] == event.group_id
  1996. assert data[0]["count(id)"] == 2
  1997. assert data[0]["count_unique(user)"] == 2
  1998. def test_aggregation_alias_comparison(self):
  1999. data = self.load_data(
  2000. timestamp=self.ten_mins_ago,
  2001. duration=timedelta(seconds=5),
  2002. )
  2003. data["transaction"] = "/aggregates/1"
  2004. self.store_event(data, project_id=self.project.id)
  2005. data = self.load_data(
  2006. timestamp=self.ten_mins_ago,
  2007. duration=timedelta(seconds=3),
  2008. )
  2009. data["transaction"] = "/aggregates/2"
  2010. event = self.store_event(data, project_id=self.project.id)
  2011. query = {
  2012. "field": ["transaction", "p95()"],
  2013. "query": "event.type:transaction p95():<4000",
  2014. "orderby": ["transaction"],
  2015. }
  2016. response = self.do_request(query)
  2017. assert response.status_code == 200, response.content
  2018. assert len(response.data["data"]) == 1
  2019. data = response.data["data"]
  2020. assert data[0]["transaction"] == event.transaction
  2021. assert data[0]["p95()"] == 3000
  2022. def test_auto_aggregations(self):
  2023. data = self.load_data(
  2024. timestamp=self.ten_mins_ago,
  2025. duration=timedelta(seconds=5),
  2026. )
  2027. data["transaction"] = "/aggregates/1"
  2028. self.store_event(data, project_id=self.project.id)
  2029. data = self.load_data(
  2030. timestamp=self.ten_mins_ago,
  2031. duration=timedelta(seconds=3),
  2032. )
  2033. data["transaction"] = "/aggregates/2"
  2034. event = self.store_event(data, project_id=self.project.id)
  2035. query = {
  2036. "field": ["transaction", "p75()"],
  2037. "query": "event.type:transaction p95():<4000",
  2038. "orderby": ["transaction"],
  2039. }
  2040. response = self.do_request(query)
  2041. assert response.status_code == 200, response.content
  2042. assert len(response.data["data"]) == 1
  2043. data = response.data["data"]
  2044. assert data[0]["transaction"] == event.transaction
  2045. query = {
  2046. "field": ["transaction"],
  2047. "query": "event.type:transaction p95():<4000",
  2048. "orderby": ["transaction"],
  2049. }
  2050. response = self.do_request(query)
  2051. assert response.status_code == 400, response.content
  2052. def test_aggregation_comparison_with_conditions(self):
  2053. self.store_event(
  2054. data={
  2055. "event_id": "a" * 32,
  2056. "timestamp": self.ten_mins_ago_iso,
  2057. "fingerprint": ["group_1"],
  2058. "user": {"email": "foo@example.com"},
  2059. "environment": "prod",
  2060. },
  2061. project_id=self.project.id,
  2062. )
  2063. self.store_event(
  2064. data={
  2065. "event_id": "b" * 32,
  2066. "timestamp": self.ten_mins_ago_iso,
  2067. "fingerprint": ["group_2"],
  2068. "user": {"email": "foo@example.com"},
  2069. "environment": "staging",
  2070. },
  2071. project_id=self.project.id,
  2072. )
  2073. event = self.store_event(
  2074. data={
  2075. "event_id": "c" * 32,
  2076. "timestamp": self.ten_mins_ago_iso,
  2077. "fingerprint": ["group_2"],
  2078. "user": {"email": "foo@example.com"},
  2079. "environment": "prod",
  2080. },
  2081. project_id=self.project.id,
  2082. )
  2083. self.store_event(
  2084. data={
  2085. "event_id": "d" * 32,
  2086. "timestamp": self.ten_mins_ago_iso,
  2087. "fingerprint": ["group_2"],
  2088. "user": {"email": "foo@example.com"},
  2089. "environment": "prod",
  2090. },
  2091. project_id=self.project.id,
  2092. )
  2093. query = {
  2094. "field": ["issue.id", "count(id)"],
  2095. "query": "count(id):>1 user.email:foo@example.com environment:prod",
  2096. "orderby": "issue.id",
  2097. }
  2098. response = self.do_request(query)
  2099. assert response.status_code == 200, response.content
  2100. assert len(response.data["data"]) == 1
  2101. data = response.data["data"]
  2102. assert data[0]["issue.id"] == event.group_id
  2103. assert data[0]["count(id)"] == 2
  2104. def test_aggregation_date_comparison_with_conditions(self):
  2105. event = self.store_event(
  2106. data={
  2107. "event_id": "a" * 32,
  2108. "timestamp": self.ten_mins_ago_iso,
  2109. "fingerprint": ["group_1"],
  2110. "user": {"email": "foo@example.com"},
  2111. "environment": "prod",
  2112. },
  2113. project_id=self.project.id,
  2114. )
  2115. self.store_event(
  2116. data={
  2117. "event_id": "b" * 32,
  2118. "timestamp": self.ten_mins_ago_iso,
  2119. "fingerprint": ["group_2"],
  2120. "user": {"email": "foo@example.com"},
  2121. "environment": "staging",
  2122. },
  2123. project_id=self.project.id,
  2124. )
  2125. self.store_event(
  2126. data={
  2127. "event_id": "c" * 32,
  2128. "timestamp": self.ten_mins_ago_iso,
  2129. "fingerprint": ["group_2"],
  2130. "user": {"email": "foo@example.com"},
  2131. "environment": "prod",
  2132. },
  2133. project_id=self.project.id,
  2134. )
  2135. self.store_event(
  2136. data={
  2137. "event_id": "d" * 32,
  2138. "timestamp": self.ten_mins_ago_iso,
  2139. "fingerprint": ["group_2"],
  2140. "user": {"email": "foo@example.com"},
  2141. "environment": "prod",
  2142. },
  2143. project_id=self.project.id,
  2144. )
  2145. query = {
  2146. "field": ["issue.id", "max(timestamp)"],
  2147. "query": "max(timestamp):>1 user.email:foo@example.com environment:prod",
  2148. "orderby": "issue.id",
  2149. }
  2150. response = self.do_request(query)
  2151. assert response.status_code == 200, response.content
  2152. assert len(response.data["data"]) == 2
  2153. assert response.data["meta"]["fields"]["max(timestamp)"] == "date"
  2154. data = response.data["data"]
  2155. assert data[0]["issue.id"] == event.group_id
  2156. def test_percentile_function(self):
  2157. data = self.load_data(
  2158. timestamp=self.ten_mins_ago,
  2159. duration=timedelta(seconds=5),
  2160. )
  2161. data["transaction"] = "/aggregates/1"
  2162. event1 = self.store_event(data, project_id=self.project.id)
  2163. data = self.load_data(
  2164. timestamp=self.ten_mins_ago,
  2165. duration=timedelta(seconds=3),
  2166. )
  2167. data["transaction"] = "/aggregates/2"
  2168. event2 = self.store_event(data, project_id=self.project.id)
  2169. query = {
  2170. "field": ["transaction", "percentile(transaction.duration, 0.95)"],
  2171. "query": "event.type:transaction",
  2172. "orderby": ["transaction"],
  2173. }
  2174. response = self.do_request(query)
  2175. assert response.status_code == 200, response.content
  2176. assert len(response.data["data"]) == 2
  2177. data = response.data["data"]
  2178. assert data[0]["transaction"] == event1.transaction
  2179. assert data[0]["percentile(transaction.duration, 0.95)"] == 5000
  2180. assert data[1]["transaction"] == event2.transaction
  2181. assert data[1]["percentile(transaction.duration, 0.95)"] == 3000
  2182. def test_percentile_function_as_condition(self):
  2183. data = self.load_data(
  2184. timestamp=self.ten_mins_ago,
  2185. duration=timedelta(seconds=5),
  2186. )
  2187. data["transaction"] = "/aggregates/1"
  2188. event1 = self.store_event(data, project_id=self.project.id)
  2189. data = self.load_data(
  2190. timestamp=self.ten_mins_ago,
  2191. duration=timedelta(seconds=3),
  2192. )
  2193. data["transaction"] = "/aggregates/2"
  2194. self.store_event(data, project_id=self.project.id)
  2195. query = {
  2196. "field": ["transaction", "percentile(transaction.duration, 0.95)"],
  2197. "query": "event.type:transaction percentile(transaction.duration, 0.95):>4000",
  2198. "orderby": ["transaction"],
  2199. }
  2200. response = self.do_request(query)
  2201. assert response.status_code == 200, response.content
  2202. assert len(response.data["data"]) == 1
  2203. data = response.data["data"]
  2204. assert data[0]["transaction"] == event1.transaction
  2205. assert data[0]["percentile(transaction.duration, 0.95)"] == 5000
  2206. def test_epm_function(self):
  2207. data = self.load_data(
  2208. timestamp=self.ten_mins_ago,
  2209. duration=timedelta(seconds=5),
  2210. )
  2211. data["transaction"] = "/aggregates/1"
  2212. event1 = self.store_event(data, project_id=self.project.id)
  2213. data = self.load_data(
  2214. timestamp=self.ten_mins_ago,
  2215. duration=timedelta(seconds=3),
  2216. )
  2217. data["transaction"] = "/aggregates/2"
  2218. event2 = self.store_event(data, project_id=self.project.id)
  2219. query = {
  2220. "field": ["transaction", "epm()"],
  2221. "query": "event.type:transaction",
  2222. "orderby": ["transaction"],
  2223. "start": self.eleven_mins_ago_iso,
  2224. "end": iso_format(self.nine_mins_ago),
  2225. }
  2226. response = self.do_request(query)
  2227. assert response.status_code == 200, response.content
  2228. assert len(response.data["data"]) == 2
  2229. data = response.data["data"]
  2230. assert data[0]["transaction"] == event1.transaction
  2231. assert data[0]["epm()"] == 0.5
  2232. assert data[1]["transaction"] == event2.transaction
  2233. assert data[1]["epm()"] == 0.5
  2234. def test_nonexistent_fields(self):
  2235. self.store_event(
  2236. data={
  2237. "event_id": "a" * 32,
  2238. "message": "how to make fast",
  2239. "timestamp": self.ten_mins_ago_iso,
  2240. },
  2241. project_id=self.project.id,
  2242. )
  2243. query = {"field": ["issue_world.id"]}
  2244. response = self.do_request(query)
  2245. assert response.status_code == 200, response.content
  2246. assert response.data["data"][0]["issue_world.id"] == ""
  2247. def test_no_requested_fields_or_grouping(self):
  2248. self.store_event(
  2249. data={
  2250. "event_id": "a" * 32,
  2251. "message": "how to make fast",
  2252. "timestamp": self.ten_mins_ago_iso,
  2253. },
  2254. project_id=self.project.id,
  2255. )
  2256. query = {"query": "test"}
  2257. response = self.do_request(query)
  2258. assert response.status_code == 400, response.content
  2259. assert response.data["detail"] == "No columns selected"
  2260. def test_condition_on_aggregate_misses(self):
  2261. self.store_event(
  2262. data={
  2263. "event_id": "c" * 32,
  2264. "timestamp": self.ten_mins_ago_iso,
  2265. "fingerprint": ["group_2"],
  2266. "user": {"email": "bar@example.com"},
  2267. },
  2268. project_id=self.project.id,
  2269. )
  2270. query = {"field": ["issue.id"], "query": "event_count:>0", "orderby": "issue.id"}
  2271. response = self.do_request(query)
  2272. assert response.status_code == 200, response.content
  2273. assert len(response.data["data"]) == 0
  2274. def test_next_prev_link_headers(self):
  2275. events = [("a", "group_1"), ("b", "group_2"), ("c", "group_2"), ("d", "group_2")]
  2276. for e in events:
  2277. self.store_event(
  2278. data={
  2279. "event_id": e[0] * 32,
  2280. "timestamp": self.ten_mins_ago_iso,
  2281. "fingerprint": [e[1]],
  2282. "user": {"email": "foo@example.com"},
  2283. "tags": {"language": "C++"},
  2284. },
  2285. project_id=self.project.id,
  2286. )
  2287. query = {
  2288. "field": ["count(id)", "issue.id", "context.key"],
  2289. "sort": "-count_id",
  2290. "query": "language:C++",
  2291. }
  2292. response = self.do_request(query)
  2293. assert response.status_code == 200, response.content
  2294. links = parse_link_header(response["Link"])
  2295. for link in links:
  2296. assert "field=issue.id" in link
  2297. assert "field=count%28id%29" in link
  2298. assert "field=context.key" in link
  2299. assert "sort=-count_id" in link
  2300. assert "query=language%3AC%2B%2B" in link
  2301. assert len(response.data["data"]) == 2
  2302. data = response.data["data"]
  2303. assert data[0]["count(id)"] == 3
  2304. assert data[1]["count(id)"] == 1
  2305. def test_empty_count_query(self):
  2306. event = self.store_event(
  2307. data={
  2308. "event_id": "a" * 32,
  2309. "timestamp": self.ten_mins_ago_iso,
  2310. "fingerprint": ["1123581321"],
  2311. "user": {"email": "foo@example.com"},
  2312. "tags": {"language": "C++"},
  2313. },
  2314. project_id=self.project.id,
  2315. )
  2316. query = {
  2317. "field": ["count()"],
  2318. "query": f"issue.id:{event.group_id} timestamp:>{self.ten_mins_ago_iso}",
  2319. "statsPeriod": "14d",
  2320. }
  2321. response = self.do_request(query)
  2322. assert response.status_code == 200, response.content
  2323. data = response.data["data"]
  2324. assert len(data) == 1
  2325. assert data[0]["count()"] == 0
  2326. def test_stack_wildcard_condition(self):
  2327. data = self.load_data(platform="javascript")
  2328. data["timestamp"] = self.ten_mins_ago_iso
  2329. self.store_event(data=data, project_id=self.project.id)
  2330. query = {"field": ["stack.filename", "message"], "query": "stack.filename:*.js"}
  2331. response = self.do_request(query)
  2332. assert response.status_code == 200, response.content
  2333. assert len(response.data["data"]) == 1
  2334. assert response.data["meta"]["fields"]["message"] == "string"
  2335. def test_email_wildcard_condition(self):
  2336. data = self.load_data(platform="javascript")
  2337. data["timestamp"] = self.ten_mins_ago_iso
  2338. self.store_event(data=data, project_id=self.project.id)
  2339. query = {"field": ["stack.filename", "message"], "query": "user.email:*@example.org"}
  2340. response = self.do_request(query)
  2341. assert response.status_code == 200, response.content
  2342. assert len(response.data["data"]) == 1
  2343. assert response.data["meta"]["fields"]["message"] == "string"
  2344. def test_release_wildcard_condition(self):
  2345. release = self.create_release(version="test@1.2.3+123")
  2346. self.store_event(
  2347. data={"release": release.version, "timestamp": self.ten_mins_ago_iso},
  2348. project_id=self.project.id,
  2349. )
  2350. query = {"field": ["stack.filename", "release"], "query": "release:test*"}
  2351. response = self.do_request(query)
  2352. assert response.status_code == 200, response.content
  2353. assert len(response.data["data"]) == 1
  2354. assert response.data["data"][0]["release"] == release.version
  2355. def test_transaction_event_type(self):
  2356. self.store_event(data=self.transaction_data, project_id=self.project.id)
  2357. query = {
  2358. "field": ["transaction", "transaction.duration", "transaction.status"],
  2359. "query": "event.type:transaction",
  2360. }
  2361. response = self.do_request(query)
  2362. assert response.status_code == 200, response.content
  2363. assert len(response.data["data"]) == 1
  2364. assert response.data["meta"]["fields"]["transaction.duration"] == "duration"
  2365. assert response.data["meta"]["fields"]["transaction.status"] == "string"
  2366. assert response.data["meta"]["units"]["transaction.duration"] == "millisecond"
  2367. assert response.data["data"][0]["transaction.status"] == "ok"
  2368. def test_trace_columns(self):
  2369. self.store_event(data=self.transaction_data, project_id=self.project.id)
  2370. query = {"field": ["trace"], "query": "event.type:transaction"}
  2371. response = self.do_request(query)
  2372. assert response.status_code == 200, response.content
  2373. assert len(response.data["data"]) == 1
  2374. assert response.data["meta"]["fields"]["trace"] == "string"
  2375. assert (
  2376. response.data["data"][0]["trace"]
  2377. == self.transaction_data["contexts"]["trace"]["trace_id"]
  2378. )
  2379. def test_issue_in_columns(self):
  2380. project1 = self.create_project()
  2381. project2 = self.create_project()
  2382. event1 = self.store_event(
  2383. data={
  2384. "event_id": "a" * 32,
  2385. "transaction": "/example",
  2386. "message": "how to make fast",
  2387. "timestamp": self.ten_mins_ago_iso,
  2388. "fingerprint": ["group_1"],
  2389. },
  2390. project_id=project1.id,
  2391. )
  2392. event2 = self.store_event(
  2393. data={
  2394. "event_id": "b" * 32,
  2395. "transaction": "/example",
  2396. "message": "how to make fast",
  2397. "timestamp": self.ten_mins_ago_iso,
  2398. "fingerprint": ["group_1"],
  2399. },
  2400. project_id=project2.id,
  2401. )
  2402. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2403. query = {"field": ["id", "issue"], "orderby": ["id"]}
  2404. response = self.do_request(query, features=features)
  2405. assert response.status_code == 200, response.content
  2406. data = response.data["data"]
  2407. assert len(data) == 2
  2408. assert data[0]["id"] == event1.event_id
  2409. assert data[0]["issue.id"] == event1.group_id
  2410. assert data[0]["issue"] == event1.group.qualified_short_id
  2411. assert data[1]["id"] == event2.event_id
  2412. assert data[1]["issue.id"] == event2.group_id
  2413. assert data[1]["issue"] == event2.group.qualified_short_id
  2414. def test_issue_in_search_and_columns(self):
  2415. project1 = self.create_project()
  2416. project2 = self.create_project()
  2417. event1 = self.store_event(
  2418. data={
  2419. "event_id": "a" * 32,
  2420. "transaction": "/example",
  2421. "message": "how to make fast",
  2422. "timestamp": self.ten_mins_ago_iso,
  2423. "fingerprint": ["group_1"],
  2424. },
  2425. project_id=project1.id,
  2426. )
  2427. self.store_event(
  2428. data={
  2429. "event_id": "b" * 32,
  2430. "transaction": "/example",
  2431. "message": "how to make fast",
  2432. "timestamp": self.ten_mins_ago_iso,
  2433. "fingerprint": ["group_1"],
  2434. },
  2435. project_id=project2.id,
  2436. )
  2437. tests = [
  2438. ("issue", "issue:%s" % event1.group.qualified_short_id),
  2439. ("issue.id", "issue:%s" % event1.group.qualified_short_id),
  2440. ("issue", "issue.id:%s" % event1.group_id),
  2441. ("issue.id", "issue.id:%s" % event1.group_id),
  2442. ]
  2443. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2444. for testdata in tests:
  2445. query = {"field": [testdata[0]], "query": testdata[1]}
  2446. response = self.do_request(query, features=features)
  2447. assert response.status_code == 200, response.content
  2448. data = response.data["data"]
  2449. assert len(data) == 1
  2450. assert data[0]["id"] == event1.event_id
  2451. assert data[0]["issue.id"] == event1.group_id
  2452. if testdata[0] == "issue":
  2453. assert data[0]["issue"] == event1.group.qualified_short_id
  2454. else:
  2455. assert data[0].get("issue", None) is None
  2456. def test_issue_negation(self):
  2457. project1 = self.create_project()
  2458. project2 = self.create_project()
  2459. event1 = self.store_event(
  2460. data={
  2461. "event_id": "a" * 32,
  2462. "transaction": "/example",
  2463. "message": "how to make fast",
  2464. "timestamp": self.ten_mins_ago_iso,
  2465. "fingerprint": ["group_1"],
  2466. },
  2467. project_id=project1.id,
  2468. )
  2469. event2 = self.store_event(
  2470. data={
  2471. "event_id": "b" * 32,
  2472. "transaction": "/example",
  2473. "message": "go really fast plz",
  2474. "timestamp": self.ten_mins_ago_iso,
  2475. "fingerprint": ["group_2"],
  2476. },
  2477. project_id=project2.id,
  2478. )
  2479. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2480. query = {
  2481. "field": ["title", "issue.id"],
  2482. "query": f"!issue:{event1.group.qualified_short_id}",
  2483. }
  2484. response = self.do_request(query, features=features)
  2485. assert response.status_code == 200, response.content
  2486. data = response.data["data"]
  2487. assert len(data) == 1
  2488. assert data[0]["title"] == event2.title
  2489. assert data[0]["issue.id"] == event2.group_id
  2490. def test_search_for_nonexistent_issue(self):
  2491. self.store_event(
  2492. data={
  2493. "event_id": "a" * 32,
  2494. "transaction": "/example",
  2495. "message": "how to make fast",
  2496. "timestamp": self.ten_mins_ago_iso,
  2497. "fingerprint": ["group_1"],
  2498. },
  2499. project_id=self.project.id,
  2500. )
  2501. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2502. query = {"field": ["count()"], "query": "issue.id:112358"}
  2503. response = self.do_request(query, features=features)
  2504. assert response.status_code == 200, response.content
  2505. data = response.data["data"]
  2506. assert len(data) == 1
  2507. assert data[0]["count()"] == 0
  2508. def test_issue_alias_inside_aggregate(self):
  2509. self.store_event(
  2510. data={
  2511. "event_id": "a" * 32,
  2512. "transaction": "/example",
  2513. "message": "how to make fast",
  2514. "timestamp": self.ten_mins_ago_iso,
  2515. "fingerprint": ["group_1"],
  2516. },
  2517. project_id=self.project.id,
  2518. )
  2519. self.store_event(
  2520. data={
  2521. "event_id": "b" * 32,
  2522. "transaction": "/example",
  2523. "message": "how to make fast",
  2524. "timestamp": self.ten_mins_ago_iso,
  2525. "fingerprint": ["group_2"],
  2526. },
  2527. project_id=self.project.id,
  2528. )
  2529. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2530. query = {
  2531. "field": ["project", "count(id)", "count_unique(issue.id)", "count_unique(issue)"],
  2532. "sort": "-count(id)",
  2533. "statsPeriod": "24h",
  2534. }
  2535. response = self.do_request(query, features=features)
  2536. assert response.status_code == 200, response.content
  2537. data = response.data["data"]
  2538. assert len(data) == 1
  2539. assert data[0]["count(id)"] == 2
  2540. assert data[0]["count_unique(issue.id)"] == 2
  2541. assert data[0]["count_unique(issue)"] == 2
  2542. def test_project_alias_inside_aggregate(self):
  2543. project1 = self.create_project()
  2544. project2 = self.create_project()
  2545. self.store_event(
  2546. data={
  2547. "event_id": "a" * 32,
  2548. "transaction": "/example",
  2549. "message": "how to make fast",
  2550. "timestamp": self.ten_mins_ago_iso,
  2551. "fingerprint": ["group_1"],
  2552. },
  2553. project_id=project1.id,
  2554. )
  2555. self.store_event(
  2556. data={
  2557. "event_id": "b" * 32,
  2558. "transaction": "/example",
  2559. "message": "how to make fast",
  2560. "timestamp": self.ten_mins_ago_iso,
  2561. "fingerprint": ["group_2"],
  2562. },
  2563. project_id=project2.id,
  2564. )
  2565. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2566. query = {
  2567. "field": [
  2568. "event.type",
  2569. "count(id)",
  2570. "count_unique(project.id)",
  2571. "count_unique(project)",
  2572. ],
  2573. "sort": "-count(id)",
  2574. "statsPeriod": "24h",
  2575. }
  2576. response = self.do_request(query, features=features)
  2577. assert response.status_code == 200, response.content
  2578. data = response.data["data"]
  2579. assert len(data) == 1
  2580. assert data[0]["count(id)"] == 2
  2581. assert data[0]["count_unique(project.id)"] == 2
  2582. assert data[0]["count_unique(project)"] == 2
  2583. def test_user_display(self):
  2584. project1 = self.create_project()
  2585. project2 = self.create_project()
  2586. self.store_event(
  2587. data={
  2588. "event_id": "a" * 32,
  2589. "transaction": "/example",
  2590. "message": "how to make fast",
  2591. "timestamp": self.ten_mins_ago_iso,
  2592. "user": {"email": "cathy@example.com"},
  2593. },
  2594. project_id=project1.id,
  2595. )
  2596. self.store_event(
  2597. data={
  2598. "event_id": "b" * 32,
  2599. "transaction": "/example",
  2600. "message": "how to make fast",
  2601. "timestamp": self.ten_mins_ago_iso,
  2602. "user": {"username": "catherine"},
  2603. },
  2604. project_id=project2.id,
  2605. )
  2606. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2607. query = {
  2608. "field": ["event.type", "user.display"],
  2609. "query": "user.display:cath*",
  2610. "statsPeriod": "24h",
  2611. }
  2612. response = self.do_request(query, features=features)
  2613. assert response.status_code == 200, response.content
  2614. data = response.data["data"]
  2615. assert len(data) == 2
  2616. result = {r["user.display"] for r in data}
  2617. assert result == {"catherine", "cathy@example.com"}
  2618. def test_user_display_with_aggregates(self):
  2619. self.store_event(
  2620. data={
  2621. "event_id": "a" * 32,
  2622. "transaction": "/example",
  2623. "message": "how to make fast",
  2624. "timestamp": self.ten_mins_ago_iso,
  2625. "user": {"email": "cathy@example.com"},
  2626. },
  2627. project_id=self.project.id,
  2628. )
  2629. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2630. query = {
  2631. "field": ["event.type", "user.display", "count_unique(title)"],
  2632. "statsPeriod": "24h",
  2633. }
  2634. response = self.do_request(query, features=features)
  2635. assert response.status_code == 200, response.content
  2636. data = response.data["data"]
  2637. assert len(data) == 1
  2638. result = {r["user.display"] for r in data}
  2639. assert result == {"cathy@example.com"}
  2640. query = {"field": ["event.type", "count_unique(user.display)"], "statsPeriod": "24h"}
  2641. response = self.do_request(query, features=features)
  2642. assert response.status_code == 200, response.content
  2643. data = response.data["data"]
  2644. assert len(data) == 1
  2645. assert data[0]["count_unique(user.display)"] == 1
  2646. def test_orderby_user_display(self):
  2647. project1 = self.create_project()
  2648. project2 = self.create_project()
  2649. self.store_event(
  2650. data={
  2651. "event_id": "a" * 32,
  2652. "transaction": "/example",
  2653. "message": "how to make fast",
  2654. "timestamp": self.ten_mins_ago_iso,
  2655. "user": {"email": "cathy@example.com"},
  2656. },
  2657. project_id=project1.id,
  2658. )
  2659. self.store_event(
  2660. data={
  2661. "event_id": "b" * 32,
  2662. "transaction": "/example",
  2663. "message": "how to make fast",
  2664. "timestamp": self.ten_mins_ago_iso,
  2665. "user": {"username": "catherine"},
  2666. },
  2667. project_id=project2.id,
  2668. )
  2669. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2670. query = {
  2671. "field": ["event.type", "user.display"],
  2672. "query": "user.display:cath*",
  2673. "statsPeriod": "24h",
  2674. "orderby": "-user.display",
  2675. }
  2676. response = self.do_request(query, features=features)
  2677. assert response.status_code == 200, response.content
  2678. data = response.data["data"]
  2679. assert len(data) == 2
  2680. result = [r["user.display"] for r in data]
  2681. # because we're ordering by `-user.display`, we expect the results in reverse sorted order
  2682. assert result == ["cathy@example.com", "catherine"]
  2683. def test_orderby_user_display_with_aggregates(self):
  2684. project1 = self.create_project()
  2685. project2 = self.create_project()
  2686. self.store_event(
  2687. data={
  2688. "event_id": "a" * 32,
  2689. "transaction": "/example",
  2690. "message": "how to make fast",
  2691. "timestamp": self.ten_mins_ago_iso,
  2692. "user": {"email": "cathy@example.com"},
  2693. },
  2694. project_id=project1.id,
  2695. )
  2696. self.store_event(
  2697. data={
  2698. "event_id": "b" * 32,
  2699. "transaction": "/example",
  2700. "message": "how to make fast",
  2701. "timestamp": self.ten_mins_ago_iso,
  2702. "user": {"username": "catherine"},
  2703. },
  2704. project_id=project2.id,
  2705. )
  2706. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2707. query = {
  2708. "field": ["event.type", "user.display", "count_unique(title)"],
  2709. "query": "user.display:cath*",
  2710. "statsPeriod": "24h",
  2711. "orderby": "user.display",
  2712. }
  2713. response = self.do_request(query, features=features)
  2714. assert response.status_code == 200, response.content
  2715. data = response.data["data"]
  2716. assert len(data) == 2
  2717. result = [r["user.display"] for r in data]
  2718. # because we're ordering by `user.display`, we expect the results in sorted order
  2719. assert result == ["catherine", "cathy@example.com"]
  2720. def test_any_field_alias(self):
  2721. day_ago = before_now(days=1).replace(hour=10, minute=11, second=12, microsecond=13)
  2722. self.store_event(
  2723. data={
  2724. "event_id": "a" * 32,
  2725. "transaction": "/example",
  2726. "message": "how to make fast",
  2727. "timestamp": iso_format(day_ago),
  2728. "user": {"email": "cathy@example.com"},
  2729. },
  2730. project_id=self.project.id,
  2731. )
  2732. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2733. query = {
  2734. "field": [
  2735. "event.type",
  2736. "any(user.display)",
  2737. "any(timestamp.to_day)",
  2738. "any(timestamp.to_hour)",
  2739. ],
  2740. "statsPeriod": "7d",
  2741. }
  2742. response = self.do_request(query, features=features)
  2743. assert response.status_code == 200, response.content
  2744. data = response.data["data"]
  2745. assert len(data) == 1
  2746. result = {r["any(user.display)"] for r in data}
  2747. assert result == {"cathy@example.com"}
  2748. result = {r["any(timestamp.to_day)"][:19] for r in data}
  2749. assert result == {iso_format(day_ago.replace(hour=0, minute=0, second=0, microsecond=0))}
  2750. result = {r["any(timestamp.to_hour)"][:19] for r in data}
  2751. assert result == {iso_format(day_ago.replace(minute=0, second=0, microsecond=0))}
  2752. def test_field_aliases_in_conflicting_functions(self):
  2753. self.store_event(
  2754. data={
  2755. "event_id": "a" * 32,
  2756. "transaction": "/example",
  2757. "message": "how to make fast",
  2758. "timestamp": iso_format(
  2759. before_now(days=1).replace(hour=10, minute=11, second=12, microsecond=13)
  2760. ),
  2761. "user": {"email": "cathy@example.com"},
  2762. },
  2763. project_id=self.project.id,
  2764. )
  2765. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2766. field_aliases = ["user.display", "timestamp.to_day", "timestamp.to_hour"]
  2767. for alias in field_aliases:
  2768. query = {
  2769. "field": [alias, f"any({alias})"],
  2770. "statsPeriod": "7d",
  2771. }
  2772. response = self.do_request(query, features=features)
  2773. assert response.status_code == 400, response.content
  2774. assert (
  2775. response.data["detail"]
  2776. == f"A single field cannot be used both inside and outside a function in the same query. To use {alias} you must first remove the function(s): any({alias})"
  2777. )
  2778. @pytest.mark.skip(
  2779. """
  2780. For some reason ClickHouse errors when there are two of the same string literals
  2781. (in this case the empty string "") in a query and one is in the prewhere clause.
  2782. Does not affect production or ClickHouse versions > 20.4.
  2783. """
  2784. )
  2785. def test_has_message(self):
  2786. event = self.store_event(
  2787. {"timestamp": self.ten_mins_ago_iso, "message": "a"}, project_id=self.project.id
  2788. )
  2789. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2790. query = {"field": ["project", "message"], "query": "has:message", "statsPeriod": "14d"}
  2791. response = self.do_request(query, features=features)
  2792. assert response.status_code == 200, response.content
  2793. assert len(response.data["data"]) == 1
  2794. assert response.data["data"][0]["message"] == event.message
  2795. query = {"field": ["project", "message"], "query": "!has:message", "statsPeriod": "14d"}
  2796. response = self.do_request(query, features=features)
  2797. assert response.status_code == 200, response.content
  2798. assert len(response.data["data"]) == 0
  2799. def test_has_transaction_status(self):
  2800. self.store_event(self.transaction_data, project_id=self.project.id)
  2801. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2802. query = {
  2803. "field": ["event.type", "count(id)"],
  2804. "query": "event.type:transaction has:transaction.status",
  2805. "sort": "-count(id)",
  2806. "statsPeriod": "24h",
  2807. }
  2808. response = self.do_request(query, features=features)
  2809. assert response.status_code == 200, response.content
  2810. data = response.data["data"]
  2811. assert len(data) == 1
  2812. assert data[0]["count(id)"] == 1
  2813. def test_not_has_transaction_status(self):
  2814. self.store_event(self.transaction_data, project_id=self.project.id)
  2815. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2816. query = {
  2817. "field": ["event.type", "count(id)"],
  2818. "query": "event.type:transaction !has:transaction.status",
  2819. "sort": "-count(id)",
  2820. "statsPeriod": "24h",
  2821. }
  2822. response = self.do_request(query, features=features)
  2823. assert response.status_code == 200, response.content
  2824. data = response.data["data"]
  2825. assert len(data) == 1
  2826. assert data[0]["count(id)"] == 0
  2827. def test_tag_that_looks_like_aggregation(self):
  2828. data = {
  2829. "message": "Failure state",
  2830. "timestamp": self.ten_mins_ago_iso,
  2831. "tags": {"count_diff": 99},
  2832. }
  2833. self.store_event(data, project_id=self.project.id)
  2834. query = {
  2835. "field": ["message", "count_diff", "count()"],
  2836. "query": "",
  2837. "project": [self.project.id],
  2838. "statsPeriod": "24h",
  2839. }
  2840. response = self.do_request(query)
  2841. assert response.status_code == 200, response.content
  2842. meta = response.data["meta"]["fields"]
  2843. assert "string" == meta["count_diff"], "tags should not be counted as integers"
  2844. assert "string" == meta["message"]
  2845. assert "integer" == meta["count()"]
  2846. assert 1 == len(response.data["data"])
  2847. data = response.data["data"][0]
  2848. assert "99" == data["count_diff"]
  2849. assert "Failure state" == data["message"]
  2850. assert 1 == data["count()"]
  2851. def test_aggregate_negation(self):
  2852. data = self.load_data(
  2853. timestamp=self.ten_mins_ago,
  2854. duration=timedelta(seconds=5),
  2855. )
  2856. self.store_event(data, project_id=self.project.id)
  2857. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2858. query = {
  2859. "field": ["event.type", "count()"],
  2860. "query": "event.type:transaction count():1",
  2861. "statsPeriod": "24h",
  2862. }
  2863. response = self.do_request(query, features=features)
  2864. assert response.status_code == 200, response.content
  2865. data = response.data["data"]
  2866. assert len(data) == 1
  2867. query = {
  2868. "field": ["event.type", "count()"],
  2869. "query": "event.type:transaction !count():1",
  2870. "statsPeriod": "24h",
  2871. }
  2872. response = self.do_request(query, features=features)
  2873. assert response.status_code == 200, response.content
  2874. data = response.data["data"]
  2875. assert len(data) == 0
  2876. def test_all_aggregates_in_columns(self):
  2877. data = self.load_data(
  2878. timestamp=self.eleven_mins_ago,
  2879. duration=timedelta(seconds=5),
  2880. )
  2881. data["transaction"] = "/failure_rate/1"
  2882. self.store_event(data, project_id=self.project.id)
  2883. data = self.load_data(
  2884. timestamp=self.ten_mins_ago,
  2885. duration=timedelta(seconds=5),
  2886. )
  2887. data["transaction"] = "/failure_rate/1"
  2888. data["contexts"]["trace"]["status"] = "unauthenticated"
  2889. event = self.store_event(data, project_id=self.project.id)
  2890. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2891. query = {
  2892. "field": [
  2893. "event.type",
  2894. "p50()",
  2895. "p75()",
  2896. "p95()",
  2897. "p99()",
  2898. "p100()",
  2899. "percentile(transaction.duration, 0.99)",
  2900. "apdex(300)",
  2901. "count_miserable(user, 300)",
  2902. "user_misery(300)",
  2903. "failure_rate()",
  2904. ],
  2905. "query": "event.type:transaction",
  2906. }
  2907. response = self.do_request(query, features=features)
  2908. assert response.status_code == 200, response.content
  2909. meta = response.data["meta"]["fields"]
  2910. units = response.data["meta"]["units"]
  2911. assert meta["p50()"] == "duration"
  2912. assert meta["p75()"] == "duration"
  2913. assert meta["p95()"] == "duration"
  2914. assert meta["p99()"] == "duration"
  2915. assert meta["p100()"] == "duration"
  2916. assert meta["percentile(transaction.duration, 0.99)"] == "duration"
  2917. assert meta["apdex(300)"] == "number"
  2918. assert meta["failure_rate()"] == "percentage"
  2919. assert meta["user_misery(300)"] == "number"
  2920. assert meta["count_miserable(user, 300)"] == "integer"
  2921. assert units["p50()"] == "millisecond"
  2922. assert units["p75()"] == "millisecond"
  2923. assert units["p95()"] == "millisecond"
  2924. assert units["p99()"] == "millisecond"
  2925. assert units["p100()"] == "millisecond"
  2926. assert units["percentile(transaction.duration, 0.99)"] == "millisecond"
  2927. data = response.data["data"]
  2928. assert len(data) == 1
  2929. assert data[0]["p50()"] == 5000
  2930. assert data[0]["p75()"] == 5000
  2931. assert data[0]["p95()"] == 5000
  2932. assert data[0]["p99()"] == 5000
  2933. assert data[0]["p100()"] == 5000
  2934. assert data[0]["percentile(transaction.duration, 0.99)"] == 5000
  2935. assert data[0]["apdex(300)"] == 0.0
  2936. assert data[0]["count_miserable(user, 300)"] == 1
  2937. assert data[0]["user_misery(300)"] == 0.058
  2938. assert data[0]["failure_rate()"] == 0.5
  2939. features = {
  2940. "organizations:discover-basic": True,
  2941. "organizations:global-views": True,
  2942. }
  2943. query = {
  2944. "field": [
  2945. "event.type",
  2946. "p50()",
  2947. "p75()",
  2948. "p95()",
  2949. "p99()",
  2950. "p100()",
  2951. "percentile(transaction.duration, 0.99)",
  2952. "apdex(300)",
  2953. "apdex()",
  2954. "count_miserable(user, 300)",
  2955. "user_misery(300)",
  2956. "failure_rate()",
  2957. "count_miserable(user)",
  2958. "user_misery()",
  2959. ],
  2960. "query": "event.type:transaction",
  2961. "project": [self.project.id],
  2962. }
  2963. response = self.do_request(query, features=features)
  2964. assert response.status_code == 200, response.content
  2965. meta = response.data["meta"]["fields"]
  2966. units = response.data["meta"]["units"]
  2967. assert meta["p50()"] == "duration"
  2968. assert meta["p75()"] == "duration"
  2969. assert meta["p95()"] == "duration"
  2970. assert meta["p99()"] == "duration"
  2971. assert meta["p100()"] == "duration"
  2972. assert meta["percentile(transaction.duration, 0.99)"] == "duration"
  2973. assert meta["apdex(300)"] == "number"
  2974. assert meta["apdex()"] == "number"
  2975. assert meta["failure_rate()"] == "percentage"
  2976. assert meta["user_misery(300)"] == "number"
  2977. assert meta["count_miserable(user, 300)"] == "integer"
  2978. assert meta["project_threshold_config"] == "string"
  2979. assert meta["user_misery()"] == "number"
  2980. assert meta["count_miserable(user)"] == "integer"
  2981. assert units["p50()"] == "millisecond"
  2982. assert units["p75()"] == "millisecond"
  2983. assert units["p95()"] == "millisecond"
  2984. assert units["p99()"] == "millisecond"
  2985. assert units["p100()"] == "millisecond"
  2986. assert units["percentile(transaction.duration, 0.99)"] == "millisecond"
  2987. data = response.data["data"]
  2988. assert len(data) == 1
  2989. assert data[0]["p50()"] == 5000
  2990. assert data[0]["p75()"] == 5000
  2991. assert data[0]["p95()"] == 5000
  2992. assert data[0]["p99()"] == 5000
  2993. assert data[0]["p100()"] == 5000
  2994. assert data[0]["percentile(transaction.duration, 0.99)"] == 5000
  2995. assert data[0]["apdex(300)"] == 0.0
  2996. assert data[0]["apdex()"] == 0.0
  2997. assert data[0]["count_miserable(user, 300)"] == 1
  2998. assert data[0]["user_misery(300)"] == 0.058
  2999. assert data[0]["failure_rate()"] == 0.5
  3000. assert data[0]["project_threshold_config"] == ["duration", 300]
  3001. assert data[0]["user_misery()"] == 0.058
  3002. assert data[0]["count_miserable(user)"] == 1
  3003. query = {
  3004. "field": ["event.type", "last_seen()", "latest_event()"],
  3005. "query": "event.type:transaction",
  3006. }
  3007. response = self.do_request(query, features=features)
  3008. assert response.status_code == 200, response.content
  3009. data = response.data["data"]
  3010. assert len(data) == 1
  3011. assert self.ten_mins_ago_iso[:-5] in data[0]["last_seen()"]
  3012. assert data[0]["latest_event()"] == event.event_id
  3013. query = {
  3014. "field": [
  3015. "event.type",
  3016. "count()",
  3017. "count(id)",
  3018. "count_unique(project)",
  3019. "min(transaction.duration)",
  3020. "max(transaction.duration)",
  3021. "avg(transaction.duration)",
  3022. "stddev(transaction.duration)",
  3023. "var(transaction.duration)",
  3024. "cov(transaction.duration, transaction.duration)",
  3025. "corr(transaction.duration, transaction.duration)",
  3026. "linear_regression(transaction.duration, transaction.duration)",
  3027. "sum(transaction.duration)",
  3028. ],
  3029. "query": "event.type:transaction",
  3030. }
  3031. response = self.do_request(query, features=features)
  3032. assert response.status_code == 200, response.content
  3033. data = response.data["data"]
  3034. assert len(data) == 1
  3035. assert data[0]["count()"] == 2
  3036. assert data[0]["count(id)"] == 2
  3037. assert data[0]["count_unique(project)"] == 1
  3038. assert data[0]["min(transaction.duration)"] == 5000
  3039. assert data[0]["max(transaction.duration)"] == 5000
  3040. assert data[0]["avg(transaction.duration)"] == 5000
  3041. assert data[0]["stddev(transaction.duration)"] == 0.0
  3042. assert data[0]["var(transaction.duration)"] == 0.0
  3043. assert data[0]["cov(transaction.duration, transaction.duration)"] == 0.0
  3044. assert data[0]["corr(transaction.duration, transaction.duration)"] == 0.0
  3045. assert data[0]["linear_regression(transaction.duration, transaction.duration)"] == [0, 0]
  3046. assert data[0]["sum(transaction.duration)"] == 10000
  3047. @requires_not_arm64
  3048. def test_null_user_misery_returns_zero(self):
  3049. self.transaction_data["user"] = None
  3050. self.transaction_data["transaction"] = "/no_users/1"
  3051. self.store_event(self.transaction_data, project_id=self.project.id)
  3052. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3053. query = {
  3054. "field": ["user_misery(300)"],
  3055. "query": "event.type:transaction",
  3056. }
  3057. response = self.do_request(query, features=features)
  3058. assert response.status_code == 200, response.content
  3059. meta = response.data["meta"]["fields"]
  3060. assert meta["user_misery(300)"] == "number"
  3061. data = response.data["data"]
  3062. assert data[0]["user_misery(300)"] == 0
  3063. @requires_not_arm64
  3064. def test_null_user_misery_new_returns_zero(self):
  3065. self.transaction_data["user"] = None
  3066. self.transaction_data["transaction"] = "/no_users/1"
  3067. self.store_event(self.transaction_data, project_id=self.project.id)
  3068. features = {
  3069. "organizations:discover-basic": True,
  3070. }
  3071. query = {
  3072. "field": ["user_misery()"],
  3073. "query": "event.type:transaction",
  3074. }
  3075. response = self.do_request(query, features=features)
  3076. assert response.status_code == 200, response.content
  3077. meta = response.data["meta"]["fields"]
  3078. assert meta["user_misery()"] == "number"
  3079. data = response.data["data"]
  3080. assert data[0]["user_misery()"] == 0
  3081. def test_all_aggregates_in_query(self):
  3082. data = self.load_data(
  3083. timestamp=self.eleven_mins_ago,
  3084. duration=timedelta(seconds=5),
  3085. )
  3086. data["transaction"] = "/failure_rate/1"
  3087. self.store_event(data, project_id=self.project.id)
  3088. data = self.load_data(
  3089. timestamp=self.ten_mins_ago,
  3090. duration=timedelta(seconds=5),
  3091. )
  3092. data["transaction"] = "/failure_rate/2"
  3093. data["contexts"]["trace"]["status"] = "unauthenticated"
  3094. self.store_event(data, project_id=self.project.id)
  3095. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3096. query = {
  3097. "field": [
  3098. "event.type",
  3099. "p50()",
  3100. "p75()",
  3101. "p95()",
  3102. "percentile(transaction.duration, 0.99)",
  3103. "p100()",
  3104. ],
  3105. "query": "event.type:transaction p50():>100 p75():>1000 p95():>1000 p100():>1000 percentile(transaction.duration, 0.99):>1000",
  3106. }
  3107. response = self.do_request(query, features=features)
  3108. assert response.status_code == 200, response.content
  3109. data = response.data["data"]
  3110. assert len(data) == 1
  3111. assert data[0]["p50()"] == 5000
  3112. assert data[0]["p75()"] == 5000
  3113. assert data[0]["p95()"] == 5000
  3114. assert data[0]["p100()"] == 5000
  3115. assert data[0]["percentile(transaction.duration, 0.99)"] == 5000
  3116. query = {
  3117. "field": [
  3118. "event.type",
  3119. "apdex(300)",
  3120. "count_miserable(user, 300)",
  3121. "user_misery(300)",
  3122. "failure_rate()",
  3123. ],
  3124. "query": "event.type:transaction apdex(300):>-1.0 failure_rate():>0.25",
  3125. }
  3126. response = self.do_request(query, features=features)
  3127. assert response.status_code == 200, response.content
  3128. data = response.data["data"]
  3129. assert len(data) == 1
  3130. assert data[0]["apdex(300)"] == 0.0
  3131. assert data[0]["count_miserable(user, 300)"] == 1
  3132. assert data[0]["user_misery(300)"] == 0.058
  3133. assert data[0]["failure_rate()"] == 0.5
  3134. query = {
  3135. "field": ["event.type", "last_seen()", "latest_event()"],
  3136. "query": "event.type:transaction last_seen():>1990-12-01T00:00:00",
  3137. }
  3138. response = self.do_request(query, features=features)
  3139. assert response.status_code == 200, response.content
  3140. data = response.data["data"]
  3141. assert len(data) == 1
  3142. query = {
  3143. "field": ["event.type", "count()", "count(id)", "count_unique(transaction)"],
  3144. "query": "event.type:transaction count():>1 count(id):>1 count_unique(transaction):>1",
  3145. }
  3146. response = self.do_request(query, features=features)
  3147. assert response.status_code == 200, response.content
  3148. data = response.data["data"]
  3149. assert len(data) == 1
  3150. assert data[0]["count()"] == 2
  3151. assert data[0]["count(id)"] == 2
  3152. assert data[0]["count_unique(transaction)"] == 2
  3153. query = {
  3154. "field": [
  3155. "event.type",
  3156. "min(transaction.duration)",
  3157. "max(transaction.duration)",
  3158. "avg(transaction.duration)",
  3159. "sum(transaction.duration)",
  3160. "stddev(transaction.duration)",
  3161. "var(transaction.duration)",
  3162. "cov(transaction.duration, transaction.duration)",
  3163. "corr(transaction.duration, transaction.duration)",
  3164. ],
  3165. "query": " ".join(
  3166. [
  3167. "event.type:transaction",
  3168. "min(transaction.duration):>1000",
  3169. "max(transaction.duration):>1000",
  3170. "avg(transaction.duration):>1000",
  3171. "sum(transaction.duration):>1000",
  3172. "stddev(transaction.duration):>=0.0",
  3173. "var(transaction.duration):>=0.0",
  3174. "cov(transaction.duration, transaction.duration):>=0.0",
  3175. # correlation is nan because variance is 0
  3176. # "corr(transaction.duration, transaction.duration):>=0.0",
  3177. ]
  3178. ),
  3179. }
  3180. response = self.do_request(query, features=features)
  3181. assert response.status_code == 200, response.content
  3182. data = response.data["data"]
  3183. assert len(data) == 1
  3184. assert data[0]["min(transaction.duration)"] == 5000
  3185. assert data[0]["max(transaction.duration)"] == 5000
  3186. assert data[0]["avg(transaction.duration)"] == 5000
  3187. assert data[0]["sum(transaction.duration)"] == 10000
  3188. assert data[0]["stddev(transaction.duration)"] == 0.0
  3189. assert data[0]["var(transaction.duration)"] == 0.0
  3190. assert data[0]["cov(transaction.duration, transaction.duration)"] == 0.0
  3191. assert data[0]["corr(transaction.duration, transaction.duration)"] == 0.0
  3192. query = {
  3193. "field": ["event.type", "apdex(400)"],
  3194. "query": "event.type:transaction apdex(400):0",
  3195. }
  3196. response = self.do_request(query, features=features)
  3197. assert response.status_code == 200, response.content
  3198. data = response.data["data"]
  3199. assert len(data) == 1
  3200. assert data[0]["apdex(400)"] == 0
  3201. def test_functions_in_orderby(self):
  3202. data = self.load_data(
  3203. timestamp=self.eleven_mins_ago,
  3204. duration=timedelta(seconds=5),
  3205. )
  3206. data["transaction"] = "/failure_rate/1"
  3207. self.store_event(data, project_id=self.project.id)
  3208. data = self.load_data(
  3209. timestamp=self.ten_mins_ago,
  3210. duration=timedelta(seconds=5),
  3211. )
  3212. data["transaction"] = "/failure_rate/2"
  3213. data["contexts"]["trace"]["status"] = "unauthenticated"
  3214. event = self.store_event(data, project_id=self.project.id)
  3215. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3216. query = {
  3217. "field": ["event.type", "p75()"],
  3218. "sort": "-p75",
  3219. "query": "event.type:transaction",
  3220. }
  3221. response = self.do_request(query, features=features)
  3222. assert response.status_code == 200, response.content
  3223. data = response.data["data"]
  3224. assert len(data) == 1
  3225. assert data[0]["p75()"] == 5000
  3226. query = {
  3227. "field": ["event.type", "percentile(transaction.duration, 0.99)"],
  3228. "sort": "-percentile_transaction_duration_0_99",
  3229. "query": "event.type:transaction",
  3230. }
  3231. response = self.do_request(query, features=features)
  3232. assert response.status_code == 200, response.content
  3233. data = response.data["data"]
  3234. assert len(data) == 1
  3235. assert data[0]["percentile(transaction.duration, 0.99)"] == 5000
  3236. query = {
  3237. "field": ["event.type", "apdex(300)"],
  3238. "sort": "-apdex(300)",
  3239. "query": "event.type:transaction",
  3240. }
  3241. response = self.do_request(query, features=features)
  3242. assert response.status_code == 200, response.content
  3243. data = response.data["data"]
  3244. assert len(data) == 1
  3245. assert data[0]["apdex(300)"] == 0.0
  3246. query = {
  3247. "field": ["event.type", "latest_event()"],
  3248. "query": "event.type:transaction",
  3249. "sort": "latest_event",
  3250. }
  3251. response = self.do_request(query, features=features)
  3252. assert response.status_code == 200, response.content
  3253. data = response.data["data"]
  3254. assert len(data) == 1
  3255. assert data[0]["latest_event()"] == event.event_id
  3256. query = {
  3257. "field": ["event.type", "count_unique(transaction)"],
  3258. "query": "event.type:transaction",
  3259. "sort": "-count_unique_transaction",
  3260. }
  3261. response = self.do_request(query, features=features)
  3262. assert response.status_code == 200, response.content
  3263. data = response.data["data"]
  3264. assert len(data) == 1
  3265. assert data[0]["count_unique(transaction)"] == 2
  3266. query = {
  3267. "field": ["event.type", "min(transaction.duration)"],
  3268. "query": "event.type:transaction",
  3269. "sort": "-min_transaction_duration",
  3270. }
  3271. response = self.do_request(query, features=features)
  3272. assert response.status_code == 200, response.content
  3273. data = response.data["data"]
  3274. assert len(data) == 1
  3275. assert data[0]["min(transaction.duration)"] == 5000
  3276. def test_issue_alias_in_aggregate(self):
  3277. self.store_event(
  3278. data={
  3279. "event_id": "a" * 32,
  3280. "timestamp": self.eleven_mins_ago_iso,
  3281. "fingerprint": ["group_1"],
  3282. },
  3283. project_id=self.project.id,
  3284. )
  3285. self.store_event(
  3286. data={
  3287. "event_id": "b" * 32,
  3288. "timestamp": self.ten_mins_ago_iso,
  3289. "fingerprint": ["group_2"],
  3290. },
  3291. project_id=self.project.id,
  3292. )
  3293. query = {"field": ["event.type", "count_unique(issue)"], "query": "count_unique(issue):>1"}
  3294. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3295. response = self.do_request(query, features=features)
  3296. assert response.status_code == 200, response.content
  3297. data = response.data["data"]
  3298. assert len(data) == 1
  3299. assert data[0]["count_unique(issue)"] == 2
  3300. def test_deleted_issue_in_results(self):
  3301. event1 = self.store_event(
  3302. data={
  3303. "event_id": "a" * 32,
  3304. "timestamp": self.eleven_mins_ago_iso,
  3305. "fingerprint": ["group_1"],
  3306. },
  3307. project_id=self.project.id,
  3308. )
  3309. event2 = self.store_event(
  3310. data={
  3311. "event_id": "b" * 32,
  3312. "timestamp": self.ten_mins_ago_iso,
  3313. "fingerprint": ["group_2"],
  3314. },
  3315. project_id=self.project.id,
  3316. )
  3317. event2.group.delete()
  3318. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3319. query = {"field": ["issue", "count()"], "sort": "issue.id"}
  3320. response = self.do_request(query, features=features)
  3321. assert response.status_code == 200, response.content
  3322. data = response.data["data"]
  3323. assert len(data) == 2
  3324. assert data[0]["issue"] == event1.group.qualified_short_id
  3325. assert data[1]["issue"] == "unknown"
  3326. def test_last_seen_negative_duration(self):
  3327. self.store_event(
  3328. data={
  3329. "event_id": "f" * 32,
  3330. "timestamp": self.ten_mins_ago_iso,
  3331. "fingerprint": ["group_1"],
  3332. },
  3333. project_id=self.project.id,
  3334. )
  3335. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3336. query = {"field": ["id", "last_seen()"], "query": "last_seen():-30d"}
  3337. response = self.do_request(query, features=features)
  3338. assert response.status_code == 200, response.content
  3339. data = response.data["data"]
  3340. assert len(data) == 1
  3341. assert data[0]["id"] == "f" * 32
  3342. def test_last_seen_aggregate_condition(self):
  3343. self.store_event(
  3344. data={
  3345. "event_id": "f" * 32,
  3346. "timestamp": self.ten_mins_ago_iso,
  3347. "fingerprint": ["group_1"],
  3348. },
  3349. project_id=self.project.id,
  3350. )
  3351. query = {
  3352. "field": ["id", "last_seen()"],
  3353. "query": f"last_seen():>{iso_format(before_now(days=30))}",
  3354. }
  3355. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3356. response = self.do_request(query, features=features)
  3357. assert response.status_code == 200, response.content
  3358. data = response.data["data"]
  3359. assert len(data) == 1
  3360. assert data[0]["id"] == "f" * 32
  3361. def test_conditional_filter(self):
  3362. for v in ["a", "b"]:
  3363. self.store_event(
  3364. data={
  3365. "event_id": v * 32,
  3366. "timestamp": self.ten_mins_ago_iso,
  3367. "fingerprint": ["group_1"],
  3368. },
  3369. project_id=self.project.id,
  3370. )
  3371. query = {
  3372. "field": ["id"],
  3373. "query": "id:{} OR id:{}".format("a" * 32, "b" * 32),
  3374. "orderby": "id",
  3375. }
  3376. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3377. response = self.do_request(query, features=features)
  3378. assert response.status_code == 200, response.content
  3379. data = response.data["data"]
  3380. assert len(data) == 2
  3381. assert data[0]["id"] == "a" * 32
  3382. assert data[1]["id"] == "b" * 32
  3383. def test_aggregation_comparison_with_conditional_filter(self):
  3384. self.store_event(
  3385. data={
  3386. "event_id": "a" * 32,
  3387. "timestamp": self.ten_mins_ago_iso,
  3388. "fingerprint": ["group_1"],
  3389. "user": {"email": "foo@example.com"},
  3390. "environment": "prod",
  3391. },
  3392. project_id=self.project.id,
  3393. )
  3394. self.store_event(
  3395. data={
  3396. "event_id": "b" * 32,
  3397. "timestamp": self.ten_mins_ago_iso,
  3398. "fingerprint": ["group_2"],
  3399. "user": {"email": "foo@example.com"},
  3400. "environment": "staging",
  3401. },
  3402. project_id=self.project.id,
  3403. )
  3404. event = self.store_event(
  3405. data={
  3406. "event_id": "c" * 32,
  3407. "timestamp": self.ten_mins_ago_iso,
  3408. "fingerprint": ["group_2"],
  3409. "user": {"email": "foo@example.com"},
  3410. "environment": "prod",
  3411. },
  3412. project_id=self.project.id,
  3413. )
  3414. self.store_event(
  3415. data={
  3416. "event_id": "d" * 32,
  3417. "timestamp": self.ten_mins_ago_iso,
  3418. "fingerprint": ["group_2"],
  3419. "user": {"email": "foo@example.com"},
  3420. "environment": "canary",
  3421. },
  3422. project_id=self.project.id,
  3423. )
  3424. query = {
  3425. "field": ["issue.id", "count(id)"],
  3426. "query": "count(id):>1 user.email:foo@example.com AND (environment:prod OR environment:staging)",
  3427. "orderby": "issue.id",
  3428. }
  3429. response = self.do_request(query)
  3430. assert response.status_code == 200, response.content
  3431. assert len(response.data["data"]) == 1
  3432. data = response.data["data"]
  3433. assert data[0]["issue.id"] == event.group_id
  3434. assert data[0]["count(id)"] == 2
  3435. def run_test_in_query(self, query, expected_events, expected_negative_events=None):
  3436. params = {
  3437. "field": ["id"],
  3438. "query": query,
  3439. "orderby": "id",
  3440. }
  3441. response = self.do_request(
  3442. params, {"organizations:discover-basic": True, "organizations:global-views": True}
  3443. )
  3444. assert response.status_code == 200, response.content
  3445. assert [row["id"] for row in response.data["data"]] == [e.event_id for e in expected_events]
  3446. if expected_negative_events is not None:
  3447. params["query"] = f"!{query}"
  3448. response = self.do_request(
  3449. params,
  3450. {"organizations:discover-basic": True, "organizations:global-views": True},
  3451. )
  3452. assert response.status_code == 200, response.content
  3453. assert [row["id"] for row in response.data["data"]] == [
  3454. e.event_id for e in expected_negative_events
  3455. ]
  3456. def test_in_query_events(self):
  3457. project_1 = self.create_project()
  3458. event_1 = self.store_event(
  3459. data={
  3460. "event_id": "a" * 32,
  3461. "timestamp": self.ten_mins_ago_iso,
  3462. "fingerprint": ["group_1"],
  3463. "message": "group1",
  3464. "user": {"email": "hello@example.com"},
  3465. "environment": "prod",
  3466. "tags": {"random": "123"},
  3467. "release": "1.0",
  3468. },
  3469. project_id=project_1.id,
  3470. )
  3471. project_2 = self.create_project()
  3472. event_2 = self.store_event(
  3473. data={
  3474. "event_id": "b" * 32,
  3475. "timestamp": self.ten_mins_ago_iso,
  3476. "fingerprint": ["group_2"],
  3477. "message": "group2",
  3478. "user": {"email": "bar@example.com"},
  3479. "environment": "staging",
  3480. "tags": {"random": "456"},
  3481. "stacktrace": {"frames": [{"filename": "src/app/group2.py"}]},
  3482. "release": "1.2",
  3483. },
  3484. project_id=project_2.id,
  3485. )
  3486. project_3 = self.create_project()
  3487. event_3 = self.store_event(
  3488. data={
  3489. "event_id": "c" * 32,
  3490. "timestamp": self.ten_mins_ago_iso,
  3491. "fingerprint": ["group_3"],
  3492. "message": "group3",
  3493. "user": {"email": "foo@example.com"},
  3494. "environment": "canary",
  3495. "tags": {"random": "789"},
  3496. },
  3497. project_id=project_3.id,
  3498. )
  3499. self.run_test_in_query("environment:[prod, staging]", [event_1, event_2], [event_3])
  3500. self.run_test_in_query("environment:[staging]", [event_2], [event_1, event_3])
  3501. self.run_test_in_query(
  3502. "user.email:[foo@example.com, hello@example.com]", [event_1, event_3], [event_2]
  3503. )
  3504. self.run_test_in_query("user.email:[foo@example.com]", [event_3], [event_1, event_2])
  3505. self.run_test_in_query(
  3506. "user.display:[foo@example.com, hello@example.com]", [event_1, event_3], [event_2]
  3507. )
  3508. self.run_test_in_query(
  3509. 'message:["group2 src/app/group2.py in ?", group1]', [event_1, event_2], [event_3]
  3510. )
  3511. self.run_test_in_query(
  3512. f"issue.id:[{event_1.group_id},{event_2.group_id}]", [event_1, event_2]
  3513. )
  3514. self.run_test_in_query(
  3515. f"issue:[{event_1.group.qualified_short_id},{event_2.group.qualified_short_id}]",
  3516. [event_1, event_2],
  3517. )
  3518. self.run_test_in_query(
  3519. f"issue:[{event_1.group.qualified_short_id},{event_2.group.qualified_short_id}, unknown]",
  3520. [event_1, event_2],
  3521. )
  3522. self.run_test_in_query(f"project_id:[{project_3.id},{project_2.id}]", [event_2, event_3])
  3523. self.run_test_in_query(
  3524. f"project.name:[{project_3.slug},{project_2.slug}]", [event_2, event_3]
  3525. )
  3526. self.run_test_in_query("random:[789,456]", [event_2, event_3], [event_1])
  3527. self.run_test_in_query("tags[random]:[789,456]", [event_2, event_3], [event_1])
  3528. self.run_test_in_query("release:[1.0,1.2]", [event_1, event_2], [event_3])
  3529. def test_in_query_events_stack(self):
  3530. test_js = self.store_event(
  3531. self.load_data(
  3532. platform="javascript",
  3533. timestamp=self.ten_mins_ago,
  3534. duration=timedelta(seconds=5),
  3535. ),
  3536. project_id=self.project.id,
  3537. )
  3538. test_java = self.store_event(
  3539. self.load_data(
  3540. platform="java",
  3541. timestamp=self.ten_mins_ago,
  3542. duration=timedelta(seconds=5),
  3543. ),
  3544. project_id=self.project.id,
  3545. )
  3546. self.run_test_in_query(
  3547. "stack.filename:[../../sentry/scripts/views.js]", [test_js], [test_java]
  3548. )
  3549. def test_in_query_transactions(self):
  3550. data = self.transaction_data.copy()
  3551. data["event_id"] = "a" * 32
  3552. data["contexts"]["trace"]["status"] = "ok"
  3553. transaction_1 = self.store_event(data, project_id=self.project.id)
  3554. data = self.transaction_data.copy()
  3555. data["event_id"] = "b" * 32
  3556. data["contexts"]["trace"]["status"] = "aborted"
  3557. transaction_2 = self.store_event(data, project_id=self.project.id)
  3558. data = self.transaction_data.copy()
  3559. data["event_id"] = "c" * 32
  3560. data["contexts"]["trace"]["status"] = "already_exists"
  3561. transaction_3 = self.store_event(data, project_id=self.project.id)
  3562. self.run_test_in_query(
  3563. "transaction.status:[aborted, already_exists]",
  3564. [transaction_2, transaction_3],
  3565. [transaction_1],
  3566. )
  3567. def test_messed_up_function_values(self):
  3568. # TODO (evanh): It would be nice if this surfaced an error to the user.
  3569. # The problem: The && causes the parser to treat that term not as a bad
  3570. # function call but a valid raw search with parens in it. It's not trivial
  3571. # to change the parser to recognize "bad function values" and surface them.
  3572. for v in ["a", "b"]:
  3573. self.store_event(
  3574. data={
  3575. "event_id": v * 32,
  3576. "timestamp": self.ten_mins_ago_iso,
  3577. "fingerprint": ["group_1"],
  3578. },
  3579. project_id=self.project.id,
  3580. )
  3581. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3582. query = {
  3583. "field": [
  3584. "transaction",
  3585. "project",
  3586. "epm()",
  3587. "p50()",
  3588. "p95()",
  3589. "failure_rate()",
  3590. "apdex(300)",
  3591. "count_unique(user)",
  3592. "user_misery(300)",
  3593. "count_miserable(user, 300)",
  3594. ],
  3595. "query": "failure_rate():>0.003&& users:>10 event.type:transaction",
  3596. "sort": "-failure_rate",
  3597. "statsPeriod": "24h",
  3598. }
  3599. response = self.do_request(query, features=features)
  3600. assert response.status_code == 200, response.content
  3601. data = response.data["data"]
  3602. assert len(data) == 0
  3603. def test_context_fields_between_datasets(self):
  3604. event_data = self.load_data(platform="android")
  3605. transaction_data = self.load_data()
  3606. event_data["spans"] = transaction_data["spans"]
  3607. event_data["contexts"]["trace"] = transaction_data["contexts"]["trace"]
  3608. event_data["type"] = "transaction"
  3609. event_data["transaction"] = "/failure_rate/1"
  3610. event_data["timestamp"] = iso_format(self.ten_mins_ago)
  3611. event_data["start_timestamp"] = iso_format(before_now(minutes=10, seconds=5))
  3612. event_data["user"]["geo"] = {"country_code": "US", "region": "CA", "city": "San Francisco"}
  3613. self.store_event(event_data, project_id=self.project.id)
  3614. event_data["type"] = "error"
  3615. self.store_event(event_data, project_id=self.project.id)
  3616. fields = [
  3617. "os.build",
  3618. "os.kernel_version",
  3619. "device.arch",
  3620. # TODO: battery level is not consistent across both datasets
  3621. # "device.battery_level",
  3622. "device.brand",
  3623. "device.charging",
  3624. "device.locale",
  3625. "device.model_id",
  3626. "device.name",
  3627. "device.online",
  3628. "device.orientation",
  3629. "device.simulator",
  3630. "device.uuid",
  3631. ]
  3632. data = [
  3633. {"field": fields + ["location", "count()"], "query": "event.type:error"},
  3634. {"field": fields + ["duration", "count()"], "query": "event.type:transaction"},
  3635. ]
  3636. for datum in data:
  3637. response = self.do_request(datum)
  3638. assert response.status_code == 200, response.content
  3639. assert len(response.data["data"]) == 1, datum
  3640. results = response.data["data"]
  3641. assert results[0]["count()"] == 1, datum
  3642. for field in fields:
  3643. key, value = field.split(".", 1)
  3644. expected = str(event_data["contexts"][key][value])
  3645. assert results[0][field] == expected, field + str(datum)
  3646. def test_http_fields_between_datasets(self):
  3647. event_data = self.load_data(platform="android")
  3648. transaction_data = self.load_data()
  3649. event_data["spans"] = transaction_data["spans"]
  3650. event_data["contexts"]["trace"] = transaction_data["contexts"]["trace"]
  3651. event_data["type"] = "transaction"
  3652. event_data["transaction"] = "/failure_rate/1"
  3653. event_data["timestamp"] = iso_format(self.ten_mins_ago)
  3654. event_data["start_timestamp"] = iso_format(before_now(minutes=10, seconds=5))
  3655. event_data["user"]["geo"] = {"country_code": "US", "region": "CA", "city": "San Francisco"}
  3656. event_data["request"] = transaction_data["request"]
  3657. self.store_event(event_data, project_id=self.project.id)
  3658. event_data["type"] = "error"
  3659. self.store_event(event_data, project_id=self.project.id)
  3660. fields = ["http.method", "http.referer", "http.url"]
  3661. expected = ["GET", "fixtures.transaction", "http://countries:8010/country_by_code/"]
  3662. data = [
  3663. {"field": fields + ["location", "count()"], "query": "event.type:error"},
  3664. {"field": fields + ["duration", "count()"], "query": "event.type:transaction"},
  3665. ]
  3666. for datum in data:
  3667. response = self.do_request(datum)
  3668. assert response.status_code == 200, response.content
  3669. assert len(response.data["data"]) == 1, datum
  3670. results = response.data["data"]
  3671. assert results[0]["count()"] == 1, datum
  3672. for (field, exp) in zip(fields, expected):
  3673. assert results[0][field] == exp, field + str(datum)
  3674. def test_failure_count_alias_field(self):
  3675. data = self.transaction_data.copy()
  3676. data["transaction"] = "/failure_count/success"
  3677. self.store_event(data, project_id=self.project.id)
  3678. data = self.transaction_data.copy()
  3679. data["transaction"] = "/failure_count/unknown"
  3680. data["contexts"]["trace"]["status"] = "unknown_error"
  3681. self.store_event(data, project_id=self.project.id)
  3682. for i in range(6):
  3683. data = self.transaction_data.copy()
  3684. data["transaction"] = f"/failure_count/{i}"
  3685. data["contexts"]["trace"]["status"] = "unauthenticated"
  3686. self.store_event(data, project_id=self.project.id)
  3687. query = {"field": ["count()", "failure_count()"], "query": "event.type:transaction"}
  3688. response = self.do_request(query)
  3689. assert response.status_code == 200, response.content
  3690. assert len(response.data["data"]) == 1
  3691. data = response.data["data"]
  3692. assert data[0]["count()"] == 8
  3693. assert data[0]["failure_count()"] == 6
  3694. @mock.patch("sentry.utils.snuba.quantize_time")
  3695. def test_quantize_dates(self, mock_quantize):
  3696. self.create_project()
  3697. mock_quantize.return_value = before_now(days=1).replace(tzinfo=utc)
  3698. # Don't quantize short time periods
  3699. query = {"statsPeriod": "1h", "query": "", "field": ["id", "timestamp"]}
  3700. self.do_request(query)
  3701. # Don't quantize absolute date periods
  3702. self.do_request(query)
  3703. query = {
  3704. "start": iso_format(before_now(days=20)),
  3705. "end": iso_format(before_now(days=15)),
  3706. "query": "",
  3707. "field": ["id", "timestamp"],
  3708. }
  3709. self.do_request(query)
  3710. assert len(mock_quantize.mock_calls) == 0
  3711. # Quantize long date periods
  3712. query = {"field": ["id", "timestamp"], "statsPeriod": "90d", "query": ""}
  3713. self.do_request(query)
  3714. assert len(mock_quantize.mock_calls) == 2
  3715. @mock.patch("sentry.snuba.discover.query")
  3716. def test_valid_referrer(self, mock):
  3717. mock.return_value = {}
  3718. query = {
  3719. "field": ["user"],
  3720. "referrer": "api.performance.transaction-summary",
  3721. "project": [self.project.id],
  3722. }
  3723. self.do_request(query)
  3724. _, kwargs = mock.call_args
  3725. self.assertEqual(kwargs["referrer"], "api.performance.transaction-summary")
  3726. @mock.patch("sentry.snuba.discover.query")
  3727. def test_invalid_referrer(self, mock):
  3728. mock.return_value = {}
  3729. query = {
  3730. "field": ["user"],
  3731. "referrer": "api.performance.invalid",
  3732. "project": [self.project.id],
  3733. }
  3734. self.do_request(query)
  3735. _, kwargs = mock.call_args
  3736. self.assertEqual(kwargs["referrer"], self.referrer)
  3737. @mock.patch("sentry.snuba.discover.query")
  3738. def test_empty_referrer(self, mock):
  3739. mock.return_value = {}
  3740. query = {
  3741. "field": ["user"],
  3742. "project": [self.project.id],
  3743. }
  3744. self.do_request(query)
  3745. _, kwargs = mock.call_args
  3746. self.assertEqual(kwargs["referrer"], self.referrer)
  3747. @mock.patch("sentry.snuba.discover.query")
  3748. def test_api_token_referrer(self, mock):
  3749. mock.return_value = {}
  3750. # Project ID cannot be inferred when using an org API key, so that must
  3751. # be passed in the parameters
  3752. api_key = ApiKey.objects.create(
  3753. organization_id=self.organization.id, scope_list=["org:read"]
  3754. )
  3755. query = {
  3756. "field": ["project.name", "environment"],
  3757. "project": [self.project.id],
  3758. }
  3759. features = {"organizations:discover-basic": True}
  3760. features.update(self.features)
  3761. url = self.reverse_url()
  3762. with self.feature(features):
  3763. self.client_get(
  3764. url,
  3765. query,
  3766. format="json",
  3767. HTTP_AUTHORIZATION=b"Basic " + b64encode(f"{api_key.key}:".encode()),
  3768. )
  3769. _, kwargs = mock.call_args
  3770. self.assertEqual(kwargs["referrer"], "api.auth-token.events")
  3771. def test_limit_number_of_fields(self):
  3772. self.create_project()
  3773. for i in range(1, 25):
  3774. response = self.do_request({"field": ["id"] * i})
  3775. if i <= 20:
  3776. assert response.status_code == 200
  3777. else:
  3778. assert response.status_code == 400
  3779. assert (
  3780. response.data["detail"]
  3781. == "You can view up to 20 fields at a time. Please delete some and try again."
  3782. )
  3783. def test_percentile_function_meta_types(self):
  3784. self.store_event(self.transaction_data, project_id=self.project.id)
  3785. query = {
  3786. "field": [
  3787. "transaction",
  3788. "percentile(transaction.duration, 0.95)",
  3789. "percentile(measurements.fp, 0.95)",
  3790. "percentile(measurements.fcp, 0.95)",
  3791. "percentile(measurements.lcp, 0.95)",
  3792. "percentile(measurements.fid, 0.95)",
  3793. "percentile(measurements.ttfb, 0.95)",
  3794. "percentile(measurements.ttfb.requesttime, 0.95)",
  3795. "percentile(measurements.cls, 0.95)",
  3796. "percentile(measurements.foo, 0.95)",
  3797. "percentile(measurements.bar, 0.95)",
  3798. ],
  3799. "query": "",
  3800. "orderby": ["transaction"],
  3801. }
  3802. response = self.do_request(query)
  3803. assert response.status_code == 200, response.content
  3804. meta = response.data["meta"]["fields"]
  3805. assert meta["percentile(transaction.duration, 0.95)"] == "duration"
  3806. assert meta["percentile(measurements.fp, 0.95)"] == "duration"
  3807. assert meta["percentile(measurements.fcp, 0.95)"] == "duration"
  3808. assert meta["percentile(measurements.lcp, 0.95)"] == "duration"
  3809. assert meta["percentile(measurements.fid, 0.95)"] == "duration"
  3810. assert meta["percentile(measurements.ttfb, 0.95)"] == "duration"
  3811. assert meta["percentile(measurements.ttfb.requesttime, 0.95)"] == "duration"
  3812. assert meta["percentile(measurements.cls, 0.95)"] == "number"
  3813. assert meta["percentile(measurements.foo, 0.95)"] == "number"
  3814. assert meta["percentile(measurements.bar, 0.95)"] == "number"
  3815. units = response.data["meta"]["units"]
  3816. assert units["percentile(transaction.duration, 0.95)"] == "millisecond"
  3817. assert units["percentile(measurements.fp, 0.95)"] == "millisecond"
  3818. assert units["percentile(measurements.fcp, 0.95)"] == "millisecond"
  3819. assert units["percentile(measurements.lcp, 0.95)"] == "millisecond"
  3820. assert units["percentile(measurements.fid, 0.95)"] == "millisecond"
  3821. assert units["percentile(measurements.ttfb, 0.95)"] == "millisecond"
  3822. assert units["percentile(measurements.ttfb.requesttime, 0.95)"] == "millisecond"
  3823. def test_count_at_least_query(self):
  3824. self.store_event(self.transaction_data, self.project.id)
  3825. response = self.do_request({"field": "count_at_least(measurements.fcp, 0)"})
  3826. assert response.status_code == 200
  3827. assert len(response.data["data"]) == 1
  3828. assert response.data["data"][0]["count_at_least(measurements.fcp, 0)"] == 1
  3829. # a value that's a little bigger than the stored fcp
  3830. fcp = int(self.transaction_data["measurements"]["fcp"]["value"] + 1)
  3831. response = self.do_request({"field": f"count_at_least(measurements.fcp, {fcp})"})
  3832. assert response.status_code == 200
  3833. assert len(response.data["data"]) == 1
  3834. assert response.data["data"][0][f"count_at_least(measurements.fcp, {fcp})"] == 0
  3835. def test_measurements_query(self):
  3836. self.store_event(self.transaction_data, self.project.id)
  3837. query = {
  3838. "field": [
  3839. "measurements.fp",
  3840. "measurements.fcp",
  3841. "measurements.lcp",
  3842. "measurements.fid",
  3843. ]
  3844. }
  3845. response = self.do_request(query)
  3846. assert response.status_code == 200, response.content
  3847. assert len(response.data["data"]) == 1
  3848. for field in query["field"]:
  3849. measure = field.split(".", 1)[1]
  3850. assert (
  3851. response.data["data"][0][field]
  3852. == self.transaction_data["measurements"][measure]["value"]
  3853. )
  3854. query = {
  3855. "field": [
  3856. "measurements.fP",
  3857. "measurements.Fcp",
  3858. "measurements.LcP",
  3859. "measurements.FID",
  3860. ]
  3861. }
  3862. response = self.do_request(query)
  3863. assert response.status_code == 200, response.content
  3864. assert len(response.data["data"]) == 1
  3865. for field in query["field"]:
  3866. measure = field.split(".", 1)[1].lower()
  3867. assert (
  3868. response.data["data"][0][field]
  3869. == self.transaction_data["measurements"][measure]["value"]
  3870. )
  3871. def test_measurements_aggregations(self):
  3872. self.store_event(self.transaction_data, self.project.id)
  3873. # should try all the potential aggregates
  3874. # Skipped tests for stddev and var since sampling one data point
  3875. # results in nan.
  3876. query = {
  3877. "field": [
  3878. "percentile(measurements.fcp, 0.5)",
  3879. "count_unique(measurements.fcp)",
  3880. "min(measurements.fcp)",
  3881. "max(measurements.fcp)",
  3882. "avg(measurements.fcp)",
  3883. "sum(measurements.fcp)",
  3884. ],
  3885. }
  3886. response = self.do_request(query)
  3887. assert response.status_code == 200, response.content
  3888. assert len(response.data["data"]) == 1
  3889. assert (
  3890. response.data["data"][0]["percentile(measurements.fcp, 0.5)"]
  3891. == self.transaction_data["measurements"]["fcp"]["value"]
  3892. )
  3893. assert response.data["data"][0]["count_unique(measurements.fcp)"] == 1
  3894. assert (
  3895. response.data["data"][0]["min(measurements.fcp)"]
  3896. == self.transaction_data["measurements"]["fcp"]["value"]
  3897. )
  3898. assert (
  3899. response.data["data"][0]["max(measurements.fcp)"]
  3900. == self.transaction_data["measurements"]["fcp"]["value"]
  3901. )
  3902. assert (
  3903. response.data["data"][0]["avg(measurements.fcp)"]
  3904. == self.transaction_data["measurements"]["fcp"]["value"]
  3905. )
  3906. assert (
  3907. response.data["data"][0]["sum(measurements.fcp)"]
  3908. == self.transaction_data["measurements"]["fcp"]["value"]
  3909. )
  3910. def get_measurement_condition_response(self, query_str, field):
  3911. query = {
  3912. "field": ["transaction", "count()"] + (field if field else []),
  3913. "query": query_str,
  3914. }
  3915. response = self.do_request(query)
  3916. assert response.status_code == 200, response.content
  3917. return response
  3918. def assert_measurement_condition_without_results(self, query_str, field=None):
  3919. response = self.get_measurement_condition_response(query_str, field)
  3920. assert len(response.data["data"]) == 0
  3921. def assert_measurement_condition_with_results(self, query_str, field=None):
  3922. response = self.get_measurement_condition_response(query_str, field)
  3923. assert len(response.data["data"]) == 1
  3924. assert response.data["data"][0]["transaction"] == self.transaction_data["metadata"]["title"]
  3925. assert response.data["data"][0]["count()"] == 1
  3926. def test_measurements_conditions(self):
  3927. self.store_event(self.transaction_data, self.project.id)
  3928. fcp = self.transaction_data["measurements"]["fcp"]["value"]
  3929. # equality condition
  3930. # We use json dumps here to ensure precision when converting from float to str
  3931. # This is necessary because equality on floating point values need to be precise
  3932. self.assert_measurement_condition_with_results(f"measurements.fcp:{json.dumps(fcp)}")
  3933. # greater than condition
  3934. self.assert_measurement_condition_with_results(f"measurements.fcp:>{fcp - 1}")
  3935. self.assert_measurement_condition_without_results(f"measurements.fcp:>{fcp + 1}")
  3936. # less than condition
  3937. self.assert_measurement_condition_with_results(f"measurements.fcp:<{fcp + 1}")
  3938. self.assert_measurement_condition_without_results(f"measurements.fcp:<{fcp - 1}")
  3939. # has condition
  3940. self.assert_measurement_condition_with_results("has:measurements.fcp")
  3941. self.assert_measurement_condition_without_results("!has:measurements.fcp")
  3942. def test_measurements_aggregation_conditions(self):
  3943. self.store_event(self.transaction_data, self.project.id)
  3944. fcp = self.transaction_data["measurements"]["fcp"]["value"]
  3945. functions = [
  3946. "percentile(measurements.fcp, 0.5)",
  3947. "min(measurements.fcp)",
  3948. "max(measurements.fcp)",
  3949. "avg(measurements.fcp)",
  3950. "sum(measurements.fcp)",
  3951. ]
  3952. for function in functions:
  3953. self.assert_measurement_condition_with_results(
  3954. f"{function}:>{fcp - 1}", field=[function]
  3955. )
  3956. self.assert_measurement_condition_without_results(
  3957. f"{function}:>{fcp + 1}", field=[function]
  3958. )
  3959. self.assert_measurement_condition_with_results(
  3960. f"{function}:<{fcp + 1}", field=[function]
  3961. )
  3962. self.assert_measurement_condition_without_results(
  3963. f"{function}:<{fcp - 1}", field=[function]
  3964. )
  3965. count_unique = "count_unique(measurements.fcp)"
  3966. self.assert_measurement_condition_with_results(f"{count_unique}:1", field=[count_unique])
  3967. self.assert_measurement_condition_without_results(f"{count_unique}:0", field=[count_unique])
  3968. def test_compare_numeric_aggregate(self):
  3969. self.store_event(self.transaction_data, self.project.id)
  3970. query = {
  3971. "field": [
  3972. "p75(measurements.fcp)",
  3973. "compare_numeric_aggregate(p75_measurements_fcp,greater,0)",
  3974. ],
  3975. }
  3976. response = self.do_request(query)
  3977. assert response.status_code == 200, response.content
  3978. assert len(response.data["data"]) == 1
  3979. assert (
  3980. response.data["data"][0]["compare_numeric_aggregate(p75_measurements_fcp,greater,0)"]
  3981. == 1
  3982. )
  3983. query = {
  3984. "field": ["p75()", "compare_numeric_aggregate(p75,equals,0)"],
  3985. }
  3986. response = self.do_request(query)
  3987. assert response.status_code == 200, response.content
  3988. assert len(response.data["data"]) == 1
  3989. assert response.data["data"][0]["compare_numeric_aggregate(p75,equals,0)"] == 0
  3990. def test_no_team_key_transactions(self):
  3991. transactions = [
  3992. "/blah_transaction/",
  3993. "/foo_transaction/",
  3994. "/zoo_transaction/",
  3995. ]
  3996. for transaction in transactions:
  3997. self.transaction_data["transaction"] = transaction
  3998. self.store_event(self.transaction_data, self.project.id)
  3999. query = {
  4000. "team": "myteams",
  4001. "project": [self.project.id],
  4002. # use the order by to ensure the result order
  4003. "orderby": "transaction",
  4004. "field": [
  4005. "team_key_transaction",
  4006. "transaction",
  4007. "transaction.status",
  4008. "project",
  4009. "epm()",
  4010. "failure_rate()",
  4011. "percentile(transaction.duration, 0.95)",
  4012. ],
  4013. }
  4014. response = self.do_request(query)
  4015. assert response.status_code == 200, response.content
  4016. data = response.data["data"]
  4017. assert len(data) == 3
  4018. assert data[0]["team_key_transaction"] == 0
  4019. assert data[0]["transaction"] == "/blah_transaction/"
  4020. assert data[1]["team_key_transaction"] == 0
  4021. assert data[1]["transaction"] == "/foo_transaction/"
  4022. assert data[2]["team_key_transaction"] == 0
  4023. assert data[2]["transaction"] == "/zoo_transaction/"
  4024. def test_team_key_transactions_my_teams(self):
  4025. team1 = self.create_team(organization=self.organization, name="Team A")
  4026. self.create_team_membership(team1, user=self.user)
  4027. self.project.add_team(team1)
  4028. team2 = self.create_team(organization=self.organization, name="Team B")
  4029. self.project.add_team(team2)
  4030. transactions = ["/blah_transaction/"]
  4031. key_transactions = [
  4032. (team1, "/foo_transaction/"),
  4033. (team2, "/zoo_transaction/"),
  4034. ]
  4035. for transaction in transactions:
  4036. self.transaction_data["transaction"] = transaction
  4037. self.store_event(self.transaction_data, self.project.id)
  4038. for team, transaction in key_transactions:
  4039. self.transaction_data["transaction"] = transaction
  4040. self.store_event(self.transaction_data, self.project.id)
  4041. TeamKeyTransaction.objects.create(
  4042. organization=self.organization,
  4043. transaction=transaction,
  4044. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  4045. )
  4046. query = {
  4047. "team": "myteams",
  4048. "project": [self.project.id],
  4049. "field": [
  4050. "team_key_transaction",
  4051. "transaction",
  4052. "transaction.status",
  4053. "project",
  4054. "epm()",
  4055. "failure_rate()",
  4056. "percentile(transaction.duration, 0.95)",
  4057. ],
  4058. }
  4059. query["orderby"] = ["team_key_transaction", "transaction"]
  4060. response = self.do_request(query)
  4061. assert response.status_code == 200, response.content
  4062. data = response.data["data"]
  4063. assert len(data) == 3
  4064. assert data[0]["team_key_transaction"] == 0
  4065. assert data[0]["transaction"] == "/blah_transaction/"
  4066. assert data[1]["team_key_transaction"] == 0
  4067. assert data[1]["transaction"] == "/zoo_transaction/"
  4068. assert data[2]["team_key_transaction"] == 1
  4069. assert data[2]["transaction"] == "/foo_transaction/"
  4070. # not specifying any teams should use my teams
  4071. query = {
  4072. "project": [self.project.id],
  4073. "field": [
  4074. "team_key_transaction",
  4075. "transaction",
  4076. "transaction.status",
  4077. "project",
  4078. "epm()",
  4079. "failure_rate()",
  4080. "percentile(transaction.duration, 0.95)",
  4081. ],
  4082. }
  4083. query["orderby"] = ["team_key_transaction", "transaction"]
  4084. response = self.do_request(query)
  4085. assert response.status_code == 200, response.content
  4086. data = response.data["data"]
  4087. assert len(data) == 3
  4088. assert data[0]["team_key_transaction"] == 0
  4089. assert data[0]["transaction"] == "/blah_transaction/"
  4090. assert data[1]["team_key_transaction"] == 0
  4091. assert data[1]["transaction"] == "/zoo_transaction/"
  4092. assert data[2]["team_key_transaction"] == 1
  4093. assert data[2]["transaction"] == "/foo_transaction/"
  4094. def test_team_key_transactions_orderby(self):
  4095. team1 = self.create_team(organization=self.organization, name="Team A")
  4096. team2 = self.create_team(organization=self.organization, name="Team B")
  4097. transactions = ["/blah_transaction/"]
  4098. key_transactions = [
  4099. (team1, "/foo_transaction/"),
  4100. (team2, "/zoo_transaction/"),
  4101. ]
  4102. for transaction in transactions:
  4103. self.transaction_data["transaction"] = transaction
  4104. self.store_event(self.transaction_data, self.project.id)
  4105. for team, transaction in key_transactions:
  4106. self.create_team_membership(team, user=self.user)
  4107. self.project.add_team(team)
  4108. self.transaction_data["transaction"] = transaction
  4109. self.store_event(self.transaction_data, self.project.id)
  4110. TeamKeyTransaction.objects.create(
  4111. organization=self.organization,
  4112. transaction=transaction,
  4113. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  4114. )
  4115. query = {
  4116. "team": "myteams",
  4117. "project": [self.project.id],
  4118. "field": [
  4119. "team_key_transaction",
  4120. "transaction",
  4121. "transaction.status",
  4122. "project",
  4123. "epm()",
  4124. "failure_rate()",
  4125. "percentile(transaction.duration, 0.95)",
  4126. ],
  4127. }
  4128. # test ascending order
  4129. query["orderby"] = ["team_key_transaction", "transaction"]
  4130. response = self.do_request(query)
  4131. assert response.status_code == 200, response.content
  4132. data = response.data["data"]
  4133. assert len(data) == 3
  4134. assert data[0]["team_key_transaction"] == 0
  4135. assert data[0]["transaction"] == "/blah_transaction/"
  4136. assert data[1]["team_key_transaction"] == 1
  4137. assert data[1]["transaction"] == "/foo_transaction/"
  4138. assert data[2]["team_key_transaction"] == 1
  4139. assert data[2]["transaction"] == "/zoo_transaction/"
  4140. # test descending order
  4141. query["orderby"] = ["-team_key_transaction", "-transaction"]
  4142. response = self.do_request(query)
  4143. assert response.status_code == 200, response.content
  4144. data = response.data["data"]
  4145. assert len(data) == 3
  4146. assert data[0]["team_key_transaction"] == 1
  4147. assert data[0]["transaction"] == "/zoo_transaction/"
  4148. assert data[1]["team_key_transaction"] == 1
  4149. assert data[1]["transaction"] == "/foo_transaction/"
  4150. assert data[2]["team_key_transaction"] == 0
  4151. assert data[2]["transaction"] == "/blah_transaction/"
  4152. def test_team_key_transactions_query(self):
  4153. team1 = self.create_team(organization=self.organization, name="Team A")
  4154. team2 = self.create_team(organization=self.organization, name="Team B")
  4155. transactions = ["/blah_transaction/"]
  4156. key_transactions = [
  4157. (team1, "/foo_transaction/"),
  4158. (team2, "/zoo_transaction/"),
  4159. ]
  4160. for transaction in transactions:
  4161. self.transaction_data["transaction"] = transaction
  4162. self.store_event(self.transaction_data, self.project.id)
  4163. for team, transaction in key_transactions:
  4164. self.create_team_membership(team, user=self.user)
  4165. self.project.add_team(team)
  4166. self.transaction_data["transaction"] = transaction
  4167. self.store_event(self.transaction_data, self.project.id)
  4168. TeamKeyTransaction.objects.create(
  4169. organization=self.organization,
  4170. project_team=ProjectTeam.objects.get(
  4171. project=self.project,
  4172. team=team,
  4173. ),
  4174. transaction=transaction,
  4175. )
  4176. query = {
  4177. "team": "myteams",
  4178. "project": [self.project.id],
  4179. # use the order by to ensure the result order
  4180. "orderby": "transaction",
  4181. "field": [
  4182. "team_key_transaction",
  4183. "transaction",
  4184. "transaction.status",
  4185. "project",
  4186. "epm()",
  4187. "failure_rate()",
  4188. "percentile(transaction.duration, 0.95)",
  4189. ],
  4190. }
  4191. # key transactions
  4192. query["query"] = "has:team_key_transaction"
  4193. response = self.do_request(query)
  4194. assert response.status_code == 200, response.content
  4195. data = response.data["data"]
  4196. assert len(data) == 2
  4197. assert data[0]["team_key_transaction"] == 1
  4198. assert data[0]["transaction"] == "/foo_transaction/"
  4199. assert data[1]["team_key_transaction"] == 1
  4200. assert data[1]["transaction"] == "/zoo_transaction/"
  4201. # key transactions
  4202. query["query"] = "team_key_transaction:true"
  4203. response = self.do_request(query)
  4204. assert response.status_code == 200, response.content
  4205. data = response.data["data"]
  4206. assert len(data) == 2
  4207. assert data[0]["team_key_transaction"] == 1
  4208. assert data[0]["transaction"] == "/foo_transaction/"
  4209. assert data[1]["team_key_transaction"] == 1
  4210. assert data[1]["transaction"] == "/zoo_transaction/"
  4211. # not key transactions
  4212. query["query"] = "!has:team_key_transaction"
  4213. response = self.do_request(query)
  4214. assert response.status_code == 200, response.content
  4215. data = response.data["data"]
  4216. assert len(data) == 1
  4217. assert data[0]["team_key_transaction"] == 0
  4218. assert data[0]["transaction"] == "/blah_transaction/"
  4219. # not key transactions
  4220. query["query"] = "team_key_transaction:false"
  4221. response = self.do_request(query)
  4222. assert response.status_code == 200, response.content
  4223. data = response.data["data"]
  4224. assert len(data) == 1
  4225. assert data[0]["team_key_transaction"] == 0
  4226. assert data[0]["transaction"] == "/blah_transaction/"
  4227. def test_too_many_team_key_transactions(self):
  4228. MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS = 1
  4229. with mock.patch(
  4230. "sentry.search.events.fields.MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS",
  4231. MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS,
  4232. ):
  4233. team = self.create_team(organization=self.organization, name="Team A")
  4234. self.create_team_membership(team, user=self.user)
  4235. self.project.add_team(team)
  4236. project_team = ProjectTeam.objects.get(project=self.project, team=team)
  4237. for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1):
  4238. transaction = f"transaction-{team.id}-{i}"
  4239. self.transaction_data["transaction"] = transaction
  4240. self.store_event(self.transaction_data, self.project.id)
  4241. TeamKeyTransaction.objects.bulk_create(
  4242. [
  4243. TeamKeyTransaction(
  4244. organization=self.organization,
  4245. project_team=project_team,
  4246. transaction=f"transaction-{team.id}-{i}",
  4247. )
  4248. for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1)
  4249. ]
  4250. )
  4251. query = {
  4252. "team": "myteams",
  4253. "project": [self.project.id],
  4254. "orderby": "transaction",
  4255. "field": [
  4256. "team_key_transaction",
  4257. "transaction",
  4258. "transaction.status",
  4259. "project",
  4260. "epm()",
  4261. "failure_rate()",
  4262. "percentile(transaction.duration, 0.95)",
  4263. ],
  4264. }
  4265. response = self.do_request(query)
  4266. assert response.status_code == 200, response.content
  4267. data = response.data["data"]
  4268. assert len(data) == 2
  4269. assert (
  4270. sum(row["team_key_transaction"] for row in data)
  4271. == MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS
  4272. )
  4273. def test_no_pagination_param(self):
  4274. self.store_event(
  4275. data={
  4276. "event_id": "a" * 32,
  4277. "timestamp": self.ten_mins_ago_iso,
  4278. "fingerprint": ["group1"],
  4279. },
  4280. project_id=self.project.id,
  4281. )
  4282. query = {"field": ["id", "project.id"], "project": [self.project.id], "noPagination": True}
  4283. response = self.do_request(query)
  4284. assert response.status_code == 200
  4285. assert len(response.data["data"]) == 1
  4286. assert "Link" not in response
  4287. def test_nan_result(self):
  4288. query = {"field": ["apdex(300)"], "project": [self.project.id], "query": f"id:{'0' * 32}"}
  4289. response = self.do_request(query)
  4290. assert response.status_code == 200
  4291. assert len(response.data["data"]) == 1
  4292. assert response.data["data"][0]["apdex(300)"] == 0
  4293. def test_equation_simple(self):
  4294. event_data = self.load_data(
  4295. timestamp=self.ten_mins_ago,
  4296. )
  4297. event_data["breakdowns"]["span_ops"]["ops.http"]["value"] = 1500
  4298. self.store_event(data=event_data, project_id=self.project.id)
  4299. query = {
  4300. "field": ["spans.http", "equation|spans.http / 3"],
  4301. "project": [self.project.id],
  4302. "query": "event.type:transaction",
  4303. }
  4304. response = self.do_request(
  4305. query,
  4306. {
  4307. "organizations:discover-basic": True,
  4308. },
  4309. )
  4310. assert response.status_code == 200, response.content
  4311. assert len(response.data["data"]) == 1
  4312. assert (
  4313. response.data["data"][0]["equation|spans.http / 3"]
  4314. == event_data["breakdowns"]["span_ops"]["ops.http"]["value"] / 3
  4315. )
  4316. assert response.data["meta"]["fields"]["equation|spans.http / 3"] == "number"
  4317. def test_equation_sort(self):
  4318. event_data = self.transaction_data.copy()
  4319. event_data["breakdowns"] = {"span_ops": {"ops.http": {"value": 1500}}}
  4320. self.store_event(data=event_data, project_id=self.project.id)
  4321. event_data2 = self.transaction_data.copy()
  4322. event_data2["breakdowns"] = {"span_ops": {"ops.http": {"value": 2000}}}
  4323. self.store_event(data=event_data2, project_id=self.project.id)
  4324. query = {
  4325. "field": ["spans.http", "equation|spans.http / 3"],
  4326. "project": [self.project.id],
  4327. "orderby": "equation|spans.http / 3",
  4328. "query": "event.type:transaction",
  4329. }
  4330. response = self.do_request(
  4331. query,
  4332. {
  4333. "organizations:discover-basic": True,
  4334. },
  4335. )
  4336. assert response.status_code == 200, response.content
  4337. assert len(response.data["data"]) == 2
  4338. assert (
  4339. response.data["data"][0]["equation|spans.http / 3"]
  4340. == event_data["breakdowns"]["span_ops"]["ops.http"]["value"] / 3
  4341. )
  4342. assert (
  4343. response.data["data"][1]["equation|spans.http / 3"]
  4344. == event_data2["breakdowns"]["span_ops"]["ops.http"]["value"] / 3
  4345. )
  4346. def test_equation_operation_limit(self):
  4347. query = {
  4348. "field": ["spans.http", f"equation|spans.http{' * 2' * 11}"],
  4349. "project": [self.project.id],
  4350. "query": "event.type:transaction",
  4351. }
  4352. response = self.do_request(
  4353. query,
  4354. {
  4355. "organizations:discover-basic": True,
  4356. },
  4357. )
  4358. assert response.status_code == 400
  4359. @mock.patch("sentry.api.bases.organization_events.MAX_FIELDS", 2)
  4360. def test_equation_field_limit(self):
  4361. query = {
  4362. "field": ["spans.http", "transaction.duration", "equation|5 * 2"],
  4363. "project": [self.project.id],
  4364. "query": "event.type:transaction",
  4365. }
  4366. response = self.do_request(
  4367. query,
  4368. {
  4369. "organizations:discover-basic": True,
  4370. },
  4371. )
  4372. assert response.status_code == 400
  4373. def test_count_if(self):
  4374. unicode_phrase1 = "\u716e\u6211\u66f4\u591a\u7684\u98df\u7269\uff0c\u6211\u9913\u4e86"
  4375. for i in range(5):
  4376. data = self.load_data(
  4377. timestamp=before_now(minutes=(10 + i)),
  4378. duration=timedelta(milliseconds=100 if i < 3 else 200),
  4379. )
  4380. data["tags"] = {
  4381. "sub_customer.is-Enterprise-42": "yes" if i == 0 else "no",
  4382. "unicode-phrase": unicode_phrase1 if i == 0 else "no",
  4383. }
  4384. self.store_event(data, project_id=self.project.id)
  4385. query = {
  4386. "field": [
  4387. "count_if(transaction.duration, less, 150)",
  4388. "count_if(transaction.duration, greater, 150)",
  4389. "count_if(sub_customer.is-Enterprise-42, equals, yes)",
  4390. "count_if(sub_customer.is-Enterprise-42, notEquals, yes)",
  4391. f"count_if(unicode-phrase, equals, {unicode_phrase1})",
  4392. ],
  4393. "project": [self.project.id],
  4394. }
  4395. response = self.do_request(query)
  4396. assert response.status_code == 200
  4397. assert len(response.data["data"]) == 1
  4398. assert response.data["data"][0]["count_if(transaction.duration, less, 150)"] == 3
  4399. assert response.data["data"][0]["count_if(transaction.duration, greater, 150)"] == 2
  4400. assert response.data["data"][0]["count_if(sub_customer.is-Enterprise-42, equals, yes)"] == 1
  4401. assert (
  4402. response.data["data"][0]["count_if(sub_customer.is-Enterprise-42, notEquals, yes)"] == 4
  4403. )
  4404. assert response.data["data"][0][f"count_if(unicode-phrase, equals, {unicode_phrase1})"] == 1
  4405. def test_count_if_array_field(self):
  4406. data = self.load_data(platform="javascript")
  4407. data["timestamp"] = self.ten_mins_ago_iso
  4408. self.store_event(data=data, project_id=self.project.id)
  4409. query = {
  4410. "field": [
  4411. "count_if(stack.filename, equals, raven.js)",
  4412. ],
  4413. "project": [self.project.id],
  4414. }
  4415. response = self.do_request(query)
  4416. assert response.status_code == 200, response.content
  4417. assert len(response.data["data"]) == 1
  4418. assert response.data["data"][0]["count_if(stack.filename, equals, raven.js)"] == 1
  4419. def test_count_if_measurements_cls(self):
  4420. data = self.transaction_data.copy()
  4421. data["measurements"] = {"cls": {"value": 0.5}}
  4422. self.store_event(data, project_id=self.project.id)
  4423. data["measurements"] = {"cls": {"value": 0.1}}
  4424. self.store_event(data, project_id=self.project.id)
  4425. query = {
  4426. "field": [
  4427. "count_if(measurements.cls, greater, 0.05)",
  4428. "count_if(measurements.cls, less, 0.3)",
  4429. ],
  4430. "project": [self.project.id],
  4431. }
  4432. response = self.do_request(query)
  4433. assert response.status_code == 200
  4434. assert len(response.data["data"]) == 1
  4435. assert response.data["data"][0]["count_if(measurements.cls, greater, 0.05)"] == 2
  4436. assert response.data["data"][0]["count_if(measurements.cls, less, 0.3)"] == 1
  4437. def test_count_if_filter(self):
  4438. for i in range(5):
  4439. data = self.load_data(
  4440. timestamp=before_now(minutes=(10 + i)),
  4441. duration=timedelta(milliseconds=100 if i < 3 else 200),
  4442. )
  4443. data["tags"] = {"sub_customer.is-Enterprise-42": "yes" if i == 0 else "no"}
  4444. self.store_event(data, project_id=self.project.id)
  4445. query = {
  4446. "field": [
  4447. "count_if(transaction.duration, less, 150)",
  4448. ],
  4449. "query": "count_if(transaction.duration, less, 150):>2",
  4450. "project": [self.project.id],
  4451. }
  4452. response = self.do_request(query)
  4453. assert response.status_code == 200
  4454. assert len(response.data["data"]) == 1
  4455. assert response.data["data"][0]["count_if(transaction.duration, less, 150)"] == 3
  4456. query = {
  4457. "field": [
  4458. "count_if(transaction.duration, less, 150)",
  4459. ],
  4460. "query": "count_if(transaction.duration, less, 150):<2",
  4461. "project": [self.project.id],
  4462. }
  4463. response = self.do_request(query)
  4464. assert response.status_code == 200
  4465. assert len(response.data["data"]) == 0
  4466. def test_filters_with_escaped_asterisk(self):
  4467. self.transaction_data["transaction"] = r"/:a*/:b-:c(\d\.\e+)"
  4468. self.store_event(self.transaction_data, project_id=self.project.id)
  4469. query = {
  4470. "field": ["transaction", "transaction.duration"],
  4471. # make sure to escape the asterisk so it's not treated as a wildcard
  4472. "query": r'transaction:"/:a\*/:b-:c(\d\.\e+)"',
  4473. "project": [self.project.id],
  4474. }
  4475. response = self.do_request(query)
  4476. assert response.status_code == 200
  4477. assert len(response.data["data"]) == 1
  4478. def test_filters_with_back_slashes(self):
  4479. self.transaction_data["transaction"] = r"a\b\c@d"
  4480. self.store_event(self.transaction_data, project_id=self.project.id)
  4481. query = {
  4482. "field": ["transaction", "transaction.duration"],
  4483. "query": r'transaction:"a\b\c@d"',
  4484. "project": [self.project.id],
  4485. }
  4486. response = self.do_request(query)
  4487. assert response.status_code == 200
  4488. assert len(response.data["data"]) == 1
  4489. def test_mobile_measurements(self):
  4490. self.transaction_data["measurements"]["frames_total"] = {"value": 100}
  4491. self.transaction_data["measurements"]["frames_slow"] = {"value": 10}
  4492. self.transaction_data["measurements"]["frames_frozen"] = {"value": 5}
  4493. self.transaction_data["measurements"]["stall_count"] = {"value": 2}
  4494. self.transaction_data["measurements"]["stall_total_time"] = {"value": 12}
  4495. self.transaction_data["measurements"]["stall_longest_time"] = {"value": 7}
  4496. self.store_event(self.transaction_data, project_id=self.project.id)
  4497. query = {
  4498. "field": [
  4499. "measurements.frames_total",
  4500. "measurements.frames_slow",
  4501. "measurements.frames_frozen",
  4502. "measurements.frames_slow_rate",
  4503. "measurements.frames_frozen_rate",
  4504. "measurements.stall_count",
  4505. "measurements.stall_total_time",
  4506. "measurements.stall_longest_time",
  4507. "measurements.stall_percentage",
  4508. ],
  4509. "query": "",
  4510. "project": [self.project.id],
  4511. }
  4512. response = self.do_request(query)
  4513. assert response.status_code == 200
  4514. data = response.data["data"]
  4515. assert len(data) == 1
  4516. assert data[0]["measurements.frames_total"] == 100
  4517. assert data[0]["measurements.frames_slow"] == 10
  4518. assert data[0]["measurements.frames_frozen"] == 5
  4519. assert data[0]["measurements.frames_slow_rate"] == 0.1
  4520. assert data[0]["measurements.frames_frozen_rate"] == 0.05
  4521. assert data[0]["measurements.stall_count"] == 2
  4522. assert data[0]["measurements.stall_total_time"] == 12
  4523. assert data[0]["measurements.stall_longest_time"] == 7
  4524. assert data[0]["measurements.stall_percentage"] == 0.004
  4525. meta = response.data["meta"]["fields"]
  4526. assert meta["measurements.frames_total"] == "number"
  4527. assert meta["measurements.frames_slow"] == "number"
  4528. assert meta["measurements.frames_frozen"] == "number"
  4529. assert meta["measurements.frames_slow_rate"] == "percentage"
  4530. assert meta["measurements.frames_frozen_rate"] == "percentage"
  4531. assert meta["measurements.stall_count"] == "number"
  4532. assert meta["measurements.stall_total_time"] == "number"
  4533. assert meta["measurements.stall_longest_time"] == "number"
  4534. assert meta["measurements.stall_percentage"] == "percentage"
  4535. query = {
  4536. "field": [
  4537. "p75(measurements.frames_slow_rate)",
  4538. "p75(measurements.frames_frozen_rate)",
  4539. "percentile(measurements.frames_slow_rate,0.5)",
  4540. "percentile(measurements.frames_frozen_rate,0.5)",
  4541. "p75(measurements.stall_percentage)",
  4542. "percentile(measurements.stall_percentage,0.5)",
  4543. ],
  4544. "query": "",
  4545. "project": [self.project.id],
  4546. }
  4547. response = self.do_request(query)
  4548. assert response.status_code == 200
  4549. data = response.data["data"]
  4550. assert len(data) == 1
  4551. assert data[0]["p75(measurements.frames_slow_rate)"] == 0.1
  4552. assert data[0]["p75(measurements.frames_frozen_rate)"] == 0.05
  4553. assert data[0]["p75(measurements.stall_percentage)"] == 0.004
  4554. assert data[0]["percentile(measurements.frames_slow_rate,0.5)"] == 0.1
  4555. assert data[0]["percentile(measurements.frames_frozen_rate,0.5)"] == 0.05
  4556. assert data[0]["percentile(measurements.stall_percentage,0.5)"] == 0.004
  4557. meta = response.data["meta"]["fields"]
  4558. assert meta["p75(measurements.frames_slow_rate)"] == "percentage"
  4559. assert meta["p75(measurements.frames_frozen_rate)"] == "percentage"
  4560. assert meta["p75(measurements.stall_percentage)"] == "percentage"
  4561. assert meta["percentile(measurements.frames_slow_rate,0.5)"] == "percentage"
  4562. assert meta["percentile(measurements.stall_percentage,0.5)"] == "percentage"
  4563. def test_project_auto_fields(self):
  4564. self.store_event(
  4565. data={
  4566. "event_id": "a" * 32,
  4567. "environment": "staging",
  4568. "timestamp": self.ten_mins_ago_iso,
  4569. },
  4570. project_id=self.project.id,
  4571. )
  4572. query = {"field": ["environment"]}
  4573. response = self.do_request(query)
  4574. assert response.status_code == 200, response.content
  4575. assert len(response.data["data"]) == 1
  4576. assert response.data["data"][0]["environment"] == "staging"
  4577. assert response.data["data"][0]["project.name"] == self.project.slug
  4578. def test_timestamp_different_from_params(self):
  4579. fifteen_days_ago = iso_format(before_now(days=15))
  4580. fifteen_days_later = iso_format(before_now(days=-15))
  4581. for query_text in [
  4582. f"timestamp:<{fifteen_days_ago}",
  4583. f"timestamp:<={fifteen_days_ago}",
  4584. f"timestamp:>{fifteen_days_later}",
  4585. f"timestamp:>={fifteen_days_later}",
  4586. ]:
  4587. query = {
  4588. "field": ["count()"],
  4589. "query": query_text,
  4590. "statsPeriod": "14d",
  4591. "project": self.project.id,
  4592. }
  4593. response = self.do_request(query)
  4594. assert response.status_code == 400, query_text
  4595. @mock.patch("sentry.search.events.builder.discover.raw_snql_query")
  4596. def test_removes_unnecessary_default_project_and_transaction_thresholds(self, mock_snql_query):
  4597. mock_snql_query.side_effect = [{"meta": {}, "data": []}]
  4598. ProjectTransactionThreshold.objects.create(
  4599. project=self.project,
  4600. organization=self.organization,
  4601. # these are the default values that we use
  4602. threshold=constants.DEFAULT_PROJECT_THRESHOLD,
  4603. metric=TransactionMetric.DURATION.value,
  4604. )
  4605. ProjectTransactionThresholdOverride.objects.create(
  4606. transaction="transaction",
  4607. project=self.project,
  4608. organization=self.organization,
  4609. # these are the default values that we use
  4610. threshold=constants.DEFAULT_PROJECT_THRESHOLD,
  4611. metric=TransactionMetric.DURATION.value,
  4612. )
  4613. query = {
  4614. "field": ["apdex()", "user_misery()"],
  4615. "query": "event.type:transaction",
  4616. "project": [self.project.id],
  4617. }
  4618. response = self.do_request(
  4619. query,
  4620. features={
  4621. "organizations:discover-basic": True,
  4622. "organizations:global-views": True,
  4623. },
  4624. )
  4625. assert response.status_code == 200, response.content
  4626. assert mock_snql_query.call_count == 1
  4627. assert (
  4628. Function("tuple", ["duration", 300], "project_threshold_config")
  4629. in mock_snql_query.call_args_list[0][0][0].query.select
  4630. )
  4631. @mock.patch("sentry.search.events.builder.discover.raw_snql_query")
  4632. def test_removes_unnecessary_default_project_and_transaction_thresholds_keeps_others(
  4633. self, mock_snql_query
  4634. ):
  4635. mock_snql_query.side_effect = [{"meta": {}, "data": []}]
  4636. ProjectTransactionThreshold.objects.create(
  4637. project=self.project,
  4638. organization=self.organization,
  4639. # these are the default values that we use
  4640. threshold=constants.DEFAULT_PROJECT_THRESHOLD,
  4641. metric=TransactionMetric.DURATION.value,
  4642. )
  4643. ProjectTransactionThresholdOverride.objects.create(
  4644. transaction="transaction",
  4645. project=self.project,
  4646. organization=self.organization,
  4647. # these are the default values that we use
  4648. threshold=constants.DEFAULT_PROJECT_THRESHOLD,
  4649. metric=TransactionMetric.DURATION.value,
  4650. )
  4651. project = self.create_project()
  4652. ProjectTransactionThreshold.objects.create(
  4653. project=project,
  4654. organization=self.organization,
  4655. threshold=100,
  4656. metric=TransactionMetric.LCP.value,
  4657. )
  4658. ProjectTransactionThresholdOverride.objects.create(
  4659. transaction="transaction",
  4660. project=project,
  4661. organization=self.organization,
  4662. threshold=200,
  4663. metric=TransactionMetric.LCP.value,
  4664. )
  4665. query = {
  4666. "field": ["apdex()", "user_misery()"],
  4667. "query": "event.type:transaction",
  4668. "project": [self.project.id, project.id],
  4669. }
  4670. response = self.do_request(
  4671. query,
  4672. features={
  4673. "organizations:discover-basic": True,
  4674. "organizations:global-views": True,
  4675. },
  4676. )
  4677. assert response.status_code == 200, response.content
  4678. assert mock_snql_query.call_count == 1
  4679. project_threshold_override_config_index = Function(
  4680. "indexOf",
  4681. [
  4682. # only 1 transaction override is present here
  4683. # because the other use to the default values
  4684. [(Function("toUInt64", [project.id]), "transaction")],
  4685. (Column("project_id"), Column("transaction")),
  4686. ],
  4687. "project_threshold_override_config_index",
  4688. )
  4689. project_threshold_config_index = Function(
  4690. "indexOf",
  4691. [
  4692. # only 1 project override is present here
  4693. # because the other use to the default values
  4694. [Function("toUInt64", [project.id])],
  4695. Column("project_id"),
  4696. ],
  4697. "project_threshold_config_index",
  4698. )
  4699. assert (
  4700. Function(
  4701. "if",
  4702. [
  4703. Function("equals", [project_threshold_override_config_index, 0]),
  4704. Function(
  4705. "if",
  4706. [
  4707. Function("equals", [project_threshold_config_index, 0]),
  4708. ("duration", 300),
  4709. Function(
  4710. "arrayElement", [[("lcp", 100)], project_threshold_config_index]
  4711. ),
  4712. ],
  4713. ),
  4714. Function(
  4715. "arrayElement",
  4716. [[("lcp", 200)], project_threshold_override_config_index],
  4717. ),
  4718. ],
  4719. "project_threshold_config",
  4720. )
  4721. in mock_snql_query.call_args_list[0][0][0].query.select
  4722. )
  4723. def test_count_web_vitals(self):
  4724. # Good
  4725. self.transaction_data["measurements"] = {
  4726. "lcp": {"value": constants.VITAL_THRESHOLDS["lcp"]["meh"] - 100},
  4727. }
  4728. self.store_event(self.transaction_data, self.project.id)
  4729. # Meh
  4730. self.transaction_data["measurements"] = {
  4731. "lcp": {"value": constants.VITAL_THRESHOLDS["lcp"]["meh"] + 100},
  4732. }
  4733. self.store_event(self.transaction_data, self.project.id)
  4734. self.store_event(self.transaction_data, self.project.id)
  4735. query = {
  4736. "field": [
  4737. "count_web_vitals(measurements.lcp, poor)",
  4738. "count_web_vitals(measurements.lcp, meh)",
  4739. "count_web_vitals(measurements.lcp, good)",
  4740. ]
  4741. }
  4742. response = self.do_request(query)
  4743. assert response.status_code == 200, response.content
  4744. assert len(response.data["data"]) == 1
  4745. assert response.data["data"][0] == {
  4746. "count_web_vitals(measurements.lcp, poor)": 0,
  4747. "count_web_vitals(measurements.lcp, meh)": 2,
  4748. "count_web_vitals(measurements.lcp, good)": 1,
  4749. }
  4750. def test_count_web_vitals_invalid_vital(self):
  4751. query = {
  4752. "field": [
  4753. "count_web_vitals(measurements.foo, poor)",
  4754. ],
  4755. "project": [self.project.id],
  4756. }
  4757. response = self.do_request(query)
  4758. assert response.status_code == 400, response.content
  4759. query = {
  4760. "field": [
  4761. "count_web_vitals(tags[lcp], poor)",
  4762. ],
  4763. "project": [self.project.id],
  4764. }
  4765. response = self.do_request(query)
  4766. assert response.status_code == 400, response.content
  4767. query = {
  4768. "field": [
  4769. "count_web_vitals(transaction.duration, poor)",
  4770. ],
  4771. "project": [self.project.id],
  4772. }
  4773. response = self.do_request(query)
  4774. assert response.status_code == 400, response.content
  4775. query = {
  4776. "field": [
  4777. "count_web_vitals(measurements.lcp, bad)",
  4778. ],
  4779. "project": [self.project.id],
  4780. }
  4781. response = self.do_request(query)
  4782. assert response.status_code == 400, response.content
  4783. def test_tag_that_looks_like_aggregate(self):
  4784. data = self.load_data()
  4785. data["tags"] = {"p95": "<5k"}
  4786. self.store_event(data, project_id=self.project.id)
  4787. query = {
  4788. "field": ["p95"],
  4789. "query": "p95:<5k",
  4790. "project": [self.project.id],
  4791. }
  4792. response = self.do_request(query)
  4793. assert response.status_code == 200, response.content
  4794. data = response.data["data"]
  4795. assert len(data) == 1
  4796. assert data[0]["p95"] == "<5k"
  4797. def test_chained_or_query_meta_tip(self):
  4798. query = {
  4799. "field": ["transaction"],
  4800. "query": "transaction:a OR transaction:b",
  4801. "project": [self.project.id],
  4802. }
  4803. response = self.do_request(query)
  4804. assert response.status_code == 200, response.content
  4805. meta = response.data["meta"]
  4806. assert meta["tips"] == {
  4807. "query": "Did you know you can replace chained or conditions like `field:a OR field:b OR field:c` with `field:[a,b,c]`",
  4808. "columns": None,
  4809. }
  4810. @override_settings(SENTRY_SELF_HOSTED=False)
  4811. def test_ratelimit(self):
  4812. query = {
  4813. "field": ["transaction"],
  4814. "project": [self.project.id],
  4815. }
  4816. with freeze_time("2000-01-01"):
  4817. for _ in range(15):
  4818. self.do_request(query, features={"organizations:discover-events-rate-limit": True})
  4819. response = self.do_request(
  4820. query, features={"organizations:discover-events-rate-limit": True}
  4821. )
  4822. assert response.status_code == 429, response.content
  4823. @override_settings(SENTRY_SELF_HOSTED=False)
  4824. def test_no_ratelimit(self):
  4825. query = {
  4826. "field": ["transaction"],
  4827. "project": [self.project.id],
  4828. }
  4829. with freeze_time("2000-01-01"):
  4830. for _ in range(15):
  4831. self.do_request(query)
  4832. response = self.do_request(query)
  4833. assert response.status_code == 200, response.content
  4834. def test_transaction_source(self):
  4835. query = {
  4836. "field": ["transaction"],
  4837. "query": "transaction.source:task",
  4838. "project": [self.project.id],
  4839. }
  4840. response = self.do_request(query)
  4841. assert response.status_code == 200, response.content
  4842. def test_readable_device_name(self):
  4843. data = self.load_data()
  4844. data["tags"] = {"device": "iPhone14,3"}
  4845. self.store_event(data, project_id=self.project.id)
  4846. query = {
  4847. "field": ["device"],
  4848. "query": "",
  4849. "project": [self.project.id],
  4850. "readable": True,
  4851. }
  4852. response = self.do_request(query)
  4853. assert response.status_code == 200, response.content
  4854. data = response.data["data"]
  4855. assert len(data) == 1
  4856. assert data[0]["device"] == "iPhone14,3"
  4857. assert data[0]["readable"] == "iPhone 13 Pro Max"
  4858. def test_http_status_code(self):
  4859. project1 = self.create_project()
  4860. project2 = self.create_project()
  4861. self.store_event(
  4862. data={
  4863. "event_id": "a" * 32,
  4864. "transaction": "/example",
  4865. "message": "how to make fast",
  4866. "timestamp": self.ten_mins_ago_iso,
  4867. "tags": {"http.status_code": "200"},
  4868. },
  4869. project_id=project1.id,
  4870. )
  4871. self.store_event(
  4872. data={
  4873. "event_id": "b" * 32,
  4874. "transaction": "/example",
  4875. "message": "how to make fast",
  4876. "timestamp": self.ten_mins_ago_iso,
  4877. "contexts": {"response": {"status_code": 400}},
  4878. },
  4879. project_id=project2.id,
  4880. )
  4881. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  4882. query = {
  4883. "field": ["event.type", "http.status_code"],
  4884. "query": "",
  4885. "statsPeriod": "24h",
  4886. }
  4887. response = self.do_request(query, features=features)
  4888. assert response.status_code == 200, response.content
  4889. data = response.data["data"]
  4890. assert len(data) == 2
  4891. result = {r["http.status_code"] for r in data}
  4892. assert result == {"200", "400"}
  4893. def test_http_status_code_context_priority(self):
  4894. project1 = self.create_project()
  4895. self.store_event(
  4896. data={
  4897. "event_id": "a" * 32,
  4898. "transaction": "/example",
  4899. "message": "how to make fast",
  4900. "timestamp": self.ten_mins_ago_iso,
  4901. "tags": {"http.status_code": "200"},
  4902. "contexts": {"response": {"status_code": 400}},
  4903. },
  4904. project_id=project1.id,
  4905. )
  4906. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  4907. query = {
  4908. "field": ["event.type", "http.status_code"],
  4909. "query": "",
  4910. "statsPeriod": "24h",
  4911. }
  4912. response = self.do_request(query, features=features)
  4913. assert response.status_code == 200, response.content
  4914. data = response.data["data"]
  4915. assert len(data) == 1
  4916. assert data[0]["http.status_code"] == "400"
  4917. def test_total_count(self):
  4918. project1 = self.create_project()
  4919. for i in range(3):
  4920. self.store_event(data=self.load_data(platform="javascript"), project_id=project1.id)
  4921. self.store_event(
  4922. data={
  4923. "event_id": "a" * 32,
  4924. "transaction": "/example",
  4925. "message": "how to make fast",
  4926. "timestamp": self.ten_mins_ago_iso,
  4927. },
  4928. project_id=project1.id,
  4929. )
  4930. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  4931. query = {
  4932. "field": ["transaction", "total.count", "count()"],
  4933. "query": "!transaction:/example",
  4934. "statsPeriod": "24h",
  4935. }
  4936. response = self.do_request(query, features=features)
  4937. assert response.status_code == 200, response.content
  4938. data = response.data["data"]
  4939. assert len(data) == 1
  4940. assert data[0]["total.count"] == 3
  4941. def test_total_count_by_itself(self):
  4942. project1 = self.create_project()
  4943. for i in range(3):
  4944. self.store_event(data=self.load_data(platform="javascript"), project_id=project1.id)
  4945. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  4946. query = {
  4947. "field": ["total.count"],
  4948. "statsPeriod": "24h",
  4949. }
  4950. response = self.do_request(query, features=features)
  4951. assert response.status_code == 400, response.content
  4952. def test_total_count_equation(self):
  4953. project1 = self.create_project()
  4954. for i in range(3):
  4955. self.store_event(data=self.load_data(platform="javascript"), project_id=project1.id)
  4956. self.store_event(
  4957. data={
  4958. "event_id": "a" * 32,
  4959. "transaction": "/example",
  4960. "message": "how to make fast",
  4961. "timestamp": self.ten_mins_ago_iso,
  4962. },
  4963. project_id=project1.id,
  4964. )
  4965. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  4966. query = {
  4967. "field": ["transaction", "count()", "total.count", "equation|count()/total.count"],
  4968. "query": "",
  4969. "orderby": "equation|count()/total.count",
  4970. "statsPeriod": "24h",
  4971. }
  4972. response = self.do_request(query, features=features)
  4973. assert response.status_code == 200, response.content
  4974. data = response.data["data"]
  4975. assert len(data) == 2
  4976. assert data[0]["equation|count()/total.count"] == 0.25
  4977. assert data[1]["equation|count()/total.count"] == 0.75
  4978. def test_total_count_filter(self):
  4979. project1 = self.create_project()
  4980. for i in range(3):
  4981. self.store_event(data=self.load_data(platform="javascript"), project_id=project1.id)
  4982. self.store_event(
  4983. data={
  4984. "event_id": "a" * 32,
  4985. "transaction": "/example",
  4986. "message": "how to make fast",
  4987. "timestamp": self.ten_mins_ago_iso,
  4988. "tags": {"total.count": ">45"},
  4989. },
  4990. project_id=project1.id,
  4991. )
  4992. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  4993. query = {
  4994. "field": ["transaction", "count()", "total.count"],
  4995. "query": "total.count:>45",
  4996. "orderby": "count()",
  4997. "statsPeriod": "24h",
  4998. }
  4999. response = self.do_request(query, features=features)
  5000. assert response.status_code == 200, response.content
  5001. data = response.data["data"]
  5002. assert len(data) == 1
  5003. assert data[0]["total.count"] == 1
  5004. def test_total_sum_transaction_duration_equation(self):
  5005. for i in range(3):
  5006. data = self.load_data(
  5007. timestamp=self.eleven_mins_ago,
  5008. duration=timedelta(seconds=5),
  5009. )
  5010. data["transaction"] = "/endpoint/1"
  5011. self.store_event(data, project_id=self.project.id)
  5012. data = self.load_data(
  5013. timestamp=self.ten_mins_ago,
  5014. duration=timedelta(seconds=5),
  5015. )
  5016. data["transaction"] = "/endpoint/2"
  5017. self.store_event(data, project_id=self.project.id)
  5018. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  5019. query = {
  5020. "field": [
  5021. "transaction",
  5022. "sum(transaction.duration)",
  5023. "total.transaction_duration",
  5024. "equation|sum(transaction.duration)/total.transaction_duration",
  5025. ],
  5026. "query": "",
  5027. "orderby": "-equation|sum(transaction.duration)/total.transaction_duration",
  5028. "statsPeriod": "24h",
  5029. }
  5030. response = self.do_request(query, features=features)
  5031. assert response.status_code == 200, response.content
  5032. data = response.data["data"]
  5033. assert len(data) == 2
  5034. assert data[0]["equation|sum(transaction.duration)/total.transaction_duration"] == 0.75
  5035. assert data[1]["equation|sum(transaction.duration)/total.transaction_duration"] == 0.25
  5036. def test_device_class(self):
  5037. project1 = self.create_project()
  5038. for i in range(3):
  5039. self.store_event(
  5040. data={
  5041. "event_id": "a" * 32,
  5042. "transaction": "/example",
  5043. "message": "how to make fast",
  5044. "timestamp": self.ten_mins_ago_iso,
  5045. "tags": {"device.class": f"{i + 1}"},
  5046. },
  5047. project_id=project1.id,
  5048. )
  5049. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  5050. query = {
  5051. "field": ["device.class"],
  5052. "statsPeriod": "24h",
  5053. }
  5054. response = self.do_request(query, features=features)
  5055. assert response.status_code == 200, response.content
  5056. data = response.data["data"]
  5057. assert len(data) == 3
  5058. result = (*map(lambda columns: columns["device.class"], data),)
  5059. assert "low" in result
  5060. assert "medium" in result
  5061. assert "high" in result
  5062. def test_device_class_filter_low(self):
  5063. project1 = self.create_project()
  5064. for i in range(3):
  5065. self.store_event(data=self.load_data(platform="javascript"), project_id=project1.id)
  5066. self.store_event(
  5067. data={
  5068. "event_id": "a" * 32,
  5069. "transaction": "/example",
  5070. "message": "how to make fast",
  5071. "timestamp": self.ten_mins_ago_iso,
  5072. "tags": {"device.class": "1"},
  5073. },
  5074. project_id=project1.id,
  5075. )
  5076. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  5077. query = {
  5078. "field": ["device.class", "count()"],
  5079. "query": "device.class:low",
  5080. "orderby": "count()",
  5081. "statsPeriod": "24h",
  5082. }
  5083. response = self.do_request(query, features=features)
  5084. assert response.status_code == 200, response.content
  5085. data = response.data["data"]
  5086. assert len(data) == 1
  5087. assert data[0]["count()"] == 1
  5088. def test_group_id_as_custom_tag(self):
  5089. project1 = self.create_project()
  5090. self.store_event(
  5091. data={
  5092. "event_id": "a" * 32,
  5093. "message": "poof",
  5094. "timestamp": self.ten_mins_ago_iso,
  5095. "user": {"email": self.user.email},
  5096. "tags": {"group_id": "this should just get returned"},
  5097. },
  5098. project_id=project1.id,
  5099. )
  5100. query = {
  5101. "field": ["group_id"],
  5102. "query": "",
  5103. "orderby": "group_id",
  5104. "statsPeriod": "24h",
  5105. }
  5106. response = self.do_request(query)
  5107. assert response.status_code == 200, response.content
  5108. assert response.data["data"][0]["group_id"] == "this should just get returned"
  5109. def test_floored_epm(self):
  5110. for _ in range(5):
  5111. data = self.load_data(
  5112. timestamp=self.ten_mins_ago,
  5113. duration=timedelta(seconds=5),
  5114. )
  5115. data["transaction"] = "/aggregates/1"
  5116. event1 = self.store_event(data, project_id=self.project.id)
  5117. query = {
  5118. "field": ["transaction", "floored_epm()", "epm()"],
  5119. "query": "event.type:transaction",
  5120. "orderby": ["transaction"],
  5121. "start": self.eleven_mins_ago_iso,
  5122. "end": iso_format(self.nine_mins_ago),
  5123. }
  5124. response = self.do_request(query)
  5125. assert response.status_code == 200, response.content
  5126. assert len(response.data["data"]) == 1
  5127. data = response.data["data"]
  5128. assert data[0]["transaction"] == event1.transaction
  5129. assert data[0]["floored_epm()"] == 1
  5130. assert data[0]["epm()"] == 2.5
  5131. def test_floored_epm_more_events(self):
  5132. for _ in range(25):
  5133. data = self.load_data(
  5134. timestamp=self.ten_mins_ago,
  5135. duration=timedelta(seconds=5),
  5136. )
  5137. data["transaction"] = "/aggregates/1"
  5138. event1 = self.store_event(data, project_id=self.project.id)
  5139. query = {
  5140. "field": ["transaction", "floored_epm()", "epm()"],
  5141. "query": "event.type:transaction",
  5142. "orderby": ["transaction"],
  5143. "start": self.eleven_mins_ago_iso,
  5144. "end": iso_format(self.nine_mins_ago),
  5145. }
  5146. response = self.do_request(query)
  5147. assert response.status_code == 200, response.content
  5148. assert len(response.data["data"]) == 1
  5149. data = response.data["data"]
  5150. assert data[0]["transaction"] == event1.transaction
  5151. assert data[0]["epm()"] == 12.5
  5152. assert data[0]["floored_epm()"] == 10
  5153. @region_silo_test
  5154. class OrganizationEventsProfilesDatasetEndpointTest(OrganizationEventsEndpointTestBase):
  5155. @mock.patch("sentry.search.events.builder.discover.raw_snql_query")
  5156. def test_profiles_dataset_simple(self, mock_snql_query):
  5157. mock_snql_query.side_effect = [
  5158. {
  5159. "data": [
  5160. {
  5161. "project": self.project.id,
  5162. "transaction": "foo",
  5163. "last_seen": "2022-10-20T16:41:22+00:00",
  5164. "latest_event": "a" * 32,
  5165. "count": 1,
  5166. "count_unique_transaction": 1,
  5167. "percentile_profile_duration_0_25": 1,
  5168. "p50_profile_duration": 1,
  5169. "p75_profile_duration": 1,
  5170. "p95_profile_duration": 1,
  5171. "p99_profile_duration": 1,
  5172. "p100_profile_duration": 1,
  5173. "min_profile_duration": 1,
  5174. "max_profile_duration": 1,
  5175. "avg_profile_duration": 1,
  5176. "sum_profile_duration": 1,
  5177. },
  5178. ],
  5179. "meta": [
  5180. {
  5181. "name": "project",
  5182. "type": "UInt64",
  5183. },
  5184. {
  5185. "name": "transaction",
  5186. "type": "LowCardinality(String)",
  5187. },
  5188. {
  5189. "name": "last_seen",
  5190. "type": "DateTime",
  5191. },
  5192. {
  5193. "name": "latest_event",
  5194. "type": "String",
  5195. },
  5196. {
  5197. "name": "count",
  5198. "type": "UInt64",
  5199. },
  5200. {
  5201. "name": "count_unique_transaction",
  5202. "type": "UInt64",
  5203. },
  5204. {
  5205. "name": "percentile_profile_duration_0_25",
  5206. "type": "Float64",
  5207. },
  5208. *[
  5209. {
  5210. "name": f"{fn}_profile_duration",
  5211. "type": "Float64",
  5212. }
  5213. for fn in ["p50", "p75", "p95", "p99", "p100", "min", "max", "avg", "sum"]
  5214. ],
  5215. ],
  5216. },
  5217. ]
  5218. fields = [
  5219. "project",
  5220. "transaction",
  5221. "last_seen()",
  5222. "latest_event()",
  5223. "count()",
  5224. "count_unique(transaction)",
  5225. "percentile(profile.duration, 0.25)",
  5226. "p50(profile.duration)",
  5227. "p75(profile.duration)",
  5228. "p95(profile.duration)",
  5229. "p99(profile.duration)",
  5230. "p100(profile.duration)",
  5231. "min(profile.duration)",
  5232. "max(profile.duration)",
  5233. "avg(profile.duration)",
  5234. "sum(profile.duration)",
  5235. ]
  5236. query = {
  5237. "field": fields,
  5238. "project": [self.project.id],
  5239. "dataset": "profiles",
  5240. }
  5241. response = self.do_request(query, features={"organizations:profiling": True})
  5242. assert response.status_code == 200, response.content
  5243. # making sure the response keys are in the form we expect and not aliased
  5244. data_keys = {key for row in response.data["data"] for key in row}
  5245. field_keys = {key for key in response.data["meta"]["fields"]}
  5246. unit_keys = {key for key in response.data["meta"]["units"]}
  5247. assert set(fields) == data_keys
  5248. assert set(fields) == field_keys
  5249. assert set(fields) == unit_keys
  5250. @region_silo_test
  5251. class OrganizationEventsProfileFunctionsDatasetEndpointTest(OrganizationEventsEndpointTestBase):
  5252. @mock.patch("sentry.search.events.builder.discover.raw_snql_query")
  5253. def test_functions_dataset_simple(self, mock_snql_query):
  5254. mock_snql_query.side_effect = [
  5255. {
  5256. "data": [
  5257. {
  5258. "function": "foo_fn",
  5259. "transaction": "foo_tx",
  5260. "is_application": 1,
  5261. "project": "python",
  5262. "release": "backend@1",
  5263. "platform.name": "python",
  5264. "retention_days": 90,
  5265. "package": "lib_foo",
  5266. "environment": "development",
  5267. "p50()": 34695708.0,
  5268. "p75()": 45980969.0,
  5269. "p95()": 92592143.6,
  5270. "p99()": 103764495.12000002,
  5271. "avg()": 51235123.0,
  5272. "sum()": 951283182.0,
  5273. "examples()": [
  5274. "6919e4076b4a46bbaf1f3ef1f0666147",
  5275. "b04bafc378ea421b83b8680fd1caea52",
  5276. "ceac40e40a0c44adb7f7b1e07f02586f",
  5277. "6919e4076b4a46bbaf1f3ef1f0666147",
  5278. "68acd0a8bdea46a59ba23907152b1ce5",
  5279. "8ad2946586694c1b83d817fab3bccbc1",
  5280. ],
  5281. "count()": 12,
  5282. },
  5283. ],
  5284. "meta": [
  5285. {
  5286. "name": "function",
  5287. "type": "String",
  5288. },
  5289. {
  5290. "name": "transaction",
  5291. "type": "String",
  5292. },
  5293. {
  5294. "name": "is_application",
  5295. "type": "UInt8",
  5296. },
  5297. {
  5298. "name": "project",
  5299. "type": "String",
  5300. },
  5301. {
  5302. "name": "release",
  5303. "type": "String",
  5304. },
  5305. {
  5306. "name": "platform.name",
  5307. "type": "String",
  5308. },
  5309. {
  5310. "name": "retention_days",
  5311. "type": "UInt16",
  5312. },
  5313. {
  5314. "name": "package",
  5315. "type": "String",
  5316. },
  5317. {
  5318. "name": "environment",
  5319. "type": "String",
  5320. },
  5321. {
  5322. "name": "p50()",
  5323. "type": "Float64",
  5324. },
  5325. {
  5326. "name": "p75()",
  5327. "type": "Float64",
  5328. },
  5329. {
  5330. "name": "p95()",
  5331. "type": "Float64",
  5332. },
  5333. {
  5334. "name": "p99()",
  5335. "type": "Float64",
  5336. },
  5337. {
  5338. "name": "avg()",
  5339. "type": "Float64",
  5340. },
  5341. {
  5342. "name": "sum()",
  5343. "type": "Float64",
  5344. },
  5345. {
  5346. "name": "examples()",
  5347. "type": "Array(String)",
  5348. },
  5349. {
  5350. "name": "count()",
  5351. "type": "UInt64",
  5352. },
  5353. ],
  5354. }
  5355. ]
  5356. fields = [
  5357. "transaction",
  5358. "project",
  5359. "function",
  5360. "package",
  5361. "is_application",
  5362. "platform.name",
  5363. "environment",
  5364. "release",
  5365. "retention_days",
  5366. "count()",
  5367. "examples()",
  5368. "p50()",
  5369. "p75()",
  5370. "p95()",
  5371. "p99()",
  5372. "avg()",
  5373. "sum()",
  5374. ]
  5375. query = {
  5376. "field": fields,
  5377. "project": [self.project.id],
  5378. "dataset": "profileFunctions",
  5379. }
  5380. response = self.do_request(query, features={"organizations:profiling": True})
  5381. assert response.status_code == 200, response.content
  5382. # making sure the response keys are in the form we expect and not aliased
  5383. data_keys = {key for row in response.data["data"] for key in row}
  5384. field_keys = {key for key in response.data["meta"]["fields"]}
  5385. unit_keys = {key for key in response.data["meta"]["units"]}
  5386. assert set(fields) == data_keys
  5387. assert set(fields) == field_keys
  5388. assert set(fields) == unit_keys
  5389. @region_silo_test
  5390. class OrganizationEventsIssuePlatformDatasetEndpointTest(
  5391. OrganizationEventsEndpointTestBase, SearchIssueTestMixin, PerformanceIssueTestCase
  5392. ):
  5393. def test_performance_issue_id_filter(self):
  5394. event = self.create_performance_issue()
  5395. query = {
  5396. "field": ["count()"],
  5397. "statsPeriod": "2h",
  5398. "query": f"issue.id:{event.group.id}",
  5399. "dataset": "issuePlatform",
  5400. }
  5401. response = self.do_request(query)
  5402. assert response.status_code == 200, response.content
  5403. assert response.data["data"][0]["count()"] == 1
  5404. def test_generic_issue_ids_filter(self):
  5405. user_data = {
  5406. "id": self.user.id,
  5407. "username": "user",
  5408. "email": "hellboy@bar.com",
  5409. "ip_address": "127.0.0.1",
  5410. }
  5411. event, _, group_info = self.store_search_issue(
  5412. self.project.id,
  5413. self.user.id,
  5414. [f"{ProfileFileIOGroupType.type_id}-group1"],
  5415. "prod",
  5416. before_now(hours=1).replace(tzinfo=timezone.utc),
  5417. user=user_data,
  5418. )
  5419. event, _, group_info = self.store_search_issue(
  5420. self.project.id,
  5421. self.user.id,
  5422. [f"{ProfileFileIOGroupType.type_id}-group2"],
  5423. "prod",
  5424. before_now(hours=1).replace(tzinfo=timezone.utc),
  5425. user=user_data,
  5426. )
  5427. query = {
  5428. "field": ["title", "release", "environment", "user.display", "timestamp"],
  5429. "statsPeriod": "90d",
  5430. "query": f"issue.id:{group_info.group.id}",
  5431. "dataset": "issuePlatform",
  5432. }
  5433. with self.feature(["organizations:profiling"]):
  5434. response = self.do_request(query)
  5435. assert response.status_code == 200, response.content
  5436. assert len(response.data["data"]) == 1
  5437. assert response.data["data"][0]["title"] == group_info.group.title
  5438. assert response.data["data"][0]["environment"] == "prod"
  5439. assert response.data["data"][0]["user.display"] == user_data["email"]
  5440. assert response.data["data"][0]["timestamp"] == event.timestamp
  5441. query = {
  5442. "field": ["title", "release", "environment", "user.display", "timestamp"],
  5443. "statsPeriod": "90d",
  5444. "query": f"issue:{group_info.group.qualified_short_id}",
  5445. "dataset": "issuePlatform",
  5446. }
  5447. with self.feature(["organizations:profiling"]):
  5448. response = self.do_request(query)
  5449. assert response.status_code == 200, response.content
  5450. assert len(response.data["data"]) == 1
  5451. assert response.data["data"][0]["title"] == group_info.group.title
  5452. assert response.data["data"][0]["environment"] == "prod"
  5453. assert response.data["data"][0]["user.display"] == user_data["email"]
  5454. assert response.data["data"][0]["timestamp"] == event.timestamp
  5455. def test_performance_short_group_id(self):
  5456. event = self.create_performance_issue()
  5457. query = {
  5458. "field": ["count()"],
  5459. "statsPeriod": "1h",
  5460. "query": f"project:{event.group.project.slug} issue:{event.group.qualified_short_id}",
  5461. "dataset": "issuePlatform",
  5462. }
  5463. response = self.do_request(query)
  5464. assert response.status_code == 200, response.content
  5465. assert response.data["data"][0]["count()"] == 1
  5466. def test_multiple_performance_short_group_ids_filter(self):
  5467. event1 = self.create_performance_issue()
  5468. event2 = self.create_performance_issue()
  5469. query = {
  5470. "field": ["count()"],
  5471. "statsPeriod": "1h",
  5472. "query": f"project:{event1.group.project.slug} issue:[{event1.group.qualified_short_id},{event2.group.qualified_short_id}]",
  5473. "dataset": "issuePlatform",
  5474. }
  5475. response = self.do_request(query)
  5476. assert response.status_code == 200, response.content
  5477. assert response.data["data"][0]["count()"] == 2
  5478. def test_user_display_issue_platform(self):
  5479. project1 = self.create_project()
  5480. user_data = {
  5481. "id": self.user.id,
  5482. "username": "user",
  5483. "email": "hellboy@bar.com",
  5484. "ip_address": "127.0.0.1",
  5485. }
  5486. _, _, group_info = self.store_search_issue(
  5487. project1.id,
  5488. 1,
  5489. ["group1-fingerprint"],
  5490. None,
  5491. before_now(hours=1).replace(tzinfo=timezone.utc),
  5492. user=user_data,
  5493. )
  5494. features = {
  5495. "organizations:discover-basic": True,
  5496. "organizations:global-views": True,
  5497. "organizations:profiling": True,
  5498. }
  5499. query = {
  5500. "field": ["user.display"],
  5501. "query": f"user.display:hell* issue.id:{group_info.group.id}",
  5502. "statsPeriod": "24h",
  5503. "dataset": "issuePlatform",
  5504. }
  5505. response = self.do_request(query, features=features)
  5506. assert response.status_code == 200, response.content
  5507. data = response.data["data"]
  5508. assert len(data) == 1
  5509. result = {r["user.display"] for r in data}
  5510. assert result == {user_data["email"]}