test_organization_events.py 232 KB

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