test_organization_events.py 213 KB

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