test_organization_events.py 235 KB

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