test_organization_events.py 258 KB

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