test_organization_events.py 196 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132
  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_denominator(self):
  1246. """This is to test against a bug where the denominator of misery(total unique users) was wrong
  1247. This is because the total unique users for a LCP misery should only count users that have had a txn with lcp,
  1248. and not count all transactions (ie. uniq_if(transaction has lcp) not just uniq())
  1249. """
  1250. ProjectTransactionThreshold.objects.create(
  1251. project=self.project,
  1252. organization=self.project.organization,
  1253. threshold=600,
  1254. metric=TransactionMetric.LCP.value,
  1255. )
  1256. lcps = [
  1257. 400,
  1258. 400,
  1259. 300,
  1260. 3000,
  1261. 3000,
  1262. 3000,
  1263. ]
  1264. for idx, lcp in enumerate(lcps):
  1265. data = load_data(
  1266. "transaction",
  1267. timestamp=before_now(minutes=(10 + idx)),
  1268. )
  1269. data["event_id"] = f"{idx}" * 32
  1270. data["transaction"] = "/misery/new/"
  1271. data["user"] = {"email": f"{idx}@example.com"}
  1272. data["measurements"] = {
  1273. "lcp": {"value": lcp},
  1274. }
  1275. self.store_event(data, project_id=self.project.id)
  1276. # Shouldn't count towards misery
  1277. data = load_data(
  1278. "transaction",
  1279. timestamp=before_now(minutes=(10)),
  1280. start_timestamp=before_now(minutes=(10)),
  1281. )
  1282. data["transaction"] = "/misery/new/"
  1283. data["user"] = {"email": "7@example.com"}
  1284. data["measurements"] = {}
  1285. self.store_event(data, project_id=self.project.id)
  1286. query = {
  1287. "field": [
  1288. "transaction",
  1289. "user_misery()",
  1290. ],
  1291. "query": "event.type:transaction",
  1292. "project": [self.project.id],
  1293. "sort": "-user_misery",
  1294. }
  1295. response = self.do_request(
  1296. query,
  1297. )
  1298. assert response.status_code == 200, response.content
  1299. assert len(response.data["data"]) == 1
  1300. data = response.data["data"]
  1301. # (3 frustrated + 5.8875) / (6 + 117.75)
  1302. assert abs(data[0]["user_misery()"] - 0.071818) < 0.0001
  1303. def test_user_misery_alias_field(self):
  1304. events = [
  1305. ("one", 300),
  1306. ("one", 300),
  1307. ("two", 3000),
  1308. ("two", 3000),
  1309. ("three", 300),
  1310. ("three", 3000),
  1311. ]
  1312. for idx, event in enumerate(events):
  1313. data = load_data(
  1314. "transaction",
  1315. timestamp=before_now(minutes=(10 + idx)),
  1316. start_timestamp=before_now(minutes=(10 + idx), milliseconds=event[1]),
  1317. )
  1318. data["event_id"] = f"{idx}" * 32
  1319. data["transaction"] = f"/user_misery/{idx}"
  1320. data["user"] = {"email": f"{event[0]}@example.com"}
  1321. self.store_event(data, project_id=self.project.id)
  1322. query = {"field": ["user_misery(300)"], "query": "event.type:transaction"}
  1323. response = self.do_request(query)
  1324. assert response.status_code == 200, response.content
  1325. assert len(response.data["data"]) == 1
  1326. data = response.data["data"]
  1327. assert abs(data[0]["user_misery(300)"] - 0.0653) < 0.0001
  1328. def test_apdex_denominator_correct(self):
  1329. """This is to test against a bug where the denominator of apdex(total count) was wrong
  1330. This is because the total_count for a LCP apdex should only count transactions that have lcp, and not count
  1331. all transactions (ie. count_if(transaction has lcp) not just count())
  1332. """
  1333. ProjectTransactionThreshold.objects.create(
  1334. project=self.project,
  1335. organization=self.project.organization,
  1336. threshold=600,
  1337. metric=TransactionMetric.LCP.value,
  1338. )
  1339. lcps = [
  1340. 400,
  1341. 400,
  1342. 300,
  1343. 800,
  1344. 3000,
  1345. 3000,
  1346. 3000,
  1347. ]
  1348. for idx, lcp in enumerate(lcps):
  1349. data = load_data(
  1350. "transaction",
  1351. timestamp=before_now(minutes=(10 + idx)),
  1352. )
  1353. data["event_id"] = f"{idx}" * 32
  1354. data["transaction"] = "/apdex/new/"
  1355. data["user"] = {"email": f"{idx}@example.com"}
  1356. data["measurements"] = {
  1357. "lcp": {"value": lcp},
  1358. }
  1359. self.store_event(data, project_id=self.project.id)
  1360. # Shouldn't count towards apdex
  1361. data = load_data(
  1362. "transaction",
  1363. timestamp=before_now(minutes=(10)),
  1364. start_timestamp=before_now(minutes=(10)),
  1365. )
  1366. data["transaction"] = "/apdex/new/"
  1367. data["user"] = {"email": "7@example.com"}
  1368. data["measurements"] = {}
  1369. self.store_event(data, project_id=self.project.id)
  1370. query = {
  1371. "field": [
  1372. "transaction",
  1373. "apdex()",
  1374. ],
  1375. "query": "event.type:transaction",
  1376. "project": [self.project.id],
  1377. "sort": "-apdex",
  1378. }
  1379. response = self.do_request(
  1380. query,
  1381. )
  1382. assert response.status_code == 200, response.content
  1383. assert len(response.data["data"]) == 1
  1384. data = response.data["data"]
  1385. # 3 satisfied + 1 tolerated => 3.5/7
  1386. assert data[0]["apdex()"] == 0.5
  1387. def test_apdex_new_alias_field(self):
  1388. ProjectTransactionThreshold.objects.create(
  1389. project=self.project,
  1390. organization=self.project.organization,
  1391. threshold=400,
  1392. metric=TransactionMetric.DURATION.value,
  1393. )
  1394. events = [
  1395. ("one", 400),
  1396. ("one", 400),
  1397. ("two", 3000),
  1398. ("two", 3000),
  1399. ("three", 300),
  1400. ("three", 3000),
  1401. ]
  1402. for idx, event in enumerate(events):
  1403. data = load_data(
  1404. "transaction",
  1405. timestamp=before_now(minutes=(10 + idx)),
  1406. start_timestamp=before_now(minutes=(10 + idx), milliseconds=event[1]),
  1407. )
  1408. data["event_id"] = f"{idx}" * 32
  1409. data["transaction"] = f"/apdex/new/{event[0]}"
  1410. data["user"] = {"email": f"{idx}@example.com"}
  1411. self.store_event(data, project_id=self.project.id)
  1412. query = {
  1413. "field": [
  1414. "transaction",
  1415. "apdex()",
  1416. ],
  1417. "query": "event.type:transaction",
  1418. "project": [self.project.id],
  1419. "sort": "-apdex",
  1420. }
  1421. response = self.do_request(
  1422. query,
  1423. )
  1424. assert response.status_code == 200, response.content
  1425. assert len(response.data["data"]) == 3
  1426. data = response.data["data"]
  1427. assert data[0]["apdex()"] == 1.0
  1428. assert data[1]["apdex()"] == 0.5
  1429. assert data[2]["apdex()"] == 0.0
  1430. query["query"] = "event.type:transaction apdex():>0.50"
  1431. response = self.do_request(
  1432. query,
  1433. )
  1434. assert response.status_code == 200, response.content
  1435. assert len(response.data["data"]) == 1
  1436. data = response.data["data"]
  1437. assert data[0]["apdex()"] == 1.0
  1438. def test_user_misery_alias_field_with_project_threshold(self):
  1439. ProjectTransactionThreshold.objects.create(
  1440. project=self.project,
  1441. organization=self.project.organization,
  1442. threshold=400,
  1443. metric=TransactionMetric.DURATION.value,
  1444. )
  1445. events = [
  1446. ("one", 400),
  1447. ("one", 400),
  1448. ("two", 3000),
  1449. ("two", 3000),
  1450. ("three", 300),
  1451. ("three", 3000),
  1452. ]
  1453. for idx, event in enumerate(events):
  1454. data = load_data(
  1455. "transaction",
  1456. timestamp=before_now(minutes=(10 + idx)),
  1457. start_timestamp=before_now(minutes=(10 + idx), milliseconds=event[1]),
  1458. )
  1459. data["event_id"] = f"{idx}" * 32
  1460. data["transaction"] = f"/count_miserable/horribilis/{event[0]}"
  1461. data["user"] = {"email": f"{idx}@example.com"}
  1462. self.store_event(data, project_id=self.project.id)
  1463. query = {
  1464. "field": [
  1465. "transaction",
  1466. "user_misery()",
  1467. ],
  1468. "orderby": "user_misery()",
  1469. "query": "event.type:transaction",
  1470. "project": [self.project.id],
  1471. }
  1472. response = self.do_request(query)
  1473. assert response.status_code == 200, response.content
  1474. assert len(response.data["data"]) == 3
  1475. data = response.data["data"]
  1476. assert data[0]["user_misery()"] == pytest.approx(0.04916, rel=1e-3)
  1477. assert data[1]["user_misery()"] == pytest.approx(0.05751, rel=1e-3)
  1478. assert data[2]["user_misery()"] == pytest.approx(0.06586, rel=1e-3)
  1479. query["query"] = "event.type:transaction user_misery():>0.050"
  1480. response = self.do_request(
  1481. query,
  1482. )
  1483. assert response.status_code == 200, response.content
  1484. assert len(response.data["data"]) == 2
  1485. data = response.data["data"]
  1486. assert data[0]["user_misery()"] == pytest.approx(0.05751, rel=1e-3)
  1487. assert data[1]["user_misery()"] == pytest.approx(0.06586, rel=1e-3)
  1488. def test_user_misery_alias_field_with_transaction_threshold(self):
  1489. events = [
  1490. ("one", 300),
  1491. ("two", 300),
  1492. ("one", 3000),
  1493. ("two", 3000),
  1494. ("three", 400),
  1495. ("four", 4000),
  1496. ]
  1497. for idx, event in enumerate(events):
  1498. data = load_data(
  1499. "transaction",
  1500. timestamp=before_now(minutes=(10 + idx)),
  1501. start_timestamp=before_now(minutes=(10 + idx), milliseconds=event[1]),
  1502. )
  1503. data["event_id"] = f"{idx}" * 32
  1504. data["transaction"] = f"/count_miserable/horribilis/{idx}"
  1505. data["user"] = {"email": f"{event[0]}@example.com"}
  1506. self.store_event(data, project_id=self.project.id)
  1507. if idx % 2:
  1508. ProjectTransactionThresholdOverride.objects.create(
  1509. transaction=f"/count_miserable/horribilis/{idx}",
  1510. project=self.project,
  1511. organization=self.project.organization,
  1512. threshold=100 * idx,
  1513. metric=TransactionMetric.DURATION.value,
  1514. )
  1515. query = {
  1516. "field": [
  1517. "transaction",
  1518. "user_misery()",
  1519. ],
  1520. "query": "event.type:transaction",
  1521. "orderby": "transaction",
  1522. "project": [self.project.id],
  1523. }
  1524. response = self.do_request(
  1525. query,
  1526. )
  1527. assert response.status_code == 200, response.content
  1528. expected = [
  1529. ("/count_miserable/horribilis/0", ["duration", 300], 0.049578),
  1530. ("/count_miserable/horribilis/1", ["duration", 100], 0.049578),
  1531. ("/count_miserable/horribilis/2", ["duration", 300], 0.058),
  1532. ("/count_miserable/horribilis/3", ["duration", 300], 0.058),
  1533. ("/count_miserable/horribilis/4", ["duration", 300], 0.049578),
  1534. ("/count_miserable/horribilis/5", ["duration", 500], 0.058),
  1535. ]
  1536. assert len(response.data["data"]) == 6
  1537. data = response.data["data"]
  1538. for i, record in enumerate(expected):
  1539. name, threshold_config, misery = record
  1540. assert data[i]["transaction"] == name
  1541. assert data[i]["project_threshold_config"] == threshold_config
  1542. assert data[i]["user_misery()"] == pytest.approx(misery, rel=1e-3)
  1543. query["query"] = "event.type:transaction user_misery():>0.050"
  1544. response = self.do_request(
  1545. query,
  1546. )
  1547. assert response.status_code == 200, response.content
  1548. assert len(response.data["data"]) == 3
  1549. data = response.data["data"]
  1550. assert data[0]["user_misery()"] == pytest.approx(0.058, rel=1e-3)
  1551. assert data[1]["user_misery()"] == pytest.approx(0.058, rel=1e-3)
  1552. assert data[2]["user_misery()"] == pytest.approx(0.058, rel=1e-3)
  1553. def test_user_misery_alias_field_with_transaction_threshold_and_project_threshold(self):
  1554. project = self.create_project()
  1555. ProjectTransactionThreshold.objects.create(
  1556. project=project,
  1557. organization=project.organization,
  1558. threshold=100,
  1559. metric=TransactionMetric.DURATION.value,
  1560. )
  1561. events = [
  1562. ("one", 300),
  1563. ("two", 300),
  1564. ("one", 3000),
  1565. ("two", 3000),
  1566. ("three", 400),
  1567. ("four", 4000),
  1568. ]
  1569. for idx, event in enumerate(events):
  1570. data = load_data(
  1571. "transaction",
  1572. timestamp=before_now(minutes=(10 + idx)),
  1573. start_timestamp=before_now(minutes=(10 + idx), milliseconds=event[1]),
  1574. )
  1575. data["event_id"] = f"{idx}" * 32
  1576. data["transaction"] = f"/count_miserable/horribilis/{idx}"
  1577. data["user"] = {"email": f"{event[0]}@example.com"}
  1578. self.store_event(data, project_id=project.id)
  1579. if idx % 2:
  1580. ProjectTransactionThresholdOverride.objects.create(
  1581. transaction=f"/count_miserable/horribilis/{idx}",
  1582. project=project,
  1583. organization=project.organization,
  1584. threshold=100 * idx,
  1585. metric=TransactionMetric.DURATION.value,
  1586. )
  1587. project2 = self.create_project()
  1588. data = load_data("transaction", timestamp=before_now(minutes=1))
  1589. data["transaction"] = "/count_miserable/horribilis/project2"
  1590. data["user"] = {"email": "project2@example.com"}
  1591. self.store_event(data, project_id=project2.id)
  1592. query = {
  1593. "field": [
  1594. "transaction",
  1595. "user_misery()",
  1596. ],
  1597. "query": "event.type:transaction",
  1598. "orderby": "transaction",
  1599. "project": [project.id, project2.id],
  1600. }
  1601. response = self.do_request(
  1602. query,
  1603. features={
  1604. "organizations:discover-basic": True,
  1605. "organizations:global-views": True,
  1606. },
  1607. )
  1608. assert response.status_code == 200, response.content
  1609. expected = [
  1610. (
  1611. "/count_miserable/horribilis/0",
  1612. ["duration", 100],
  1613. 0.049578,
  1614. ), # Uses project threshold
  1615. ("/count_miserable/horribilis/1", ["duration", 100], 0.049578), # Uses txn threshold
  1616. ("/count_miserable/horribilis/2", ["duration", 100], 0.058), # Uses project threshold
  1617. ("/count_miserable/horribilis/3", ["duration", 300], 0.058), # Uses txn threshold
  1618. (
  1619. "/count_miserable/horribilis/4",
  1620. ["duration", 100],
  1621. 0.049578,
  1622. ), # Uses project threshold
  1623. ("/count_miserable/horribilis/5", ["duration", 500], 0.058), # Uses txn threshold
  1624. ("/count_miserable/horribilis/project2", ["duration", 300], 0.058), # Uses fallback
  1625. ]
  1626. assert len(response.data["data"]) == 7
  1627. data = response.data["data"]
  1628. for i, record in enumerate(expected):
  1629. name, threshold_config, misery = record
  1630. assert data[i]["transaction"] == name
  1631. assert data[i]["project_threshold_config"] == threshold_config
  1632. assert data[i]["user_misery()"] == pytest.approx(misery, rel=1e-3)
  1633. query["query"] = "event.type:transaction user_misery():>0.050"
  1634. response = self.do_request(
  1635. query,
  1636. features={
  1637. "organizations:discover-basic": True,
  1638. "organizations:global-views": True,
  1639. },
  1640. )
  1641. assert response.status_code == 200, response.content
  1642. assert len(response.data["data"]) == 4
  1643. def test_aggregation(self):
  1644. self.store_event(
  1645. data={
  1646. "event_id": "a" * 32,
  1647. "timestamp": self.ten_mins_ago,
  1648. "fingerprint": ["group_1"],
  1649. "user": {"email": "foo@example.com"},
  1650. "environment": "prod",
  1651. "tags": {"sub_customer.is-Enterprise-42": "1"},
  1652. },
  1653. project_id=self.project.id,
  1654. )
  1655. self.store_event(
  1656. data={
  1657. "event_id": "b" * 32,
  1658. "timestamp": self.ten_mins_ago,
  1659. "fingerprint": ["group_2"],
  1660. "user": {"email": "foo@example.com"},
  1661. "environment": "staging",
  1662. "tags": {"sub_customer.is-Enterprise-42": "1"},
  1663. },
  1664. project_id=self.project.id,
  1665. )
  1666. self.store_event(
  1667. data={
  1668. "event_id": "c" * 32,
  1669. "timestamp": self.ten_mins_ago,
  1670. "fingerprint": ["group_2"],
  1671. "user": {"email": "foo@example.com"},
  1672. "environment": "prod",
  1673. "tags": {"sub_customer.is-Enterprise-42": "0"},
  1674. },
  1675. project_id=self.project.id,
  1676. )
  1677. self.store_event(
  1678. data={
  1679. "event_id": "d" * 32,
  1680. "timestamp": self.ten_mins_ago,
  1681. "fingerprint": ["group_2"],
  1682. "user": {"email": "foo@example.com"},
  1683. "environment": "prod",
  1684. "tags": {"sub_customer.is-Enterprise-42": "1"},
  1685. },
  1686. project_id=self.project.id,
  1687. )
  1688. query = {
  1689. "field": ["sub_customer.is-Enterprise-42", "count(sub_customer.is-Enterprise-42)"],
  1690. "orderby": "sub_customer.is-Enterprise-42",
  1691. }
  1692. response = self.do_request(query)
  1693. assert response.status_code == 200, response.content
  1694. assert len(response.data["data"]) == 2
  1695. data = response.data["data"]
  1696. assert data[0]["count(sub_customer.is-Enterprise-42)"] == 1
  1697. assert data[1]["count(sub_customer.is-Enterprise-42)"] == 3
  1698. def test_aggregation_comparison(self):
  1699. self.store_event(
  1700. data={
  1701. "event_id": "a" * 32,
  1702. "timestamp": self.ten_mins_ago,
  1703. "fingerprint": ["group_1"],
  1704. "user": {"email": "foo@example.com"},
  1705. },
  1706. project_id=self.project.id,
  1707. )
  1708. event = 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. },
  1715. project_id=self.project.id,
  1716. )
  1717. self.store_event(
  1718. data={
  1719. "event_id": "c" * 32,
  1720. "timestamp": self.ten_mins_ago,
  1721. "fingerprint": ["group_2"],
  1722. "user": {"email": "bar@example.com"},
  1723. },
  1724. project_id=self.project.id,
  1725. )
  1726. self.store_event(
  1727. data={
  1728. "event_id": "d" * 32,
  1729. "timestamp": self.ten_mins_ago,
  1730. "fingerprint": ["group_3"],
  1731. "user": {"email": "bar@example.com"},
  1732. },
  1733. project_id=self.project.id,
  1734. )
  1735. self.store_event(
  1736. data={
  1737. "event_id": "e" * 32,
  1738. "timestamp": self.ten_mins_ago,
  1739. "fingerprint": ["group_3"],
  1740. "user": {"email": "bar@example.com"},
  1741. },
  1742. project_id=self.project.id,
  1743. )
  1744. query = {
  1745. "field": ["issue.id", "count(id)", "count_unique(user)"],
  1746. "query": "count(id):>1 count_unique(user):>1",
  1747. "orderby": "issue.id",
  1748. }
  1749. response = self.do_request(query)
  1750. assert response.status_code == 200, response.content
  1751. assert len(response.data["data"]) == 1
  1752. data = response.data["data"]
  1753. assert data[0]["issue.id"] == event.group_id
  1754. assert data[0]["count(id)"] == 2
  1755. assert data[0]["count_unique(user)"] == 2
  1756. def test_aggregation_alias_comparison(self):
  1757. data = load_data(
  1758. "transaction",
  1759. timestamp=before_now(minutes=10),
  1760. start_timestamp=before_now(minutes=10, seconds=5),
  1761. )
  1762. data["transaction"] = "/aggregates/1"
  1763. self.store_event(data, project_id=self.project.id)
  1764. data = load_data(
  1765. "transaction",
  1766. timestamp=before_now(minutes=10),
  1767. start_timestamp=before_now(minutes=10, seconds=3),
  1768. )
  1769. data["transaction"] = "/aggregates/2"
  1770. event = self.store_event(data, project_id=self.project.id)
  1771. query = {
  1772. "field": ["transaction", "p95()"],
  1773. "query": "event.type:transaction p95():<4000",
  1774. "orderby": ["transaction"],
  1775. }
  1776. response = self.do_request(query)
  1777. assert response.status_code == 200, response.content
  1778. assert len(response.data["data"]) == 1
  1779. data = response.data["data"]
  1780. assert data[0]["transaction"] == event.transaction
  1781. assert data[0]["p95()"] == 3000
  1782. def test_auto_aggregations(self):
  1783. data = load_data(
  1784. "transaction",
  1785. timestamp=before_now(minutes=10),
  1786. start_timestamp=before_now(minutes=10, seconds=5),
  1787. )
  1788. data["transaction"] = "/aggregates/1"
  1789. self.store_event(data, project_id=self.project.id)
  1790. data = load_data(
  1791. "transaction",
  1792. timestamp=before_now(minutes=10),
  1793. start_timestamp=before_now(minutes=10, seconds=3),
  1794. )
  1795. data["transaction"] = "/aggregates/2"
  1796. event = self.store_event(data, project_id=self.project.id)
  1797. query = {
  1798. "field": ["transaction", "p75()"],
  1799. "query": "event.type:transaction p95():<4000",
  1800. "orderby": ["transaction"],
  1801. }
  1802. response = self.do_request(query)
  1803. assert response.status_code == 200, response.content
  1804. assert len(response.data["data"]) == 1
  1805. data = response.data["data"]
  1806. assert data[0]["transaction"] == event.transaction
  1807. query = {
  1808. "field": ["transaction"],
  1809. "query": "event.type:transaction p95():<4000",
  1810. "orderby": ["transaction"],
  1811. }
  1812. response = self.do_request(query)
  1813. assert response.status_code == 400, response.content
  1814. def test_aggregation_comparison_with_conditions(self):
  1815. self.store_event(
  1816. data={
  1817. "event_id": "a" * 32,
  1818. "timestamp": self.ten_mins_ago,
  1819. "fingerprint": ["group_1"],
  1820. "user": {"email": "foo@example.com"},
  1821. "environment": "prod",
  1822. },
  1823. project_id=self.project.id,
  1824. )
  1825. self.store_event(
  1826. data={
  1827. "event_id": "b" * 32,
  1828. "timestamp": self.ten_mins_ago,
  1829. "fingerprint": ["group_2"],
  1830. "user": {"email": "foo@example.com"},
  1831. "environment": "staging",
  1832. },
  1833. project_id=self.project.id,
  1834. )
  1835. event = self.store_event(
  1836. data={
  1837. "event_id": "c" * 32,
  1838. "timestamp": self.ten_mins_ago,
  1839. "fingerprint": ["group_2"],
  1840. "user": {"email": "foo@example.com"},
  1841. "environment": "prod",
  1842. },
  1843. project_id=self.project.id,
  1844. )
  1845. self.store_event(
  1846. data={
  1847. "event_id": "d" * 32,
  1848. "timestamp": self.ten_mins_ago,
  1849. "fingerprint": ["group_2"],
  1850. "user": {"email": "foo@example.com"},
  1851. "environment": "prod",
  1852. },
  1853. project_id=self.project.id,
  1854. )
  1855. query = {
  1856. "field": ["issue.id", "count(id)"],
  1857. "query": "count(id):>1 user.email:foo@example.com environment:prod",
  1858. "orderby": "issue.id",
  1859. }
  1860. response = self.do_request(query)
  1861. assert response.status_code == 200, response.content
  1862. assert len(response.data["data"]) == 1
  1863. data = response.data["data"]
  1864. assert data[0]["issue.id"] == event.group_id
  1865. assert data[0]["count(id)"] == 2
  1866. def test_aggregation_date_comparison_with_conditions(self):
  1867. event = self.store_event(
  1868. data={
  1869. "event_id": "a" * 32,
  1870. "timestamp": self.ten_mins_ago,
  1871. "fingerprint": ["group_1"],
  1872. "user": {"email": "foo@example.com"},
  1873. "environment": "prod",
  1874. },
  1875. project_id=self.project.id,
  1876. )
  1877. self.store_event(
  1878. data={
  1879. "event_id": "b" * 32,
  1880. "timestamp": self.ten_mins_ago,
  1881. "fingerprint": ["group_2"],
  1882. "user": {"email": "foo@example.com"},
  1883. "environment": "staging",
  1884. },
  1885. project_id=self.project.id,
  1886. )
  1887. self.store_event(
  1888. data={
  1889. "event_id": "c" * 32,
  1890. "timestamp": self.ten_mins_ago,
  1891. "fingerprint": ["group_2"],
  1892. "user": {"email": "foo@example.com"},
  1893. "environment": "prod",
  1894. },
  1895. project_id=self.project.id,
  1896. )
  1897. self.store_event(
  1898. data={
  1899. "event_id": "d" * 32,
  1900. "timestamp": self.ten_mins_ago,
  1901. "fingerprint": ["group_2"],
  1902. "user": {"email": "foo@example.com"},
  1903. "environment": "prod",
  1904. },
  1905. project_id=self.project.id,
  1906. )
  1907. query = {
  1908. "field": ["issue.id", "max(timestamp)"],
  1909. "query": "max(timestamp):>1 user.email:foo@example.com environment:prod",
  1910. "orderby": "issue.id",
  1911. }
  1912. response = self.do_request(query)
  1913. assert response.status_code == 200, response.content
  1914. assert len(response.data["data"]) == 2
  1915. assert response.data["meta"]["fields"]["max(timestamp)"] == "date"
  1916. data = response.data["data"]
  1917. assert data[0]["issue.id"] == event.group_id
  1918. def test_percentile_function(self):
  1919. data = load_data(
  1920. "transaction",
  1921. timestamp=before_now(minutes=10),
  1922. start_timestamp=before_now(minutes=10, seconds=5),
  1923. )
  1924. data["transaction"] = "/aggregates/1"
  1925. event1 = self.store_event(data, project_id=self.project.id)
  1926. data = load_data(
  1927. "transaction",
  1928. timestamp=before_now(minutes=10),
  1929. start_timestamp=before_now(minutes=10, seconds=3),
  1930. )
  1931. data["transaction"] = "/aggregates/2"
  1932. event2 = self.store_event(data, project_id=self.project.id)
  1933. query = {
  1934. "field": ["transaction", "percentile(transaction.duration, 0.95)"],
  1935. "query": "event.type:transaction",
  1936. "orderby": ["transaction"],
  1937. }
  1938. response = self.do_request(query)
  1939. assert response.status_code == 200, response.content
  1940. assert len(response.data["data"]) == 2
  1941. data = response.data["data"]
  1942. assert data[0]["transaction"] == event1.transaction
  1943. assert data[0]["percentile(transaction.duration, 0.95)"] == 5000
  1944. assert data[1]["transaction"] == event2.transaction
  1945. assert data[1]["percentile(transaction.duration, 0.95)"] == 3000
  1946. def test_percentile_function_as_condition(self):
  1947. data = load_data(
  1948. "transaction",
  1949. timestamp=before_now(minutes=10),
  1950. start_timestamp=before_now(minutes=10, seconds=5),
  1951. )
  1952. data["transaction"] = "/aggregates/1"
  1953. event1 = self.store_event(data, project_id=self.project.id)
  1954. data = load_data(
  1955. "transaction",
  1956. timestamp=before_now(minutes=10),
  1957. start_timestamp=before_now(minutes=10, seconds=3),
  1958. )
  1959. data["transaction"] = "/aggregates/2"
  1960. self.store_event(data, project_id=self.project.id)
  1961. query = {
  1962. "field": ["transaction", "percentile(transaction.duration, 0.95)"],
  1963. "query": "event.type:transaction percentile(transaction.duration, 0.95):>4000",
  1964. "orderby": ["transaction"],
  1965. }
  1966. response = self.do_request(query)
  1967. assert response.status_code == 200, response.content
  1968. assert len(response.data["data"]) == 1
  1969. data = response.data["data"]
  1970. assert data[0]["transaction"] == event1.transaction
  1971. assert data[0]["percentile(transaction.duration, 0.95)"] == 5000
  1972. def test_epm_function(self):
  1973. data = load_data(
  1974. "transaction",
  1975. timestamp=before_now(minutes=10),
  1976. start_timestamp=before_now(minutes=10, seconds=5),
  1977. )
  1978. data["transaction"] = "/aggregates/1"
  1979. event1 = self.store_event(data, project_id=self.project.id)
  1980. data = load_data(
  1981. "transaction",
  1982. timestamp=before_now(minutes=10),
  1983. start_timestamp=before_now(minutes=10, seconds=3),
  1984. )
  1985. data["transaction"] = "/aggregates/2"
  1986. event2 = self.store_event(data, project_id=self.project.id)
  1987. query = {
  1988. "field": ["transaction", "epm()"],
  1989. "query": "event.type:transaction",
  1990. "orderby": ["transaction"],
  1991. "start": iso_format(before_now(minutes=11)),
  1992. "end": iso_format(before_now(minutes=9)),
  1993. }
  1994. response = self.do_request(query)
  1995. assert response.status_code == 200, response.content
  1996. assert len(response.data["data"]) == 2
  1997. data = response.data["data"]
  1998. assert data[0]["transaction"] == event1.transaction
  1999. assert data[0]["epm()"] == 0.5
  2000. assert data[1]["transaction"] == event2.transaction
  2001. assert data[1]["epm()"] == 0.5
  2002. def test_nonexistent_fields(self):
  2003. self.store_event(
  2004. data={
  2005. "event_id": "a" * 32,
  2006. "message": "how to make fast",
  2007. "timestamp": self.ten_mins_ago,
  2008. },
  2009. project_id=self.project.id,
  2010. )
  2011. query = {"field": ["issue_world.id"]}
  2012. response = self.do_request(query)
  2013. assert response.status_code == 200, response.content
  2014. assert response.data["data"][0]["issue_world.id"] == ""
  2015. def test_no_requested_fields_or_grouping(self):
  2016. self.store_event(
  2017. data={
  2018. "event_id": "a" * 32,
  2019. "message": "how to make fast",
  2020. "timestamp": self.ten_mins_ago,
  2021. },
  2022. project_id=self.project.id,
  2023. )
  2024. query = {"query": "test"}
  2025. response = self.do_request(query)
  2026. assert response.status_code == 400, response.content
  2027. assert response.data["detail"] == "No columns selected"
  2028. def test_condition_on_aggregate_misses(self):
  2029. self.store_event(
  2030. data={
  2031. "event_id": "c" * 32,
  2032. "timestamp": self.ten_mins_ago,
  2033. "fingerprint": ["group_2"],
  2034. "user": {"email": "bar@example.com"},
  2035. },
  2036. project_id=self.project.id,
  2037. )
  2038. query = {"field": ["issue.id"], "query": "event_count:>0", "orderby": "issue.id"}
  2039. response = self.do_request(query)
  2040. assert response.status_code == 200, response.content
  2041. assert len(response.data["data"]) == 0
  2042. def test_next_prev_link_headers(self):
  2043. events = [("a", "group_1"), ("b", "group_2"), ("c", "group_2"), ("d", "group_2")]
  2044. for e in events:
  2045. self.store_event(
  2046. data={
  2047. "event_id": e[0] * 32,
  2048. "timestamp": self.ten_mins_ago,
  2049. "fingerprint": [e[1]],
  2050. "user": {"email": "foo@example.com"},
  2051. "tags": {"language": "C++"},
  2052. },
  2053. project_id=self.project.id,
  2054. )
  2055. query = {
  2056. "field": ["count(id)", "issue.id", "context.key"],
  2057. "sort": "-count_id",
  2058. "query": "language:C++",
  2059. }
  2060. response = self.do_request(query)
  2061. assert response.status_code == 200, response.content
  2062. links = parse_link_header(response["Link"])
  2063. for link in links:
  2064. assert "field=issue.id" in link
  2065. assert "field=count%28id%29" in link
  2066. assert "field=context.key" in link
  2067. assert "sort=-count_id" in link
  2068. assert "query=language%3AC%2B%2B" in link
  2069. assert len(response.data["data"]) == 2
  2070. data = response.data["data"]
  2071. assert data[0]["count(id)"] == 3
  2072. assert data[1]["count(id)"] == 1
  2073. def test_empty_count_query(self):
  2074. event = self.store_event(
  2075. data={
  2076. "event_id": "a" * 32,
  2077. "timestamp": self.ten_mins_ago,
  2078. "fingerprint": ["1123581321"],
  2079. "user": {"email": "foo@example.com"},
  2080. "tags": {"language": "C++"},
  2081. },
  2082. project_id=self.project.id,
  2083. )
  2084. query = {
  2085. "field": ["count()"],
  2086. "query": f"issue.id:{event.group_id} timestamp:>{self.ten_mins_ago}",
  2087. "statsPeriod": "14d",
  2088. }
  2089. response = self.do_request(query)
  2090. assert response.status_code == 200, response.content
  2091. data = response.data["data"]
  2092. assert len(data) == 1
  2093. assert data[0]["count()"] == 0
  2094. def test_stack_wildcard_condition(self):
  2095. data = load_data("javascript")
  2096. data["timestamp"] = self.ten_mins_ago
  2097. self.store_event(data=data, project_id=self.project.id)
  2098. query = {"field": ["stack.filename", "message"], "query": "stack.filename:*.js"}
  2099. response = self.do_request(query)
  2100. assert response.status_code == 200, response.content
  2101. assert len(response.data["data"]) == 1
  2102. assert response.data["meta"]["fields"]["message"] == "string"
  2103. def test_email_wildcard_condition(self):
  2104. data = load_data("javascript")
  2105. data["timestamp"] = self.ten_mins_ago
  2106. self.store_event(data=data, project_id=self.project.id)
  2107. query = {"field": ["stack.filename", "message"], "query": "user.email:*@example.org"}
  2108. response = self.do_request(query)
  2109. assert response.status_code == 200, response.content
  2110. assert len(response.data["data"]) == 1
  2111. assert response.data["meta"]["fields"]["message"] == "string"
  2112. def test_release_wildcard_condition(self):
  2113. release = self.create_release(version="test@1.2.3+123")
  2114. self.store_event(
  2115. data={"release": release.version, "timestamp": self.ten_mins_ago},
  2116. project_id=self.project.id,
  2117. )
  2118. query = {"field": ["stack.filename", "release"], "query": "release:test*"}
  2119. response = self.do_request(query)
  2120. assert response.status_code == 200, response.content
  2121. assert len(response.data["data"]) == 1
  2122. assert response.data["data"][0]["release"] == release.version
  2123. def test_transaction_event_type(self):
  2124. self.store_event(data=self.transaction_data, project_id=self.project.id)
  2125. query = {
  2126. "field": ["transaction", "transaction.duration", "transaction.status"],
  2127. "query": "event.type:transaction",
  2128. }
  2129. response = self.do_request(query)
  2130. assert response.status_code == 200, response.content
  2131. assert len(response.data["data"]) == 1
  2132. assert response.data["meta"]["fields"]["transaction.duration"] == "duration"
  2133. assert response.data["meta"]["fields"]["transaction.status"] == "string"
  2134. assert response.data["meta"]["units"]["transaction.duration"] == "millisecond"
  2135. assert response.data["data"][0]["transaction.status"] == "ok"
  2136. def test_trace_columns(self):
  2137. self.store_event(data=self.transaction_data, project_id=self.project.id)
  2138. query = {"field": ["trace"], "query": "event.type:transaction"}
  2139. response = self.do_request(query)
  2140. assert response.status_code == 200, response.content
  2141. assert len(response.data["data"]) == 1
  2142. assert response.data["meta"]["fields"]["trace"] == "string"
  2143. assert (
  2144. response.data["data"][0]["trace"]
  2145. == self.transaction_data["contexts"]["trace"]["trace_id"]
  2146. )
  2147. def test_issue_in_columns(self):
  2148. project1 = self.create_project()
  2149. project2 = self.create_project()
  2150. event1 = self.store_event(
  2151. data={
  2152. "event_id": "a" * 32,
  2153. "transaction": "/example",
  2154. "message": "how to make fast",
  2155. "timestamp": self.ten_mins_ago,
  2156. "fingerprint": ["group_1"],
  2157. },
  2158. project_id=project1.id,
  2159. )
  2160. event2 = self.store_event(
  2161. data={
  2162. "event_id": "b" * 32,
  2163. "transaction": "/example",
  2164. "message": "how to make fast",
  2165. "timestamp": self.ten_mins_ago,
  2166. "fingerprint": ["group_1"],
  2167. },
  2168. project_id=project2.id,
  2169. )
  2170. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2171. query = {"field": ["id", "issue"], "orderby": ["id"]}
  2172. response = self.do_request(query, features=features)
  2173. assert response.status_code == 200, response.content
  2174. data = response.data["data"]
  2175. assert len(data) == 2
  2176. assert data[0]["id"] == event1.event_id
  2177. assert data[0]["issue.id"] == event1.group_id
  2178. assert data[0]["issue"] == event1.group.qualified_short_id
  2179. assert data[1]["id"] == event2.event_id
  2180. assert data[1]["issue.id"] == event2.group_id
  2181. assert data[1]["issue"] == event2.group.qualified_short_id
  2182. def test_issue_in_search_and_columns(self):
  2183. project1 = self.create_project()
  2184. project2 = self.create_project()
  2185. event1 = self.store_event(
  2186. data={
  2187. "event_id": "a" * 32,
  2188. "transaction": "/example",
  2189. "message": "how to make fast",
  2190. "timestamp": self.ten_mins_ago,
  2191. "fingerprint": ["group_1"],
  2192. },
  2193. project_id=project1.id,
  2194. )
  2195. self.store_event(
  2196. data={
  2197. "event_id": "b" * 32,
  2198. "transaction": "/example",
  2199. "message": "how to make fast",
  2200. "timestamp": self.ten_mins_ago,
  2201. "fingerprint": ["group_1"],
  2202. },
  2203. project_id=project2.id,
  2204. )
  2205. tests = [
  2206. ("issue", "issue:%s" % event1.group.qualified_short_id),
  2207. ("issue.id", "issue:%s" % event1.group.qualified_short_id),
  2208. ("issue", "issue.id:%s" % event1.group_id),
  2209. ("issue.id", "issue.id:%s" % event1.group_id),
  2210. ]
  2211. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2212. for testdata in tests:
  2213. query = {"field": [testdata[0]], "query": testdata[1]}
  2214. response = self.do_request(query, features=features)
  2215. assert response.status_code == 200, response.content
  2216. data = response.data["data"]
  2217. assert len(data) == 1
  2218. assert data[0]["id"] == event1.event_id
  2219. assert data[0]["issue.id"] == event1.group_id
  2220. if testdata[0] == "issue":
  2221. assert data[0]["issue"] == event1.group.qualified_short_id
  2222. else:
  2223. assert data[0].get("issue", None) is None
  2224. def test_issue_negation(self):
  2225. project1 = self.create_project()
  2226. project2 = self.create_project()
  2227. event1 = self.store_event(
  2228. data={
  2229. "event_id": "a" * 32,
  2230. "transaction": "/example",
  2231. "message": "how to make fast",
  2232. "timestamp": self.ten_mins_ago,
  2233. "fingerprint": ["group_1"],
  2234. },
  2235. project_id=project1.id,
  2236. )
  2237. event2 = self.store_event(
  2238. data={
  2239. "event_id": "b" * 32,
  2240. "transaction": "/example",
  2241. "message": "go really fast plz",
  2242. "timestamp": self.ten_mins_ago,
  2243. "fingerprint": ["group_2"],
  2244. },
  2245. project_id=project2.id,
  2246. )
  2247. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2248. query = {
  2249. "field": ["title", "issue.id"],
  2250. "query": f"!issue:{event1.group.qualified_short_id}",
  2251. }
  2252. response = self.do_request(query, features=features)
  2253. assert response.status_code == 200, response.content
  2254. data = response.data["data"]
  2255. assert len(data) == 1
  2256. assert data[0]["title"] == event2.title
  2257. assert data[0]["issue.id"] == event2.group_id
  2258. def test_search_for_nonexistent_issue(self):
  2259. self.store_event(
  2260. data={
  2261. "event_id": "a" * 32,
  2262. "transaction": "/example",
  2263. "message": "how to make fast",
  2264. "timestamp": self.ten_mins_ago,
  2265. "fingerprint": ["group_1"],
  2266. },
  2267. project_id=self.project.id,
  2268. )
  2269. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2270. query = {"field": ["count()"], "query": "issue.id:112358"}
  2271. response = self.do_request(query, features=features)
  2272. assert response.status_code == 200, response.content
  2273. data = response.data["data"]
  2274. assert len(data) == 1
  2275. assert data[0]["count()"] == 0
  2276. def test_issue_alias_inside_aggregate(self):
  2277. self.store_event(
  2278. data={
  2279. "event_id": "a" * 32,
  2280. "transaction": "/example",
  2281. "message": "how to make fast",
  2282. "timestamp": self.ten_mins_ago,
  2283. "fingerprint": ["group_1"],
  2284. },
  2285. project_id=self.project.id,
  2286. )
  2287. self.store_event(
  2288. data={
  2289. "event_id": "b" * 32,
  2290. "transaction": "/example",
  2291. "message": "how to make fast",
  2292. "timestamp": self.ten_mins_ago,
  2293. "fingerprint": ["group_2"],
  2294. },
  2295. project_id=self.project.id,
  2296. )
  2297. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2298. query = {
  2299. "field": ["project", "count(id)", "count_unique(issue.id)", "count_unique(issue)"],
  2300. "sort": "-count(id)",
  2301. "statsPeriod": "24h",
  2302. }
  2303. response = self.do_request(query, features=features)
  2304. assert response.status_code == 200, response.content
  2305. data = response.data["data"]
  2306. assert len(data) == 1
  2307. assert data[0]["count(id)"] == 2
  2308. assert data[0]["count_unique(issue.id)"] == 2
  2309. assert data[0]["count_unique(issue)"] == 2
  2310. def test_project_alias_inside_aggregate(self):
  2311. project1 = self.create_project()
  2312. project2 = self.create_project()
  2313. self.store_event(
  2314. data={
  2315. "event_id": "a" * 32,
  2316. "transaction": "/example",
  2317. "message": "how to make fast",
  2318. "timestamp": self.ten_mins_ago,
  2319. "fingerprint": ["group_1"],
  2320. },
  2321. project_id=project1.id,
  2322. )
  2323. self.store_event(
  2324. data={
  2325. "event_id": "b" * 32,
  2326. "transaction": "/example",
  2327. "message": "how to make fast",
  2328. "timestamp": self.ten_mins_ago,
  2329. "fingerprint": ["group_2"],
  2330. },
  2331. project_id=project2.id,
  2332. )
  2333. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2334. query = {
  2335. "field": [
  2336. "event.type",
  2337. "count(id)",
  2338. "count_unique(project.id)",
  2339. "count_unique(project)",
  2340. ],
  2341. "sort": "-count(id)",
  2342. "statsPeriod": "24h",
  2343. }
  2344. response = self.do_request(query, features=features)
  2345. assert response.status_code == 200, response.content
  2346. data = response.data["data"]
  2347. assert len(data) == 1
  2348. assert data[0]["count(id)"] == 2
  2349. assert data[0]["count_unique(project.id)"] == 2
  2350. assert data[0]["count_unique(project)"] == 2
  2351. def test_user_display(self):
  2352. project1 = self.create_project()
  2353. project2 = self.create_project()
  2354. self.store_event(
  2355. data={
  2356. "event_id": "a" * 32,
  2357. "transaction": "/example",
  2358. "message": "how to make fast",
  2359. "timestamp": self.ten_mins_ago,
  2360. "user": {"email": "cathy@example.com"},
  2361. },
  2362. project_id=project1.id,
  2363. )
  2364. self.store_event(
  2365. data={
  2366. "event_id": "b" * 32,
  2367. "transaction": "/example",
  2368. "message": "how to make fast",
  2369. "timestamp": self.ten_mins_ago,
  2370. "user": {"username": "catherine"},
  2371. },
  2372. project_id=project2.id,
  2373. )
  2374. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2375. query = {
  2376. "field": ["event.type", "user.display"],
  2377. "query": "user.display:cath*",
  2378. "statsPeriod": "24h",
  2379. }
  2380. response = self.do_request(query, features=features)
  2381. assert response.status_code == 200, response.content
  2382. data = response.data["data"]
  2383. assert len(data) == 2
  2384. result = {r["user.display"] for r in data}
  2385. assert result == {"catherine", "cathy@example.com"}
  2386. def test_user_display_with_aggregates(self):
  2387. self.store_event(
  2388. data={
  2389. "event_id": "a" * 32,
  2390. "transaction": "/example",
  2391. "message": "how to make fast",
  2392. "timestamp": self.ten_mins_ago,
  2393. "user": {"email": "cathy@example.com"},
  2394. },
  2395. project_id=self.project.id,
  2396. )
  2397. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2398. query = {
  2399. "field": ["event.type", "user.display", "count_unique(title)"],
  2400. "statsPeriod": "24h",
  2401. }
  2402. response = self.do_request(query, features=features)
  2403. assert response.status_code == 200, response.content
  2404. data = response.data["data"]
  2405. assert len(data) == 1
  2406. result = {r["user.display"] for r in data}
  2407. assert result == {"cathy@example.com"}
  2408. query = {"field": ["event.type", "count_unique(user.display)"], "statsPeriod": "24h"}
  2409. response = self.do_request(query, features=features)
  2410. assert response.status_code == 200, response.content
  2411. data = response.data["data"]
  2412. assert len(data) == 1
  2413. assert data[0]["count_unique(user.display)"] == 1
  2414. def test_orderby_user_display(self):
  2415. project1 = self.create_project()
  2416. project2 = self.create_project()
  2417. self.store_event(
  2418. data={
  2419. "event_id": "a" * 32,
  2420. "transaction": "/example",
  2421. "message": "how to make fast",
  2422. "timestamp": self.ten_mins_ago,
  2423. "user": {"email": "cathy@example.com"},
  2424. },
  2425. project_id=project1.id,
  2426. )
  2427. self.store_event(
  2428. data={
  2429. "event_id": "b" * 32,
  2430. "transaction": "/example",
  2431. "message": "how to make fast",
  2432. "timestamp": self.ten_mins_ago,
  2433. "user": {"username": "catherine"},
  2434. },
  2435. project_id=project2.id,
  2436. )
  2437. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2438. query = {
  2439. "field": ["event.type", "user.display"],
  2440. "query": "user.display:cath*",
  2441. "statsPeriod": "24h",
  2442. "orderby": "-user.display",
  2443. }
  2444. response = self.do_request(query, features=features)
  2445. assert response.status_code == 200, response.content
  2446. data = response.data["data"]
  2447. assert len(data) == 2
  2448. result = [r["user.display"] for r in data]
  2449. # because we're ordering by `-user.display`, we expect the results in reverse sorted order
  2450. assert result == ["cathy@example.com", "catherine"]
  2451. def test_orderby_user_display_with_aggregates(self):
  2452. project1 = self.create_project()
  2453. project2 = self.create_project()
  2454. self.store_event(
  2455. data={
  2456. "event_id": "a" * 32,
  2457. "transaction": "/example",
  2458. "message": "how to make fast",
  2459. "timestamp": self.ten_mins_ago,
  2460. "user": {"email": "cathy@example.com"},
  2461. },
  2462. project_id=project1.id,
  2463. )
  2464. self.store_event(
  2465. data={
  2466. "event_id": "b" * 32,
  2467. "transaction": "/example",
  2468. "message": "how to make fast",
  2469. "timestamp": self.ten_mins_ago,
  2470. "user": {"username": "catherine"},
  2471. },
  2472. project_id=project2.id,
  2473. )
  2474. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2475. query = {
  2476. "field": ["event.type", "user.display", "count_unique(title)"],
  2477. "query": "user.display:cath*",
  2478. "statsPeriod": "24h",
  2479. "orderby": "user.display",
  2480. }
  2481. response = self.do_request(query, features=features)
  2482. assert response.status_code == 200, response.content
  2483. data = response.data["data"]
  2484. assert len(data) == 2
  2485. result = [r["user.display"] for r in data]
  2486. # because we're ordering by `user.display`, we expect the results in sorted order
  2487. assert result == ["catherine", "cathy@example.com"]
  2488. def test_any_field_alias(self):
  2489. day_ago = before_now(days=1).replace(hour=10, minute=11, second=12, microsecond=13)
  2490. self.store_event(
  2491. data={
  2492. "event_id": "a" * 32,
  2493. "transaction": "/example",
  2494. "message": "how to make fast",
  2495. "timestamp": iso_format(day_ago),
  2496. "user": {"email": "cathy@example.com"},
  2497. },
  2498. project_id=self.project.id,
  2499. )
  2500. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2501. query = {
  2502. "field": [
  2503. "event.type",
  2504. "any(user.display)",
  2505. "any(timestamp.to_day)",
  2506. "any(timestamp.to_hour)",
  2507. ],
  2508. "statsPeriod": "7d",
  2509. }
  2510. response = self.do_request(query, features=features)
  2511. assert response.status_code == 200, response.content
  2512. data = response.data["data"]
  2513. assert len(data) == 1
  2514. result = {r["any(user.display)"] for r in data}
  2515. assert result == {"cathy@example.com"}
  2516. result = {r["any(timestamp.to_day)"][:19] for r in data}
  2517. assert result == {iso_format(day_ago.replace(hour=0, minute=0, second=0, microsecond=0))}
  2518. result = {r["any(timestamp.to_hour)"][:19] for r in data}
  2519. assert result == {iso_format(day_ago.replace(minute=0, second=0, microsecond=0))}
  2520. def test_field_aliases_in_conflicting_functions(self):
  2521. self.store_event(
  2522. data={
  2523. "event_id": "a" * 32,
  2524. "transaction": "/example",
  2525. "message": "how to make fast",
  2526. "timestamp": iso_format(
  2527. before_now(days=1).replace(hour=10, minute=11, second=12, microsecond=13)
  2528. ),
  2529. "user": {"email": "cathy@example.com"},
  2530. },
  2531. project_id=self.project.id,
  2532. )
  2533. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2534. field_aliases = ["user.display", "timestamp.to_day", "timestamp.to_hour"]
  2535. for alias in field_aliases:
  2536. query = {
  2537. "field": [alias, f"any({alias})"],
  2538. "statsPeriod": "7d",
  2539. }
  2540. response = self.do_request(query, features=features)
  2541. assert response.status_code == 400, response.content
  2542. assert (
  2543. response.data["detail"]
  2544. == 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})"
  2545. )
  2546. @pytest.mark.skip(
  2547. """
  2548. For some reason ClickHouse errors when there are two of the same string literals
  2549. (in this case the empty string "") in a query and one is in the prewhere clause.
  2550. Does not affect production or ClickHouse versions > 20.4.
  2551. """
  2552. )
  2553. def test_has_message(self):
  2554. event = self.store_event(
  2555. {"timestamp": self.ten_mins_ago, "message": "a"}, project_id=self.project.id
  2556. )
  2557. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2558. query = {"field": ["project", "message"], "query": "has:message", "statsPeriod": "14d"}
  2559. response = self.do_request(query, features=features)
  2560. assert response.status_code == 200, response.content
  2561. assert len(response.data["data"]) == 1
  2562. assert response.data["data"][0]["message"] == event.message
  2563. query = {"field": ["project", "message"], "query": "!has:message", "statsPeriod": "14d"}
  2564. response = self.do_request(query, features=features)
  2565. assert response.status_code == 200, response.content
  2566. assert len(response.data["data"]) == 0
  2567. def test_has_transaction_status(self):
  2568. self.store_event(self.transaction_data, project_id=self.project.id)
  2569. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2570. query = {
  2571. "field": ["event.type", "count(id)"],
  2572. "query": "event.type:transaction has:transaction.status",
  2573. "sort": "-count(id)",
  2574. "statsPeriod": "24h",
  2575. }
  2576. response = self.do_request(query, features=features)
  2577. assert response.status_code == 200, response.content
  2578. data = response.data["data"]
  2579. assert len(data) == 1
  2580. assert data[0]["count(id)"] == 1
  2581. def test_not_has_transaction_status(self):
  2582. self.store_event(self.transaction_data, project_id=self.project.id)
  2583. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2584. query = {
  2585. "field": ["event.type", "count(id)"],
  2586. "query": "event.type:transaction !has:transaction.status",
  2587. "sort": "-count(id)",
  2588. "statsPeriod": "24h",
  2589. }
  2590. response = self.do_request(query, features=features)
  2591. assert response.status_code == 200, response.content
  2592. data = response.data["data"]
  2593. assert len(data) == 1
  2594. assert data[0]["count(id)"] == 0
  2595. def test_tag_that_looks_like_aggregation(self):
  2596. data = {
  2597. "message": "Failure state",
  2598. "timestamp": self.ten_mins_ago,
  2599. "tags": {"count_diff": 99},
  2600. }
  2601. self.store_event(data, project_id=self.project.id)
  2602. query = {
  2603. "field": ["message", "count_diff", "count()"],
  2604. "query": "",
  2605. "project": [self.project.id],
  2606. "statsPeriod": "24h",
  2607. }
  2608. response = self.do_request(query)
  2609. assert response.status_code == 200, response.content
  2610. meta = response.data["meta"]["fields"]
  2611. assert "string" == meta["count_diff"], "tags should not be counted as integers"
  2612. assert "string" == meta["message"]
  2613. assert "integer" == meta["count()"]
  2614. assert 1 == len(response.data["data"])
  2615. data = response.data["data"][0]
  2616. assert "99" == data["count_diff"]
  2617. assert "Failure state" == data["message"]
  2618. assert 1 == data["count()"]
  2619. def test_aggregate_negation(self):
  2620. data = load_data(
  2621. "transaction",
  2622. timestamp=before_now(minutes=10),
  2623. start_timestamp=before_now(minutes=10, seconds=5),
  2624. )
  2625. self.store_event(data, project_id=self.project.id)
  2626. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2627. query = {
  2628. "field": ["event.type", "count()"],
  2629. "query": "event.type:transaction count():1",
  2630. "statsPeriod": "24h",
  2631. }
  2632. response = self.do_request(query, features=features)
  2633. assert response.status_code == 200, response.content
  2634. data = response.data["data"]
  2635. assert len(data) == 1
  2636. query = {
  2637. "field": ["event.type", "count()"],
  2638. "query": "event.type:transaction !count():1",
  2639. "statsPeriod": "24h",
  2640. }
  2641. response = self.do_request(query, features=features)
  2642. assert response.status_code == 200, response.content
  2643. data = response.data["data"]
  2644. assert len(data) == 0
  2645. def test_all_aggregates_in_columns(self):
  2646. data = load_data(
  2647. "transaction",
  2648. timestamp=before_now(minutes=11),
  2649. start_timestamp=before_now(minutes=11, seconds=5),
  2650. )
  2651. data["transaction"] = "/failure_rate/1"
  2652. self.store_event(data, project_id=self.project.id)
  2653. data = load_data(
  2654. "transaction",
  2655. timestamp=before_now(minutes=10),
  2656. start_timestamp=before_now(minutes=10, seconds=5),
  2657. )
  2658. data["transaction"] = "/failure_rate/1"
  2659. data["contexts"]["trace"]["status"] = "unauthenticated"
  2660. event = self.store_event(data, project_id=self.project.id)
  2661. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2662. query = {
  2663. "field": [
  2664. "event.type",
  2665. "p50()",
  2666. "p75()",
  2667. "p95()",
  2668. "p99()",
  2669. "p100()",
  2670. "percentile(transaction.duration, 0.99)",
  2671. "apdex(300)",
  2672. "count_miserable(user, 300)",
  2673. "user_misery(300)",
  2674. "failure_rate()",
  2675. ],
  2676. "query": "event.type:transaction",
  2677. }
  2678. response = self.do_request(query, features=features)
  2679. assert response.status_code == 200, response.content
  2680. meta = response.data["meta"]["fields"]
  2681. units = response.data["meta"]["units"]
  2682. assert meta["p50()"] == "duration"
  2683. assert meta["p75()"] == "duration"
  2684. assert meta["p95()"] == "duration"
  2685. assert meta["p99()"] == "duration"
  2686. assert meta["p100()"] == "duration"
  2687. assert meta["percentile(transaction.duration, 0.99)"] == "duration"
  2688. assert meta["apdex(300)"] == "number"
  2689. assert meta["failure_rate()"] == "percentage"
  2690. assert meta["user_misery(300)"] == "number"
  2691. assert meta["count_miserable(user, 300)"] == "integer"
  2692. assert units["p50()"] == "millisecond"
  2693. assert units["p75()"] == "millisecond"
  2694. assert units["p95()"] == "millisecond"
  2695. assert units["p99()"] == "millisecond"
  2696. assert units["p100()"] == "millisecond"
  2697. assert units["percentile(transaction.duration, 0.99)"] == "millisecond"
  2698. data = response.data["data"]
  2699. assert len(data) == 1
  2700. assert data[0]["p50()"] == 5000
  2701. assert data[0]["p75()"] == 5000
  2702. assert data[0]["p95()"] == 5000
  2703. assert data[0]["p99()"] == 5000
  2704. assert data[0]["p100()"] == 5000
  2705. assert data[0]["percentile(transaction.duration, 0.99)"] == 5000
  2706. assert data[0]["apdex(300)"] == 0.0
  2707. assert data[0]["count_miserable(user, 300)"] == 1
  2708. assert data[0]["user_misery(300)"] == 0.058
  2709. assert data[0]["failure_rate()"] == 0.5
  2710. features = {
  2711. "organizations:discover-basic": True,
  2712. "organizations:global-views": True,
  2713. }
  2714. query = {
  2715. "field": [
  2716. "event.type",
  2717. "p50()",
  2718. "p75()",
  2719. "p95()",
  2720. "p99()",
  2721. "p100()",
  2722. "percentile(transaction.duration, 0.99)",
  2723. "apdex(300)",
  2724. "apdex()",
  2725. "count_miserable(user, 300)",
  2726. "user_misery(300)",
  2727. "failure_rate()",
  2728. "count_miserable(user)",
  2729. "user_misery()",
  2730. ],
  2731. "query": "event.type:transaction",
  2732. "project": [self.project.id],
  2733. }
  2734. response = self.do_request(query, features=features)
  2735. assert response.status_code == 200, response.content
  2736. meta = response.data["meta"]["fields"]
  2737. units = response.data["meta"]["units"]
  2738. assert meta["p50()"] == "duration"
  2739. assert meta["p75()"] == "duration"
  2740. assert meta["p95()"] == "duration"
  2741. assert meta["p99()"] == "duration"
  2742. assert meta["p100()"] == "duration"
  2743. assert meta["percentile(transaction.duration, 0.99)"] == "duration"
  2744. assert meta["apdex(300)"] == "number"
  2745. assert meta["apdex()"] == "number"
  2746. assert meta["failure_rate()"] == "percentage"
  2747. assert meta["user_misery(300)"] == "number"
  2748. assert meta["count_miserable(user, 300)"] == "integer"
  2749. assert meta["project_threshold_config"] == "string"
  2750. assert meta["user_misery()"] == "number"
  2751. assert meta["count_miserable(user)"] == "integer"
  2752. assert units["p50()"] == "millisecond"
  2753. assert units["p75()"] == "millisecond"
  2754. assert units["p95()"] == "millisecond"
  2755. assert units["p99()"] == "millisecond"
  2756. assert units["p100()"] == "millisecond"
  2757. assert units["percentile(transaction.duration, 0.99)"] == "millisecond"
  2758. data = response.data["data"]
  2759. assert len(data) == 1
  2760. assert data[0]["p50()"] == 5000
  2761. assert data[0]["p75()"] == 5000
  2762. assert data[0]["p95()"] == 5000
  2763. assert data[0]["p99()"] == 5000
  2764. assert data[0]["p100()"] == 5000
  2765. assert data[0]["percentile(transaction.duration, 0.99)"] == 5000
  2766. assert data[0]["apdex(300)"] == 0.0
  2767. assert data[0]["apdex()"] == 0.0
  2768. assert data[0]["count_miserable(user, 300)"] == 1
  2769. assert data[0]["user_misery(300)"] == 0.058
  2770. assert data[0]["failure_rate()"] == 0.5
  2771. assert data[0]["project_threshold_config"] == ["duration", 300]
  2772. assert data[0]["user_misery()"] == 0.058
  2773. assert data[0]["count_miserable(user)"] == 1
  2774. query = {
  2775. "field": ["event.type", "last_seen()", "latest_event()"],
  2776. "query": "event.type:transaction",
  2777. }
  2778. response = self.do_request(query, features=features)
  2779. assert response.status_code == 200, response.content
  2780. data = response.data["data"]
  2781. assert len(data) == 1
  2782. assert self.ten_mins_ago[:-5] in data[0]["last_seen()"]
  2783. assert data[0]["latest_event()"] == event.event_id
  2784. query = {
  2785. "field": [
  2786. "event.type",
  2787. "count()",
  2788. "count(id)",
  2789. "count_unique(project)",
  2790. "min(transaction.duration)",
  2791. "max(transaction.duration)",
  2792. "avg(transaction.duration)",
  2793. "stddev(transaction.duration)",
  2794. "var(transaction.duration)",
  2795. "cov(transaction.duration, transaction.duration)",
  2796. "corr(transaction.duration, transaction.duration)",
  2797. "sum(transaction.duration)",
  2798. ],
  2799. "query": "event.type:transaction",
  2800. }
  2801. response = self.do_request(query, features=features)
  2802. assert response.status_code == 200, response.content
  2803. data = response.data["data"]
  2804. assert len(data) == 1
  2805. assert data[0]["count()"] == 2
  2806. assert data[0]["count(id)"] == 2
  2807. assert data[0]["count_unique(project)"] == 1
  2808. assert data[0]["min(transaction.duration)"] == 5000
  2809. assert data[0]["max(transaction.duration)"] == 5000
  2810. assert data[0]["avg(transaction.duration)"] == 5000
  2811. assert data[0]["stddev(transaction.duration)"] == 0.0
  2812. assert data[0]["var(transaction.duration)"] == 0.0
  2813. assert data[0]["cov(transaction.duration, transaction.duration)"] == 0.0
  2814. assert data[0]["corr(transaction.duration, transaction.duration)"] == 0.0
  2815. assert data[0]["sum(transaction.duration)"] == 10000
  2816. @requires_not_arm64
  2817. def test_null_user_misery_returns_zero(self):
  2818. self.transaction_data["user"] = None
  2819. self.transaction_data["transaction"] = "/no_users/1"
  2820. self.store_event(self.transaction_data, project_id=self.project.id)
  2821. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2822. query = {
  2823. "field": ["user_misery(300)"],
  2824. "query": "event.type:transaction",
  2825. }
  2826. response = self.do_request(query, features=features)
  2827. assert response.status_code == 200, response.content
  2828. meta = response.data["meta"]["fields"]
  2829. assert meta["user_misery(300)"] == "number"
  2830. data = response.data["data"]
  2831. assert data[0]["user_misery(300)"] == 0
  2832. @requires_not_arm64
  2833. def test_null_user_misery_new_returns_zero(self):
  2834. self.transaction_data["user"] = None
  2835. self.transaction_data["transaction"] = "/no_users/1"
  2836. self.store_event(self.transaction_data, project_id=self.project.id)
  2837. features = {
  2838. "organizations:discover-basic": True,
  2839. }
  2840. query = {
  2841. "field": ["user_misery()"],
  2842. "query": "event.type:transaction",
  2843. }
  2844. response = self.do_request(query, features=features)
  2845. assert response.status_code == 200, response.content
  2846. meta = response.data["meta"]["fields"]
  2847. assert meta["user_misery()"] == "number"
  2848. data = response.data["data"]
  2849. assert data[0]["user_misery()"] == 0
  2850. def test_all_aggregates_in_query(self):
  2851. data = load_data(
  2852. "transaction",
  2853. timestamp=before_now(minutes=11),
  2854. start_timestamp=before_now(minutes=11, seconds=5),
  2855. )
  2856. data["transaction"] = "/failure_rate/1"
  2857. self.store_event(data, project_id=self.project.id)
  2858. data = load_data(
  2859. "transaction",
  2860. timestamp=before_now(minutes=10),
  2861. start_timestamp=before_now(minutes=10, seconds=5),
  2862. )
  2863. data["transaction"] = "/failure_rate/2"
  2864. data["contexts"]["trace"]["status"] = "unauthenticated"
  2865. self.store_event(data, project_id=self.project.id)
  2866. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2867. query = {
  2868. "field": [
  2869. "event.type",
  2870. "p50()",
  2871. "p75()",
  2872. "p95()",
  2873. "percentile(transaction.duration, 0.99)",
  2874. "p100()",
  2875. ],
  2876. "query": "event.type:transaction p50():>100 p75():>1000 p95():>1000 p100():>1000 percentile(transaction.duration, 0.99):>1000",
  2877. }
  2878. response = self.do_request(query, features=features)
  2879. assert response.status_code == 200, response.content
  2880. data = response.data["data"]
  2881. assert len(data) == 1
  2882. assert data[0]["p50()"] == 5000
  2883. assert data[0]["p75()"] == 5000
  2884. assert data[0]["p95()"] == 5000
  2885. assert data[0]["p100()"] == 5000
  2886. assert data[0]["percentile(transaction.duration, 0.99)"] == 5000
  2887. query = {
  2888. "field": [
  2889. "event.type",
  2890. "apdex(300)",
  2891. "count_miserable(user, 300)",
  2892. "user_misery(300)",
  2893. "failure_rate()",
  2894. ],
  2895. "query": "event.type:transaction apdex(300):>-1.0 failure_rate():>0.25",
  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. assert data[0]["count_miserable(user, 300)"] == 1
  2903. assert data[0]["user_misery(300)"] == 0.058
  2904. assert data[0]["failure_rate()"] == 0.5
  2905. query = {
  2906. "field": ["event.type", "last_seen()", "latest_event()"],
  2907. "query": "event.type:transaction last_seen():>1990-12-01T00:00:00",
  2908. }
  2909. response = self.do_request(query, features=features)
  2910. assert response.status_code == 200, response.content
  2911. data = response.data["data"]
  2912. assert len(data) == 1
  2913. query = {
  2914. "field": ["event.type", "count()", "count(id)", "count_unique(transaction)"],
  2915. "query": "event.type:transaction count():>1 count(id):>1 count_unique(transaction):>1",
  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()"] == 2
  2922. assert data[0]["count(id)"] == 2
  2923. assert data[0]["count_unique(transaction)"] == 2
  2924. query = {
  2925. "field": [
  2926. "event.type",
  2927. "min(transaction.duration)",
  2928. "max(transaction.duration)",
  2929. "avg(transaction.duration)",
  2930. "sum(transaction.duration)",
  2931. "stddev(transaction.duration)",
  2932. "var(transaction.duration)",
  2933. "cov(transaction.duration, transaction.duration)",
  2934. "corr(transaction.duration, transaction.duration)",
  2935. ],
  2936. "query": " ".join(
  2937. [
  2938. "event.type:transaction",
  2939. "min(transaction.duration):>1000",
  2940. "max(transaction.duration):>1000",
  2941. "avg(transaction.duration):>1000",
  2942. "sum(transaction.duration):>1000",
  2943. "stddev(transaction.duration):>=0.0",
  2944. "var(transaction.duration):>=0.0",
  2945. "cov(transaction.duration, transaction.duration):>=0.0",
  2946. # correlation is nan because variance is 0
  2947. # "corr(transaction.duration, transaction.duration):>=0.0",
  2948. ]
  2949. ),
  2950. }
  2951. response = self.do_request(query, features=features)
  2952. assert response.status_code == 200, response.content
  2953. data = response.data["data"]
  2954. assert len(data) == 1
  2955. assert data[0]["min(transaction.duration)"] == 5000
  2956. assert data[0]["max(transaction.duration)"] == 5000
  2957. assert data[0]["avg(transaction.duration)"] == 5000
  2958. assert data[0]["sum(transaction.duration)"] == 10000
  2959. assert data[0]["stddev(transaction.duration)"] == 0.0
  2960. assert data[0]["var(transaction.duration)"] == 0.0
  2961. assert data[0]["cov(transaction.duration, transaction.duration)"] == 0.0
  2962. assert data[0]["corr(transaction.duration, transaction.duration)"] == 0.0
  2963. query = {
  2964. "field": ["event.type", "apdex(400)"],
  2965. "query": "event.type:transaction apdex(400):0",
  2966. }
  2967. response = self.do_request(query, features=features)
  2968. assert response.status_code == 200, response.content
  2969. data = response.data["data"]
  2970. assert len(data) == 1
  2971. assert data[0]["apdex(400)"] == 0
  2972. def test_functions_in_orderby(self):
  2973. data = load_data(
  2974. "transaction",
  2975. timestamp=before_now(minutes=11),
  2976. start_timestamp=before_now(minutes=11, seconds=5),
  2977. )
  2978. data["transaction"] = "/failure_rate/1"
  2979. self.store_event(data, project_id=self.project.id)
  2980. data = load_data(
  2981. "transaction",
  2982. timestamp=before_now(minutes=10),
  2983. start_timestamp=before_now(minutes=10, seconds=5),
  2984. )
  2985. data["transaction"] = "/failure_rate/2"
  2986. data["contexts"]["trace"]["status"] = "unauthenticated"
  2987. event = self.store_event(data, project_id=self.project.id)
  2988. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2989. query = {
  2990. "field": ["event.type", "p75()"],
  2991. "sort": "-p75",
  2992. "query": "event.type:transaction",
  2993. }
  2994. response = self.do_request(query, features=features)
  2995. assert response.status_code == 200, response.content
  2996. data = response.data["data"]
  2997. assert len(data) == 1
  2998. assert data[0]["p75()"] == 5000
  2999. query = {
  3000. "field": ["event.type", "percentile(transaction.duration, 0.99)"],
  3001. "sort": "-percentile_transaction_duration_0_99",
  3002. "query": "event.type:transaction",
  3003. }
  3004. response = self.do_request(query, features=features)
  3005. assert response.status_code == 200, response.content
  3006. data = response.data["data"]
  3007. assert len(data) == 1
  3008. assert data[0]["percentile(transaction.duration, 0.99)"] == 5000
  3009. query = {
  3010. "field": ["event.type", "apdex(300)"],
  3011. "sort": "-apdex(300)",
  3012. "query": "event.type:transaction",
  3013. }
  3014. response = self.do_request(query, features=features)
  3015. assert response.status_code == 200, response.content
  3016. data = response.data["data"]
  3017. assert len(data) == 1
  3018. assert data[0]["apdex(300)"] == 0.0
  3019. query = {
  3020. "field": ["event.type", "latest_event()"],
  3021. "query": "event.type:transaction",
  3022. "sort": "latest_event",
  3023. }
  3024. response = self.do_request(query, features=features)
  3025. assert response.status_code == 200, response.content
  3026. data = response.data["data"]
  3027. assert len(data) == 1
  3028. assert data[0]["latest_event()"] == event.event_id
  3029. query = {
  3030. "field": ["event.type", "count_unique(transaction)"],
  3031. "query": "event.type:transaction",
  3032. "sort": "-count_unique_transaction",
  3033. }
  3034. response = self.do_request(query, features=features)
  3035. assert response.status_code == 200, response.content
  3036. data = response.data["data"]
  3037. assert len(data) == 1
  3038. assert data[0]["count_unique(transaction)"] == 2
  3039. query = {
  3040. "field": ["event.type", "min(transaction.duration)"],
  3041. "query": "event.type:transaction",
  3042. "sort": "-min_transaction_duration",
  3043. }
  3044. response = self.do_request(query, features=features)
  3045. assert response.status_code == 200, response.content
  3046. data = response.data["data"]
  3047. assert len(data) == 1
  3048. assert data[0]["min(transaction.duration)"] == 5000
  3049. def test_issue_alias_in_aggregate(self):
  3050. self.store_event(
  3051. data={
  3052. "event_id": "a" * 32,
  3053. "timestamp": self.eleven_mins_ago,
  3054. "fingerprint": ["group_1"],
  3055. },
  3056. project_id=self.project.id,
  3057. )
  3058. self.store_event(
  3059. data={"event_id": "b" * 32, "timestamp": self.ten_mins_ago, "fingerprint": ["group_2"]},
  3060. project_id=self.project.id,
  3061. )
  3062. query = {"field": ["event.type", "count_unique(issue)"], "query": "count_unique(issue):>1"}
  3063. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3064. response = self.do_request(query, features=features)
  3065. assert response.status_code == 200, response.content
  3066. data = response.data["data"]
  3067. assert len(data) == 1
  3068. assert data[0]["count_unique(issue)"] == 2
  3069. def test_deleted_issue_in_results(self):
  3070. event1 = self.store_event(
  3071. data={
  3072. "event_id": "a" * 32,
  3073. "timestamp": self.eleven_mins_ago,
  3074. "fingerprint": ["group_1"],
  3075. },
  3076. project_id=self.project.id,
  3077. )
  3078. event2 = self.store_event(
  3079. data={"event_id": "b" * 32, "timestamp": self.ten_mins_ago, "fingerprint": ["group_2"]},
  3080. project_id=self.project.id,
  3081. )
  3082. event2.group.delete()
  3083. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3084. query = {"field": ["issue", "count()"], "sort": "issue.id"}
  3085. response = self.do_request(query, features=features)
  3086. assert response.status_code == 200, response.content
  3087. data = response.data["data"]
  3088. assert len(data) == 2
  3089. assert data[0]["issue"] == event1.group.qualified_short_id
  3090. assert data[1]["issue"] == "unknown"
  3091. def test_last_seen_negative_duration(self):
  3092. self.store_event(
  3093. data={"event_id": "f" * 32, "timestamp": self.ten_mins_ago, "fingerprint": ["group_1"]},
  3094. project_id=self.project.id,
  3095. )
  3096. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3097. query = {"field": ["id", "last_seen()"], "query": "last_seen():-30d"}
  3098. response = self.do_request(query, features=features)
  3099. assert response.status_code == 200, response.content
  3100. data = response.data["data"]
  3101. assert len(data) == 1
  3102. assert data[0]["id"] == "f" * 32
  3103. def test_last_seen_aggregate_condition(self):
  3104. self.store_event(
  3105. data={"event_id": "f" * 32, "timestamp": self.ten_mins_ago, "fingerprint": ["group_1"]},
  3106. project_id=self.project.id,
  3107. )
  3108. query = {
  3109. "field": ["id", "last_seen()"],
  3110. "query": f"last_seen():>{iso_format(before_now(days=30))}",
  3111. }
  3112. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3113. response = self.do_request(query, features=features)
  3114. assert response.status_code == 200, response.content
  3115. data = response.data["data"]
  3116. assert len(data) == 1
  3117. assert data[0]["id"] == "f" * 32
  3118. def test_conditional_filter(self):
  3119. for v in ["a", "b"]:
  3120. self.store_event(
  3121. data={
  3122. "event_id": v * 32,
  3123. "timestamp": self.ten_mins_ago,
  3124. "fingerprint": ["group_1"],
  3125. },
  3126. project_id=self.project.id,
  3127. )
  3128. query = {
  3129. "field": ["id"],
  3130. "query": "id:{} OR id:{}".format("a" * 32, "b" * 32),
  3131. "orderby": "id",
  3132. }
  3133. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3134. response = self.do_request(query, features=features)
  3135. assert response.status_code == 200, response.content
  3136. data = response.data["data"]
  3137. assert len(data) == 2
  3138. assert data[0]["id"] == "a" * 32
  3139. assert data[1]["id"] == "b" * 32
  3140. def test_aggregation_comparison_with_conditional_filter(self):
  3141. self.store_event(
  3142. data={
  3143. "event_id": "a" * 32,
  3144. "timestamp": self.ten_mins_ago,
  3145. "fingerprint": ["group_1"],
  3146. "user": {"email": "foo@example.com"},
  3147. "environment": "prod",
  3148. },
  3149. project_id=self.project.id,
  3150. )
  3151. self.store_event(
  3152. data={
  3153. "event_id": "b" * 32,
  3154. "timestamp": self.ten_mins_ago,
  3155. "fingerprint": ["group_2"],
  3156. "user": {"email": "foo@example.com"},
  3157. "environment": "staging",
  3158. },
  3159. project_id=self.project.id,
  3160. )
  3161. event = self.store_event(
  3162. data={
  3163. "event_id": "c" * 32,
  3164. "timestamp": self.ten_mins_ago,
  3165. "fingerprint": ["group_2"],
  3166. "user": {"email": "foo@example.com"},
  3167. "environment": "prod",
  3168. },
  3169. project_id=self.project.id,
  3170. )
  3171. self.store_event(
  3172. data={
  3173. "event_id": "d" * 32,
  3174. "timestamp": self.ten_mins_ago,
  3175. "fingerprint": ["group_2"],
  3176. "user": {"email": "foo@example.com"},
  3177. "environment": "canary",
  3178. },
  3179. project_id=self.project.id,
  3180. )
  3181. query = {
  3182. "field": ["issue.id", "count(id)"],
  3183. "query": "count(id):>1 user.email:foo@example.com AND (environment:prod OR environment:staging)",
  3184. "orderby": "issue.id",
  3185. }
  3186. response = self.do_request(query)
  3187. assert response.status_code == 200, response.content
  3188. assert len(response.data["data"]) == 1
  3189. data = response.data["data"]
  3190. assert data[0]["issue.id"] == event.group_id
  3191. assert data[0]["count(id)"] == 2
  3192. def run_test_in_query(self, query, expected_events, expected_negative_events=None):
  3193. params = {
  3194. "field": ["id"],
  3195. "query": query,
  3196. "orderby": "id",
  3197. }
  3198. response = self.do_request(
  3199. params, {"organizations:discover-basic": True, "organizations:global-views": True}
  3200. )
  3201. assert response.status_code == 200, response.content
  3202. assert [row["id"] for row in response.data["data"]] == [e.event_id for e in expected_events]
  3203. if expected_negative_events is not None:
  3204. params["query"] = f"!{query}"
  3205. response = self.do_request(
  3206. params,
  3207. {"organizations:discover-basic": True, "organizations:global-views": True},
  3208. )
  3209. assert response.status_code == 200, response.content
  3210. assert [row["id"] for row in response.data["data"]] == [
  3211. e.event_id for e in expected_negative_events
  3212. ]
  3213. def test_in_query_events(self):
  3214. project_1 = self.create_project()
  3215. event_1 = self.store_event(
  3216. data={
  3217. "event_id": "a" * 32,
  3218. "timestamp": self.ten_mins_ago,
  3219. "fingerprint": ["group_1"],
  3220. "message": "group1",
  3221. "user": {"email": "hello@example.com"},
  3222. "environment": "prod",
  3223. "tags": {"random": "123"},
  3224. "release": "1.0",
  3225. },
  3226. project_id=project_1.id,
  3227. )
  3228. project_2 = self.create_project()
  3229. event_2 = self.store_event(
  3230. data={
  3231. "event_id": "b" * 32,
  3232. "timestamp": self.ten_mins_ago,
  3233. "fingerprint": ["group_2"],
  3234. "message": "group2",
  3235. "user": {"email": "bar@example.com"},
  3236. "environment": "staging",
  3237. "tags": {"random": "456"},
  3238. "stacktrace": {"frames": [{"filename": "src/app/group2.py"}]},
  3239. "release": "1.2",
  3240. },
  3241. project_id=project_2.id,
  3242. )
  3243. project_3 = self.create_project()
  3244. event_3 = self.store_event(
  3245. data={
  3246. "event_id": "c" * 32,
  3247. "timestamp": self.ten_mins_ago,
  3248. "fingerprint": ["group_3"],
  3249. "message": "group3",
  3250. "user": {"email": "foo@example.com"},
  3251. "environment": "canary",
  3252. "tags": {"random": "789"},
  3253. },
  3254. project_id=project_3.id,
  3255. )
  3256. self.run_test_in_query("environment:[prod, staging]", [event_1, event_2], [event_3])
  3257. self.run_test_in_query("environment:[staging]", [event_2], [event_1, event_3])
  3258. self.run_test_in_query(
  3259. "user.email:[foo@example.com, hello@example.com]", [event_1, event_3], [event_2]
  3260. )
  3261. self.run_test_in_query("user.email:[foo@example.com]", [event_3], [event_1, event_2])
  3262. self.run_test_in_query(
  3263. "user.display:[foo@example.com, hello@example.com]", [event_1, event_3], [event_2]
  3264. )
  3265. self.run_test_in_query(
  3266. 'message:["group2 src/app/group2.py in ?", group1]', [event_1, event_2], [event_3]
  3267. )
  3268. self.run_test_in_query(
  3269. f"issue.id:[{event_1.group_id},{event_2.group_id}]", [event_1, event_2]
  3270. )
  3271. self.run_test_in_query(
  3272. f"issue:[{event_1.group.qualified_short_id},{event_2.group.qualified_short_id}]",
  3273. [event_1, event_2],
  3274. )
  3275. self.run_test_in_query(
  3276. f"issue:[{event_1.group.qualified_short_id},{event_2.group.qualified_short_id}, unknown]",
  3277. [event_1, event_2],
  3278. )
  3279. self.run_test_in_query(f"project_id:[{project_3.id},{project_2.id}]", [event_2, event_3])
  3280. self.run_test_in_query(
  3281. f"project.name:[{project_3.slug},{project_2.slug}]", [event_2, event_3]
  3282. )
  3283. self.run_test_in_query("random:[789,456]", [event_2, event_3], [event_1])
  3284. self.run_test_in_query("tags[random]:[789,456]", [event_2, event_3], [event_1])
  3285. self.run_test_in_query("release:[1.0,1.2]", [event_1, event_2], [event_3])
  3286. def test_in_query_events_stack(self):
  3287. test_js = self.store_event(
  3288. load_data(
  3289. "javascript",
  3290. timestamp=before_now(minutes=10),
  3291. start_timestamp=before_now(minutes=10, seconds=5),
  3292. ),
  3293. project_id=self.project.id,
  3294. )
  3295. test_java = self.store_event(
  3296. load_data(
  3297. "java",
  3298. timestamp=before_now(minutes=10),
  3299. start_timestamp=before_now(minutes=10, seconds=5),
  3300. ),
  3301. project_id=self.project.id,
  3302. )
  3303. self.run_test_in_query(
  3304. "stack.filename:[../../sentry/scripts/views.js]", [test_js], [test_java]
  3305. )
  3306. def test_in_query_transactions(self):
  3307. data = self.transaction_data.copy()
  3308. data["event_id"] = "a" * 32
  3309. data["contexts"]["trace"]["status"] = "ok"
  3310. transaction_1 = self.store_event(data, project_id=self.project.id)
  3311. data = self.transaction_data.copy()
  3312. data["event_id"] = "b" * 32
  3313. data["contexts"]["trace"]["status"] = "aborted"
  3314. transaction_2 = self.store_event(data, project_id=self.project.id)
  3315. data = self.transaction_data.copy()
  3316. data["event_id"] = "c" * 32
  3317. data["contexts"]["trace"]["status"] = "already_exists"
  3318. transaction_3 = self.store_event(data, project_id=self.project.id)
  3319. self.run_test_in_query(
  3320. "transaction.status:[aborted, already_exists]",
  3321. [transaction_2, transaction_3],
  3322. [transaction_1],
  3323. )
  3324. def test_messed_up_function_values(self):
  3325. # TODO (evanh): It would be nice if this surfaced an error to the user.
  3326. # The problem: The && causes the parser to treat that term not as a bad
  3327. # function call but a valid raw search with parens in it. It's not trivial
  3328. # to change the parser to recognize "bad function values" and surface them.
  3329. for v in ["a", "b"]:
  3330. self.store_event(
  3331. data={
  3332. "event_id": v * 32,
  3333. "timestamp": self.ten_mins_ago,
  3334. "fingerprint": ["group_1"],
  3335. },
  3336. project_id=self.project.id,
  3337. )
  3338. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3339. query = {
  3340. "field": [
  3341. "transaction",
  3342. "project",
  3343. "epm()",
  3344. "p50()",
  3345. "p95()",
  3346. "failure_rate()",
  3347. "apdex(300)",
  3348. "count_unique(user)",
  3349. "user_misery(300)",
  3350. "count_miserable(user, 300)",
  3351. ],
  3352. "query": "failure_rate():>0.003&& users:>10 event.type:transaction",
  3353. "sort": "-failure_rate",
  3354. "statsPeriod": "24h",
  3355. }
  3356. response = self.do_request(query, features=features)
  3357. assert response.status_code == 200, response.content
  3358. data = response.data["data"]
  3359. assert len(data) == 0
  3360. def test_context_fields_between_datasets(self):
  3361. event_data = load_data("android")
  3362. transaction_data = load_data("transaction")
  3363. event_data["spans"] = transaction_data["spans"]
  3364. event_data["contexts"]["trace"] = transaction_data["contexts"]["trace"]
  3365. event_data["type"] = "transaction"
  3366. event_data["transaction"] = "/failure_rate/1"
  3367. event_data["timestamp"] = iso_format(before_now(minutes=10))
  3368. event_data["start_timestamp"] = iso_format(before_now(minutes=10, seconds=5))
  3369. event_data["user"]["geo"] = {"country_code": "US", "region": "CA", "city": "San Francisco"}
  3370. self.store_event(event_data, project_id=self.project.id)
  3371. event_data["type"] = "error"
  3372. self.store_event(event_data, project_id=self.project.id)
  3373. fields = [
  3374. "os.build",
  3375. "os.kernel_version",
  3376. "device.arch",
  3377. # TODO: battery level is not consistent across both datasets
  3378. # "device.battery_level",
  3379. "device.brand",
  3380. "device.charging",
  3381. "device.locale",
  3382. "device.model_id",
  3383. "device.name",
  3384. "device.online",
  3385. "device.orientation",
  3386. "device.simulator",
  3387. "device.uuid",
  3388. ]
  3389. data = [
  3390. {"field": fields + ["location", "count()"], "query": "event.type:error"},
  3391. {"field": fields + ["duration", "count()"], "query": "event.type:transaction"},
  3392. ]
  3393. for datum in data:
  3394. response = self.do_request(datum)
  3395. assert response.status_code == 200, response.content
  3396. assert len(response.data["data"]) == 1, datum
  3397. results = response.data["data"]
  3398. assert results[0]["count()"] == 1, datum
  3399. for field in fields:
  3400. key, value = field.split(".", 1)
  3401. expected = str(event_data["contexts"][key][value])
  3402. assert results[0][field] == expected, field + str(datum)
  3403. def test_http_fields_between_datasets(self):
  3404. event_data = load_data("android")
  3405. transaction_data = load_data("transaction")
  3406. event_data["spans"] = transaction_data["spans"]
  3407. event_data["contexts"]["trace"] = transaction_data["contexts"]["trace"]
  3408. event_data["type"] = "transaction"
  3409. event_data["transaction"] = "/failure_rate/1"
  3410. event_data["timestamp"] = iso_format(before_now(minutes=10))
  3411. event_data["start_timestamp"] = iso_format(before_now(minutes=10, seconds=5))
  3412. event_data["user"]["geo"] = {"country_code": "US", "region": "CA", "city": "San Francisco"}
  3413. event_data["request"] = transaction_data["request"]
  3414. self.store_event(event_data, project_id=self.project.id)
  3415. event_data["type"] = "error"
  3416. self.store_event(event_data, project_id=self.project.id)
  3417. fields = ["http.method", "http.referer", "http.url"]
  3418. expected = ["GET", "fixtures.transaction", "http://countries:8010/country_by_code/"]
  3419. data = [
  3420. {"field": fields + ["location", "count()"], "query": "event.type:error"},
  3421. {"field": fields + ["duration", "count()"], "query": "event.type:transaction"},
  3422. ]
  3423. for datum in data:
  3424. response = self.do_request(datum)
  3425. assert response.status_code == 200, response.content
  3426. assert len(response.data["data"]) == 1, datum
  3427. results = response.data["data"]
  3428. assert results[0]["count()"] == 1, datum
  3429. for (field, exp) in zip(fields, expected):
  3430. assert results[0][field] == exp, field + str(datum)
  3431. def test_failure_count_alias_field(self):
  3432. data = self.transaction_data.copy()
  3433. data["transaction"] = "/failure_count/success"
  3434. self.store_event(data, project_id=self.project.id)
  3435. data = self.transaction_data.copy()
  3436. data["transaction"] = "/failure_count/unknown"
  3437. data["contexts"]["trace"]["status"] = "unknown_error"
  3438. self.store_event(data, project_id=self.project.id)
  3439. for i in range(6):
  3440. data = self.transaction_data.copy()
  3441. data["transaction"] = f"/failure_count/{i}"
  3442. data["contexts"]["trace"]["status"] = "unauthenticated"
  3443. self.store_event(data, project_id=self.project.id)
  3444. query = {"field": ["count()", "failure_count()"], "query": "event.type:transaction"}
  3445. response = self.do_request(query)
  3446. assert response.status_code == 200, response.content
  3447. assert len(response.data["data"]) == 1
  3448. data = response.data["data"]
  3449. assert data[0]["count()"] == 8
  3450. assert data[0]["failure_count()"] == 6
  3451. @mock.patch("sentry.utils.snuba.quantize_time")
  3452. def test_quantize_dates(self, mock_quantize):
  3453. self.create_project()
  3454. mock_quantize.return_value = before_now(days=1).replace(tzinfo=utc)
  3455. # Don't quantize short time periods
  3456. query = {"statsPeriod": "1h", "query": "", "field": ["id", "timestamp"]}
  3457. self.do_request(query)
  3458. # Don't quantize absolute date periods
  3459. self.do_request(query)
  3460. query = {
  3461. "start": iso_format(before_now(days=20)),
  3462. "end": iso_format(before_now(days=15)),
  3463. "query": "",
  3464. "field": ["id", "timestamp"],
  3465. }
  3466. self.do_request(query)
  3467. assert len(mock_quantize.mock_calls) == 0
  3468. # Quantize long date periods
  3469. query = {"field": ["id", "timestamp"], "statsPeriod": "90d", "query": ""}
  3470. self.do_request(query)
  3471. assert len(mock_quantize.mock_calls) == 2
  3472. @mock.patch("sentry.snuba.discover.query")
  3473. def test_valid_referrer(self, mock):
  3474. mock.return_value = {}
  3475. query = {
  3476. "field": ["user"],
  3477. "referrer": "api.performance.transaction-summary",
  3478. "project": [self.project.id],
  3479. }
  3480. self.do_request(query)
  3481. _, kwargs = mock.call_args
  3482. self.assertEqual(kwargs["referrer"], "api.performance.transaction-summary")
  3483. @mock.patch("sentry.snuba.discover.query")
  3484. def test_invalid_referrer(self, mock):
  3485. mock.return_value = {}
  3486. query = {
  3487. "field": ["user"],
  3488. "referrer": "api.performance.invalid",
  3489. "project": [self.project.id],
  3490. }
  3491. self.do_request(query)
  3492. _, kwargs = mock.call_args
  3493. self.assertEqual(kwargs["referrer"], self.referrer)
  3494. @mock.patch("sentry.snuba.discover.query")
  3495. def test_empty_referrer(self, mock):
  3496. mock.return_value = {}
  3497. query = {
  3498. "field": ["user"],
  3499. "project": [self.project.id],
  3500. }
  3501. self.do_request(query)
  3502. _, kwargs = mock.call_args
  3503. self.assertEqual(kwargs["referrer"], self.referrer)
  3504. @mock.patch("sentry.snuba.discover.query")
  3505. def test_api_token_referrer(self, mock):
  3506. mock.return_value = {}
  3507. # Project ID cannot be inferred when using an org API key, so that must
  3508. # be passed in the parameters
  3509. api_key = ApiKey.objects.create(organization=self.organization, scope_list=["org:read"])
  3510. query = {
  3511. "field": ["project.name", "environment"],
  3512. "project": [self.project.id],
  3513. }
  3514. features = {"organizations:discover-basic": True}
  3515. features.update(self.features)
  3516. url = self.reverse_url()
  3517. with self.feature(features):
  3518. self.client_get(
  3519. url,
  3520. query,
  3521. format="json",
  3522. HTTP_AUTHORIZATION=b"Basic " + b64encode(f"{api_key.key}:".encode()),
  3523. )
  3524. _, kwargs = mock.call_args
  3525. self.assertEqual(kwargs["referrer"], "api.auth-token.events")
  3526. def test_limit_number_of_fields(self):
  3527. self.create_project()
  3528. for i in range(1, 25):
  3529. response = self.do_request({"field": ["id"] * i})
  3530. if i <= 20:
  3531. assert response.status_code == 200
  3532. else:
  3533. assert response.status_code == 400
  3534. assert (
  3535. response.data["detail"]
  3536. == "You can view up to 20 fields at a time. Please delete some and try again."
  3537. )
  3538. def test_percentile_function_meta_types(self):
  3539. self.store_event(self.transaction_data, project_id=self.project.id)
  3540. query = {
  3541. "field": [
  3542. "transaction",
  3543. "percentile(transaction.duration, 0.95)",
  3544. "percentile(measurements.fp, 0.95)",
  3545. "percentile(measurements.fcp, 0.95)",
  3546. "percentile(measurements.lcp, 0.95)",
  3547. "percentile(measurements.fid, 0.95)",
  3548. "percentile(measurements.ttfb, 0.95)",
  3549. "percentile(measurements.ttfb.requesttime, 0.95)",
  3550. "percentile(measurements.cls, 0.95)",
  3551. "percentile(measurements.foo, 0.95)",
  3552. "percentile(measurements.bar, 0.95)",
  3553. ],
  3554. "query": "",
  3555. "orderby": ["transaction"],
  3556. }
  3557. response = self.do_request(query)
  3558. assert response.status_code == 200, response.content
  3559. meta = response.data["meta"]["fields"]
  3560. assert meta["percentile(transaction.duration, 0.95)"] == "duration"
  3561. assert meta["percentile(measurements.fp, 0.95)"] == "duration"
  3562. assert meta["percentile(measurements.fcp, 0.95)"] == "duration"
  3563. assert meta["percentile(measurements.lcp, 0.95)"] == "duration"
  3564. assert meta["percentile(measurements.fid, 0.95)"] == "duration"
  3565. assert meta["percentile(measurements.ttfb, 0.95)"] == "duration"
  3566. assert meta["percentile(measurements.ttfb.requesttime, 0.95)"] == "duration"
  3567. assert meta["percentile(measurements.cls, 0.95)"] == "number"
  3568. assert meta["percentile(measurements.foo, 0.95)"] == "number"
  3569. assert meta["percentile(measurements.bar, 0.95)"] == "number"
  3570. units = response.data["meta"]["units"]
  3571. assert units["percentile(transaction.duration, 0.95)"] == "millisecond"
  3572. assert units["percentile(measurements.fp, 0.95)"] == "millisecond"
  3573. assert units["percentile(measurements.fcp, 0.95)"] == "millisecond"
  3574. assert units["percentile(measurements.lcp, 0.95)"] == "millisecond"
  3575. assert units["percentile(measurements.fid, 0.95)"] == "millisecond"
  3576. assert units["percentile(measurements.ttfb, 0.95)"] == "millisecond"
  3577. assert units["percentile(measurements.ttfb.requesttime, 0.95)"] == "millisecond"
  3578. def test_count_at_least_query(self):
  3579. self.store_event(self.transaction_data, self.project.id)
  3580. response = self.do_request({"field": "count_at_least(measurements.fcp, 0)"})
  3581. assert response.status_code == 200
  3582. assert len(response.data["data"]) == 1
  3583. assert response.data["data"][0]["count_at_least(measurements.fcp, 0)"] == 1
  3584. # a value that's a little bigger than the stored fcp
  3585. fcp = int(self.transaction_data["measurements"]["fcp"]["value"] + 1)
  3586. response = self.do_request({"field": f"count_at_least(measurements.fcp, {fcp})"})
  3587. assert response.status_code == 200
  3588. assert len(response.data["data"]) == 1
  3589. assert response.data["data"][0][f"count_at_least(measurements.fcp, {fcp})"] == 0
  3590. def test_measurements_query(self):
  3591. self.store_event(self.transaction_data, self.project.id)
  3592. query = {
  3593. "field": [
  3594. "measurements.fp",
  3595. "measurements.fcp",
  3596. "measurements.lcp",
  3597. "measurements.fid",
  3598. ]
  3599. }
  3600. response = self.do_request(query)
  3601. assert response.status_code == 200, response.content
  3602. assert len(response.data["data"]) == 1
  3603. for field in query["field"]:
  3604. measure = field.split(".", 1)[1]
  3605. assert (
  3606. response.data["data"][0][field]
  3607. == self.transaction_data["measurements"][measure]["value"]
  3608. )
  3609. query = {
  3610. "field": [
  3611. "measurements.fP",
  3612. "measurements.Fcp",
  3613. "measurements.LcP",
  3614. "measurements.FID",
  3615. ]
  3616. }
  3617. response = self.do_request(query)
  3618. assert response.status_code == 200, response.content
  3619. assert len(response.data["data"]) == 1
  3620. for field in query["field"]:
  3621. measure = field.split(".", 1)[1].lower()
  3622. assert (
  3623. response.data["data"][0][field]
  3624. == self.transaction_data["measurements"][measure]["value"]
  3625. )
  3626. def test_measurements_aggregations(self):
  3627. self.store_event(self.transaction_data, self.project.id)
  3628. # should try all the potential aggregates
  3629. # Skipped tests for stddev and var since sampling one data point
  3630. # results in nan.
  3631. query = {
  3632. "field": [
  3633. "percentile(measurements.fcp, 0.5)",
  3634. "count_unique(measurements.fcp)",
  3635. "min(measurements.fcp)",
  3636. "max(measurements.fcp)",
  3637. "avg(measurements.fcp)",
  3638. "sum(measurements.fcp)",
  3639. ],
  3640. }
  3641. response = self.do_request(query)
  3642. assert response.status_code == 200, response.content
  3643. assert len(response.data["data"]) == 1
  3644. assert (
  3645. response.data["data"][0]["percentile(measurements.fcp, 0.5)"]
  3646. == self.transaction_data["measurements"]["fcp"]["value"]
  3647. )
  3648. assert response.data["data"][0]["count_unique(measurements.fcp)"] == 1
  3649. assert (
  3650. response.data["data"][0]["min(measurements.fcp)"]
  3651. == self.transaction_data["measurements"]["fcp"]["value"]
  3652. )
  3653. assert (
  3654. response.data["data"][0]["max(measurements.fcp)"]
  3655. == self.transaction_data["measurements"]["fcp"]["value"]
  3656. )
  3657. assert (
  3658. response.data["data"][0]["avg(measurements.fcp)"]
  3659. == self.transaction_data["measurements"]["fcp"]["value"]
  3660. )
  3661. assert (
  3662. response.data["data"][0]["sum(measurements.fcp)"]
  3663. == self.transaction_data["measurements"]["fcp"]["value"]
  3664. )
  3665. def get_measurement_condition_response(self, query_str, field):
  3666. query = {
  3667. "field": ["transaction", "count()"] + (field if field else []),
  3668. "query": query_str,
  3669. }
  3670. response = self.do_request(query)
  3671. assert response.status_code == 200, response.content
  3672. return response
  3673. def assert_measurement_condition_without_results(self, query_str, field=None):
  3674. response = self.get_measurement_condition_response(query_str, field)
  3675. assert len(response.data["data"]) == 0
  3676. def assert_measurement_condition_with_results(self, query_str, field=None):
  3677. response = self.get_measurement_condition_response(query_str, field)
  3678. assert len(response.data["data"]) == 1
  3679. assert response.data["data"][0]["transaction"] == self.transaction_data["metadata"]["title"]
  3680. assert response.data["data"][0]["count()"] == 1
  3681. def test_measurements_conditions(self):
  3682. self.store_event(self.transaction_data, self.project.id)
  3683. fcp = self.transaction_data["measurements"]["fcp"]["value"]
  3684. # equality condition
  3685. # We use json dumps here to ensure precision when converting from float to str
  3686. # This is necessary because equality on floating point values need to be precise
  3687. self.assert_measurement_condition_with_results(f"measurements.fcp:{json.dumps(fcp)}")
  3688. # greater than condition
  3689. self.assert_measurement_condition_with_results(f"measurements.fcp:>{fcp - 1}")
  3690. self.assert_measurement_condition_without_results(f"measurements.fcp:>{fcp + 1}")
  3691. # less than condition
  3692. self.assert_measurement_condition_with_results(f"measurements.fcp:<{fcp + 1}")
  3693. self.assert_measurement_condition_without_results(f"measurements.fcp:<{fcp - 1}")
  3694. # has condition
  3695. self.assert_measurement_condition_with_results("has:measurements.fcp")
  3696. self.assert_measurement_condition_without_results("!has:measurements.fcp")
  3697. def test_measurements_aggregation_conditions(self):
  3698. self.store_event(self.transaction_data, self.project.id)
  3699. fcp = self.transaction_data["measurements"]["fcp"]["value"]
  3700. functions = [
  3701. "percentile(measurements.fcp, 0.5)",
  3702. "min(measurements.fcp)",
  3703. "max(measurements.fcp)",
  3704. "avg(measurements.fcp)",
  3705. "sum(measurements.fcp)",
  3706. ]
  3707. for function in functions:
  3708. self.assert_measurement_condition_with_results(
  3709. f"{function}:>{fcp - 1}", field=[function]
  3710. )
  3711. self.assert_measurement_condition_without_results(
  3712. f"{function}:>{fcp + 1}", field=[function]
  3713. )
  3714. self.assert_measurement_condition_with_results(
  3715. f"{function}:<{fcp + 1}", field=[function]
  3716. )
  3717. self.assert_measurement_condition_without_results(
  3718. f"{function}:<{fcp - 1}", field=[function]
  3719. )
  3720. count_unique = "count_unique(measurements.fcp)"
  3721. self.assert_measurement_condition_with_results(f"{count_unique}:1", field=[count_unique])
  3722. self.assert_measurement_condition_without_results(f"{count_unique}:0", field=[count_unique])
  3723. def test_compare_numeric_aggregate(self):
  3724. self.store_event(self.transaction_data, self.project.id)
  3725. query = {
  3726. "field": [
  3727. "p75(measurements.fcp)",
  3728. "compare_numeric_aggregate(p75_measurements_fcp,greater,0)",
  3729. ],
  3730. }
  3731. response = self.do_request(query)
  3732. assert response.status_code == 200, response.content
  3733. assert len(response.data["data"]) == 1
  3734. assert (
  3735. response.data["data"][0]["compare_numeric_aggregate(p75_measurements_fcp,greater,0)"]
  3736. == 1
  3737. )
  3738. query = {
  3739. "field": ["p75()", "compare_numeric_aggregate(p75,equals,0)"],
  3740. }
  3741. response = self.do_request(query)
  3742. assert response.status_code == 200, response.content
  3743. assert len(response.data["data"]) == 1
  3744. assert response.data["data"][0]["compare_numeric_aggregate(p75,equals,0)"] == 0
  3745. def test_no_team_key_transactions(self):
  3746. transactions = [
  3747. "/blah_transaction/",
  3748. "/foo_transaction/",
  3749. "/zoo_transaction/",
  3750. ]
  3751. for transaction in transactions:
  3752. self.transaction_data["transaction"] = transaction
  3753. self.store_event(self.transaction_data, self.project.id)
  3754. query = {
  3755. "team": "myteams",
  3756. "project": [self.project.id],
  3757. # use the order by to ensure the result order
  3758. "orderby": "transaction",
  3759. "field": [
  3760. "team_key_transaction",
  3761. "transaction",
  3762. "transaction.status",
  3763. "project",
  3764. "epm()",
  3765. "failure_rate()",
  3766. "percentile(transaction.duration, 0.95)",
  3767. ],
  3768. }
  3769. response = self.do_request(query)
  3770. assert response.status_code == 200, response.content
  3771. data = response.data["data"]
  3772. assert len(data) == 3
  3773. assert data[0]["team_key_transaction"] == 0
  3774. assert data[0]["transaction"] == "/blah_transaction/"
  3775. assert data[1]["team_key_transaction"] == 0
  3776. assert data[1]["transaction"] == "/foo_transaction/"
  3777. assert data[2]["team_key_transaction"] == 0
  3778. assert data[2]["transaction"] == "/zoo_transaction/"
  3779. def test_team_key_transactions_my_teams(self):
  3780. team1 = self.create_team(organization=self.organization, name="Team A")
  3781. self.create_team_membership(team1, user=self.user)
  3782. self.project.add_team(team1)
  3783. team2 = self.create_team(organization=self.organization, name="Team B")
  3784. self.project.add_team(team2)
  3785. transactions = ["/blah_transaction/"]
  3786. key_transactions = [
  3787. (team1, "/foo_transaction/"),
  3788. (team2, "/zoo_transaction/"),
  3789. ]
  3790. for transaction in transactions:
  3791. self.transaction_data["transaction"] = transaction
  3792. self.store_event(self.transaction_data, self.project.id)
  3793. for team, transaction in key_transactions:
  3794. self.transaction_data["transaction"] = transaction
  3795. self.store_event(self.transaction_data, self.project.id)
  3796. TeamKeyTransaction.objects.create(
  3797. organization=self.organization,
  3798. transaction=transaction,
  3799. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  3800. )
  3801. query = {
  3802. "team": "myteams",
  3803. "project": [self.project.id],
  3804. "field": [
  3805. "team_key_transaction",
  3806. "transaction",
  3807. "transaction.status",
  3808. "project",
  3809. "epm()",
  3810. "failure_rate()",
  3811. "percentile(transaction.duration, 0.95)",
  3812. ],
  3813. }
  3814. query["orderby"] = ["team_key_transaction", "transaction"]
  3815. response = self.do_request(query)
  3816. assert response.status_code == 200, response.content
  3817. data = response.data["data"]
  3818. assert len(data) == 3
  3819. assert data[0]["team_key_transaction"] == 0
  3820. assert data[0]["transaction"] == "/blah_transaction/"
  3821. assert data[1]["team_key_transaction"] == 0
  3822. assert data[1]["transaction"] == "/zoo_transaction/"
  3823. assert data[2]["team_key_transaction"] == 1
  3824. assert data[2]["transaction"] == "/foo_transaction/"
  3825. # not specifying any teams should use my teams
  3826. query = {
  3827. "project": [self.project.id],
  3828. "field": [
  3829. "team_key_transaction",
  3830. "transaction",
  3831. "transaction.status",
  3832. "project",
  3833. "epm()",
  3834. "failure_rate()",
  3835. "percentile(transaction.duration, 0.95)",
  3836. ],
  3837. }
  3838. query["orderby"] = ["team_key_transaction", "transaction"]
  3839. response = self.do_request(query)
  3840. assert response.status_code == 200, response.content
  3841. data = response.data["data"]
  3842. assert len(data) == 3
  3843. assert data[0]["team_key_transaction"] == 0
  3844. assert data[0]["transaction"] == "/blah_transaction/"
  3845. assert data[1]["team_key_transaction"] == 0
  3846. assert data[1]["transaction"] == "/zoo_transaction/"
  3847. assert data[2]["team_key_transaction"] == 1
  3848. assert data[2]["transaction"] == "/foo_transaction/"
  3849. def test_team_key_transactions_orderby(self):
  3850. team1 = self.create_team(organization=self.organization, name="Team A")
  3851. team2 = self.create_team(organization=self.organization, name="Team B")
  3852. transactions = ["/blah_transaction/"]
  3853. key_transactions = [
  3854. (team1, "/foo_transaction/"),
  3855. (team2, "/zoo_transaction/"),
  3856. ]
  3857. for transaction in transactions:
  3858. self.transaction_data["transaction"] = transaction
  3859. self.store_event(self.transaction_data, self.project.id)
  3860. for team, transaction in key_transactions:
  3861. self.create_team_membership(team, user=self.user)
  3862. self.project.add_team(team)
  3863. self.transaction_data["transaction"] = transaction
  3864. self.store_event(self.transaction_data, self.project.id)
  3865. TeamKeyTransaction.objects.create(
  3866. organization=self.organization,
  3867. transaction=transaction,
  3868. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  3869. )
  3870. query = {
  3871. "team": "myteams",
  3872. "project": [self.project.id],
  3873. "field": [
  3874. "team_key_transaction",
  3875. "transaction",
  3876. "transaction.status",
  3877. "project",
  3878. "epm()",
  3879. "failure_rate()",
  3880. "percentile(transaction.duration, 0.95)",
  3881. ],
  3882. }
  3883. # test ascending order
  3884. query["orderby"] = ["team_key_transaction", "transaction"]
  3885. response = self.do_request(query)
  3886. assert response.status_code == 200, response.content
  3887. data = response.data["data"]
  3888. assert len(data) == 3
  3889. assert data[0]["team_key_transaction"] == 0
  3890. assert data[0]["transaction"] == "/blah_transaction/"
  3891. assert data[1]["team_key_transaction"] == 1
  3892. assert data[1]["transaction"] == "/foo_transaction/"
  3893. assert data[2]["team_key_transaction"] == 1
  3894. assert data[2]["transaction"] == "/zoo_transaction/"
  3895. # test descending order
  3896. query["orderby"] = ["-team_key_transaction", "-transaction"]
  3897. response = self.do_request(query)
  3898. assert response.status_code == 200, response.content
  3899. data = response.data["data"]
  3900. assert len(data) == 3
  3901. assert data[0]["team_key_transaction"] == 1
  3902. assert data[0]["transaction"] == "/zoo_transaction/"
  3903. assert data[1]["team_key_transaction"] == 1
  3904. assert data[1]["transaction"] == "/foo_transaction/"
  3905. assert data[2]["team_key_transaction"] == 0
  3906. assert data[2]["transaction"] == "/blah_transaction/"
  3907. def test_team_key_transactions_query(self):
  3908. team1 = self.create_team(organization=self.organization, name="Team A")
  3909. team2 = self.create_team(organization=self.organization, name="Team B")
  3910. transactions = ["/blah_transaction/"]
  3911. key_transactions = [
  3912. (team1, "/foo_transaction/"),
  3913. (team2, "/zoo_transaction/"),
  3914. ]
  3915. for transaction in transactions:
  3916. self.transaction_data["transaction"] = transaction
  3917. self.store_event(self.transaction_data, self.project.id)
  3918. for team, transaction in key_transactions:
  3919. self.create_team_membership(team, user=self.user)
  3920. self.project.add_team(team)
  3921. self.transaction_data["transaction"] = transaction
  3922. self.store_event(self.transaction_data, self.project.id)
  3923. TeamKeyTransaction.objects.create(
  3924. organization=self.organization,
  3925. project_team=ProjectTeam.objects.get(
  3926. project=self.project,
  3927. team=team,
  3928. ),
  3929. transaction=transaction,
  3930. )
  3931. query = {
  3932. "team": "myteams",
  3933. "project": [self.project.id],
  3934. # use the order by to ensure the result order
  3935. "orderby": "transaction",
  3936. "field": [
  3937. "team_key_transaction",
  3938. "transaction",
  3939. "transaction.status",
  3940. "project",
  3941. "epm()",
  3942. "failure_rate()",
  3943. "percentile(transaction.duration, 0.95)",
  3944. ],
  3945. }
  3946. # key transactions
  3947. query["query"] = "has:team_key_transaction"
  3948. response = self.do_request(query)
  3949. assert response.status_code == 200, response.content
  3950. data = response.data["data"]
  3951. assert len(data) == 2
  3952. assert data[0]["team_key_transaction"] == 1
  3953. assert data[0]["transaction"] == "/foo_transaction/"
  3954. assert data[1]["team_key_transaction"] == 1
  3955. assert data[1]["transaction"] == "/zoo_transaction/"
  3956. # key transactions
  3957. query["query"] = "team_key_transaction:true"
  3958. response = self.do_request(query)
  3959. assert response.status_code == 200, response.content
  3960. data = response.data["data"]
  3961. assert len(data) == 2
  3962. assert data[0]["team_key_transaction"] == 1
  3963. assert data[0]["transaction"] == "/foo_transaction/"
  3964. assert data[1]["team_key_transaction"] == 1
  3965. assert data[1]["transaction"] == "/zoo_transaction/"
  3966. # not key transactions
  3967. query["query"] = "!has:team_key_transaction"
  3968. response = self.do_request(query)
  3969. assert response.status_code == 200, response.content
  3970. data = response.data["data"]
  3971. assert len(data) == 1
  3972. assert data[0]["team_key_transaction"] == 0
  3973. assert data[0]["transaction"] == "/blah_transaction/"
  3974. # not key transactions
  3975. query["query"] = "team_key_transaction:false"
  3976. response = self.do_request(query)
  3977. assert response.status_code == 200, response.content
  3978. data = response.data["data"]
  3979. assert len(data) == 1
  3980. assert data[0]["team_key_transaction"] == 0
  3981. assert data[0]["transaction"] == "/blah_transaction/"
  3982. def test_too_many_team_key_transactions(self):
  3983. MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS = 1
  3984. with mock.patch(
  3985. "sentry.search.events.fields.MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS",
  3986. MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS,
  3987. ):
  3988. team = self.create_team(organization=self.organization, name="Team A")
  3989. self.create_team_membership(team, user=self.user)
  3990. self.project.add_team(team)
  3991. project_team = ProjectTeam.objects.get(project=self.project, team=team)
  3992. for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1):
  3993. transaction = f"transaction-{team.id}-{i}"
  3994. self.transaction_data["transaction"] = transaction
  3995. self.store_event(self.transaction_data, self.project.id)
  3996. TeamKeyTransaction.objects.bulk_create(
  3997. [
  3998. TeamKeyTransaction(
  3999. organization=self.organization,
  4000. project_team=project_team,
  4001. transaction=f"transaction-{team.id}-{i}",
  4002. )
  4003. for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1)
  4004. ]
  4005. )
  4006. query = {
  4007. "team": "myteams",
  4008. "project": [self.project.id],
  4009. "orderby": "transaction",
  4010. "field": [
  4011. "team_key_transaction",
  4012. "transaction",
  4013. "transaction.status",
  4014. "project",
  4015. "epm()",
  4016. "failure_rate()",
  4017. "percentile(transaction.duration, 0.95)",
  4018. ],
  4019. }
  4020. response = self.do_request(query)
  4021. assert response.status_code == 200, response.content
  4022. data = response.data["data"]
  4023. assert len(data) == 2
  4024. assert (
  4025. sum(row["team_key_transaction"] for row in data)
  4026. == MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS
  4027. )
  4028. def test_no_pagination_param(self):
  4029. self.store_event(
  4030. data={"event_id": "a" * 32, "timestamp": self.ten_mins_ago, "fingerprint": ["group1"]},
  4031. project_id=self.project.id,
  4032. )
  4033. query = {"field": ["id", "project.id"], "project": [self.project.id], "noPagination": True}
  4034. response = self.do_request(query)
  4035. assert response.status_code == 200
  4036. assert len(response.data["data"]) == 1
  4037. assert "Link" not in response
  4038. def test_nan_result(self):
  4039. query = {"field": ["apdex(300)"], "project": [self.project.id], "query": f"id:{'0' * 32}"}
  4040. response = self.do_request(query)
  4041. assert response.status_code == 200
  4042. assert len(response.data["data"]) == 1
  4043. assert response.data["data"][0]["apdex(300)"] == 0
  4044. def test_equation_simple(self):
  4045. event_data = load_data("transaction", timestamp=before_now(minutes=10))
  4046. event_data["breakdowns"]["span_ops"]["ops.http"]["value"] = 1500
  4047. self.store_event(data=event_data, project_id=self.project.id)
  4048. query = {
  4049. "field": ["spans.http", "equation|spans.http / 3"],
  4050. "project": [self.project.id],
  4051. "query": "event.type:transaction",
  4052. }
  4053. response = self.do_request(
  4054. query,
  4055. {
  4056. "organizations:discover-basic": True,
  4057. },
  4058. )
  4059. assert response.status_code == 200, response.content
  4060. assert len(response.data["data"]) == 1
  4061. assert (
  4062. response.data["data"][0]["equation|spans.http / 3"]
  4063. == event_data["breakdowns"]["span_ops"]["ops.http"]["value"] / 3
  4064. )
  4065. assert response.data["meta"]["fields"]["equation|spans.http / 3"] == "number"
  4066. def test_equation_sort(self):
  4067. event_data = self.transaction_data.copy()
  4068. event_data["breakdowns"] = {"span_ops": {"ops.http": {"value": 1500}}}
  4069. self.store_event(data=event_data, project_id=self.project.id)
  4070. event_data2 = self.transaction_data.copy()
  4071. event_data2["breakdowns"] = {"span_ops": {"ops.http": {"value": 2000}}}
  4072. self.store_event(data=event_data2, project_id=self.project.id)
  4073. query = {
  4074. "field": ["spans.http", "equation|spans.http / 3"],
  4075. "project": [self.project.id],
  4076. "orderby": "equation|spans.http / 3",
  4077. "query": "event.type:transaction",
  4078. }
  4079. response = self.do_request(
  4080. query,
  4081. {
  4082. "organizations:discover-basic": True,
  4083. },
  4084. )
  4085. assert response.status_code == 200, response.content
  4086. assert len(response.data["data"]) == 2
  4087. assert (
  4088. response.data["data"][0]["equation|spans.http / 3"]
  4089. == event_data["breakdowns"]["span_ops"]["ops.http"]["value"] / 3
  4090. )
  4091. assert (
  4092. response.data["data"][1]["equation|spans.http / 3"]
  4093. == event_data2["breakdowns"]["span_ops"]["ops.http"]["value"] / 3
  4094. )
  4095. def test_equation_operation_limit(self):
  4096. query = {
  4097. "field": ["spans.http", f"equation|spans.http{' * 2' * 11}"],
  4098. "project": [self.project.id],
  4099. "query": "event.type:transaction",
  4100. }
  4101. response = self.do_request(
  4102. query,
  4103. {
  4104. "organizations:discover-basic": True,
  4105. },
  4106. )
  4107. assert response.status_code == 400
  4108. @mock.patch("sentry.api.bases.organization_events.MAX_FIELDS", 2)
  4109. def test_equation_field_limit(self):
  4110. query = {
  4111. "field": ["spans.http", "transaction.duration", "equation|5 * 2"],
  4112. "project": [self.project.id],
  4113. "query": "event.type:transaction",
  4114. }
  4115. response = self.do_request(
  4116. query,
  4117. {
  4118. "organizations:discover-basic": True,
  4119. },
  4120. )
  4121. assert response.status_code == 400
  4122. def test_count_if(self):
  4123. unicode_phrase1 = "\u716e\u6211\u66f4\u591a\u7684\u98df\u7269\uff0c\u6211\u9913\u4e86"
  4124. for i in range(5):
  4125. data = load_data(
  4126. "transaction",
  4127. timestamp=before_now(minutes=(10 + i)),
  4128. start_timestamp=before_now(minutes=(10 + i), milliseconds=100 if i < 3 else 200),
  4129. )
  4130. data["tags"] = {
  4131. "sub_customer.is-Enterprise-42": "yes" if i == 0 else "no",
  4132. "unicode-phrase": unicode_phrase1 if i == 0 else "no",
  4133. }
  4134. self.store_event(data, project_id=self.project.id)
  4135. query = {
  4136. "field": [
  4137. "count_if(transaction.duration, less, 150)",
  4138. "count_if(transaction.duration, greater, 150)",
  4139. "count_if(sub_customer.is-Enterprise-42, equals, yes)",
  4140. "count_if(sub_customer.is-Enterprise-42, notEquals, yes)",
  4141. f"count_if(unicode-phrase, equals, {unicode_phrase1})",
  4142. ],
  4143. "project": [self.project.id],
  4144. }
  4145. response = self.do_request(query)
  4146. assert response.status_code == 200
  4147. assert len(response.data["data"]) == 1
  4148. assert response.data["data"][0]["count_if(transaction.duration, less, 150)"] == 3
  4149. assert response.data["data"][0]["count_if(transaction.duration, greater, 150)"] == 2
  4150. assert response.data["data"][0]["count_if(sub_customer.is-Enterprise-42, equals, yes)"] == 1
  4151. assert (
  4152. response.data["data"][0]["count_if(sub_customer.is-Enterprise-42, notEquals, yes)"] == 4
  4153. )
  4154. assert response.data["data"][0][f"count_if(unicode-phrase, equals, {unicode_phrase1})"] == 1
  4155. def test_count_if_measurements_cls(self):
  4156. data = self.transaction_data.copy()
  4157. data["measurements"] = {"cls": {"value": 0.5}}
  4158. self.store_event(data, project_id=self.project.id)
  4159. data["measurements"] = {"cls": {"value": 0.1}}
  4160. self.store_event(data, project_id=self.project.id)
  4161. query = {
  4162. "field": [
  4163. "count_if(measurements.cls, greater, 0.05)",
  4164. "count_if(measurements.cls, less, 0.3)",
  4165. ],
  4166. "project": [self.project.id],
  4167. }
  4168. response = self.do_request(query)
  4169. assert response.status_code == 200
  4170. assert len(response.data["data"]) == 1
  4171. assert response.data["data"][0]["count_if(measurements.cls, greater, 0.05)"] == 2
  4172. assert response.data["data"][0]["count_if(measurements.cls, less, 0.3)"] == 1
  4173. def test_count_if_filter(self):
  4174. for i in range(5):
  4175. data = load_data(
  4176. "transaction",
  4177. timestamp=before_now(minutes=(10 + i)),
  4178. start_timestamp=before_now(minutes=(10 + i), milliseconds=100 if i < 3 else 200),
  4179. )
  4180. data["tags"] = {"sub_customer.is-Enterprise-42": "yes" if i == 0 else "no"}
  4181. self.store_event(data, project_id=self.project.id)
  4182. query = {
  4183. "field": [
  4184. "count_if(transaction.duration, less, 150)",
  4185. ],
  4186. "query": "count_if(transaction.duration, less, 150):>2",
  4187. "project": [self.project.id],
  4188. }
  4189. response = self.do_request(query)
  4190. assert response.status_code == 200
  4191. assert len(response.data["data"]) == 1
  4192. assert response.data["data"][0]["count_if(transaction.duration, less, 150)"] == 3
  4193. query = {
  4194. "field": [
  4195. "count_if(transaction.duration, less, 150)",
  4196. ],
  4197. "query": "count_if(transaction.duration, less, 150):<2",
  4198. "project": [self.project.id],
  4199. }
  4200. response = self.do_request(query)
  4201. assert response.status_code == 200
  4202. assert len(response.data["data"]) == 0
  4203. def test_filters_with_escaped_asterisk(self):
  4204. self.transaction_data["transaction"] = r"/:a*/:b-:c(\d\.\e+)"
  4205. self.store_event(self.transaction_data, project_id=self.project.id)
  4206. query = {
  4207. "field": ["transaction", "transaction.duration"],
  4208. # make sure to escape the asterisk so it's not treated as a wildcard
  4209. "query": r'transaction:"/:a\*/:b-:c(\d\.\e+)"',
  4210. "project": [self.project.id],
  4211. }
  4212. response = self.do_request(query)
  4213. assert response.status_code == 200
  4214. assert len(response.data["data"]) == 1
  4215. def test_filters_with_back_slashes(self):
  4216. self.transaction_data["transaction"] = r"a\b\c@d"
  4217. self.store_event(self.transaction_data, project_id=self.project.id)
  4218. query = {
  4219. "field": ["transaction", "transaction.duration"],
  4220. "query": r'transaction:"a\b\c@d"',
  4221. "project": [self.project.id],
  4222. }
  4223. response = self.do_request(query)
  4224. assert response.status_code == 200
  4225. assert len(response.data["data"]) == 1
  4226. def test_mobile_measurements(self):
  4227. self.transaction_data["measurements"]["frames_total"] = {"value": 100}
  4228. self.transaction_data["measurements"]["frames_slow"] = {"value": 10}
  4229. self.transaction_data["measurements"]["frames_frozen"] = {"value": 5}
  4230. self.transaction_data["measurements"]["stall_count"] = {"value": 2}
  4231. self.transaction_data["measurements"]["stall_total_time"] = {"value": 12}
  4232. self.transaction_data["measurements"]["stall_longest_time"] = {"value": 7}
  4233. self.store_event(self.transaction_data, project_id=self.project.id)
  4234. query = {
  4235. "field": [
  4236. "measurements.frames_total",
  4237. "measurements.frames_slow",
  4238. "measurements.frames_frozen",
  4239. "measurements.frames_slow_rate",
  4240. "measurements.frames_frozen_rate",
  4241. "measurements.stall_count",
  4242. "measurements.stall_total_time",
  4243. "measurements.stall_longest_time",
  4244. "measurements.stall_percentage",
  4245. ],
  4246. "query": "",
  4247. "project": [self.project.id],
  4248. }
  4249. response = self.do_request(query)
  4250. assert response.status_code == 200
  4251. data = response.data["data"]
  4252. assert len(data) == 1
  4253. assert data[0]["measurements.frames_total"] == 100
  4254. assert data[0]["measurements.frames_slow"] == 10
  4255. assert data[0]["measurements.frames_frozen"] == 5
  4256. assert data[0]["measurements.frames_slow_rate"] == 0.1
  4257. assert data[0]["measurements.frames_frozen_rate"] == 0.05
  4258. assert data[0]["measurements.stall_count"] == 2
  4259. assert data[0]["measurements.stall_total_time"] == 12
  4260. assert data[0]["measurements.stall_longest_time"] == 7
  4261. assert data[0]["measurements.stall_percentage"] == 0.004
  4262. meta = response.data["meta"]["fields"]
  4263. assert meta["measurements.frames_total"] == "number"
  4264. assert meta["measurements.frames_slow"] == "number"
  4265. assert meta["measurements.frames_frozen"] == "number"
  4266. assert meta["measurements.frames_slow_rate"] == "percentage"
  4267. assert meta["measurements.frames_frozen_rate"] == "percentage"
  4268. assert meta["measurements.stall_count"] == "number"
  4269. assert meta["measurements.stall_total_time"] == "number"
  4270. assert meta["measurements.stall_longest_time"] == "number"
  4271. assert meta["measurements.stall_percentage"] == "percentage"
  4272. query = {
  4273. "field": [
  4274. "p75(measurements.frames_slow_rate)",
  4275. "p75(measurements.frames_frozen_rate)",
  4276. "percentile(measurements.frames_slow_rate,0.5)",
  4277. "percentile(measurements.frames_frozen_rate,0.5)",
  4278. "p75(measurements.stall_percentage)",
  4279. "percentile(measurements.stall_percentage,0.5)",
  4280. ],
  4281. "query": "",
  4282. "project": [self.project.id],
  4283. }
  4284. response = self.do_request(query)
  4285. assert response.status_code == 200
  4286. data = response.data["data"]
  4287. assert len(data) == 1
  4288. assert data[0]["p75(measurements.frames_slow_rate)"] == 0.1
  4289. assert data[0]["p75(measurements.frames_frozen_rate)"] == 0.05
  4290. assert data[0]["p75(measurements.stall_percentage)"] == 0.004
  4291. assert data[0]["percentile(measurements.frames_slow_rate,0.5)"] == 0.1
  4292. assert data[0]["percentile(measurements.frames_frozen_rate,0.5)"] == 0.05
  4293. assert data[0]["percentile(measurements.stall_percentage,0.5)"] == 0.004
  4294. meta = response.data["meta"]["fields"]
  4295. assert meta["p75(measurements.frames_slow_rate)"] == "percentage"
  4296. assert meta["p75(measurements.frames_frozen_rate)"] == "percentage"
  4297. assert meta["p75(measurements.stall_percentage)"] == "percentage"
  4298. assert meta["percentile(measurements.frames_slow_rate,0.5)"] == "percentage"
  4299. assert meta["percentile(measurements.stall_percentage,0.5)"] == "percentage"
  4300. def test_project_auto_fields(self):
  4301. self.store_event(
  4302. data={"event_id": "a" * 32, "environment": "staging", "timestamp": self.ten_mins_ago},
  4303. project_id=self.project.id,
  4304. )
  4305. query = {"field": ["environment"]}
  4306. response = self.do_request(query)
  4307. assert response.status_code == 200, response.content
  4308. assert len(response.data["data"]) == 1
  4309. assert response.data["data"][0]["environment"] == "staging"
  4310. assert response.data["data"][0]["project.name"] == self.project.slug
  4311. def test_timestamp_different_from_params(self):
  4312. fifteen_days_ago = iso_format(before_now(days=15))
  4313. fifteen_days_later = iso_format(before_now(days=-15))
  4314. for query_text in [
  4315. f"timestamp:<{fifteen_days_ago}",
  4316. f"timestamp:<={fifteen_days_ago}",
  4317. f"timestamp:>{fifteen_days_later}",
  4318. f"timestamp:>={fifteen_days_later}",
  4319. ]:
  4320. query = {
  4321. "field": ["count()"],
  4322. "query": query_text,
  4323. "statsPeriod": "14d",
  4324. "project": self.project.id,
  4325. }
  4326. response = self.do_request(query)
  4327. assert response.status_code == 400, query_text
  4328. @mock.patch("sentry.search.events.builder.raw_snql_query")
  4329. def test_removes_unnecessary_default_project_and_transaction_thresholds(self, mock_snql_query):
  4330. mock_snql_query.side_effect = [{"meta": {}, "data": []}]
  4331. ProjectTransactionThreshold.objects.create(
  4332. project=self.project,
  4333. organization=self.organization,
  4334. # these are the default values that we use
  4335. threshold=constants.DEFAULT_PROJECT_THRESHOLD,
  4336. metric=TransactionMetric.DURATION.value,
  4337. )
  4338. ProjectTransactionThresholdOverride.objects.create(
  4339. transaction="transaction",
  4340. project=self.project,
  4341. organization=self.organization,
  4342. # these are the default values that we use
  4343. threshold=constants.DEFAULT_PROJECT_THRESHOLD,
  4344. metric=TransactionMetric.DURATION.value,
  4345. )
  4346. query = {
  4347. "field": ["apdex()", "user_misery()"],
  4348. "query": "event.type:transaction",
  4349. "project": [self.project.id],
  4350. }
  4351. response = self.do_request(
  4352. query,
  4353. features={
  4354. "organizations:discover-basic": True,
  4355. "organizations:global-views": True,
  4356. },
  4357. )
  4358. assert response.status_code == 200, response.content
  4359. assert mock_snql_query.call_count == 1
  4360. assert (
  4361. Function("tuple", ["duration", 300], "project_threshold_config")
  4362. in mock_snql_query.call_args_list[0][0][0].query.select
  4363. )
  4364. @mock.patch("sentry.search.events.builder.raw_snql_query")
  4365. def test_removes_unnecessary_default_project_and_transaction_thresholds_keeps_others(
  4366. self, mock_snql_query
  4367. ):
  4368. mock_snql_query.side_effect = [{"meta": {}, "data": []}]
  4369. ProjectTransactionThreshold.objects.create(
  4370. project=self.project,
  4371. organization=self.organization,
  4372. # these are the default values that we use
  4373. threshold=constants.DEFAULT_PROJECT_THRESHOLD,
  4374. metric=TransactionMetric.DURATION.value,
  4375. )
  4376. ProjectTransactionThresholdOverride.objects.create(
  4377. transaction="transaction",
  4378. project=self.project,
  4379. organization=self.organization,
  4380. # these are the default values that we use
  4381. threshold=constants.DEFAULT_PROJECT_THRESHOLD,
  4382. metric=TransactionMetric.DURATION.value,
  4383. )
  4384. project = self.create_project()
  4385. ProjectTransactionThreshold.objects.create(
  4386. project=project,
  4387. organization=self.organization,
  4388. threshold=100,
  4389. metric=TransactionMetric.LCP.value,
  4390. )
  4391. ProjectTransactionThresholdOverride.objects.create(
  4392. transaction="transaction",
  4393. project=project,
  4394. organization=self.organization,
  4395. threshold=200,
  4396. metric=TransactionMetric.LCP.value,
  4397. )
  4398. query = {
  4399. "field": ["apdex()", "user_misery()"],
  4400. "query": "event.type:transaction",
  4401. "project": [self.project.id, project.id],
  4402. }
  4403. response = self.do_request(
  4404. query,
  4405. features={
  4406. "organizations:discover-basic": True,
  4407. "organizations:global-views": True,
  4408. },
  4409. )
  4410. assert response.status_code == 200, response.content
  4411. assert mock_snql_query.call_count == 1
  4412. project_threshold_override_config_index = Function(
  4413. "indexOf",
  4414. [
  4415. # only 1 transaction override is present here
  4416. # because the other use to the default values
  4417. [(Function("toUInt64", [project.id]), "transaction")],
  4418. (Column("project_id"), Column("transaction")),
  4419. ],
  4420. "project_threshold_override_config_index",
  4421. )
  4422. project_threshold_config_index = Function(
  4423. "indexOf",
  4424. [
  4425. # only 1 project override is present here
  4426. # because the other use to the default values
  4427. [Function("toUInt64", [project.id])],
  4428. Column("project_id"),
  4429. ],
  4430. "project_threshold_config_index",
  4431. )
  4432. assert (
  4433. Function(
  4434. "if",
  4435. [
  4436. Function("equals", [project_threshold_override_config_index, 0]),
  4437. Function(
  4438. "if",
  4439. [
  4440. Function("equals", [project_threshold_config_index, 0]),
  4441. ("duration", 300),
  4442. Function(
  4443. "arrayElement", [[("lcp", 100)], project_threshold_config_index]
  4444. ),
  4445. ],
  4446. ),
  4447. Function(
  4448. "arrayElement",
  4449. [[("lcp", 200)], project_threshold_override_config_index],
  4450. ),
  4451. ],
  4452. "project_threshold_config",
  4453. )
  4454. in mock_snql_query.call_args_list[0][0][0].query.select
  4455. )
  4456. def test_count_web_vitals(self):
  4457. # Good
  4458. self.transaction_data["measurements"] = {
  4459. "lcp": {"value": constants.VITAL_THRESHOLDS["lcp"]["meh"] - 100},
  4460. }
  4461. self.store_event(self.transaction_data, self.project.id)
  4462. # Meh
  4463. self.transaction_data["measurements"] = {
  4464. "lcp": {"value": constants.VITAL_THRESHOLDS["lcp"]["meh"] + 100},
  4465. }
  4466. self.store_event(self.transaction_data, self.project.id)
  4467. self.store_event(self.transaction_data, self.project.id)
  4468. query = {
  4469. "field": [
  4470. "count_web_vitals(measurements.lcp, poor)",
  4471. "count_web_vitals(measurements.lcp, meh)",
  4472. "count_web_vitals(measurements.lcp, good)",
  4473. ]
  4474. }
  4475. response = self.do_request(query)
  4476. assert response.status_code == 200, response.content
  4477. assert len(response.data["data"]) == 1
  4478. assert response.data["data"][0] == {
  4479. "count_web_vitals(measurements.lcp, poor)": 0,
  4480. "count_web_vitals(measurements.lcp, meh)": 2,
  4481. "count_web_vitals(measurements.lcp, good)": 1,
  4482. }
  4483. def test_count_web_vitals_invalid_vital(self):
  4484. query = {
  4485. "field": [
  4486. "count_web_vitals(measurements.foo, poor)",
  4487. ],
  4488. "project": [self.project.id],
  4489. }
  4490. response = self.do_request(query)
  4491. assert response.status_code == 400, response.content
  4492. query = {
  4493. "field": [
  4494. "count_web_vitals(tags[lcp], poor)",
  4495. ],
  4496. "project": [self.project.id],
  4497. }
  4498. response = self.do_request(query)
  4499. assert response.status_code == 400, response.content
  4500. query = {
  4501. "field": [
  4502. "count_web_vitals(transaction.duration, poor)",
  4503. ],
  4504. "project": [self.project.id],
  4505. }
  4506. response = self.do_request(query)
  4507. assert response.status_code == 400, response.content
  4508. query = {
  4509. "field": [
  4510. "count_web_vitals(measurements.lcp, bad)",
  4511. ],
  4512. "project": [self.project.id],
  4513. }
  4514. response = self.do_request(query)
  4515. assert response.status_code == 400, response.content
  4516. def test_tag_that_looks_like_aggregate(self):
  4517. data = load_data("transaction", timestamp=before_now(minutes=1))
  4518. data["tags"] = {"p95": "<5k"}
  4519. self.store_event(data, project_id=self.project.id)
  4520. query = {
  4521. "field": ["p95"],
  4522. "query": "p95:<5k",
  4523. "project": [self.project.id],
  4524. }
  4525. response = self.do_request(query)
  4526. assert response.status_code == 200, response.content
  4527. data = response.data["data"]
  4528. assert len(data) == 1
  4529. assert data[0]["p95"] == "<5k"
  4530. def test_chained_or_query_meta_tip(self):
  4531. query = {
  4532. "field": ["transaction"],
  4533. "query": "transaction:a OR transaction:b",
  4534. "project": [self.project.id],
  4535. }
  4536. response = self.do_request(query)
  4537. assert response.status_code == 200, response.content
  4538. meta = response.data["meta"]
  4539. assert meta["tips"] == {
  4540. "query": "Did you know you can replace chained or conditions like `field:a OR field:b OR field:c` with `field:[a,b,c]`",
  4541. "columns": None,
  4542. }
  4543. @override_settings(SENTRY_SELF_HOSTED=False)
  4544. def test_ratelimit(self):
  4545. query = {
  4546. "field": ["transaction"],
  4547. "project": [self.project.id],
  4548. }
  4549. with freeze_time("2000-01-01"):
  4550. for _ in range(15):
  4551. self.do_request(query, features={"organizations:discover-events-rate-limit": True})
  4552. response = self.do_request(
  4553. query, features={"organizations:discover-events-rate-limit": True}
  4554. )
  4555. assert response.status_code == 429, response.content
  4556. @override_settings(SENTRY_SELF_HOSTED=False)
  4557. def test_no_ratelimit(self):
  4558. query = {
  4559. "field": ["transaction"],
  4560. "project": [self.project.id],
  4561. }
  4562. with freeze_time("2000-01-01"):
  4563. for _ in range(15):
  4564. self.do_request(query)
  4565. response = self.do_request(query)
  4566. assert response.status_code == 200, response.content
  4567. def test_transaction_source(self):
  4568. query = {
  4569. "field": ["transaction"],
  4570. "query": "transaction.source:task",
  4571. "project": [self.project.id],
  4572. }
  4573. response = self.do_request(query)
  4574. assert response.status_code == 200, response.content