test_organization_events.py 252 KB

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