test_organization_events.py 233 KB

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