test_organization_events.py 219 KB

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