test_organization_events.py 227 KB

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