test_organization_events.py 235 KB

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