test_organization_events.py 226 KB

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