test_organization_events.py 228 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028
  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. @pytest.mark.xfail(reason="Started failing on ClickHouse 21.8")
  2777. def test_not_has_transaction_status(self):
  2778. self.store_event(self.transaction_data, project_id=self.project.id)
  2779. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2780. query = {
  2781. "field": ["event.type", "count(id)"],
  2782. "query": "event.type:transaction !has:transaction.status",
  2783. "sort": "-count(id)",
  2784. "statsPeriod": "24h",
  2785. }
  2786. response = self.do_request(query, features=features)
  2787. assert response.status_code == 200, response.content
  2788. data = response.data["data"]
  2789. assert len(data) == 1
  2790. assert data[0]["count(id)"] == 0
  2791. def test_tag_that_looks_like_aggregation(self):
  2792. data = {
  2793. "message": "Failure state",
  2794. "timestamp": self.ten_mins_ago_iso,
  2795. "tags": {"count_diff": 99},
  2796. }
  2797. self.store_event(data, project_id=self.project.id)
  2798. query = {
  2799. "field": ["message", "count_diff", "count()"],
  2800. "query": "",
  2801. "project": [self.project.id],
  2802. "statsPeriod": "24h",
  2803. }
  2804. response = self.do_request(query)
  2805. assert response.status_code == 200, response.content
  2806. meta = response.data["meta"]["fields"]
  2807. assert "string" == meta["count_diff"], "tags should not be counted as integers"
  2808. assert "string" == meta["message"]
  2809. assert "integer" == meta["count()"]
  2810. assert 1 == len(response.data["data"])
  2811. data = response.data["data"][0]
  2812. assert "99" == data["count_diff"]
  2813. assert "Failure state" == data["message"]
  2814. assert 1 == data["count()"]
  2815. def test_aggregate_negation(self):
  2816. data = self.load_data(
  2817. timestamp=self.ten_mins_ago,
  2818. duration=timedelta(seconds=5),
  2819. )
  2820. self.store_event(data, project_id=self.project.id)
  2821. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2822. query = {
  2823. "field": ["event.type", "count()"],
  2824. "query": "event.type:transaction count():1",
  2825. "statsPeriod": "24h",
  2826. }
  2827. response = self.do_request(query, features=features)
  2828. assert response.status_code == 200, response.content
  2829. data = response.data["data"]
  2830. assert len(data) == 1
  2831. query = {
  2832. "field": ["event.type", "count()"],
  2833. "query": "event.type:transaction !count():1",
  2834. "statsPeriod": "24h",
  2835. }
  2836. response = self.do_request(query, features=features)
  2837. assert response.status_code == 200, response.content
  2838. data = response.data["data"]
  2839. assert len(data) == 0
  2840. def test_all_aggregates_in_columns(self):
  2841. data = self.load_data(
  2842. timestamp=self.eleven_mins_ago,
  2843. duration=timedelta(seconds=5),
  2844. )
  2845. data["transaction"] = "/failure_rate/1"
  2846. self.store_event(data, project_id=self.project.id)
  2847. data = self.load_data(
  2848. timestamp=self.ten_mins_ago,
  2849. duration=timedelta(seconds=5),
  2850. )
  2851. data["transaction"] = "/failure_rate/1"
  2852. data["contexts"]["trace"]["status"] = "unauthenticated"
  2853. event = self.store_event(data, project_id=self.project.id)
  2854. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  2855. query = {
  2856. "field": [
  2857. "event.type",
  2858. "p50()",
  2859. "p75()",
  2860. "p95()",
  2861. "p99()",
  2862. "p100()",
  2863. "percentile(transaction.duration, 0.99)",
  2864. "apdex(300)",
  2865. "count_miserable(user, 300)",
  2866. "user_misery(300)",
  2867. "failure_rate()",
  2868. ],
  2869. "query": "event.type:transaction",
  2870. }
  2871. response = self.do_request(query, features=features)
  2872. assert response.status_code == 200, response.content
  2873. meta = response.data["meta"]["fields"]
  2874. units = response.data["meta"]["units"]
  2875. assert meta["p50()"] == "duration"
  2876. assert meta["p75()"] == "duration"
  2877. assert meta["p95()"] == "duration"
  2878. assert meta["p99()"] == "duration"
  2879. assert meta["p100()"] == "duration"
  2880. assert meta["percentile(transaction.duration, 0.99)"] == "duration"
  2881. assert meta["apdex(300)"] == "number"
  2882. assert meta["failure_rate()"] == "percentage"
  2883. assert meta["user_misery(300)"] == "number"
  2884. assert meta["count_miserable(user, 300)"] == "integer"
  2885. assert units["p50()"] == "millisecond"
  2886. assert units["p75()"] == "millisecond"
  2887. assert units["p95()"] == "millisecond"
  2888. assert units["p99()"] == "millisecond"
  2889. assert units["p100()"] == "millisecond"
  2890. assert units["percentile(transaction.duration, 0.99)"] == "millisecond"
  2891. data = response.data["data"]
  2892. assert len(data) == 1
  2893. assert data[0]["p50()"] == 5000
  2894. assert data[0]["p75()"] == 5000
  2895. assert data[0]["p95()"] == 5000
  2896. assert data[0]["p99()"] == 5000
  2897. assert data[0]["p100()"] == 5000
  2898. assert data[0]["percentile(transaction.duration, 0.99)"] == 5000
  2899. assert data[0]["apdex(300)"] == 0.0
  2900. assert data[0]["count_miserable(user, 300)"] == 1
  2901. assert data[0]["user_misery(300)"] == 0.058
  2902. assert data[0]["failure_rate()"] == 0.5
  2903. features = {
  2904. "organizations:discover-basic": True,
  2905. "organizations:global-views": True,
  2906. }
  2907. query = {
  2908. "field": [
  2909. "event.type",
  2910. "p50()",
  2911. "p75()",
  2912. "p95()",
  2913. "p99()",
  2914. "p100()",
  2915. "percentile(transaction.duration, 0.99)",
  2916. "apdex(300)",
  2917. "apdex()",
  2918. "count_miserable(user, 300)",
  2919. "user_misery(300)",
  2920. "failure_rate()",
  2921. "count_miserable(user)",
  2922. "user_misery()",
  2923. ],
  2924. "query": "event.type:transaction",
  2925. "project": [self.project.id],
  2926. }
  2927. response = self.do_request(query, features=features)
  2928. assert response.status_code == 200, response.content
  2929. meta = response.data["meta"]["fields"]
  2930. units = response.data["meta"]["units"]
  2931. assert meta["p50()"] == "duration"
  2932. assert meta["p75()"] == "duration"
  2933. assert meta["p95()"] == "duration"
  2934. assert meta["p99()"] == "duration"
  2935. assert meta["p100()"] == "duration"
  2936. assert meta["percentile(transaction.duration, 0.99)"] == "duration"
  2937. assert meta["apdex(300)"] == "number"
  2938. assert meta["apdex()"] == "number"
  2939. assert meta["failure_rate()"] == "percentage"
  2940. assert meta["user_misery(300)"] == "number"
  2941. assert meta["count_miserable(user, 300)"] == "integer"
  2942. assert meta["project_threshold_config"] == "string"
  2943. assert meta["user_misery()"] == "number"
  2944. assert meta["count_miserable(user)"] == "integer"
  2945. assert units["p50()"] == "millisecond"
  2946. assert units["p75()"] == "millisecond"
  2947. assert units["p95()"] == "millisecond"
  2948. assert units["p99()"] == "millisecond"
  2949. assert units["p100()"] == "millisecond"
  2950. assert units["percentile(transaction.duration, 0.99)"] == "millisecond"
  2951. data = response.data["data"]
  2952. assert len(data) == 1
  2953. assert data[0]["p50()"] == 5000
  2954. assert data[0]["p75()"] == 5000
  2955. assert data[0]["p95()"] == 5000
  2956. assert data[0]["p99()"] == 5000
  2957. assert data[0]["p100()"] == 5000
  2958. assert data[0]["percentile(transaction.duration, 0.99)"] == 5000
  2959. assert data[0]["apdex(300)"] == 0.0
  2960. assert data[0]["apdex()"] == 0.0
  2961. assert data[0]["count_miserable(user, 300)"] == 1
  2962. assert data[0]["user_misery(300)"] == 0.058
  2963. assert data[0]["failure_rate()"] == 0.5
  2964. assert data[0]["project_threshold_config"] == ["duration", 300]
  2965. assert data[0]["user_misery()"] == 0.058
  2966. assert data[0]["count_miserable(user)"] == 1
  2967. query = {
  2968. "field": ["event.type", "last_seen()", "latest_event()"],
  2969. "query": "event.type:transaction",
  2970. }
  2971. response = self.do_request(query, features=features)
  2972. assert response.status_code == 200, response.content
  2973. data = response.data["data"]
  2974. assert len(data) == 1
  2975. assert self.ten_mins_ago_iso[:-5] in data[0]["last_seen()"]
  2976. assert data[0]["latest_event()"] == event.event_id
  2977. query = {
  2978. "field": [
  2979. "event.type",
  2980. "count()",
  2981. "count(id)",
  2982. "count_unique(project)",
  2983. "min(transaction.duration)",
  2984. "max(transaction.duration)",
  2985. "avg(transaction.duration)",
  2986. "stddev(transaction.duration)",
  2987. "var(transaction.duration)",
  2988. "cov(transaction.duration, transaction.duration)",
  2989. "corr(transaction.duration, transaction.duration)",
  2990. "linear_regression(transaction.duration, transaction.duration)",
  2991. "sum(transaction.duration)",
  2992. ],
  2993. "query": "event.type:transaction",
  2994. }
  2995. response = self.do_request(query, features=features)
  2996. assert response.status_code == 200, response.content
  2997. data = response.data["data"]
  2998. assert len(data) == 1
  2999. assert data[0]["count()"] == 2
  3000. assert data[0]["count(id)"] == 2
  3001. assert data[0]["count_unique(project)"] == 1
  3002. assert data[0]["min(transaction.duration)"] == 5000
  3003. assert data[0]["max(transaction.duration)"] == 5000
  3004. assert data[0]["avg(transaction.duration)"] == 5000
  3005. assert data[0]["stddev(transaction.duration)"] == 0.0
  3006. assert data[0]["var(transaction.duration)"] == 0.0
  3007. assert data[0]["cov(transaction.duration, transaction.duration)"] == 0.0
  3008. assert data[0]["corr(transaction.duration, transaction.duration)"] == 0.0
  3009. assert data[0]["linear_regression(transaction.duration, transaction.duration)"] == [0, 0]
  3010. assert data[0]["sum(transaction.duration)"] == 10000
  3011. @requires_not_arm64
  3012. def test_null_user_misery_returns_zero(self):
  3013. self.transaction_data["user"] = None
  3014. self.transaction_data["transaction"] = "/no_users/1"
  3015. self.store_event(self.transaction_data, project_id=self.project.id)
  3016. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3017. query = {
  3018. "field": ["user_misery(300)"],
  3019. "query": "event.type:transaction",
  3020. }
  3021. response = self.do_request(query, features=features)
  3022. assert response.status_code == 200, response.content
  3023. meta = response.data["meta"]["fields"]
  3024. assert meta["user_misery(300)"] == "number"
  3025. data = response.data["data"]
  3026. assert data[0]["user_misery(300)"] == 0
  3027. @requires_not_arm64
  3028. def test_null_user_misery_new_returns_zero(self):
  3029. self.transaction_data["user"] = None
  3030. self.transaction_data["transaction"] = "/no_users/1"
  3031. self.store_event(self.transaction_data, project_id=self.project.id)
  3032. features = {
  3033. "organizations:discover-basic": True,
  3034. }
  3035. query = {
  3036. "field": ["user_misery()"],
  3037. "query": "event.type:transaction",
  3038. }
  3039. response = self.do_request(query, features=features)
  3040. assert response.status_code == 200, response.content
  3041. meta = response.data["meta"]["fields"]
  3042. assert meta["user_misery()"] == "number"
  3043. data = response.data["data"]
  3044. assert data[0]["user_misery()"] == 0
  3045. def test_all_aggregates_in_query(self):
  3046. data = self.load_data(
  3047. timestamp=self.eleven_mins_ago,
  3048. duration=timedelta(seconds=5),
  3049. )
  3050. data["transaction"] = "/failure_rate/1"
  3051. self.store_event(data, project_id=self.project.id)
  3052. data = self.load_data(
  3053. timestamp=self.ten_mins_ago,
  3054. duration=timedelta(seconds=5),
  3055. )
  3056. data["transaction"] = "/failure_rate/2"
  3057. data["contexts"]["trace"]["status"] = "unauthenticated"
  3058. self.store_event(data, project_id=self.project.id)
  3059. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3060. query = {
  3061. "field": [
  3062. "event.type",
  3063. "p50()",
  3064. "p75()",
  3065. "p95()",
  3066. "percentile(transaction.duration, 0.99)",
  3067. "p100()",
  3068. ],
  3069. "query": "event.type:transaction p50():>100 p75():>1000 p95():>1000 p100():>1000 percentile(transaction.duration, 0.99):>1000",
  3070. }
  3071. response = self.do_request(query, features=features)
  3072. assert response.status_code == 200, response.content
  3073. data = response.data["data"]
  3074. assert len(data) == 1
  3075. assert data[0]["p50()"] == 5000
  3076. assert data[0]["p75()"] == 5000
  3077. assert data[0]["p95()"] == 5000
  3078. assert data[0]["p100()"] == 5000
  3079. assert data[0]["percentile(transaction.duration, 0.99)"] == 5000
  3080. query = {
  3081. "field": [
  3082. "event.type",
  3083. "apdex(300)",
  3084. "count_miserable(user, 300)",
  3085. "user_misery(300)",
  3086. "failure_rate()",
  3087. ],
  3088. "query": "event.type:transaction apdex(300):>-1.0 failure_rate():>0.25",
  3089. }
  3090. response = self.do_request(query, features=features)
  3091. assert response.status_code == 200, response.content
  3092. data = response.data["data"]
  3093. assert len(data) == 1
  3094. assert data[0]["apdex(300)"] == 0.0
  3095. assert data[0]["count_miserable(user, 300)"] == 1
  3096. assert data[0]["user_misery(300)"] == 0.058
  3097. assert data[0]["failure_rate()"] == 0.5
  3098. query = {
  3099. "field": ["event.type", "last_seen()", "latest_event()"],
  3100. "query": "event.type:transaction last_seen():>1990-12-01T00:00:00",
  3101. }
  3102. response = self.do_request(query, features=features)
  3103. assert response.status_code == 200, response.content
  3104. data = response.data["data"]
  3105. assert len(data) == 1
  3106. query = {
  3107. "field": ["event.type", "count()", "count(id)", "count_unique(transaction)"],
  3108. "query": "event.type:transaction count():>1 count(id):>1 count_unique(transaction):>1",
  3109. }
  3110. response = self.do_request(query, features=features)
  3111. assert response.status_code == 200, response.content
  3112. data = response.data["data"]
  3113. assert len(data) == 1
  3114. assert data[0]["count()"] == 2
  3115. assert data[0]["count(id)"] == 2
  3116. assert data[0]["count_unique(transaction)"] == 2
  3117. query = {
  3118. "field": [
  3119. "event.type",
  3120. "min(transaction.duration)",
  3121. "max(transaction.duration)",
  3122. "avg(transaction.duration)",
  3123. "sum(transaction.duration)",
  3124. "stddev(transaction.duration)",
  3125. "var(transaction.duration)",
  3126. "cov(transaction.duration, transaction.duration)",
  3127. "corr(transaction.duration, transaction.duration)",
  3128. ],
  3129. "query": " ".join(
  3130. [
  3131. "event.type:transaction",
  3132. "min(transaction.duration):>1000",
  3133. "max(transaction.duration):>1000",
  3134. "avg(transaction.duration):>1000",
  3135. "sum(transaction.duration):>1000",
  3136. "stddev(transaction.duration):>=0.0",
  3137. "var(transaction.duration):>=0.0",
  3138. "cov(transaction.duration, transaction.duration):>=0.0",
  3139. # correlation is nan because variance is 0
  3140. # "corr(transaction.duration, transaction.duration):>=0.0",
  3141. ]
  3142. ),
  3143. }
  3144. response = self.do_request(query, features=features)
  3145. assert response.status_code == 200, response.content
  3146. data = response.data["data"]
  3147. assert len(data) == 1
  3148. assert data[0]["min(transaction.duration)"] == 5000
  3149. assert data[0]["max(transaction.duration)"] == 5000
  3150. assert data[0]["avg(transaction.duration)"] == 5000
  3151. assert data[0]["sum(transaction.duration)"] == 10000
  3152. assert data[0]["stddev(transaction.duration)"] == 0.0
  3153. assert data[0]["var(transaction.duration)"] == 0.0
  3154. assert data[0]["cov(transaction.duration, transaction.duration)"] == 0.0
  3155. assert data[0]["corr(transaction.duration, transaction.duration)"] == 0.0
  3156. query = {
  3157. "field": ["event.type", "apdex(400)"],
  3158. "query": "event.type:transaction apdex(400):0",
  3159. }
  3160. response = self.do_request(query, features=features)
  3161. assert response.status_code == 200, response.content
  3162. data = response.data["data"]
  3163. assert len(data) == 1
  3164. assert data[0]["apdex(400)"] == 0
  3165. def test_functions_in_orderby(self):
  3166. data = self.load_data(
  3167. timestamp=self.eleven_mins_ago,
  3168. duration=timedelta(seconds=5),
  3169. )
  3170. data["transaction"] = "/failure_rate/1"
  3171. self.store_event(data, project_id=self.project.id)
  3172. data = self.load_data(
  3173. timestamp=self.ten_mins_ago,
  3174. duration=timedelta(seconds=5),
  3175. )
  3176. data["transaction"] = "/failure_rate/2"
  3177. data["contexts"]["trace"]["status"] = "unauthenticated"
  3178. event = self.store_event(data, project_id=self.project.id)
  3179. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3180. query = {
  3181. "field": ["event.type", "p75()"],
  3182. "sort": "-p75",
  3183. "query": "event.type:transaction",
  3184. }
  3185. response = self.do_request(query, features=features)
  3186. assert response.status_code == 200, response.content
  3187. data = response.data["data"]
  3188. assert len(data) == 1
  3189. assert data[0]["p75()"] == 5000
  3190. query = {
  3191. "field": ["event.type", "percentile(transaction.duration, 0.99)"],
  3192. "sort": "-percentile_transaction_duration_0_99",
  3193. "query": "event.type:transaction",
  3194. }
  3195. response = self.do_request(query, features=features)
  3196. assert response.status_code == 200, response.content
  3197. data = response.data["data"]
  3198. assert len(data) == 1
  3199. assert data[0]["percentile(transaction.duration, 0.99)"] == 5000
  3200. query = {
  3201. "field": ["event.type", "apdex(300)"],
  3202. "sort": "-apdex(300)",
  3203. "query": "event.type:transaction",
  3204. }
  3205. response = self.do_request(query, features=features)
  3206. assert response.status_code == 200, response.content
  3207. data = response.data["data"]
  3208. assert len(data) == 1
  3209. assert data[0]["apdex(300)"] == 0.0
  3210. query = {
  3211. "field": ["event.type", "latest_event()"],
  3212. "query": "event.type:transaction",
  3213. "sort": "latest_event",
  3214. }
  3215. response = self.do_request(query, features=features)
  3216. assert response.status_code == 200, response.content
  3217. data = response.data["data"]
  3218. assert len(data) == 1
  3219. assert data[0]["latest_event()"] == event.event_id
  3220. query = {
  3221. "field": ["event.type", "count_unique(transaction)"],
  3222. "query": "event.type:transaction",
  3223. "sort": "-count_unique_transaction",
  3224. }
  3225. response = self.do_request(query, features=features)
  3226. assert response.status_code == 200, response.content
  3227. data = response.data["data"]
  3228. assert len(data) == 1
  3229. assert data[0]["count_unique(transaction)"] == 2
  3230. query = {
  3231. "field": ["event.type", "min(transaction.duration)"],
  3232. "query": "event.type:transaction",
  3233. "sort": "-min_transaction_duration",
  3234. }
  3235. response = self.do_request(query, features=features)
  3236. assert response.status_code == 200, response.content
  3237. data = response.data["data"]
  3238. assert len(data) == 1
  3239. assert data[0]["min(transaction.duration)"] == 5000
  3240. def test_issue_alias_in_aggregate(self):
  3241. self.store_event(
  3242. data={
  3243. "event_id": "a" * 32,
  3244. "timestamp": self.eleven_mins_ago_iso,
  3245. "fingerprint": ["group_1"],
  3246. },
  3247. project_id=self.project.id,
  3248. )
  3249. self.store_event(
  3250. data={
  3251. "event_id": "b" * 32,
  3252. "timestamp": self.ten_mins_ago_iso,
  3253. "fingerprint": ["group_2"],
  3254. },
  3255. project_id=self.project.id,
  3256. )
  3257. query = {"field": ["event.type", "count_unique(issue)"], "query": "count_unique(issue):>1"}
  3258. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3259. response = self.do_request(query, features=features)
  3260. assert response.status_code == 200, response.content
  3261. data = response.data["data"]
  3262. assert len(data) == 1
  3263. assert data[0]["count_unique(issue)"] == 2
  3264. def test_deleted_issue_in_results(self):
  3265. event1 = self.store_event(
  3266. data={
  3267. "event_id": "a" * 32,
  3268. "timestamp": self.eleven_mins_ago_iso,
  3269. "fingerprint": ["group_1"],
  3270. },
  3271. project_id=self.project.id,
  3272. )
  3273. event2 = self.store_event(
  3274. data={
  3275. "event_id": "b" * 32,
  3276. "timestamp": self.ten_mins_ago_iso,
  3277. "fingerprint": ["group_2"],
  3278. },
  3279. project_id=self.project.id,
  3280. )
  3281. event2.group.delete()
  3282. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3283. query = {"field": ["issue", "count()"], "sort": "issue.id"}
  3284. response = self.do_request(query, features=features)
  3285. assert response.status_code == 200, response.content
  3286. data = response.data["data"]
  3287. assert len(data) == 2
  3288. assert data[0]["issue"] == event1.group.qualified_short_id
  3289. assert data[1]["issue"] == "unknown"
  3290. def test_last_seen_negative_duration(self):
  3291. self.store_event(
  3292. data={
  3293. "event_id": "f" * 32,
  3294. "timestamp": self.ten_mins_ago_iso,
  3295. "fingerprint": ["group_1"],
  3296. },
  3297. project_id=self.project.id,
  3298. )
  3299. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3300. query = {"field": ["id", "last_seen()"], "query": "last_seen():-30d"}
  3301. response = self.do_request(query, features=features)
  3302. assert response.status_code == 200, response.content
  3303. data = response.data["data"]
  3304. assert len(data) == 1
  3305. assert data[0]["id"] == "f" * 32
  3306. def test_last_seen_aggregate_condition(self):
  3307. self.store_event(
  3308. data={
  3309. "event_id": "f" * 32,
  3310. "timestamp": self.ten_mins_ago_iso,
  3311. "fingerprint": ["group_1"],
  3312. },
  3313. project_id=self.project.id,
  3314. )
  3315. query = {
  3316. "field": ["id", "last_seen()"],
  3317. "query": f"last_seen():>{iso_format(before_now(days=30))}",
  3318. }
  3319. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3320. response = self.do_request(query, features=features)
  3321. assert response.status_code == 200, response.content
  3322. data = response.data["data"]
  3323. assert len(data) == 1
  3324. assert data[0]["id"] == "f" * 32
  3325. def test_conditional_filter(self):
  3326. for v in ["a", "b"]:
  3327. self.store_event(
  3328. data={
  3329. "event_id": v * 32,
  3330. "timestamp": self.ten_mins_ago_iso,
  3331. "fingerprint": ["group_1"],
  3332. },
  3333. project_id=self.project.id,
  3334. )
  3335. query = {
  3336. "field": ["id"],
  3337. "query": "id:{} OR id:{}".format("a" * 32, "b" * 32),
  3338. "orderby": "id",
  3339. }
  3340. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3341. response = self.do_request(query, features=features)
  3342. assert response.status_code == 200, response.content
  3343. data = response.data["data"]
  3344. assert len(data) == 2
  3345. assert data[0]["id"] == "a" * 32
  3346. assert data[1]["id"] == "b" * 32
  3347. def test_aggregation_comparison_with_conditional_filter(self):
  3348. self.store_event(
  3349. data={
  3350. "event_id": "a" * 32,
  3351. "timestamp": self.ten_mins_ago_iso,
  3352. "fingerprint": ["group_1"],
  3353. "user": {"email": "foo@example.com"},
  3354. "environment": "prod",
  3355. },
  3356. project_id=self.project.id,
  3357. )
  3358. self.store_event(
  3359. data={
  3360. "event_id": "b" * 32,
  3361. "timestamp": self.ten_mins_ago_iso,
  3362. "fingerprint": ["group_2"],
  3363. "user": {"email": "foo@example.com"},
  3364. "environment": "staging",
  3365. },
  3366. project_id=self.project.id,
  3367. )
  3368. event = self.store_event(
  3369. data={
  3370. "event_id": "c" * 32,
  3371. "timestamp": self.ten_mins_ago_iso,
  3372. "fingerprint": ["group_2"],
  3373. "user": {"email": "foo@example.com"},
  3374. "environment": "prod",
  3375. },
  3376. project_id=self.project.id,
  3377. )
  3378. self.store_event(
  3379. data={
  3380. "event_id": "d" * 32,
  3381. "timestamp": self.ten_mins_ago_iso,
  3382. "fingerprint": ["group_2"],
  3383. "user": {"email": "foo@example.com"},
  3384. "environment": "canary",
  3385. },
  3386. project_id=self.project.id,
  3387. )
  3388. query = {
  3389. "field": ["issue.id", "count(id)"],
  3390. "query": "count(id):>1 user.email:foo@example.com AND (environment:prod OR environment:staging)",
  3391. "orderby": "issue.id",
  3392. }
  3393. response = self.do_request(query)
  3394. assert response.status_code == 200, response.content
  3395. assert len(response.data["data"]) == 1
  3396. data = response.data["data"]
  3397. assert data[0]["issue.id"] == event.group_id
  3398. assert data[0]["count(id)"] == 2
  3399. def run_test_in_query(self, query, expected_events, expected_negative_events=None):
  3400. params = {
  3401. "field": ["id"],
  3402. "query": query,
  3403. "orderby": "id",
  3404. }
  3405. response = self.do_request(
  3406. params, {"organizations:discover-basic": True, "organizations:global-views": True}
  3407. )
  3408. assert response.status_code == 200, response.content
  3409. assert [row["id"] for row in response.data["data"]] == [e.event_id for e in expected_events]
  3410. if expected_negative_events is not None:
  3411. params["query"] = f"!{query}"
  3412. response = self.do_request(
  3413. params,
  3414. {"organizations:discover-basic": True, "organizations:global-views": True},
  3415. )
  3416. assert response.status_code == 200, response.content
  3417. assert [row["id"] for row in response.data["data"]] == [
  3418. e.event_id for e in expected_negative_events
  3419. ]
  3420. def test_in_query_events(self):
  3421. project_1 = self.create_project()
  3422. event_1 = self.store_event(
  3423. data={
  3424. "event_id": "a" * 32,
  3425. "timestamp": self.ten_mins_ago_iso,
  3426. "fingerprint": ["group_1"],
  3427. "message": "group1",
  3428. "user": {"email": "hello@example.com"},
  3429. "environment": "prod",
  3430. "tags": {"random": "123"},
  3431. "release": "1.0",
  3432. },
  3433. project_id=project_1.id,
  3434. )
  3435. project_2 = self.create_project()
  3436. event_2 = self.store_event(
  3437. data={
  3438. "event_id": "b" * 32,
  3439. "timestamp": self.ten_mins_ago_iso,
  3440. "fingerprint": ["group_2"],
  3441. "message": "group2",
  3442. "user": {"email": "bar@example.com"},
  3443. "environment": "staging",
  3444. "tags": {"random": "456"},
  3445. "stacktrace": {"frames": [{"filename": "src/app/group2.py"}]},
  3446. "release": "1.2",
  3447. },
  3448. project_id=project_2.id,
  3449. )
  3450. project_3 = self.create_project()
  3451. event_3 = self.store_event(
  3452. data={
  3453. "event_id": "c" * 32,
  3454. "timestamp": self.ten_mins_ago_iso,
  3455. "fingerprint": ["group_3"],
  3456. "message": "group3",
  3457. "user": {"email": "foo@example.com"},
  3458. "environment": "canary",
  3459. "tags": {"random": "789"},
  3460. },
  3461. project_id=project_3.id,
  3462. )
  3463. self.run_test_in_query("environment:[prod, staging]", [event_1, event_2], [event_3])
  3464. self.run_test_in_query("environment:[staging]", [event_2], [event_1, event_3])
  3465. self.run_test_in_query(
  3466. "user.email:[foo@example.com, hello@example.com]", [event_1, event_3], [event_2]
  3467. )
  3468. self.run_test_in_query("user.email:[foo@example.com]", [event_3], [event_1, event_2])
  3469. self.run_test_in_query(
  3470. "user.display:[foo@example.com, hello@example.com]", [event_1, event_3], [event_2]
  3471. )
  3472. self.run_test_in_query(
  3473. 'message:["group2 src/app/group2.py in ?", group1]', [event_1, event_2], [event_3]
  3474. )
  3475. self.run_test_in_query(
  3476. f"issue.id:[{event_1.group_id},{event_2.group_id}]", [event_1, event_2]
  3477. )
  3478. self.run_test_in_query(
  3479. f"issue:[{event_1.group.qualified_short_id},{event_2.group.qualified_short_id}]",
  3480. [event_1, event_2],
  3481. )
  3482. self.run_test_in_query(
  3483. f"issue:[{event_1.group.qualified_short_id},{event_2.group.qualified_short_id}, unknown]",
  3484. [event_1, event_2],
  3485. )
  3486. self.run_test_in_query(f"project_id:[{project_3.id},{project_2.id}]", [event_2, event_3])
  3487. self.run_test_in_query(
  3488. f"project.name:[{project_3.slug},{project_2.slug}]", [event_2, event_3]
  3489. )
  3490. self.run_test_in_query("random:[789,456]", [event_2, event_3], [event_1])
  3491. self.run_test_in_query("tags[random]:[789,456]", [event_2, event_3], [event_1])
  3492. self.run_test_in_query("release:[1.0,1.2]", [event_1, event_2], [event_3])
  3493. def test_in_query_events_stack(self):
  3494. test_js = self.store_event(
  3495. self.load_data(
  3496. platform="javascript",
  3497. timestamp=self.ten_mins_ago,
  3498. duration=timedelta(seconds=5),
  3499. ),
  3500. project_id=self.project.id,
  3501. )
  3502. test_java = self.store_event(
  3503. self.load_data(
  3504. platform="java",
  3505. timestamp=self.ten_mins_ago,
  3506. duration=timedelta(seconds=5),
  3507. ),
  3508. project_id=self.project.id,
  3509. )
  3510. self.run_test_in_query(
  3511. "stack.filename:[../../sentry/scripts/views.js]", [test_js], [test_java]
  3512. )
  3513. def test_in_query_transactions(self):
  3514. data = self.transaction_data.copy()
  3515. data["event_id"] = "a" * 32
  3516. data["contexts"]["trace"]["status"] = "ok"
  3517. transaction_1 = self.store_event(data, project_id=self.project.id)
  3518. data = self.transaction_data.copy()
  3519. data["event_id"] = "b" * 32
  3520. data["contexts"]["trace"]["status"] = "aborted"
  3521. transaction_2 = self.store_event(data, project_id=self.project.id)
  3522. data = self.transaction_data.copy()
  3523. data["event_id"] = "c" * 32
  3524. data["contexts"]["trace"]["status"] = "already_exists"
  3525. transaction_3 = self.store_event(data, project_id=self.project.id)
  3526. self.run_test_in_query(
  3527. "transaction.status:[aborted, already_exists]",
  3528. [transaction_2, transaction_3],
  3529. [transaction_1],
  3530. )
  3531. def test_messed_up_function_values(self):
  3532. # TODO (evanh): It would be nice if this surfaced an error to the user.
  3533. # The problem: The && causes the parser to treat that term not as a bad
  3534. # function call but a valid raw search with parens in it. It's not trivial
  3535. # to change the parser to recognize "bad function values" and surface them.
  3536. for v in ["a", "b"]:
  3537. self.store_event(
  3538. data={
  3539. "event_id": v * 32,
  3540. "timestamp": self.ten_mins_ago_iso,
  3541. "fingerprint": ["group_1"],
  3542. },
  3543. project_id=self.project.id,
  3544. )
  3545. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  3546. query = {
  3547. "field": [
  3548. "transaction",
  3549. "project",
  3550. "epm()",
  3551. "p50()",
  3552. "p95()",
  3553. "failure_rate()",
  3554. "apdex(300)",
  3555. "count_unique(user)",
  3556. "user_misery(300)",
  3557. "count_miserable(user, 300)",
  3558. ],
  3559. "query": "failure_rate():>0.003&& users:>10 event.type:transaction",
  3560. "sort": "-failure_rate",
  3561. "statsPeriod": "24h",
  3562. }
  3563. response = self.do_request(query, features=features)
  3564. assert response.status_code == 200, response.content
  3565. data = response.data["data"]
  3566. assert len(data) == 0
  3567. def test_context_fields_between_datasets(self):
  3568. event_data = self.load_data(platform="android")
  3569. transaction_data = self.load_data()
  3570. event_data["spans"] = transaction_data["spans"]
  3571. event_data["contexts"]["trace"] = transaction_data["contexts"]["trace"]
  3572. event_data["type"] = "transaction"
  3573. event_data["transaction"] = "/failure_rate/1"
  3574. event_data["timestamp"] = iso_format(self.ten_mins_ago)
  3575. event_data["start_timestamp"] = iso_format(before_now(minutes=10, seconds=5))
  3576. event_data["user"]["geo"] = {"country_code": "US", "region": "CA", "city": "San Francisco"}
  3577. self.store_event(event_data, project_id=self.project.id)
  3578. event_data["type"] = "error"
  3579. self.store_event(event_data, project_id=self.project.id)
  3580. fields = [
  3581. "os.build",
  3582. "os.kernel_version",
  3583. "device.arch",
  3584. # TODO: battery level is not consistent across both datasets
  3585. # "device.battery_level",
  3586. "device.brand",
  3587. "device.charging",
  3588. "device.locale",
  3589. "device.model_id",
  3590. "device.name",
  3591. "device.online",
  3592. "device.orientation",
  3593. "device.simulator",
  3594. "device.uuid",
  3595. ]
  3596. data = [
  3597. {"field": fields + ["location", "count()"], "query": "event.type:error"},
  3598. {"field": fields + ["duration", "count()"], "query": "event.type:transaction"},
  3599. ]
  3600. for datum in data:
  3601. response = self.do_request(datum)
  3602. assert response.status_code == 200, response.content
  3603. assert len(response.data["data"]) == 1, datum
  3604. results = response.data["data"]
  3605. assert results[0]["count()"] == 1, datum
  3606. for field in fields:
  3607. key, value = field.split(".", 1)
  3608. expected = str(event_data["contexts"][key][value])
  3609. assert results[0][field] == expected, field + str(datum)
  3610. def test_http_fields_between_datasets(self):
  3611. event_data = self.load_data(platform="android")
  3612. transaction_data = self.load_data()
  3613. event_data["spans"] = transaction_data["spans"]
  3614. event_data["contexts"]["trace"] = transaction_data["contexts"]["trace"]
  3615. event_data["type"] = "transaction"
  3616. event_data["transaction"] = "/failure_rate/1"
  3617. event_data["timestamp"] = iso_format(self.ten_mins_ago)
  3618. event_data["start_timestamp"] = iso_format(before_now(minutes=10, seconds=5))
  3619. event_data["user"]["geo"] = {"country_code": "US", "region": "CA", "city": "San Francisco"}
  3620. event_data["request"] = transaction_data["request"]
  3621. self.store_event(event_data, project_id=self.project.id)
  3622. event_data["type"] = "error"
  3623. self.store_event(event_data, project_id=self.project.id)
  3624. fields = ["http.method", "http.referer", "http.url"]
  3625. expected = ["GET", "fixtures.transaction", "http://countries:8010/country_by_code/"]
  3626. data = [
  3627. {"field": fields + ["location", "count()"], "query": "event.type:error"},
  3628. {"field": fields + ["duration", "count()"], "query": "event.type:transaction"},
  3629. ]
  3630. for datum in data:
  3631. response = self.do_request(datum)
  3632. assert response.status_code == 200, response.content
  3633. assert len(response.data["data"]) == 1, datum
  3634. results = response.data["data"]
  3635. assert results[0]["count()"] == 1, datum
  3636. for (field, exp) in zip(fields, expected):
  3637. assert results[0][field] == exp, field + str(datum)
  3638. def test_failure_count_alias_field(self):
  3639. data = self.transaction_data.copy()
  3640. data["transaction"] = "/failure_count/success"
  3641. self.store_event(data, project_id=self.project.id)
  3642. data = self.transaction_data.copy()
  3643. data["transaction"] = "/failure_count/unknown"
  3644. data["contexts"]["trace"]["status"] = "unknown_error"
  3645. self.store_event(data, project_id=self.project.id)
  3646. for i in range(6):
  3647. data = self.transaction_data.copy()
  3648. data["transaction"] = f"/failure_count/{i}"
  3649. data["contexts"]["trace"]["status"] = "unauthenticated"
  3650. self.store_event(data, project_id=self.project.id)
  3651. query = {"field": ["count()", "failure_count()"], "query": "event.type:transaction"}
  3652. response = self.do_request(query)
  3653. assert response.status_code == 200, response.content
  3654. assert len(response.data["data"]) == 1
  3655. data = response.data["data"]
  3656. assert data[0]["count()"] == 8
  3657. assert data[0]["failure_count()"] == 6
  3658. @mock.patch("sentry.utils.snuba.quantize_time")
  3659. def test_quantize_dates(self, mock_quantize):
  3660. self.create_project()
  3661. mock_quantize.return_value = before_now(days=1).replace(tzinfo=timezone.utc)
  3662. # Don't quantize short time periods
  3663. query = {"statsPeriod": "1h", "query": "", "field": ["id", "timestamp"]}
  3664. self.do_request(query)
  3665. # Don't quantize absolute date periods
  3666. self.do_request(query)
  3667. query = {
  3668. "start": iso_format(before_now(days=20)),
  3669. "end": iso_format(before_now(days=15)),
  3670. "query": "",
  3671. "field": ["id", "timestamp"],
  3672. }
  3673. self.do_request(query)
  3674. assert len(mock_quantize.mock_calls) == 0
  3675. # Quantize long date periods
  3676. query = {"field": ["id", "timestamp"], "statsPeriod": "90d", "query": ""}
  3677. self.do_request(query)
  3678. assert len(mock_quantize.mock_calls) == 2
  3679. def test_limit_number_of_fields(self):
  3680. self.create_project()
  3681. for i in range(1, 25):
  3682. response = self.do_request({"field": ["id"] * i})
  3683. if i <= 20:
  3684. assert response.status_code == 200
  3685. else:
  3686. assert response.status_code == 400
  3687. assert (
  3688. response.data["detail"]
  3689. == "You can view up to 20 fields at a time. Please delete some and try again."
  3690. )
  3691. def test_percentile_function_meta_types(self):
  3692. self.store_event(self.transaction_data, project_id=self.project.id)
  3693. query = {
  3694. "field": [
  3695. "transaction",
  3696. "percentile(transaction.duration, 0.95)",
  3697. "percentile(measurements.fp, 0.95)",
  3698. "percentile(measurements.fcp, 0.95)",
  3699. "percentile(measurements.lcp, 0.95)",
  3700. "percentile(measurements.fid, 0.95)",
  3701. "percentile(measurements.ttfb, 0.95)",
  3702. "percentile(measurements.ttfb.requesttime, 0.95)",
  3703. "percentile(measurements.cls, 0.95)",
  3704. "percentile(measurements.foo, 0.95)",
  3705. "percentile(measurements.bar, 0.95)",
  3706. ],
  3707. "query": "",
  3708. "orderby": ["transaction"],
  3709. }
  3710. response = self.do_request(query)
  3711. assert response.status_code == 200, response.content
  3712. meta = response.data["meta"]["fields"]
  3713. assert meta["percentile(transaction.duration, 0.95)"] == "duration"
  3714. assert meta["percentile(measurements.fp, 0.95)"] == "duration"
  3715. assert meta["percentile(measurements.fcp, 0.95)"] == "duration"
  3716. assert meta["percentile(measurements.lcp, 0.95)"] == "duration"
  3717. assert meta["percentile(measurements.fid, 0.95)"] == "duration"
  3718. assert meta["percentile(measurements.ttfb, 0.95)"] == "duration"
  3719. assert meta["percentile(measurements.ttfb.requesttime, 0.95)"] == "duration"
  3720. assert meta["percentile(measurements.cls, 0.95)"] == "number"
  3721. assert meta["percentile(measurements.foo, 0.95)"] == "number"
  3722. assert meta["percentile(measurements.bar, 0.95)"] == "number"
  3723. units = response.data["meta"]["units"]
  3724. assert units["percentile(transaction.duration, 0.95)"] == "millisecond"
  3725. assert units["percentile(measurements.fp, 0.95)"] == "millisecond"
  3726. assert units["percentile(measurements.fcp, 0.95)"] == "millisecond"
  3727. assert units["percentile(measurements.lcp, 0.95)"] == "millisecond"
  3728. assert units["percentile(measurements.fid, 0.95)"] == "millisecond"
  3729. assert units["percentile(measurements.ttfb, 0.95)"] == "millisecond"
  3730. assert units["percentile(measurements.ttfb.requesttime, 0.95)"] == "millisecond"
  3731. def test_count_at_least_query(self):
  3732. self.store_event(self.transaction_data, self.project.id)
  3733. response = self.do_request({"field": "count_at_least(measurements.fcp, 0)"})
  3734. assert response.status_code == 200
  3735. assert len(response.data["data"]) == 1
  3736. assert response.data["data"][0]["count_at_least(measurements.fcp, 0)"] == 1
  3737. # a value that's a little bigger than the stored fcp
  3738. fcp = int(self.transaction_data["measurements"]["fcp"]["value"] + 1)
  3739. response = self.do_request({"field": f"count_at_least(measurements.fcp, {fcp})"})
  3740. assert response.status_code == 200
  3741. assert len(response.data["data"]) == 1
  3742. assert response.data["data"][0][f"count_at_least(measurements.fcp, {fcp})"] == 0
  3743. def test_measurements_query(self):
  3744. self.store_event(self.transaction_data, self.project.id)
  3745. query = {
  3746. "field": [
  3747. "measurements.fp",
  3748. "measurements.fcp",
  3749. "measurements.lcp",
  3750. "measurements.fid",
  3751. ]
  3752. }
  3753. response = self.do_request(query)
  3754. assert response.status_code == 200, response.content
  3755. assert len(response.data["data"]) == 1
  3756. for field in query["field"]:
  3757. measure = field.split(".", 1)[1]
  3758. assert (
  3759. response.data["data"][0][field]
  3760. == self.transaction_data["measurements"][measure]["value"]
  3761. )
  3762. query = {
  3763. "field": [
  3764. "measurements.fP",
  3765. "measurements.Fcp",
  3766. "measurements.LcP",
  3767. "measurements.FID",
  3768. ]
  3769. }
  3770. response = self.do_request(query)
  3771. assert response.status_code == 200, response.content
  3772. assert len(response.data["data"]) == 1
  3773. for field in query["field"]:
  3774. measure = field.split(".", 1)[1].lower()
  3775. assert (
  3776. response.data["data"][0][field]
  3777. == self.transaction_data["measurements"][measure]["value"]
  3778. )
  3779. def test_measurements_aggregations(self):
  3780. self.store_event(self.transaction_data, self.project.id)
  3781. # should try all the potential aggregates
  3782. # Skipped tests for stddev and var since sampling one data point
  3783. # results in nan.
  3784. query = {
  3785. "field": [
  3786. "percentile(measurements.fcp, 0.5)",
  3787. "count_unique(measurements.fcp)",
  3788. "min(measurements.fcp)",
  3789. "max(measurements.fcp)",
  3790. "avg(measurements.fcp)",
  3791. "sum(measurements.fcp)",
  3792. ],
  3793. }
  3794. response = self.do_request(query)
  3795. assert response.status_code == 200, response.content
  3796. assert len(response.data["data"]) == 1
  3797. assert (
  3798. response.data["data"][0]["percentile(measurements.fcp, 0.5)"]
  3799. == self.transaction_data["measurements"]["fcp"]["value"]
  3800. )
  3801. assert response.data["data"][0]["count_unique(measurements.fcp)"] == 1
  3802. assert (
  3803. response.data["data"][0]["min(measurements.fcp)"]
  3804. == self.transaction_data["measurements"]["fcp"]["value"]
  3805. )
  3806. assert (
  3807. response.data["data"][0]["max(measurements.fcp)"]
  3808. == self.transaction_data["measurements"]["fcp"]["value"]
  3809. )
  3810. assert (
  3811. response.data["data"][0]["avg(measurements.fcp)"]
  3812. == self.transaction_data["measurements"]["fcp"]["value"]
  3813. )
  3814. assert (
  3815. response.data["data"][0]["sum(measurements.fcp)"]
  3816. == self.transaction_data["measurements"]["fcp"]["value"]
  3817. )
  3818. def get_measurement_condition_response(self, query_str, field):
  3819. query = {
  3820. "field": ["transaction", "count()"] + (field if field else []),
  3821. "query": query_str,
  3822. }
  3823. response = self.do_request(query)
  3824. assert response.status_code == 200, response.content
  3825. return response
  3826. def assert_measurement_condition_without_results(self, query_str, field=None):
  3827. response = self.get_measurement_condition_response(query_str, field)
  3828. assert len(response.data["data"]) == 0
  3829. def assert_measurement_condition_with_results(self, query_str, field=None):
  3830. response = self.get_measurement_condition_response(query_str, field)
  3831. assert len(response.data["data"]) == 1
  3832. assert response.data["data"][0]["transaction"] == self.transaction_data["metadata"]["title"]
  3833. assert response.data["data"][0]["count()"] == 1
  3834. def test_measurements_conditions(self):
  3835. self.store_event(self.transaction_data, self.project.id)
  3836. fcp = self.transaction_data["measurements"]["fcp"]["value"]
  3837. # equality condition
  3838. # We use json dumps here to ensure precision when converting from float to str
  3839. # This is necessary because equality on floating point values need to be precise
  3840. self.assert_measurement_condition_with_results(f"measurements.fcp:{json.dumps(fcp)}")
  3841. # greater than condition
  3842. self.assert_measurement_condition_with_results(f"measurements.fcp:>{fcp - 1}")
  3843. self.assert_measurement_condition_without_results(f"measurements.fcp:>{fcp + 1}")
  3844. # less than condition
  3845. self.assert_measurement_condition_with_results(f"measurements.fcp:<{fcp + 1}")
  3846. self.assert_measurement_condition_without_results(f"measurements.fcp:<{fcp - 1}")
  3847. # has condition
  3848. self.assert_measurement_condition_with_results("has:measurements.fcp")
  3849. self.assert_measurement_condition_without_results("!has:measurements.fcp")
  3850. def test_measurements_aggregation_conditions(self):
  3851. self.store_event(self.transaction_data, self.project.id)
  3852. fcp = self.transaction_data["measurements"]["fcp"]["value"]
  3853. functions = [
  3854. "percentile(measurements.fcp, 0.5)",
  3855. "min(measurements.fcp)",
  3856. "max(measurements.fcp)",
  3857. "avg(measurements.fcp)",
  3858. "sum(measurements.fcp)",
  3859. ]
  3860. for function in functions:
  3861. self.assert_measurement_condition_with_results(
  3862. f"{function}:>{fcp - 1}", field=[function]
  3863. )
  3864. self.assert_measurement_condition_without_results(
  3865. f"{function}:>{fcp + 1}", field=[function]
  3866. )
  3867. self.assert_measurement_condition_with_results(
  3868. f"{function}:<{fcp + 1}", field=[function]
  3869. )
  3870. self.assert_measurement_condition_without_results(
  3871. f"{function}:<{fcp - 1}", field=[function]
  3872. )
  3873. count_unique = "count_unique(measurements.fcp)"
  3874. self.assert_measurement_condition_with_results(f"{count_unique}:1", field=[count_unique])
  3875. self.assert_measurement_condition_without_results(f"{count_unique}:0", field=[count_unique])
  3876. def test_compare_numeric_aggregate(self):
  3877. self.store_event(self.transaction_data, self.project.id)
  3878. query = {
  3879. "field": [
  3880. "p75(measurements.fcp)",
  3881. "compare_numeric_aggregate(p75_measurements_fcp,greater,0)",
  3882. ],
  3883. }
  3884. response = self.do_request(query)
  3885. assert response.status_code == 200, response.content
  3886. assert len(response.data["data"]) == 1
  3887. assert (
  3888. response.data["data"][0]["compare_numeric_aggregate(p75_measurements_fcp,greater,0)"]
  3889. == 1
  3890. )
  3891. query = {
  3892. "field": ["p75()", "compare_numeric_aggregate(p75,equals,0)"],
  3893. }
  3894. response = self.do_request(query)
  3895. assert response.status_code == 200, response.content
  3896. assert len(response.data["data"]) == 1
  3897. assert response.data["data"][0]["compare_numeric_aggregate(p75,equals,0)"] == 0
  3898. def test_no_team_key_transactions(self):
  3899. transactions = [
  3900. "/blah_transaction/",
  3901. "/foo_transaction/",
  3902. "/zoo_transaction/",
  3903. ]
  3904. for transaction in transactions:
  3905. self.transaction_data["transaction"] = transaction
  3906. self.store_event(self.transaction_data, self.project.id)
  3907. query = {
  3908. "team": "myteams",
  3909. "project": [self.project.id],
  3910. # use the order by to ensure the result order
  3911. "orderby": "transaction",
  3912. "field": [
  3913. "team_key_transaction",
  3914. "transaction",
  3915. "transaction.status",
  3916. "project",
  3917. "epm()",
  3918. "failure_rate()",
  3919. "percentile(transaction.duration, 0.95)",
  3920. ],
  3921. }
  3922. response = self.do_request(query)
  3923. assert response.status_code == 200, response.content
  3924. data = response.data["data"]
  3925. assert len(data) == 3
  3926. assert data[0]["team_key_transaction"] == 0
  3927. assert data[0]["transaction"] == "/blah_transaction/"
  3928. assert data[1]["team_key_transaction"] == 0
  3929. assert data[1]["transaction"] == "/foo_transaction/"
  3930. assert data[2]["team_key_transaction"] == 0
  3931. assert data[2]["transaction"] == "/zoo_transaction/"
  3932. def test_team_key_transactions_my_teams(self):
  3933. team1 = self.create_team(organization=self.organization, name="Team A")
  3934. self.create_team_membership(team1, user=self.user)
  3935. self.project.add_team(team1)
  3936. team2 = self.create_team(organization=self.organization, name="Team B")
  3937. self.project.add_team(team2)
  3938. transactions = ["/blah_transaction/"]
  3939. key_transactions = [
  3940. (team1, "/foo_transaction/"),
  3941. (team2, "/zoo_transaction/"),
  3942. ]
  3943. for transaction in transactions:
  3944. self.transaction_data["transaction"] = transaction
  3945. self.store_event(self.transaction_data, self.project.id)
  3946. for team, transaction in key_transactions:
  3947. self.transaction_data["transaction"] = transaction
  3948. self.store_event(self.transaction_data, self.project.id)
  3949. TeamKeyTransaction.objects.create(
  3950. organization=self.organization,
  3951. transaction=transaction,
  3952. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  3953. )
  3954. query = {
  3955. "team": "myteams",
  3956. "project": [self.project.id],
  3957. "field": [
  3958. "team_key_transaction",
  3959. "transaction",
  3960. "transaction.status",
  3961. "project",
  3962. "epm()",
  3963. "failure_rate()",
  3964. "percentile(transaction.duration, 0.95)",
  3965. ],
  3966. }
  3967. query["orderby"] = ["team_key_transaction", "transaction"]
  3968. response = self.do_request(query)
  3969. assert response.status_code == 200, response.content
  3970. data = response.data["data"]
  3971. assert len(data) == 3
  3972. assert data[0]["team_key_transaction"] == 0
  3973. assert data[0]["transaction"] == "/blah_transaction/"
  3974. assert data[1]["team_key_transaction"] == 0
  3975. assert data[1]["transaction"] == "/zoo_transaction/"
  3976. assert data[2]["team_key_transaction"] == 1
  3977. assert data[2]["transaction"] == "/foo_transaction/"
  3978. # not specifying any teams should use my teams
  3979. query = {
  3980. "project": [self.project.id],
  3981. "field": [
  3982. "team_key_transaction",
  3983. "transaction",
  3984. "transaction.status",
  3985. "project",
  3986. "epm()",
  3987. "failure_rate()",
  3988. "percentile(transaction.duration, 0.95)",
  3989. ],
  3990. }
  3991. query["orderby"] = ["team_key_transaction", "transaction"]
  3992. response = self.do_request(query)
  3993. assert response.status_code == 200, response.content
  3994. data = response.data["data"]
  3995. assert len(data) == 3
  3996. assert data[0]["team_key_transaction"] == 0
  3997. assert data[0]["transaction"] == "/blah_transaction/"
  3998. assert data[1]["team_key_transaction"] == 0
  3999. assert data[1]["transaction"] == "/zoo_transaction/"
  4000. assert data[2]["team_key_transaction"] == 1
  4001. assert data[2]["transaction"] == "/foo_transaction/"
  4002. def test_team_key_transactions_orderby(self):
  4003. team1 = self.create_team(organization=self.organization, name="Team A")
  4004. team2 = self.create_team(organization=self.organization, name="Team B")
  4005. transactions = ["/blah_transaction/"]
  4006. key_transactions = [
  4007. (team1, "/foo_transaction/"),
  4008. (team2, "/zoo_transaction/"),
  4009. ]
  4010. for transaction in transactions:
  4011. self.transaction_data["transaction"] = transaction
  4012. self.store_event(self.transaction_data, self.project.id)
  4013. for team, transaction in key_transactions:
  4014. self.create_team_membership(team, user=self.user)
  4015. self.project.add_team(team)
  4016. self.transaction_data["transaction"] = transaction
  4017. self.store_event(self.transaction_data, self.project.id)
  4018. TeamKeyTransaction.objects.create(
  4019. organization=self.organization,
  4020. transaction=transaction,
  4021. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  4022. )
  4023. query = {
  4024. "team": "myteams",
  4025. "project": [self.project.id],
  4026. "field": [
  4027. "team_key_transaction",
  4028. "transaction",
  4029. "transaction.status",
  4030. "project",
  4031. "epm()",
  4032. "failure_rate()",
  4033. "percentile(transaction.duration, 0.95)",
  4034. ],
  4035. }
  4036. # test ascending order
  4037. query["orderby"] = ["team_key_transaction", "transaction"]
  4038. response = self.do_request(query)
  4039. assert response.status_code == 200, response.content
  4040. data = response.data["data"]
  4041. assert len(data) == 3
  4042. assert data[0]["team_key_transaction"] == 0
  4043. assert data[0]["transaction"] == "/blah_transaction/"
  4044. assert data[1]["team_key_transaction"] == 1
  4045. assert data[1]["transaction"] == "/foo_transaction/"
  4046. assert data[2]["team_key_transaction"] == 1
  4047. assert data[2]["transaction"] == "/zoo_transaction/"
  4048. # test descending order
  4049. query["orderby"] = ["-team_key_transaction", "-transaction"]
  4050. response = self.do_request(query)
  4051. assert response.status_code == 200, response.content
  4052. data = response.data["data"]
  4053. assert len(data) == 3
  4054. assert data[0]["team_key_transaction"] == 1
  4055. assert data[0]["transaction"] == "/zoo_transaction/"
  4056. assert data[1]["team_key_transaction"] == 1
  4057. assert data[1]["transaction"] == "/foo_transaction/"
  4058. assert data[2]["team_key_transaction"] == 0
  4059. assert data[2]["transaction"] == "/blah_transaction/"
  4060. def test_team_key_transactions_query(self):
  4061. team1 = self.create_team(organization=self.organization, name="Team A")
  4062. team2 = self.create_team(organization=self.organization, name="Team B")
  4063. transactions = ["/blah_transaction/"]
  4064. key_transactions = [
  4065. (team1, "/foo_transaction/"),
  4066. (team2, "/zoo_transaction/"),
  4067. ]
  4068. for transaction in transactions:
  4069. self.transaction_data["transaction"] = transaction
  4070. self.store_event(self.transaction_data, self.project.id)
  4071. for team, transaction in key_transactions:
  4072. self.create_team_membership(team, user=self.user)
  4073. self.project.add_team(team)
  4074. self.transaction_data["transaction"] = transaction
  4075. self.store_event(self.transaction_data, self.project.id)
  4076. TeamKeyTransaction.objects.create(
  4077. organization=self.organization,
  4078. project_team=ProjectTeam.objects.get(
  4079. project=self.project,
  4080. team=team,
  4081. ),
  4082. transaction=transaction,
  4083. )
  4084. query = {
  4085. "team": "myteams",
  4086. "project": [self.project.id],
  4087. # use the order by to ensure the result order
  4088. "orderby": "transaction",
  4089. "field": [
  4090. "team_key_transaction",
  4091. "transaction",
  4092. "transaction.status",
  4093. "project",
  4094. "epm()",
  4095. "failure_rate()",
  4096. "percentile(transaction.duration, 0.95)",
  4097. ],
  4098. }
  4099. # key transactions
  4100. query["query"] = "has:team_key_transaction"
  4101. response = self.do_request(query)
  4102. assert response.status_code == 200, response.content
  4103. data = response.data["data"]
  4104. assert len(data) == 2
  4105. assert data[0]["team_key_transaction"] == 1
  4106. assert data[0]["transaction"] == "/foo_transaction/"
  4107. assert data[1]["team_key_transaction"] == 1
  4108. assert data[1]["transaction"] == "/zoo_transaction/"
  4109. # key transactions
  4110. query["query"] = "team_key_transaction:true"
  4111. response = self.do_request(query)
  4112. assert response.status_code == 200, response.content
  4113. data = response.data["data"]
  4114. assert len(data) == 2
  4115. assert data[0]["team_key_transaction"] == 1
  4116. assert data[0]["transaction"] == "/foo_transaction/"
  4117. assert data[1]["team_key_transaction"] == 1
  4118. assert data[1]["transaction"] == "/zoo_transaction/"
  4119. # not key transactions
  4120. query["query"] = "!has:team_key_transaction"
  4121. response = self.do_request(query)
  4122. assert response.status_code == 200, response.content
  4123. data = response.data["data"]
  4124. assert len(data) == 1
  4125. assert data[0]["team_key_transaction"] == 0
  4126. assert data[0]["transaction"] == "/blah_transaction/"
  4127. # not key transactions
  4128. query["query"] = "team_key_transaction:false"
  4129. response = self.do_request(query)
  4130. assert response.status_code == 200, response.content
  4131. data = response.data["data"]
  4132. assert len(data) == 1
  4133. assert data[0]["team_key_transaction"] == 0
  4134. assert data[0]["transaction"] == "/blah_transaction/"
  4135. def test_too_many_team_key_transactions(self):
  4136. MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS = 1
  4137. with mock.patch(
  4138. "sentry.search.events.fields.MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS",
  4139. MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS,
  4140. ):
  4141. team = self.create_team(organization=self.organization, name="Team A")
  4142. self.create_team_membership(team, user=self.user)
  4143. self.project.add_team(team)
  4144. project_team = ProjectTeam.objects.get(project=self.project, team=team)
  4145. for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1):
  4146. transaction = f"transaction-{team.id}-{i}"
  4147. self.transaction_data["transaction"] = transaction
  4148. self.store_event(self.transaction_data, self.project.id)
  4149. TeamKeyTransaction.objects.bulk_create(
  4150. [
  4151. TeamKeyTransaction(
  4152. organization=self.organization,
  4153. project_team=project_team,
  4154. transaction=f"transaction-{team.id}-{i}",
  4155. )
  4156. for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1)
  4157. ]
  4158. )
  4159. query = {
  4160. "team": "myteams",
  4161. "project": [self.project.id],
  4162. "orderby": "transaction",
  4163. "field": [
  4164. "team_key_transaction",
  4165. "transaction",
  4166. "transaction.status",
  4167. "project",
  4168. "epm()",
  4169. "failure_rate()",
  4170. "percentile(transaction.duration, 0.95)",
  4171. ],
  4172. }
  4173. response = self.do_request(query)
  4174. assert response.status_code == 200, response.content
  4175. data = response.data["data"]
  4176. assert len(data) == 2
  4177. assert (
  4178. sum(row["team_key_transaction"] for row in data)
  4179. == MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS
  4180. )
  4181. def test_no_pagination_param(self):
  4182. self.store_event(
  4183. data={
  4184. "event_id": "a" * 32,
  4185. "timestamp": self.ten_mins_ago_iso,
  4186. "fingerprint": ["group1"],
  4187. },
  4188. project_id=self.project.id,
  4189. )
  4190. query = {"field": ["id", "project.id"], "project": [self.project.id], "noPagination": True}
  4191. response = self.do_request(query)
  4192. assert response.status_code == 200
  4193. assert len(response.data["data"]) == 1
  4194. assert "Link" not in response
  4195. def test_nan_result(self):
  4196. query = {"field": ["apdex(300)"], "project": [self.project.id], "query": f"id:{'0' * 32}"}
  4197. response = self.do_request(query)
  4198. assert response.status_code == 200
  4199. assert len(response.data["data"]) == 1
  4200. assert response.data["data"][0]["apdex(300)"] == 0
  4201. def test_equation_simple(self):
  4202. event_data = self.load_data(
  4203. timestamp=self.ten_mins_ago,
  4204. )
  4205. event_data["breakdowns"]["span_ops"]["ops.http"]["value"] = 1500
  4206. self.store_event(data=event_data, project_id=self.project.id)
  4207. query = {
  4208. "field": ["spans.http", "equation|spans.http / 3"],
  4209. "project": [self.project.id],
  4210. "query": "event.type:transaction",
  4211. }
  4212. response = self.do_request(
  4213. query,
  4214. {
  4215. "organizations:discover-basic": True,
  4216. },
  4217. )
  4218. assert response.status_code == 200, response.content
  4219. assert len(response.data["data"]) == 1
  4220. assert (
  4221. response.data["data"][0]["equation|spans.http / 3"]
  4222. == event_data["breakdowns"]["span_ops"]["ops.http"]["value"] / 3
  4223. )
  4224. assert response.data["meta"]["fields"]["equation|spans.http / 3"] == "number"
  4225. def test_equation_sort(self):
  4226. event_data = self.transaction_data.copy()
  4227. event_data["breakdowns"] = {"span_ops": {"ops.http": {"value": 1500}}}
  4228. self.store_event(data=event_data, project_id=self.project.id)
  4229. event_data2 = self.transaction_data.copy()
  4230. event_data2["breakdowns"] = {"span_ops": {"ops.http": {"value": 2000}}}
  4231. self.store_event(data=event_data2, project_id=self.project.id)
  4232. query = {
  4233. "field": ["spans.http", "equation|spans.http / 3"],
  4234. "project": [self.project.id],
  4235. "orderby": "equation|spans.http / 3",
  4236. "query": "event.type:transaction",
  4237. }
  4238. response = self.do_request(
  4239. query,
  4240. {
  4241. "organizations:discover-basic": True,
  4242. },
  4243. )
  4244. assert response.status_code == 200, response.content
  4245. assert len(response.data["data"]) == 2
  4246. assert (
  4247. response.data["data"][0]["equation|spans.http / 3"]
  4248. == event_data["breakdowns"]["span_ops"]["ops.http"]["value"] / 3
  4249. )
  4250. assert (
  4251. response.data["data"][1]["equation|spans.http / 3"]
  4252. == event_data2["breakdowns"]["span_ops"]["ops.http"]["value"] / 3
  4253. )
  4254. def test_equation_operation_limit(self):
  4255. query = {
  4256. "field": ["spans.http", f"equation|spans.http{' * 2' * 11}"],
  4257. "project": [self.project.id],
  4258. "query": "event.type:transaction",
  4259. }
  4260. response = self.do_request(
  4261. query,
  4262. {
  4263. "organizations:discover-basic": True,
  4264. },
  4265. )
  4266. assert response.status_code == 400
  4267. @mock.patch("sentry.api.bases.organization_events.MAX_FIELDS", 2)
  4268. def test_equation_field_limit(self):
  4269. query = {
  4270. "field": ["spans.http", "transaction.duration", "equation|5 * 2"],
  4271. "project": [self.project.id],
  4272. "query": "event.type:transaction",
  4273. }
  4274. response = self.do_request(
  4275. query,
  4276. {
  4277. "organizations:discover-basic": True,
  4278. },
  4279. )
  4280. assert response.status_code == 400
  4281. def test_count_if(self):
  4282. unicode_phrase1 = "\u716e\u6211\u66f4\u591a\u7684\u98df\u7269\uff0c\u6211\u9913\u4e86"
  4283. for i in range(5):
  4284. data = self.load_data(
  4285. timestamp=before_now(minutes=(10 + i)),
  4286. duration=timedelta(milliseconds=100 if i < 3 else 200),
  4287. )
  4288. data["tags"] = {
  4289. "sub_customer.is-Enterprise-42": "yes" if i == 0 else "no",
  4290. "unicode-phrase": unicode_phrase1 if i == 0 else "no",
  4291. }
  4292. self.store_event(data, project_id=self.project.id)
  4293. query = {
  4294. "field": [
  4295. "count_if(transaction.duration, less, 150)",
  4296. "count_if(transaction.duration, greater, 150)",
  4297. "count_if(sub_customer.is-Enterprise-42, equals, yes)",
  4298. "count_if(sub_customer.is-Enterprise-42, notEquals, yes)",
  4299. f"count_if(unicode-phrase, equals, {unicode_phrase1})",
  4300. ],
  4301. "project": [self.project.id],
  4302. }
  4303. response = self.do_request(query)
  4304. assert response.status_code == 200
  4305. assert len(response.data["data"]) == 1
  4306. assert response.data["data"][0]["count_if(transaction.duration, less, 150)"] == 3
  4307. assert response.data["data"][0]["count_if(transaction.duration, greater, 150)"] == 2
  4308. assert response.data["data"][0]["count_if(sub_customer.is-Enterprise-42, equals, yes)"] == 1
  4309. assert (
  4310. response.data["data"][0]["count_if(sub_customer.is-Enterprise-42, notEquals, yes)"] == 4
  4311. )
  4312. assert response.data["data"][0][f"count_if(unicode-phrase, equals, {unicode_phrase1})"] == 1
  4313. def test_count_if_array_field(self):
  4314. data = self.load_data(platform="javascript")
  4315. data["timestamp"] = self.ten_mins_ago_iso
  4316. self.store_event(data=data, project_id=self.project.id)
  4317. query = {
  4318. "field": [
  4319. "count_if(stack.filename, equals, raven.js)",
  4320. ],
  4321. "project": [self.project.id],
  4322. }
  4323. response = self.do_request(query)
  4324. assert response.status_code == 200, response.content
  4325. assert len(response.data["data"]) == 1
  4326. assert response.data["data"][0]["count_if(stack.filename, equals, raven.js)"] == 1
  4327. def test_count_if_measurements_cls(self):
  4328. data = self.transaction_data.copy()
  4329. data["measurements"] = {"cls": {"value": 0.5}}
  4330. self.store_event(data, project_id=self.project.id)
  4331. data["measurements"] = {"cls": {"value": 0.1}}
  4332. self.store_event(data, project_id=self.project.id)
  4333. query = {
  4334. "field": [
  4335. "count_if(measurements.cls, greater, 0.05)",
  4336. "count_if(measurements.cls, less, 0.3)",
  4337. ],
  4338. "project": [self.project.id],
  4339. }
  4340. response = self.do_request(query)
  4341. assert response.status_code == 200
  4342. assert len(response.data["data"]) == 1
  4343. assert response.data["data"][0]["count_if(measurements.cls, greater, 0.05)"] == 2
  4344. assert response.data["data"][0]["count_if(measurements.cls, less, 0.3)"] == 1
  4345. def test_count_if_filter(self):
  4346. for i in range(5):
  4347. data = self.load_data(
  4348. timestamp=before_now(minutes=(10 + i)),
  4349. duration=timedelta(milliseconds=100 if i < 3 else 200),
  4350. )
  4351. data["tags"] = {"sub_customer.is-Enterprise-42": "yes" if i == 0 else "no"}
  4352. self.store_event(data, project_id=self.project.id)
  4353. query = {
  4354. "field": [
  4355. "count_if(transaction.duration, less, 150)",
  4356. ],
  4357. "query": "count_if(transaction.duration, less, 150):>2",
  4358. "project": [self.project.id],
  4359. }
  4360. response = self.do_request(query)
  4361. assert response.status_code == 200
  4362. assert len(response.data["data"]) == 1
  4363. assert response.data["data"][0]["count_if(transaction.duration, less, 150)"] == 3
  4364. query = {
  4365. "field": [
  4366. "count_if(transaction.duration, less, 150)",
  4367. ],
  4368. "query": "count_if(transaction.duration, less, 150):<2",
  4369. "project": [self.project.id],
  4370. }
  4371. response = self.do_request(query)
  4372. assert response.status_code == 200
  4373. assert len(response.data["data"]) == 0
  4374. def test_filters_with_escaped_asterisk(self):
  4375. self.transaction_data["transaction"] = r"/:a*/:b-:c(\d\.\e+)"
  4376. self.store_event(self.transaction_data, project_id=self.project.id)
  4377. query = {
  4378. "field": ["transaction", "transaction.duration"],
  4379. # make sure to escape the asterisk so it's not treated as a wildcard
  4380. "query": r'transaction:"/:a\*/:b-:c(\d\.\e+)"',
  4381. "project": [self.project.id],
  4382. }
  4383. response = self.do_request(query)
  4384. assert response.status_code == 200
  4385. assert len(response.data["data"]) == 1
  4386. def test_filters_with_back_slashes(self):
  4387. self.transaction_data["transaction"] = r"a\b\c@d"
  4388. self.store_event(self.transaction_data, project_id=self.project.id)
  4389. query = {
  4390. "field": ["transaction", "transaction.duration"],
  4391. "query": r'transaction:"a\b\c@d"',
  4392. "project": [self.project.id],
  4393. }
  4394. response = self.do_request(query)
  4395. assert response.status_code == 200
  4396. assert len(response.data["data"]) == 1
  4397. def test_mobile_measurements(self):
  4398. self.transaction_data["measurements"]["frames_total"] = {"value": 100}
  4399. self.transaction_data["measurements"]["frames_slow"] = {"value": 10}
  4400. self.transaction_data["measurements"]["frames_frozen"] = {"value": 5}
  4401. self.transaction_data["measurements"]["stall_count"] = {"value": 2}
  4402. self.transaction_data["measurements"]["stall_total_time"] = {"value": 12}
  4403. self.transaction_data["measurements"]["stall_longest_time"] = {"value": 7}
  4404. self.store_event(self.transaction_data, project_id=self.project.id)
  4405. query = {
  4406. "field": [
  4407. "measurements.frames_total",
  4408. "measurements.frames_slow",
  4409. "measurements.frames_frozen",
  4410. "measurements.frames_slow_rate",
  4411. "measurements.frames_frozen_rate",
  4412. "measurements.stall_count",
  4413. "measurements.stall_total_time",
  4414. "measurements.stall_longest_time",
  4415. "measurements.stall_percentage",
  4416. ],
  4417. "query": "",
  4418. "project": [self.project.id],
  4419. }
  4420. response = self.do_request(query)
  4421. assert response.status_code == 200
  4422. data = response.data["data"]
  4423. assert len(data) == 1
  4424. assert data[0]["measurements.frames_total"] == 100
  4425. assert data[0]["measurements.frames_slow"] == 10
  4426. assert data[0]["measurements.frames_frozen"] == 5
  4427. assert data[0]["measurements.frames_slow_rate"] == 0.1
  4428. assert data[0]["measurements.frames_frozen_rate"] == 0.05
  4429. assert data[0]["measurements.stall_count"] == 2
  4430. assert data[0]["measurements.stall_total_time"] == 12
  4431. assert data[0]["measurements.stall_longest_time"] == 7
  4432. assert data[0]["measurements.stall_percentage"] == 0.004
  4433. meta = response.data["meta"]["fields"]
  4434. assert meta["measurements.frames_total"] == "number"
  4435. assert meta["measurements.frames_slow"] == "number"
  4436. assert meta["measurements.frames_frozen"] == "number"
  4437. assert meta["measurements.frames_slow_rate"] == "percentage"
  4438. assert meta["measurements.frames_frozen_rate"] == "percentage"
  4439. assert meta["measurements.stall_count"] == "number"
  4440. assert meta["measurements.stall_total_time"] == "number"
  4441. assert meta["measurements.stall_longest_time"] == "number"
  4442. assert meta["measurements.stall_percentage"] == "percentage"
  4443. query = {
  4444. "field": [
  4445. "p75(measurements.frames_slow_rate)",
  4446. "p75(measurements.frames_frozen_rate)",
  4447. "percentile(measurements.frames_slow_rate,0.5)",
  4448. "percentile(measurements.frames_frozen_rate,0.5)",
  4449. "p75(measurements.stall_percentage)",
  4450. "percentile(measurements.stall_percentage,0.5)",
  4451. ],
  4452. "query": "",
  4453. "project": [self.project.id],
  4454. }
  4455. response = self.do_request(query)
  4456. assert response.status_code == 200
  4457. data = response.data["data"]
  4458. assert len(data) == 1
  4459. assert data[0]["p75(measurements.frames_slow_rate)"] == 0.1
  4460. assert data[0]["p75(measurements.frames_frozen_rate)"] == 0.05
  4461. assert data[0]["p75(measurements.stall_percentage)"] == 0.004
  4462. assert data[0]["percentile(measurements.frames_slow_rate,0.5)"] == 0.1
  4463. assert data[0]["percentile(measurements.frames_frozen_rate,0.5)"] == 0.05
  4464. assert data[0]["percentile(measurements.stall_percentage,0.5)"] == 0.004
  4465. meta = response.data["meta"]["fields"]
  4466. assert meta["p75(measurements.frames_slow_rate)"] == "percentage"
  4467. assert meta["p75(measurements.frames_frozen_rate)"] == "percentage"
  4468. assert meta["p75(measurements.stall_percentage)"] == "percentage"
  4469. assert meta["percentile(measurements.frames_slow_rate,0.5)"] == "percentage"
  4470. assert meta["percentile(measurements.stall_percentage,0.5)"] == "percentage"
  4471. def test_project_auto_fields(self):
  4472. self.store_event(
  4473. data={
  4474. "event_id": "a" * 32,
  4475. "environment": "staging",
  4476. "timestamp": self.ten_mins_ago_iso,
  4477. },
  4478. project_id=self.project.id,
  4479. )
  4480. query = {"field": ["environment"]}
  4481. response = self.do_request(query)
  4482. assert response.status_code == 200, response.content
  4483. assert len(response.data["data"]) == 1
  4484. assert response.data["data"][0]["environment"] == "staging"
  4485. assert response.data["data"][0]["project.name"] == self.project.slug
  4486. def test_timestamp_different_from_params(self):
  4487. fifteen_days_ago = iso_format(before_now(days=15))
  4488. fifteen_days_later = iso_format(before_now(days=-15))
  4489. for query_text in [
  4490. f"timestamp:<{fifteen_days_ago}",
  4491. f"timestamp:<={fifteen_days_ago}",
  4492. f"timestamp:>{fifteen_days_later}",
  4493. f"timestamp:>={fifteen_days_later}",
  4494. ]:
  4495. query = {
  4496. "field": ["count()"],
  4497. "query": query_text,
  4498. "statsPeriod": "14d",
  4499. "project": self.project.id,
  4500. }
  4501. response = self.do_request(query)
  4502. assert response.status_code == 400, query_text
  4503. @mock.patch("sentry.search.events.builder.discover.raw_snql_query")
  4504. def test_removes_unnecessary_default_project_and_transaction_thresholds(self, mock_snql_query):
  4505. mock_snql_query.side_effect = [{"meta": {}, "data": []}]
  4506. ProjectTransactionThreshold.objects.create(
  4507. project=self.project,
  4508. organization=self.organization,
  4509. # these are the default values that we use
  4510. threshold=constants.DEFAULT_PROJECT_THRESHOLD,
  4511. metric=TransactionMetric.DURATION.value,
  4512. )
  4513. ProjectTransactionThresholdOverride.objects.create(
  4514. transaction="transaction",
  4515. project=self.project,
  4516. organization=self.organization,
  4517. # these are the default values that we use
  4518. threshold=constants.DEFAULT_PROJECT_THRESHOLD,
  4519. metric=TransactionMetric.DURATION.value,
  4520. )
  4521. query = {
  4522. "field": ["apdex()", "user_misery()"],
  4523. "query": "event.type:transaction",
  4524. "project": [self.project.id],
  4525. }
  4526. response = self.do_request(
  4527. query,
  4528. features={
  4529. "organizations:discover-basic": True,
  4530. "organizations:global-views": True,
  4531. },
  4532. )
  4533. assert response.status_code == 200, response.content
  4534. assert mock_snql_query.call_count == 1
  4535. assert (
  4536. Function("tuple", ["duration", 300], "project_threshold_config")
  4537. in mock_snql_query.call_args_list[0][0][0].query.select
  4538. )
  4539. @mock.patch("sentry.search.events.builder.discover.raw_snql_query")
  4540. def test_removes_unnecessary_default_project_and_transaction_thresholds_keeps_others(
  4541. self, mock_snql_query
  4542. ):
  4543. mock_snql_query.side_effect = [{"meta": {}, "data": []}]
  4544. ProjectTransactionThreshold.objects.create(
  4545. project=self.project,
  4546. organization=self.organization,
  4547. # these are the default values that we use
  4548. threshold=constants.DEFAULT_PROJECT_THRESHOLD,
  4549. metric=TransactionMetric.DURATION.value,
  4550. )
  4551. ProjectTransactionThresholdOverride.objects.create(
  4552. transaction="transaction",
  4553. project=self.project,
  4554. organization=self.organization,
  4555. # these are the default values that we use
  4556. threshold=constants.DEFAULT_PROJECT_THRESHOLD,
  4557. metric=TransactionMetric.DURATION.value,
  4558. )
  4559. project = self.create_project()
  4560. ProjectTransactionThreshold.objects.create(
  4561. project=project,
  4562. organization=self.organization,
  4563. threshold=100,
  4564. metric=TransactionMetric.LCP.value,
  4565. )
  4566. ProjectTransactionThresholdOverride.objects.create(
  4567. transaction="transaction",
  4568. project=project,
  4569. organization=self.organization,
  4570. threshold=200,
  4571. metric=TransactionMetric.LCP.value,
  4572. )
  4573. query = {
  4574. "field": ["apdex()", "user_misery()"],
  4575. "query": "event.type:transaction",
  4576. "project": [self.project.id, project.id],
  4577. }
  4578. response = self.do_request(
  4579. query,
  4580. features={
  4581. "organizations:discover-basic": True,
  4582. "organizations:global-views": True,
  4583. },
  4584. )
  4585. assert response.status_code == 200, response.content
  4586. assert mock_snql_query.call_count == 1
  4587. project_threshold_override_config_index = Function(
  4588. "indexOf",
  4589. [
  4590. # only 1 transaction override is present here
  4591. # because the other use to the default values
  4592. [(Function("toUInt64", [project.id]), "transaction")],
  4593. (Column("project_id"), Column("transaction")),
  4594. ],
  4595. "project_threshold_override_config_index",
  4596. )
  4597. project_threshold_config_index = Function(
  4598. "indexOf",
  4599. [
  4600. # only 1 project override is present here
  4601. # because the other use to the default values
  4602. [Function("toUInt64", [project.id])],
  4603. Column("project_id"),
  4604. ],
  4605. "project_threshold_config_index",
  4606. )
  4607. assert (
  4608. Function(
  4609. "if",
  4610. [
  4611. Function("equals", [project_threshold_override_config_index, 0]),
  4612. Function(
  4613. "if",
  4614. [
  4615. Function("equals", [project_threshold_config_index, 0]),
  4616. ("duration", 300),
  4617. Function(
  4618. "arrayElement", [[("lcp", 100)], project_threshold_config_index]
  4619. ),
  4620. ],
  4621. ),
  4622. Function(
  4623. "arrayElement",
  4624. [[("lcp", 200)], project_threshold_override_config_index],
  4625. ),
  4626. ],
  4627. "project_threshold_config",
  4628. )
  4629. in mock_snql_query.call_args_list[0][0][0].query.select
  4630. )
  4631. def test_count_web_vitals(self):
  4632. # Good
  4633. self.transaction_data["measurements"] = {
  4634. "lcp": {"value": constants.VITAL_THRESHOLDS["lcp"]["meh"] - 100},
  4635. }
  4636. self.store_event(self.transaction_data, self.project.id)
  4637. # Meh
  4638. self.transaction_data["measurements"] = {
  4639. "lcp": {"value": constants.VITAL_THRESHOLDS["lcp"]["meh"] + 100},
  4640. }
  4641. self.store_event(self.transaction_data, self.project.id)
  4642. self.store_event(self.transaction_data, self.project.id)
  4643. query = {
  4644. "field": [
  4645. "count_web_vitals(measurements.lcp, poor)",
  4646. "count_web_vitals(measurements.lcp, meh)",
  4647. "count_web_vitals(measurements.lcp, good)",
  4648. ]
  4649. }
  4650. response = self.do_request(query)
  4651. assert response.status_code == 200, response.content
  4652. assert len(response.data["data"]) == 1
  4653. assert response.data["data"][0] == {
  4654. "count_web_vitals(measurements.lcp, poor)": 0,
  4655. "count_web_vitals(measurements.lcp, meh)": 2,
  4656. "count_web_vitals(measurements.lcp, good)": 1,
  4657. }
  4658. def test_count_web_vitals_invalid_vital(self):
  4659. query = {
  4660. "field": [
  4661. "count_web_vitals(measurements.foo, poor)",
  4662. ],
  4663. "project": [self.project.id],
  4664. }
  4665. response = self.do_request(query)
  4666. assert response.status_code == 400, response.content
  4667. query = {
  4668. "field": [
  4669. "count_web_vitals(tags[lcp], poor)",
  4670. ],
  4671. "project": [self.project.id],
  4672. }
  4673. response = self.do_request(query)
  4674. assert response.status_code == 400, response.content
  4675. query = {
  4676. "field": [
  4677. "count_web_vitals(transaction.duration, poor)",
  4678. ],
  4679. "project": [self.project.id],
  4680. }
  4681. response = self.do_request(query)
  4682. assert response.status_code == 400, response.content
  4683. query = {
  4684. "field": [
  4685. "count_web_vitals(measurements.lcp, bad)",
  4686. ],
  4687. "project": [self.project.id],
  4688. }
  4689. response = self.do_request(query)
  4690. assert response.status_code == 400, response.content
  4691. def test_tag_that_looks_like_aggregate(self):
  4692. data = self.load_data()
  4693. data["tags"] = {"p95": "<5k"}
  4694. self.store_event(data, project_id=self.project.id)
  4695. query = {
  4696. "field": ["p95"],
  4697. "query": "p95:<5k",
  4698. "project": [self.project.id],
  4699. }
  4700. response = self.do_request(query)
  4701. assert response.status_code == 200, response.content
  4702. data = response.data["data"]
  4703. assert len(data) == 1
  4704. assert data[0]["p95"] == "<5k"
  4705. def test_chained_or_query_meta_tip(self):
  4706. query = {
  4707. "field": ["transaction"],
  4708. "query": "transaction:a OR transaction:b",
  4709. "project": [self.project.id],
  4710. }
  4711. response = self.do_request(query)
  4712. assert response.status_code == 200, response.content
  4713. meta = response.data["meta"]
  4714. assert meta["tips"] == {
  4715. "query": "Did you know you can replace chained or conditions like `field:a OR field:b OR field:c` with `field:[a,b,c]`",
  4716. "columns": None,
  4717. }
  4718. @override_settings(SENTRY_SELF_HOSTED=False)
  4719. def test_no_ratelimit(self):
  4720. query = {
  4721. "field": ["transaction"],
  4722. "project": [self.project.id],
  4723. }
  4724. with freeze_time("2000-01-01"):
  4725. for _ in range(15):
  4726. self.do_request(query)
  4727. response = self.do_request(query)
  4728. assert response.status_code == 200, response.content
  4729. def test_transaction_source(self):
  4730. query = {
  4731. "field": ["transaction"],
  4732. "query": "transaction.source:task",
  4733. "project": [self.project.id],
  4734. }
  4735. response = self.do_request(query)
  4736. assert response.status_code == 200, response.content
  4737. def test_readable_device_name(self):
  4738. data = self.load_data()
  4739. data["tags"] = {"device": "iPhone14,3"}
  4740. self.store_event(data, project_id=self.project.id)
  4741. query = {
  4742. "field": ["device"],
  4743. "query": "",
  4744. "project": [self.project.id],
  4745. "readable": True,
  4746. }
  4747. response = self.do_request(query)
  4748. assert response.status_code == 200, response.content
  4749. data = response.data["data"]
  4750. assert len(data) == 1
  4751. assert data[0]["device"] == "iPhone14,3"
  4752. assert data[0]["readable"] == "iPhone 13 Pro Max"
  4753. def test_http_status_code(self):
  4754. project1 = self.create_project()
  4755. project2 = self.create_project()
  4756. self.store_event(
  4757. data={
  4758. "event_id": "a" * 32,
  4759. "transaction": "/example",
  4760. "message": "how to make fast",
  4761. "timestamp": self.ten_mins_ago_iso,
  4762. "tags": {"http.status_code": "200"},
  4763. },
  4764. project_id=project1.id,
  4765. )
  4766. self.store_event(
  4767. data={
  4768. "event_id": "b" * 32,
  4769. "transaction": "/example",
  4770. "message": "how to make fast",
  4771. "timestamp": self.ten_mins_ago_iso,
  4772. "contexts": {"response": {"status_code": 400}},
  4773. },
  4774. project_id=project2.id,
  4775. )
  4776. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  4777. query = {
  4778. "field": ["event.type", "http.status_code"],
  4779. "query": "",
  4780. "statsPeriod": "24h",
  4781. }
  4782. response = self.do_request(query, features=features)
  4783. assert response.status_code == 200, response.content
  4784. data = response.data["data"]
  4785. assert len(data) == 2
  4786. result = {r["http.status_code"] for r in data}
  4787. assert result == {"200", "400"}
  4788. def test_http_status_code_context_priority(self):
  4789. project1 = self.create_project()
  4790. self.store_event(
  4791. data={
  4792. "event_id": "a" * 32,
  4793. "transaction": "/example",
  4794. "message": "how to make fast",
  4795. "timestamp": self.ten_mins_ago_iso,
  4796. "tags": {"http.status_code": "200"},
  4797. "contexts": {"response": {"status_code": 400}},
  4798. },
  4799. project_id=project1.id,
  4800. )
  4801. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  4802. query = {
  4803. "field": ["event.type", "http.status_code"],
  4804. "query": "",
  4805. "statsPeriod": "24h",
  4806. }
  4807. response = self.do_request(query, features=features)
  4808. assert response.status_code == 200, response.content
  4809. data = response.data["data"]
  4810. assert len(data) == 1
  4811. assert data[0]["http.status_code"] == "400"
  4812. def test_total_count(self):
  4813. project1 = self.create_project()
  4814. for i in range(3):
  4815. self.store_event(data=self.load_data(platform="javascript"), project_id=project1.id)
  4816. self.store_event(
  4817. data={
  4818. "event_id": "a" * 32,
  4819. "transaction": "/example",
  4820. "message": "how to make fast",
  4821. "timestamp": self.ten_mins_ago_iso,
  4822. },
  4823. project_id=project1.id,
  4824. )
  4825. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  4826. query = {
  4827. "field": ["transaction", "total.count", "count()"],
  4828. "query": "!transaction:/example",
  4829. "statsPeriod": "24h",
  4830. }
  4831. response = self.do_request(query, features=features)
  4832. assert response.status_code == 200, response.content
  4833. data = response.data["data"]
  4834. assert len(data) == 1
  4835. assert data[0]["total.count"] == 3
  4836. def test_total_count_by_itself(self):
  4837. project1 = self.create_project()
  4838. for i in range(3):
  4839. self.store_event(data=self.load_data(platform="javascript"), project_id=project1.id)
  4840. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  4841. query = {
  4842. "field": ["total.count"],
  4843. "statsPeriod": "24h",
  4844. }
  4845. response = self.do_request(query, features=features)
  4846. assert response.status_code == 400, response.content
  4847. def test_total_count_equation(self):
  4848. project1 = self.create_project()
  4849. for i in range(3):
  4850. self.store_event(data=self.load_data(platform="javascript"), project_id=project1.id)
  4851. self.store_event(
  4852. data={
  4853. "event_id": "a" * 32,
  4854. "transaction": "/example",
  4855. "message": "how to make fast",
  4856. "timestamp": self.ten_mins_ago_iso,
  4857. },
  4858. project_id=project1.id,
  4859. )
  4860. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  4861. query = {
  4862. "field": ["transaction", "count()", "total.count", "equation|count()/total.count"],
  4863. "query": "",
  4864. "orderby": "equation|count()/total.count",
  4865. "statsPeriod": "24h",
  4866. }
  4867. response = self.do_request(query, features=features)
  4868. assert response.status_code == 200, response.content
  4869. data = response.data["data"]
  4870. assert len(data) == 2
  4871. assert data[0]["equation|count()/total.count"] == 0.25
  4872. assert data[1]["equation|count()/total.count"] == 0.75
  4873. def test_total_count_filter(self):
  4874. project1 = self.create_project()
  4875. for i in range(3):
  4876. self.store_event(data=self.load_data(platform="javascript"), project_id=project1.id)
  4877. self.store_event(
  4878. data={
  4879. "event_id": "a" * 32,
  4880. "transaction": "/example",
  4881. "message": "how to make fast",
  4882. "timestamp": self.ten_mins_ago_iso,
  4883. "tags": {"total.count": ">45"},
  4884. },
  4885. project_id=project1.id,
  4886. )
  4887. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  4888. query = {
  4889. "field": ["transaction", "count()", "total.count"],
  4890. "query": "total.count:>45",
  4891. "orderby": "count()",
  4892. "statsPeriod": "24h",
  4893. }
  4894. response = self.do_request(query, features=features)
  4895. assert response.status_code == 200, response.content
  4896. data = response.data["data"]
  4897. assert len(data) == 1
  4898. assert data[0]["total.count"] == 1
  4899. def test_total_sum_transaction_duration_equation(self):
  4900. for i in range(3):
  4901. data = self.load_data(
  4902. timestamp=self.eleven_mins_ago,
  4903. duration=timedelta(seconds=5),
  4904. )
  4905. data["transaction"] = "/endpoint/1"
  4906. self.store_event(data, project_id=self.project.id)
  4907. data = self.load_data(
  4908. timestamp=self.ten_mins_ago,
  4909. duration=timedelta(seconds=5),
  4910. )
  4911. data["transaction"] = "/endpoint/2"
  4912. self.store_event(data, project_id=self.project.id)
  4913. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  4914. query = {
  4915. "field": [
  4916. "transaction",
  4917. "sum(transaction.duration)",
  4918. "total.transaction_duration",
  4919. "equation|sum(transaction.duration)/total.transaction_duration",
  4920. ],
  4921. "query": "",
  4922. "orderby": "-equation|sum(transaction.duration)/total.transaction_duration",
  4923. "statsPeriod": "24h",
  4924. }
  4925. response = self.do_request(query, features=features)
  4926. assert response.status_code == 200, response.content
  4927. data = response.data["data"]
  4928. assert len(data) == 2
  4929. assert data[0]["equation|sum(transaction.duration)/total.transaction_duration"] == 0.75
  4930. assert data[1]["equation|sum(transaction.duration)/total.transaction_duration"] == 0.25
  4931. def test_device_class(self):
  4932. project1 = self.create_project()
  4933. for i in range(3):
  4934. self.store_event(
  4935. data={
  4936. "event_id": "a" * 32,
  4937. "transaction": "/example",
  4938. "message": "how to make fast",
  4939. "timestamp": self.ten_mins_ago_iso,
  4940. "tags": {"device.class": f"{i + 1}"},
  4941. },
  4942. project_id=project1.id,
  4943. )
  4944. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  4945. query = {
  4946. "field": ["device.class"],
  4947. "statsPeriod": "24h",
  4948. }
  4949. response = self.do_request(query, features=features)
  4950. assert response.status_code == 200, response.content
  4951. data = response.data["data"]
  4952. assert len(data) == 3
  4953. result = (*map(lambda columns: columns["device.class"], data),)
  4954. assert "low" in result
  4955. assert "medium" in result
  4956. assert "high" in result
  4957. def test_device_class_filter_low(self):
  4958. project1 = self.create_project()
  4959. for i in range(3):
  4960. self.store_event(data=self.load_data(platform="javascript"), project_id=project1.id)
  4961. self.store_event(
  4962. data={
  4963. "event_id": "a" * 32,
  4964. "transaction": "/example",
  4965. "message": "how to make fast",
  4966. "timestamp": self.ten_mins_ago_iso,
  4967. "tags": {"device.class": "1"},
  4968. },
  4969. project_id=project1.id,
  4970. )
  4971. features = {"organizations:discover-basic": True, "organizations:global-views": True}
  4972. query = {
  4973. "field": ["device.class", "count()"],
  4974. "query": "device.class:low",
  4975. "orderby": "count()",
  4976. "statsPeriod": "24h",
  4977. }
  4978. response = self.do_request(query, features=features)
  4979. assert response.status_code == 200, response.content
  4980. data = response.data["data"]
  4981. assert len(data) == 1
  4982. assert data[0]["count()"] == 1
  4983. def test_group_id_as_custom_tag(self):
  4984. project1 = self.create_project()
  4985. self.store_event(
  4986. data={
  4987. "event_id": "a" * 32,
  4988. "message": "poof",
  4989. "timestamp": self.ten_mins_ago_iso,
  4990. "user": {"email": self.user.email},
  4991. "tags": {"group_id": "this should just get returned"},
  4992. },
  4993. project_id=project1.id,
  4994. )
  4995. query = {
  4996. "field": ["group_id"],
  4997. "query": "",
  4998. "orderby": "group_id",
  4999. "statsPeriod": "24h",
  5000. }
  5001. response = self.do_request(query)
  5002. assert response.status_code == 200, response.content
  5003. assert response.data["data"][0]["group_id"] == "this should just get returned"
  5004. def test_floored_epm(self):
  5005. for _ in range(5):
  5006. data = self.load_data(
  5007. timestamp=self.ten_mins_ago,
  5008. duration=timedelta(seconds=5),
  5009. )
  5010. data["transaction"] = "/aggregates/1"
  5011. event1 = self.store_event(data, project_id=self.project.id)
  5012. query = {
  5013. "field": ["transaction", "floored_epm()", "epm()"],
  5014. "query": "event.type:transaction",
  5015. "orderby": ["transaction"],
  5016. "start": self.eleven_mins_ago_iso,
  5017. "end": iso_format(self.nine_mins_ago),
  5018. }
  5019. response = self.do_request(query)
  5020. assert response.status_code == 200, response.content
  5021. assert len(response.data["data"]) == 1
  5022. data = response.data["data"]
  5023. assert data[0]["transaction"] == event1.transaction
  5024. assert data[0]["floored_epm()"] == 1
  5025. assert data[0]["epm()"] == 2.5
  5026. def test_floored_epm_more_events(self):
  5027. for _ in range(25):
  5028. data = self.load_data(
  5029. timestamp=self.ten_mins_ago,
  5030. duration=timedelta(seconds=5),
  5031. )
  5032. data["transaction"] = "/aggregates/1"
  5033. event1 = self.store_event(data, project_id=self.project.id)
  5034. query = {
  5035. "field": ["transaction", "floored_epm()", "epm()"],
  5036. "query": "event.type:transaction",
  5037. "orderby": ["transaction"],
  5038. "start": self.eleven_mins_ago_iso,
  5039. "end": iso_format(self.nine_mins_ago),
  5040. }
  5041. response = self.do_request(query)
  5042. assert response.status_code == 200, response.content
  5043. assert len(response.data["data"]) == 1
  5044. data = response.data["data"]
  5045. assert data[0]["transaction"] == event1.transaction
  5046. assert data[0]["epm()"] == 12.5
  5047. assert data[0]["floored_epm()"] == 10
  5048. @region_silo_test
  5049. class OrganizationEventsProfilesDatasetEndpointTest(OrganizationEventsEndpointTestBase):
  5050. @mock.patch("sentry.search.events.builder.discover.raw_snql_query")
  5051. def test_profiles_dataset_simple(self, mock_snql_query):
  5052. mock_snql_query.side_effect = [
  5053. {
  5054. "data": [
  5055. {
  5056. "project": self.project.id,
  5057. "transaction": "foo",
  5058. "last_seen": "2022-10-20T16:41:22+00:00",
  5059. "latest_event": "a" * 32,
  5060. "count": 1,
  5061. "count_unique_transaction": 1,
  5062. "percentile_profile_duration_0_25": 1,
  5063. "p50_profile_duration": 1,
  5064. "p75_profile_duration": 1,
  5065. "p95_profile_duration": 1,
  5066. "p99_profile_duration": 1,
  5067. "p100_profile_duration": 1,
  5068. "min_profile_duration": 1,
  5069. "max_profile_duration": 1,
  5070. "avg_profile_duration": 1,
  5071. "sum_profile_duration": 1,
  5072. },
  5073. ],
  5074. "meta": [
  5075. {
  5076. "name": "project",
  5077. "type": "UInt64",
  5078. },
  5079. {
  5080. "name": "transaction",
  5081. "type": "LowCardinality(String)",
  5082. },
  5083. {
  5084. "name": "last_seen",
  5085. "type": "DateTime",
  5086. },
  5087. {
  5088. "name": "latest_event",
  5089. "type": "String",
  5090. },
  5091. {
  5092. "name": "count",
  5093. "type": "UInt64",
  5094. },
  5095. {
  5096. "name": "count_unique_transaction",
  5097. "type": "UInt64",
  5098. },
  5099. {
  5100. "name": "percentile_profile_duration_0_25",
  5101. "type": "Float64",
  5102. },
  5103. *[
  5104. {
  5105. "name": f"{fn}_profile_duration",
  5106. "type": "Float64",
  5107. }
  5108. for fn in ["p50", "p75", "p95", "p99", "p100", "min", "max", "avg", "sum"]
  5109. ],
  5110. ],
  5111. },
  5112. ]
  5113. fields = [
  5114. "project",
  5115. "transaction",
  5116. "last_seen()",
  5117. "latest_event()",
  5118. "count()",
  5119. "count_unique(transaction)",
  5120. "percentile(profile.duration, 0.25)",
  5121. "p50(profile.duration)",
  5122. "p75(profile.duration)",
  5123. "p95(profile.duration)",
  5124. "p99(profile.duration)",
  5125. "p100(profile.duration)",
  5126. "min(profile.duration)",
  5127. "max(profile.duration)",
  5128. "avg(profile.duration)",
  5129. "sum(profile.duration)",
  5130. ]
  5131. query = {
  5132. "field": fields,
  5133. "project": [self.project.id],
  5134. "dataset": "profiles",
  5135. }
  5136. response = self.do_request(query, features={"organizations:profiling": True})
  5137. assert response.status_code == 200, response.content
  5138. # making sure the response keys are in the form we expect and not aliased
  5139. data_keys = {key for row in response.data["data"] for key in row}
  5140. field_keys = {key for key in response.data["meta"]["fields"]}
  5141. unit_keys = {key for key in response.data["meta"]["units"]}
  5142. assert set(fields) == data_keys
  5143. assert set(fields) == field_keys
  5144. assert set(fields) == unit_keys
  5145. @region_silo_test
  5146. class OrganizationEventsProfileFunctionsDatasetEndpointTest(
  5147. OrganizationEventsEndpointTestBase, ProfilesSnubaTestCase
  5148. ):
  5149. def test_functions_dataset_simple(self):
  5150. self.store_functions(
  5151. [
  5152. {
  5153. "self_times_ns": [100 for _ in range(100)],
  5154. "package": "foo",
  5155. "function": "bar",
  5156. "in_app": True,
  5157. },
  5158. ],
  5159. project=self.project,
  5160. timestamp=before_now(hours=3),
  5161. )
  5162. self.store_functions(
  5163. [
  5164. {
  5165. "self_times_ns": [150 for _ in range(100)],
  5166. "package": "foo",
  5167. "function": "bar",
  5168. "in_app": True,
  5169. },
  5170. ],
  5171. project=self.project,
  5172. timestamp=before_now(hours=1),
  5173. )
  5174. mid = before_now(hours=2)
  5175. fields = [
  5176. "transaction",
  5177. "project",
  5178. "function",
  5179. "package",
  5180. "is_application",
  5181. "platform.name",
  5182. "environment",
  5183. "release",
  5184. "count()",
  5185. "examples()",
  5186. "p50()",
  5187. "p75()",
  5188. "p95()",
  5189. "p99()",
  5190. "avg()",
  5191. "sum()",
  5192. f"regression_score(function.duration, 0.95, {int(mid.timestamp())})",
  5193. ]
  5194. response = self.do_request(
  5195. {
  5196. "field": fields,
  5197. "statsPeriod": "4h",
  5198. "project": [self.project.id],
  5199. "dataset": "profileFunctions",
  5200. },
  5201. features={"organizations:profiling": True},
  5202. )
  5203. assert response.status_code == 200, response.content
  5204. # making sure the response keys are in the form we expect and not aliased
  5205. data_keys = {key for row in response.data["data"] for key in row}
  5206. field_keys = {key for key in response.data["meta"]["fields"]}
  5207. unit_keys = {key for key in response.data["meta"]["units"]}
  5208. assert set(fields) == data_keys
  5209. assert set(fields) == field_keys
  5210. assert set(fields) == unit_keys
  5211. assert response.data["meta"]["units"] == {
  5212. "transaction": None,
  5213. "project": None,
  5214. "function": None,
  5215. "package": None,
  5216. "is_application": None,
  5217. "platform.name": None,
  5218. "environment": None,
  5219. "release": None,
  5220. "count()": None,
  5221. "examples()": None,
  5222. "p50()": "nanosecond",
  5223. "p75()": "nanosecond",
  5224. "p95()": "nanosecond",
  5225. "p99()": "nanosecond",
  5226. "avg()": "nanosecond",
  5227. "sum()": "nanosecond",
  5228. f"regression_score(function.duration, 0.95, {int(mid.timestamp())})": None,
  5229. }
  5230. @region_silo_test
  5231. class OrganizationEventsIssuePlatformDatasetEndpointTest(
  5232. OrganizationEventsEndpointTestBase, SearchIssueTestMixin, PerformanceIssueTestCase
  5233. ):
  5234. def test_performance_issue_id_filter(self):
  5235. event = self.create_performance_issue()
  5236. query = {
  5237. "field": ["count()"],
  5238. "statsPeriod": "2h",
  5239. "query": f"issue.id:{event.group.id}",
  5240. "dataset": "issuePlatform",
  5241. }
  5242. response = self.do_request(query)
  5243. assert response.status_code == 200, response.content
  5244. assert response.data["data"][0]["count()"] == 1
  5245. def test_generic_issue_ids_filter(self):
  5246. user_data = {
  5247. "id": self.user.id,
  5248. "username": "user",
  5249. "email": "hellboy@bar.com",
  5250. "ip_address": "127.0.0.1",
  5251. }
  5252. event, _, group_info = self.store_search_issue(
  5253. self.project.id,
  5254. self.user.id,
  5255. [f"{ProfileFileIOGroupType.type_id}-group1"],
  5256. "prod",
  5257. before_now(hours=1).replace(tzinfo=timezone.utc),
  5258. user=user_data,
  5259. )
  5260. event, _, group_info = self.store_search_issue(
  5261. self.project.id,
  5262. self.user.id,
  5263. [f"{ProfileFileIOGroupType.type_id}-group2"],
  5264. "prod",
  5265. before_now(hours=1).replace(tzinfo=timezone.utc),
  5266. user=user_data,
  5267. )
  5268. assert group_info is not None
  5269. query = {
  5270. "field": ["title", "release", "environment", "user.display", "timestamp"],
  5271. "statsPeriod": "90d",
  5272. "query": f"issue.id:{group_info.group.id}",
  5273. "dataset": "issuePlatform",
  5274. }
  5275. with self.feature(["organizations:profiling"]):
  5276. response = self.do_request(query)
  5277. assert response.status_code == 200, response.content
  5278. assert len(response.data["data"]) == 1
  5279. assert response.data["data"][0]["title"] == group_info.group.title
  5280. assert response.data["data"][0]["environment"] == "prod"
  5281. assert response.data["data"][0]["user.display"] == user_data["email"]
  5282. assert response.data["data"][0]["timestamp"] == event.timestamp
  5283. query = {
  5284. "field": ["title", "release", "environment", "user.display", "timestamp"],
  5285. "statsPeriod": "90d",
  5286. "query": f"issue:{group_info.group.qualified_short_id}",
  5287. "dataset": "issuePlatform",
  5288. }
  5289. with self.feature(["organizations:profiling"]):
  5290. response = self.do_request(query)
  5291. assert response.status_code == 200, response.content
  5292. assert len(response.data["data"]) == 1
  5293. assert response.data["data"][0]["title"] == group_info.group.title
  5294. assert response.data["data"][0]["environment"] == "prod"
  5295. assert response.data["data"][0]["user.display"] == user_data["email"]
  5296. assert response.data["data"][0]["timestamp"] == event.timestamp
  5297. def test_performance_short_group_id(self):
  5298. event = self.create_performance_issue()
  5299. query = {
  5300. "field": ["count()"],
  5301. "statsPeriod": "1h",
  5302. "query": f"project:{event.group.project.slug} issue:{event.group.qualified_short_id}",
  5303. "dataset": "issuePlatform",
  5304. }
  5305. response = self.do_request(query)
  5306. assert response.status_code == 200, response.content
  5307. assert response.data["data"][0]["count()"] == 1
  5308. def test_multiple_performance_short_group_ids_filter(self):
  5309. event1 = self.create_performance_issue()
  5310. event2 = self.create_performance_issue()
  5311. query = {
  5312. "field": ["count()"],
  5313. "statsPeriod": "1h",
  5314. "query": f"project:{event1.group.project.slug} issue:[{event1.group.qualified_short_id},{event2.group.qualified_short_id}]",
  5315. "dataset": "issuePlatform",
  5316. }
  5317. response = self.do_request(query)
  5318. assert response.status_code == 200, response.content
  5319. assert response.data["data"][0]["count()"] == 2
  5320. def test_user_display_issue_platform(self):
  5321. project1 = self.create_project()
  5322. user_data = {
  5323. "id": self.user.id,
  5324. "username": "user",
  5325. "email": "hellboy@bar.com",
  5326. "ip_address": "127.0.0.1",
  5327. }
  5328. _, _, group_info = self.store_search_issue(
  5329. project1.id,
  5330. 1,
  5331. ["group1-fingerprint"],
  5332. None,
  5333. before_now(hours=1).replace(tzinfo=timezone.utc),
  5334. user=user_data,
  5335. )
  5336. assert group_info is not None
  5337. features = {
  5338. "organizations:discover-basic": True,
  5339. "organizations:global-views": True,
  5340. "organizations:profiling": True,
  5341. }
  5342. query = {
  5343. "field": ["user.display"],
  5344. "query": f"user.display:hell* issue.id:{group_info.group.id}",
  5345. "statsPeriod": "24h",
  5346. "dataset": "issuePlatform",
  5347. }
  5348. response = self.do_request(query, features=features)
  5349. assert response.status_code == 200, response.content
  5350. data = response.data["data"]
  5351. assert len(data) == 1
  5352. result = {r["user.display"] for r in data}
  5353. assert result == {user_data["email"]}
  5354. def test_all_events_fields(self):
  5355. user_data = {
  5356. "id": self.user.id,
  5357. "username": "user",
  5358. "email": "hellboy@bar.com",
  5359. "ip_address": "127.0.0.1",
  5360. }
  5361. replay_id = str(uuid.uuid4())
  5362. profile_id = str(uuid.uuid4())
  5363. event = self.create_performance_issue(
  5364. contexts={
  5365. "trace": {
  5366. "trace_id": str(uuid.uuid4().hex),
  5367. "span_id": "933e5c9a8e464da9",
  5368. "type": "trace",
  5369. },
  5370. "replay": {"replay_id": replay_id},
  5371. "profile": {"profile_id": profile_id},
  5372. },
  5373. user_data=user_data,
  5374. )
  5375. query = {
  5376. "field": [
  5377. "id",
  5378. "transaction",
  5379. "title",
  5380. "release",
  5381. "environment",
  5382. "user.display",
  5383. "device",
  5384. "os",
  5385. "url",
  5386. "runtime",
  5387. "replayId",
  5388. "profile.id",
  5389. "transaction.duration",
  5390. "timestamp",
  5391. ],
  5392. "statsPeriod": "1h",
  5393. "query": f"project:{event.group.project.slug} issue:{event.group.qualified_short_id}",
  5394. "dataset": "issuePlatform",
  5395. }
  5396. response = self.do_request(query)
  5397. assert response.status_code == 200, response.content
  5398. data = response.data["data"][0]
  5399. assert data == {
  5400. "id": event.event_id,
  5401. "transaction": event.transaction,
  5402. "project.name": event.project.name.lower(),
  5403. "title": event.group.title,
  5404. "release": event.release,
  5405. "environment": event.get_environment().name,
  5406. "user.display": user_data["email"],
  5407. "device": "Mac",
  5408. "os": "",
  5409. "url": event.interfaces.data["request"].full_url,
  5410. "runtime": dict(event.get_raw_data()["tags"])["runtime"],
  5411. "replayId": replay_id.replace("-", ""),
  5412. "profile.id": profile_id.replace("-", ""),
  5413. "transaction.duration": 3000,
  5414. "timestamp": event.datetime.replace(microsecond=0).isoformat(),
  5415. }