test_organization_events.py 252 KB

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