test_organization_events.py 235 KB

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