test_organization_events.py 228 KB

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