test_organization_events.py 219 KB

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