test_organization_events.py 192 KB

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