test_organization_events.py 235 KB

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