test_organization_events.py 253 KB

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