test_organization_events.py 235 KB

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