test_organization_events.py 243 KB

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