test_organization_events.py 258 KB

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