test_organization_events.py 209 KB

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