test_organization_events.py 196 KB

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