test_organization_events.py 258 KB

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