12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226 |
- from typing import Any
- from unittest import mock
- import pytest
- from django.urls import reverse
- from rest_framework.response import Response
- from sentry.discover.models import DatasetSourcesTypes, TeamKeyTransaction
- from sentry.models.dashboard_widget import DashboardWidgetTypes
- from sentry.models.projectteam import ProjectTeam
- from sentry.models.transaction_threshold import (
- ProjectTransactionThreshold,
- ProjectTransactionThresholdOverride,
- TransactionMetric,
- )
- from sentry.search.events import constants
- from sentry.search.utils import map_device_class_level
- from sentry.snuba.metrics.extraction import (
- SPEC_VERSION_TWO_FLAG,
- MetricSpecType,
- OnDemandMetricSpec,
- OnDemandMetricSpecVersioning,
- )
- from sentry.snuba.metrics.naming_layer.mri import TransactionMRI
- from sentry.snuba.metrics.naming_layer.public import TransactionMetricKey
- from sentry.snuba.utils import DATASET_OPTIONS
- from sentry.testutils.cases import MetricsEnhancedPerformanceTestCase
- from sentry.testutils.helpers.datetime import before_now
- from sentry.testutils.helpers.discover import user_misery_formula
- from sentry.testutils.helpers.on_demand import create_widget
- from sentry.utils.samples import load_data
- pytestmark = pytest.mark.sentry_metrics
- class OrganizationEventsMetricsEnhancedPerformanceEndpointTest(MetricsEnhancedPerformanceTestCase):
- viewname = "sentry-api-0-organization-events"
- # Poor intentionally omitted for test_measurement_rating_that_does_not_exist
- METRIC_STRINGS = [
- "foo_transaction",
- "bar_transaction",
- "baz_transaction",
- "staging",
- "measurement_rating",
- "good",
- "meh",
- "d:transactions/measurements.something_custom@millisecond",
- "d:transactions/measurements.runtime@hour",
- "d:transactions/measurements.bytes_transfered@byte",
- "d:transactions/measurements.datacenter_memory@petabyte",
- "d:transactions/measurements.custom.kilobyte@kilobyte",
- "d:transactions/measurements.longtaskcount@none",
- "d:transactions/measurements.percent@ratio",
- "d:transactions/measurements.custom_type@somethingcustom",
- ]
- def setUp(self):
- super().setUp()
- self.transaction_data = load_data("transaction", timestamp=before_now(minutes=1))
- self.features = {
- "organizations:performance-use-metrics": True,
- }
- def do_request(self, query, features=None):
- if features is None:
- features = {"organizations:discover-basic": True}
- features.update(self.features)
- self.login_as(user=self.user)
- url = reverse(
- self.viewname,
- kwargs={"organization_id_or_slug": self.organization.slug},
- )
- with self.feature(features):
- return self.client.get(url, query, format="json")
- def test_no_projects(self):
- response = self.do_request(
- {
- "dataset": "metricsEnhanced",
- }
- )
- assert response.status_code == 200, response.content
- def test_invalid_dataset(self):
- response = self.do_request(
- {
- "dataset": "aFakeDataset",
- "project": self.project.id,
- }
- )
- assert response.status_code == 400, response.content
- assert (
- response.data["detail"]
- == f"dataset must be one of: {', '.join([key for key in DATASET_OPTIONS.keys()])}"
- )
- def test_out_of_retention(self):
- self.create_project()
- with self.options({"system.event-retention-days": 10}):
- query = {
- "field": ["id", "timestamp"],
- "orderby": ["-timestamp", "-id"],
- "query": "event.type:transaction",
- "start": before_now(days=20),
- "end": before_now(days=15),
- "dataset": "metricsEnhanced",
- }
- response = self.do_request(query)
- assert response.status_code == 400, response.content
- assert response.data["detail"] == "Invalid date range. Please try a more recent date range."
- def test_invalid_search_terms(self):
- response = self.do_request(
- {
- "field": ["epm()"],
- "query": "hi \n there",
- "project": self.project.id,
- "dataset": "metricsEnhanced",
- }
- )
- assert response.status_code == 400, response.content
- assert (
- response.data["detail"]
- == "Parse error at 'hi \n ther' (column 4). This is commonly caused by unmatched parentheses. Enclose any text in double quotes."
- )
- def test_percentile_with_no_data(self):
- response = self.do_request(
- {
- "field": ["p50()"],
- "query": "",
- "project": self.project.id,
- "dataset": "metricsEnhanced",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- assert len(data) == 1
- assert data[0]["p50()"] == 0
- def test_project_name(self):
- self.store_transaction_metric(
- 1,
- tags={"environment": "staging"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["project.name", "environment", "epm()"],
- "query": "event.type:transaction",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["project.name"] == self.project.slug
- assert "project.id" not in data[0]
- assert data[0]["environment"] == "staging"
- assert meta["isMetricsData"]
- assert field_meta["project.name"] == "string"
- assert field_meta["environment"] == "string"
- assert field_meta["epm()"] == "rate"
- def test_project_id(self):
- self.store_transaction_metric(
- 1,
- tags={"environment": "staging"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["project_id", "environment", "epm()"],
- "query": "event.type:transaction",
- "dataset": "metrics",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["project_id"] == self.project.id
- assert data[0]["environment"] == "staging"
- assert meta["isMetricsData"]
- assert field_meta["project_id"] == "integer"
- assert field_meta["environment"] == "string"
- assert field_meta["epm()"] == "rate"
- def test_project_dot_id(self):
- self.store_transaction_metric(
- 1,
- tags={"environment": "staging"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["project.id", "environment", "epm()"],
- "query": "event.type:transaction",
- "dataset": "metrics",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["project.id"] == self.project.id
- assert data[0]["environment"] == "staging"
- assert meta["isMetricsData"]
- assert field_meta["project.id"] == "integer"
- assert field_meta["environment"] == "string"
- assert field_meta["epm()"] == "rate"
- def test_title_alias(self):
- """title is an alias to transaction name"""
- self.store_transaction_metric(
- 1,
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["title", "p50()"],
- "query": "event.type:transaction",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["title"] == "foo_transaction"
- assert data[0]["p50()"] == 1
- assert meta["isMetricsData"]
- assert field_meta["title"] == "string"
- assert field_meta["p50()"] == "duration"
- def test_having_condition(self):
- self.store_transaction_metric(
- 1,
- tags={"environment": "staging", "transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- # shouldn't show up
- 100,
- tags={"environment": "staging", "transaction": "bar_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["transaction", "project", "p50(transaction.duration)"],
- "query": "event.type:transaction p50(transaction.duration):<50",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["transaction"] == "foo_transaction"
- assert data[0]["project"] == self.project.slug
- assert data[0]["p50(transaction.duration)"] == 1
- assert meta["isMetricsData"]
- assert field_meta["transaction"] == "string"
- assert field_meta["project"] == "string"
- assert field_meta["p50(transaction.duration)"] == "duration"
- def test_having_condition_with_preventing_aggregates(self):
- self.store_transaction_metric(
- 1,
- tags={"environment": "staging", "transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 100,
- tags={"environment": "staging", "transaction": "bar_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["transaction", "project", "p50(transaction.duration)"],
- "query": "event.type:transaction p50(transaction.duration):<50",
- "dataset": "metricsEnhanced",
- "preventMetricAggregates": "1",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 0
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert not meta["isMetricsData"]
- assert field_meta["transaction"] == "string"
- assert field_meta["project"] == "string"
- assert field_meta["p50(transaction.duration)"] == "duration"
- def test_having_condition_with_preventing_aggregate_metrics_only(self):
- """same as the previous test, but with the dataset on explicit metrics
- which should throw a 400 error instead"""
- response = self.do_request(
- {
- "field": ["transaction", "project", "p50(transaction.duration)"],
- "query": "event.type:transaction p50(transaction.duration):<50",
- "dataset": "metrics",
- "preventMetricAggregates": "1",
- "per_page": 50,
- "project": self.project.id,
- }
- )
- assert response.status_code == 400, response.content
- def test_having_condition_not_selected(self):
- self.store_transaction_metric(
- 1,
- tags={"environment": "staging", "transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- # shouldn't show up
- 100,
- tags={"environment": "staging", "transaction": "bar_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["transaction", "project", "p50(transaction.duration)"],
- "query": "event.type:transaction p75(transaction.duration):<50",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["transaction"] == "foo_transaction"
- assert data[0]["project"] == self.project.slug
- assert data[0]["p50(transaction.duration)"] == 1
- assert meta["isMetricsData"]
- assert field_meta["transaction"] == "string"
- assert field_meta["project"] == "string"
- assert field_meta["p50(transaction.duration)"] == "duration"
- def test_non_metrics_tag_with_implicit_format(self):
- self.store_transaction_metric(
- 1,
- tags={"environment": "staging", "transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["test", "p50(transaction.duration)"],
- "query": "event.type:transaction",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 0
- assert not response.data["meta"]["isMetricsData"]
- def test_non_metrics_tag_with_implicit_format_metrics_dataset(self):
- self.store_transaction_metric(
- 1,
- tags={"environment": "staging", "transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["test", "p50(transaction.duration)"],
- "query": "event.type:transaction",
- "dataset": "metrics",
- "per_page": 50,
- }
- )
- assert response.status_code == 400, response.content
- @pytest.mark.querybuilder
- def test_performance_homepage_query(self):
- self.store_transaction_metric(
- 1,
- tags={
- "transaction": "foo_transaction",
- constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
- },
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1,
- "measurements.fcp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 2,
- "measurements.lcp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 3,
- "measurements.fid",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 4,
- "measurements.cls",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1,
- "user",
- tags={
- "transaction": "foo_transaction",
- constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_FRUSTRATED_TAG_VALUE,
- },
- timestamp=self.min_ago,
- )
- for dataset in ["metrics", "metricsEnhanced"]:
- response = self.do_request(
- {
- "field": [
- "transaction",
- "project",
- "tpm()",
- "p75(measurements.fcp)",
- "p75(measurements.lcp)",
- "p75(measurements.fid)",
- "p75(measurements.cls)",
- "count_unique(user)",
- "apdex()",
- "count_miserable(user)",
- "user_misery()",
- "failure_rate()",
- "failure_count()",
- ],
- "orderby": "tpm()",
- "query": "event.type:transaction",
- "dataset": dataset,
- "per_page": 50,
- }
- )
- assert len(response.data["data"]) == 1
- data = response.data["data"][0]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data["transaction"] == "foo_transaction"
- assert data["project"] == self.project.slug
- assert data["p75(measurements.fcp)"] == 1.0
- assert data["p75(measurements.lcp)"] == 2.0
- assert data["p75(measurements.fid)"] == 3.0
- assert data["p75(measurements.cls)"] == 4.0
- assert data["apdex()"] == 1.0
- assert data["count_miserable(user)"] == 1.0
- assert data["user_misery()"] == 0.058
- assert data["failure_rate()"] == 1
- assert data["failure_count()"] == 1
- assert meta["isMetricsData"]
- assert field_meta["transaction"] == "string"
- assert field_meta["project"] == "string"
- assert field_meta["p75(measurements.fcp)"] == "duration"
- assert field_meta["p75(measurements.lcp)"] == "duration"
- assert field_meta["p75(measurements.fid)"] == "duration"
- assert field_meta["p75(measurements.cls)"] == "number"
- assert field_meta["apdex()"] == "number"
- assert field_meta["count_miserable(user)"] == "integer"
- assert field_meta["user_misery()"] == "number"
- assert field_meta["failure_rate()"] == "percentage"
- assert field_meta["failure_count()"] == "integer"
- assert field_meta["tpm()"] == "rate"
- assert meta["units"]["tpm()"] == "1/minute"
- def test_user_misery_and_team_key_sort(self):
- self.store_transaction_metric(
- 1,
- tags={
- "transaction": "foo_transaction",
- constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
- },
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1,
- "measurements.fcp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 2,
- "measurements.lcp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 3,
- "measurements.fid",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 4,
- "measurements.cls",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1,
- "user",
- tags={
- "transaction": "foo_transaction",
- constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_FRUSTRATED_TAG_VALUE,
- },
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "team_key_transaction",
- "transaction",
- "project",
- "tpm()",
- "p75(measurements.fcp)",
- "p75(measurements.lcp)",
- "p75(measurements.fid)",
- "p75(measurements.cls)",
- "count_unique(user)",
- "apdex()",
- "count_miserable(user)",
- "user_misery()",
- "failure_rate()",
- "failure_count()",
- ],
- "orderby": ["team_key_transaction", "user_misery()"],
- "query": "event.type:transaction",
- "dataset": "metrics",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"][0]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data["transaction"] == "foo_transaction"
- assert data["project"] == self.project.slug
- assert data["p75(measurements.fcp)"] == 1.0
- assert data["p75(measurements.lcp)"] == 2.0
- assert data["p75(measurements.fid)"] == 3.0
- assert data["p75(measurements.cls)"] == 4.0
- assert data["apdex()"] == 1.0
- assert data["count_miserable(user)"] == 1.0
- assert data["user_misery()"] == 0.058
- assert data["failure_rate()"] == 1
- assert data["failure_count()"] == 1
- assert meta["isMetricsData"]
- assert field_meta["transaction"] == "string"
- assert field_meta["project"] == "string"
- assert field_meta["p75(measurements.fcp)"] == "duration"
- assert field_meta["p75(measurements.lcp)"] == "duration"
- assert field_meta["p75(measurements.fid)"] == "duration"
- assert field_meta["p75(measurements.cls)"] == "number"
- assert field_meta["apdex()"] == "number"
- assert field_meta["count_miserable(user)"] == "integer"
- assert field_meta["user_misery()"] == "number"
- assert field_meta["failure_rate()"] == "percentage"
- assert field_meta["failure_count()"] == "integer"
- def test_no_team_key_transactions(self):
- self.store_transaction_metric(
- 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago
- )
- self.store_transaction_metric(
- 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
- )
- query = {
- "team": "myteams",
- "project": [self.project.id],
- # TODO sort by transaction here once that's possible for order to match the same test without metrics
- "orderby": "p95()",
- "field": [
- "team_key_transaction",
- "transaction",
- "transaction.status",
- "project",
- "epm()",
- "failure_rate()",
- "p95()",
- ],
- "per_page": 50,
- "dataset": "metricsEnhanced",
- }
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 2
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["team_key_transaction"] == 0
- assert data[0]["transaction"] == "foo_transaction"
- assert data[1]["team_key_transaction"] == 0
- assert data[1]["transaction"] == "bar_transaction"
- assert meta["isMetricsData"]
- assert field_meta["team_key_transaction"] == "boolean"
- assert field_meta["transaction"] == "string"
- def test_team_key_transactions_my_teams(self):
- team1 = self.create_team(organization=self.organization, name="Team A")
- self.create_team_membership(team1, user=self.user)
- self.project.add_team(team1)
- team2 = self.create_team(organization=self.organization, name="Team B")
- self.project.add_team(team2)
- key_transactions = [
- (team1, "foo_transaction"),
- (team2, "baz_transaction"),
- ]
- # Not a key transaction
- self.store_transaction_metric(
- 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
- )
- for team, transaction in key_transactions:
- self.store_transaction_metric(
- 1, tags={"transaction": transaction}, timestamp=self.min_ago
- )
- TeamKeyTransaction.objects.create(
- organization=self.organization,
- transaction=transaction,
- project_team=ProjectTeam.objects.get(project=self.project, team=team),
- )
- query = {
- "team": "myteams",
- "project": [self.project.id],
- "field": [
- "team_key_transaction",
- "transaction",
- "transaction.status",
- "project",
- "epm()",
- "failure_rate()",
- "p95()",
- ],
- "per_page": 50,
- "dataset": "metricsEnhanced",
- }
- query["orderby"] = ["team_key_transaction", "p95()"]
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 3
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["team_key_transaction"] == 0
- assert data[0]["transaction"] == "baz_transaction"
- assert data[1]["team_key_transaction"] == 0
- assert data[1]["transaction"] == "bar_transaction"
- assert data[2]["team_key_transaction"] == 1
- assert data[2]["transaction"] == "foo_transaction"
- assert meta["isMetricsData"]
- assert field_meta["team_key_transaction"] == "boolean"
- assert field_meta["transaction"] == "string"
- # not specifying any teams should use my teams
- query = {
- "project": [self.project.id],
- "field": [
- "team_key_transaction",
- "transaction",
- "transaction.status",
- "project",
- "epm()",
- "failure_rate()",
- "p95()",
- ],
- "per_page": 50,
- "dataset": "metricsEnhanced",
- }
- query["orderby"] = ["team_key_transaction", "p95()"]
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 3
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["team_key_transaction"] == 0
- assert data[0]["transaction"] == "baz_transaction"
- assert data[1]["team_key_transaction"] == 0
- assert data[1]["transaction"] == "bar_transaction"
- assert data[2]["team_key_transaction"] == 1
- assert data[2]["transaction"] == "foo_transaction"
- assert meta["isMetricsData"]
- assert field_meta["team_key_transaction"] == "boolean"
- assert field_meta["transaction"] == "string"
- def test_team_key_transactions_orderby(self):
- team1 = self.create_team(organization=self.organization, name="Team A")
- team2 = self.create_team(organization=self.organization, name="Team B")
- key_transactions = [
- (team1, "foo_transaction", 1),
- (team2, "baz_transaction", 100),
- ]
- # Not a key transaction
- self.store_transaction_metric(
- 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
- )
- for team, transaction, value in key_transactions:
- self.store_transaction_metric(
- value, tags={"transaction": transaction}, timestamp=self.min_ago
- )
- self.create_team_membership(team, user=self.user)
- self.project.add_team(team)
- TeamKeyTransaction.objects.create(
- organization=self.organization,
- transaction=transaction,
- project_team=ProjectTeam.objects.get(project=self.project, team=team),
- )
- query = {
- "team": "myteams",
- "project": [self.project.id],
- "field": [
- "team_key_transaction",
- "transaction",
- "transaction.status",
- "project",
- "epm()",
- "failure_rate()",
- "p95()",
- ],
- "per_page": 50,
- "dataset": "metricsEnhanced",
- }
- # test ascending order
- query["orderby"] = ["team_key_transaction", "p95()"]
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 3
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["team_key_transaction"] == 0
- assert data[0]["transaction"] == "bar_transaction"
- assert data[1]["team_key_transaction"] == 1
- assert data[1]["transaction"] == "foo_transaction"
- assert data[2]["team_key_transaction"] == 1
- assert data[2]["transaction"] == "baz_transaction"
- assert meta["isMetricsData"]
- assert field_meta["team_key_transaction"] == "boolean"
- assert field_meta["transaction"] == "string"
- # test descending order
- query["orderby"] = ["-team_key_transaction", "p95()"]
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 3
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["team_key_transaction"] == 1
- assert data[0]["transaction"] == "foo_transaction"
- assert data[1]["team_key_transaction"] == 1
- assert data[1]["transaction"] == "baz_transaction"
- assert data[2]["team_key_transaction"] == 0
- assert data[2]["transaction"] == "bar_transaction"
- assert meta["isMetricsData"]
- assert field_meta["team_key_transaction"] == "boolean"
- assert field_meta["transaction"] == "string"
- def test_team_key_transactions_query(self):
- team1 = self.create_team(organization=self.organization, name="Team A")
- team2 = self.create_team(organization=self.organization, name="Team B")
- key_transactions = [
- (team1, "foo_transaction", 1),
- (team2, "baz_transaction", 100),
- ]
- # Not a key transaction
- self.store_transaction_metric(
- 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
- )
- for team, transaction, value in key_transactions:
- self.store_transaction_metric(
- value, tags={"transaction": transaction}, timestamp=self.min_ago
- )
- self.create_team_membership(team, user=self.user)
- self.project.add_team(team)
- TeamKeyTransaction.objects.create(
- organization=self.organization,
- transaction=transaction,
- project_team=ProjectTeam.objects.get(project=self.project, team=team),
- )
- query = {
- "team": "myteams",
- "project": [self.project.id],
- # use the order by to ensure the result order
- "orderby": "p95()",
- "field": [
- "team_key_transaction",
- "transaction",
- "transaction.status",
- "project",
- "epm()",
- "failure_rate()",
- "p95()",
- ],
- "per_page": 50,
- "dataset": "metricsEnhanced",
- }
- # key transactions
- query["query"] = "has:team_key_transaction"
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 2
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["team_key_transaction"] == 1
- assert data[0]["transaction"] == "foo_transaction"
- assert data[1]["team_key_transaction"] == 1
- assert data[1]["transaction"] == "baz_transaction"
- assert meta["isMetricsData"]
- assert field_meta["team_key_transaction"] == "boolean"
- assert field_meta["transaction"] == "string"
- # key transactions
- query["query"] = "team_key_transaction:true"
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 2
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["team_key_transaction"] == 1
- assert data[0]["transaction"] == "foo_transaction"
- assert data[1]["team_key_transaction"] == 1
- assert data[1]["transaction"] == "baz_transaction"
- assert meta["isMetricsData"]
- assert field_meta["team_key_transaction"] == "boolean"
- assert field_meta["transaction"] == "string"
- # not key transactions
- query["query"] = "!has:team_key_transaction"
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["team_key_transaction"] == 0
- assert data[0]["transaction"] == "bar_transaction"
- assert meta["isMetricsData"]
- assert field_meta["team_key_transaction"] == "boolean"
- assert field_meta["transaction"] == "string"
- # not key transactions
- query["query"] = "team_key_transaction:false"
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["team_key_transaction"] == 0
- assert data[0]["transaction"] == "bar_transaction"
- assert meta["isMetricsData"]
- assert field_meta["team_key_transaction"] == "boolean"
- assert field_meta["transaction"] == "string"
- def test_team_key_transaction_not_exists(self):
- team1 = self.create_team(organization=self.organization, name="Team A")
- team2 = self.create_team(organization=self.organization, name="Team B")
- key_transactions = [
- (team1, "foo_transaction", 1),
- (team2, "baz_transaction", 100),
- ]
- for team, transaction, value in key_transactions:
- self.store_transaction_metric(
- value, tags={"transaction": transaction}, timestamp=self.min_ago
- )
- self.create_team_membership(team, user=self.user)
- self.project.add_team(team)
- TeamKeyTransaction.objects.create(
- organization=self.organization,
- transaction=transaction,
- project_team=ProjectTeam.objects.get(project=self.project, team=team),
- )
- # Don't create a metric for this one
- TeamKeyTransaction.objects.create(
- organization=self.organization,
- transaction="not_in_metrics",
- project_team=ProjectTeam.objects.get(project=self.project, team=team1),
- )
- query = {
- "team": "myteams",
- "project": [self.project.id],
- # use the order by to ensure the result order
- "orderby": "p95()",
- "field": [
- "team_key_transaction",
- "transaction",
- "transaction.status",
- "project",
- "epm()",
- "failure_rate()",
- "p95()",
- ],
- "per_page": 50,
- "dataset": "metricsEnhanced",
- }
- # key transactions
- query["query"] = "has:team_key_transaction"
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 2
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["team_key_transaction"] == 1
- assert data[0]["transaction"] == "foo_transaction"
- assert data[1]["team_key_transaction"] == 1
- assert data[1]["transaction"] == "baz_transaction"
- assert meta["isMetricsData"]
- assert field_meta["team_key_transaction"] == "boolean"
- assert field_meta["transaction"] == "string"
- # key transactions
- query["query"] = "team_key_transaction:true"
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 2
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["team_key_transaction"] == 1
- assert data[0]["transaction"] == "foo_transaction"
- assert data[1]["team_key_transaction"] == 1
- assert data[1]["transaction"] == "baz_transaction"
- assert meta["isMetricsData"]
- assert field_meta["team_key_transaction"] == "boolean"
- assert field_meta["transaction"] == "string"
- # not key transactions
- query["query"] = "!has:team_key_transaction"
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 0
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert meta["isMetricsData"]
- assert field_meta["team_key_transaction"] == "boolean"
- assert field_meta["transaction"] == "string"
- # not key transactions
- query["query"] = "team_key_transaction:false"
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 0
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert meta["isMetricsData"]
- assert field_meta["team_key_transaction"] == "boolean"
- assert field_meta["transaction"] == "string"
- def test_too_many_team_key_transactions(self):
- MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS = 1
- with mock.patch(
- "sentry.search.events.fields.MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS",
- MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS,
- ):
- team = self.create_team(organization=self.organization, name="Team A")
- self.create_team_membership(team, user=self.user)
- self.project.add_team(team)
- project_team = ProjectTeam.objects.get(project=self.project, team=team)
- transactions = ["foo_transaction", "bar_transaction", "baz_transaction"]
- for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1):
- self.store_transaction_metric(
- 100, tags={"transaction": transactions[i]}, timestamp=self.min_ago
- )
- TeamKeyTransaction.objects.bulk_create(
- [
- TeamKeyTransaction(
- organization=self.organization,
- project_team=project_team,
- transaction=transactions[i],
- )
- for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1)
- ]
- )
- query = {
- "team": "myteams",
- "project": [self.project.id],
- "orderby": "p95()",
- "field": [
- "team_key_transaction",
- "transaction",
- "transaction.status",
- "project",
- "epm()",
- "failure_rate()",
- "p95()",
- ],
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 2
- data = response.data["data"]
- meta = response.data["meta"]
- assert (
- sum(row["team_key_transaction"] for row in data)
- == MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS
- )
- assert meta["isMetricsData"]
- def test_measurement_rating(self):
- self.store_transaction_metric(
- 50,
- metric="measurements.lcp",
- tags={"measurement_rating": "good", "transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 15,
- metric="measurements.fp",
- tags={"measurement_rating": "good", "transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1500,
- metric="measurements.fcp",
- tags={"measurement_rating": "meh", "transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 125,
- metric="measurements.fid",
- tags={"measurement_rating": "meh", "transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.15,
- metric="measurements.cls",
- tags={"measurement_rating": "good", "transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "count_web_vitals(measurements.lcp, good)",
- "count_web_vitals(measurements.fp, good)",
- "count_web_vitals(measurements.fcp, meh)",
- "count_web_vitals(measurements.fid, meh)",
- "count_web_vitals(measurements.cls, good)",
- "count_web_vitals(measurements.lcp, any)",
- ],
- "query": "event.type:transaction",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["count_web_vitals(measurements.lcp, good)"] == 1
- assert data[0]["count_web_vitals(measurements.fp, good)"] == 1
- assert data[0]["count_web_vitals(measurements.fcp, meh)"] == 1
- assert data[0]["count_web_vitals(measurements.fid, meh)"] == 1
- assert data[0]["count_web_vitals(measurements.cls, good)"] == 1
- assert data[0]["count_web_vitals(measurements.lcp, any)"] == 1
- assert meta["isMetricsData"]
- assert field_meta["count_web_vitals(measurements.lcp, good)"] == "integer"
- assert field_meta["count_web_vitals(measurements.fp, good)"] == "integer"
- assert field_meta["count_web_vitals(measurements.fcp, meh)"] == "integer"
- assert field_meta["count_web_vitals(measurements.fid, meh)"] == "integer"
- assert field_meta["count_web_vitals(measurements.cls, good)"] == "integer"
- assert field_meta["count_web_vitals(measurements.lcp, any)"] == "integer"
- def test_measurement_rating_that_does_not_exist(self):
- self.store_transaction_metric(
- 1,
- metric="measurements.lcp",
- tags={"measurement_rating": "good", "transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["transaction", "count_web_vitals(measurements.lcp, poor)"],
- "query": "event.type:transaction",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["count_web_vitals(measurements.lcp, poor)"] == 0
- assert meta["isMetricsData"]
- assert meta["fields"]["count_web_vitals(measurements.lcp, poor)"] == "integer"
- def test_count_web_vitals_invalid_vital(self):
- query = {
- "field": [
- "count_web_vitals(measurements.foo, poor)",
- ],
- "project": [self.project.id],
- "dataset": "metricsEnhanced",
- }
- response = self.do_request(query)
- assert response.status_code == 400, response.content
- query = {
- "field": [
- "count_web_vitals(tags[lcp], poor)",
- ],
- "project": [self.project.id],
- "dataset": "metricsEnhanced",
- }
- response = self.do_request(query)
- assert response.status_code == 400, response.content
- query = {
- "field": [
- "count_web_vitals(transaction.duration, poor)",
- ],
- "project": [self.project.id],
- "dataset": "metricsEnhanced",
- }
- response = self.do_request(query)
- assert response.status_code == 400, response.content
- query = {
- "field": [
- "count_web_vitals(measurements.lcp, bad)",
- ],
- "project": [self.project.id],
- "dataset": "metricsEnhanced",
- }
- response = self.do_request(query)
- assert response.status_code == 400, response.content
- def test_count_unique_user_returns_zero(self):
- self.store_transaction_metric(
- 50,
- metric="user",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 50,
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 100,
- tags={"transaction": "bar_transaction"},
- timestamp=self.min_ago,
- )
- query = {
- "project": [self.project.id],
- "orderby": "p50()",
- "field": [
- "transaction",
- "count_unique(user)",
- "p50()",
- ],
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 2
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["transaction"] == "foo_transaction"
- assert data[0]["count_unique(user)"] == 1
- assert data[1]["transaction"] == "bar_transaction"
- assert data[1]["count_unique(user)"] == 0
- assert meta["isMetricsData"]
- def test_sum_transaction_duration(self):
- self.store_transaction_metric(
- 50,
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 100,
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 150,
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- query = {
- "project": [self.project.id],
- "orderby": "sum(transaction.duration)",
- "field": [
- "transaction",
- "sum(transaction.duration)",
- ],
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["transaction"] == "foo_transaction"
- assert data[0]["sum(transaction.duration)"] == 300
- assert meta["isMetricsData"]
- def test_custom_measurements_simple(self):
- self.store_transaction_metric(
- 1,
- metric="measurements.something_custom",
- internal_metric="d:transactions/measurements.something_custom@millisecond",
- entity="metrics_distributions",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- query = {
- "project": [self.project.id],
- "orderby": "p50(measurements.something_custom)",
- "field": [
- "transaction",
- "p50(measurements.something_custom)",
- ],
- "statsPeriod": "24h",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- self.wait_for_metric_count(
- self.project,
- 1,
- metric="measurements.something_custom",
- mri="d:transactions/measurements.something_custom@millisecond",
- )
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["transaction"] == "foo_transaction"
- assert data[0]["p50(measurements.something_custom)"] == 1
- assert meta["isMetricsData"]
- assert meta["fields"]["p50(measurements.something_custom)"] == "duration"
- assert meta["units"]["p50(measurements.something_custom)"] == "millisecond"
- def test_custom_measurement_size_meta_type(self):
- self.store_transaction_metric(
- 100,
- metric="measurements.custom_type",
- internal_metric="d:transactions/measurements.custom_type@somethingcustom",
- entity="metrics_distributions",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 100,
- metric="measurements.percent",
- internal_metric="d:transactions/measurements.percent@ratio",
- entity="metrics_distributions",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 100,
- metric="measurements.longtaskcount",
- internal_metric="d:transactions/measurements.longtaskcount@none",
- entity="metrics_distributions",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- query = {
- "project": [self.project.id],
- "orderby": "p50(measurements.longtaskcount)",
- "field": [
- "transaction",
- "p50(measurements.longtaskcount)",
- "p50(measurements.percent)",
- "p50(measurements.custom_type)",
- ],
- "statsPeriod": "24h",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["transaction"] == "foo_transaction"
- assert data[0]["p50(measurements.longtaskcount)"] == 100
- assert data[0]["p50(measurements.percent)"] == 100
- assert data[0]["p50(measurements.custom_type)"] == 100
- assert meta["isMetricsData"]
- assert meta["fields"]["p50(measurements.longtaskcount)"] == "integer"
- assert meta["units"]["p50(measurements.longtaskcount)"] is None
- assert meta["fields"]["p50(measurements.percent)"] == "percentage"
- assert meta["units"]["p50(measurements.percent)"] is None
- assert meta["fields"]["p50(measurements.custom_type)"] == "number"
- assert meta["units"]["p50(measurements.custom_type)"] is None
- def test_custom_measurement_none_type(self):
- self.store_transaction_metric(
- 1,
- metric="measurements.cls",
- entity="metrics_distributions",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- query = {
- "project": [self.project.id],
- "orderby": "p75(measurements.cls)",
- "field": [
- "transaction",
- "p75(measurements.cls)",
- "p99(measurements.cls)",
- "max(measurements.cls)",
- ],
- "statsPeriod": "24h",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- self.wait_for_metric_count(
- self.project,
- 1,
- metric=TransactionMetricKey.MEASUREMENTS_CLS.value,
- mri=TransactionMRI.MEASUREMENTS_CLS.value,
- )
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["transaction"] == "foo_transaction"
- assert data[0]["p75(measurements.cls)"] == 1
- assert data[0]["p99(measurements.cls)"] == 1
- assert data[0]["max(measurements.cls)"] == 1
- assert meta["isMetricsData"]
- assert meta["fields"]["p75(measurements.cls)"] == "number"
- assert meta["units"]["p75(measurements.cls)"] is None
- assert meta["fields"]["p99(measurements.cls)"] == "number"
- assert meta["units"]["p99(measurements.cls)"] is None
- assert meta["fields"]["max(measurements.cls)"] == "number"
- assert meta["units"]["max(measurements.cls)"] is None
- def test_custom_measurement_duration_filtering(self):
- self.store_transaction_metric(
- 1,
- metric="measurements.runtime",
- internal_metric="d:transactions/measurements.runtime@hour",
- entity="metrics_distributions",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 180,
- metric="measurements.runtime",
- internal_metric="d:transactions/measurements.runtime@hour",
- entity="metrics_distributions",
- tags={"transaction": "bar_transaction"},
- timestamp=self.min_ago,
- )
- query = {
- "project": [self.project.id],
- "field": [
- "transaction",
- "max(measurements.runtime)",
- ],
- "query": "p50(measurements.runtime):>1wk",
- "statsPeriod": "24h",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["transaction"] == "bar_transaction"
- assert data[0]["max(measurements.runtime)"] == 180
- assert meta["isMetricsData"]
- def test_custom_measurement_size_filtering(self):
- self.store_transaction_metric(
- 1,
- metric="measurements.datacenter_memory",
- internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
- entity="metrics_distributions",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 100,
- metric="measurements.datacenter_memory",
- internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
- entity="metrics_distributions",
- tags={"transaction": "bar_transaction"},
- timestamp=self.min_ago,
- )
- query = {
- "project": [self.project.id],
- "field": [
- "transaction",
- "max(measurements.datacenter_memory)",
- ],
- "query": "p50(measurements.datacenter_memory):>5pb",
- "statsPeriod": "24h",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["transaction"] == "bar_transaction"
- assert data[0]["max(measurements.datacenter_memory)"] == 100
- assert meta["units"]["max(measurements.datacenter_memory)"] == "petabyte"
- assert meta["fields"]["max(measurements.datacenter_memory)"] == "size"
- assert meta["isMetricsData"]
- def test_has_custom_measurement(self):
- self.store_transaction_metric(
- 33,
- metric="measurements.datacenter_memory",
- internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
- entity="metrics_distributions",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- transaction_data = load_data("transaction", timestamp=self.min_ago)
- transaction_data["measurements"]["datacenter_memory"] = {
- "value": 33,
- "unit": "petabyte",
- }
- self.store_event(transaction_data, self.project.id)
- measurement = "measurements.datacenter_memory"
- response = self.do_request(
- {
- "field": ["transaction", measurement],
- "query": "has:measurements.datacenter_memory",
- "dataset": "discover",
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- response = self.do_request(
- {
- "field": ["transaction", measurement],
- "query": "!has:measurements.datacenter_memory",
- "dataset": "discover",
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 0
- def test_environment_param(self):
- self.create_environment(self.project, name="staging")
- self.store_transaction_metric(
- 1,
- tags={"transaction": "foo_transaction", "environment": "staging"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 100,
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- query = {
- "project": [self.project.id],
- "environment": "staging",
- "orderby": "p50(transaction.duration)",
- "field": [
- "transaction",
- "environment",
- "p50(transaction.duration)",
- ],
- "statsPeriod": "24h",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["transaction"] == "foo_transaction"
- assert data[0]["environment"] == "staging"
- assert data[0]["p50(transaction.duration)"] == 1
- assert meta["isMetricsData"]
- @pytest.mark.xfail(reason="Started failing on ClickHouse 21.8")
- def test_environment_query(self):
- self.create_environment(self.project, name="staging")
- self.store_transaction_metric(
- 1,
- tags={"transaction": "foo_transaction", "environment": "staging"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 100,
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- query = {
- "project": [self.project.id],
- "orderby": "p50(transaction.duration)",
- "field": [
- "transaction",
- "environment",
- "p50(transaction.duration)",
- ],
- "query": "!has:environment",
- "statsPeriod": "24h",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- self.wait_for_metric_count(self.project, 2)
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["transaction"] == "foo_transaction"
- assert data[0]["environment"] is None or data[0]["environment"] == ""
- assert data[0]["p50(transaction.duration)"] == 100
- assert meta["isMetricsData"]
- def test_has_transaction(self):
- self.store_transaction_metric(
- 1,
- tags={},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 100,
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- query = {
- "project": [self.project.id],
- "orderby": "p50(transaction.duration)",
- "field": [
- "transaction",
- "p50(transaction.duration)",
- ],
- "query": "has:transaction",
- "statsPeriod": "24h",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- self.wait_for_metric_count(self.project, 2)
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 2
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["transaction"] == "<< unparameterized >>"
- assert data[0]["p50(transaction.duration)"] == 1
- assert data[1]["transaction"] == "foo_transaction"
- assert data[1]["p50(transaction.duration)"] == 100
- assert meta["isMetricsData"]
- query = {
- "project": [self.project.id],
- "orderby": "p50(transaction.duration)",
- "field": [
- "transaction",
- "p50(transaction.duration)",
- ],
- "query": "!has:transaction",
- "statsPeriod": "24h",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- response = self.do_request(query)
- assert response.status_code == 400, response.content
- def test_apdex_transaction_threshold(self):
- ProjectTransactionThresholdOverride.objects.create(
- transaction="foo_transaction",
- project=self.project,
- organization=self.project.organization,
- threshold=600,
- metric=TransactionMetric.LCP.value,
- )
- ProjectTransactionThresholdOverride.objects.create(
- transaction="bar_transaction",
- project=self.project,
- organization=self.project.organization,
- threshold=600,
- metric=TransactionMetric.LCP.value,
- )
- self.store_transaction_metric(
- 1,
- tags={
- "transaction": "foo_transaction",
- constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
- },
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1,
- "measurements.lcp",
- tags={
- "transaction": "bar_transaction",
- constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
- },
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "apdex()",
- ],
- "orderby": ["apdex()"],
- "query": "event.type:transaction",
- "dataset": "metrics",
- "per_page": 50,
- }
- )
- assert len(response.data["data"]) == 2
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["transaction"] == "bar_transaction"
- # Threshold is lcp based
- assert data[0]["apdex()"] == 1
- assert data[1]["transaction"] == "foo_transaction"
- # Threshold is lcp based
- assert data[1]["apdex()"] == 0
- assert meta["isMetricsData"]
- assert field_meta["transaction"] == "string"
- assert field_meta["apdex()"] == "number"
- def test_apdex_project_threshold(self):
- ProjectTransactionThreshold.objects.create(
- project=self.project,
- organization=self.project.organization,
- threshold=600,
- metric=TransactionMetric.LCP.value,
- )
- self.store_transaction_metric(
- 1,
- tags={
- "transaction": "foo_transaction",
- constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
- },
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1,
- "measurements.lcp",
- tags={
- "transaction": "bar_transaction",
- constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
- },
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "apdex()",
- ],
- "orderby": ["apdex()"],
- "query": "event.type:transaction",
- "dataset": "metrics",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 2
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["transaction"] == "bar_transaction"
- # Threshold is lcp based
- assert data[0]["apdex()"] == 1
- assert data[1]["transaction"] == "foo_transaction"
- # Threshold is lcp based
- assert data[1]["apdex()"] == 0
- assert meta["isMetricsData"]
- assert field_meta["transaction"] == "string"
- assert field_meta["apdex()"] == "number"
- def test_apdex_satisfaction_param(self):
- for function in ["apdex(300)", "user_misery(300)", "count_miserable(user, 300)"]:
- query = {
- "project": [self.project.id],
- "field": [
- "transaction",
- function,
- ],
- "statsPeriod": "24h",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 0
- meta = response.data["meta"]
- assert not meta["isMetricsData"], function
- query = {
- "project": [self.project.id],
- "field": [
- "transaction",
- function,
- ],
- "statsPeriod": "24h",
- "dataset": "metrics",
- "per_page": 50,
- }
- response = self.do_request(query)
- assert response.status_code == 400, function
- assert b"threshold parameter" in response.content, function
- def test_mobile_metrics(self):
- self.store_transaction_metric(
- 0.4,
- "measurements.frames_frozen_rate",
- tags={
- "transaction": "bar_transaction",
- },
- timestamp=self.min_ago,
- )
- query = {
- "project": [self.project.id],
- "field": [
- "transaction",
- "p50(measurements.frames_frozen_rate)",
- ],
- "statsPeriod": "24h",
- "dataset": "metrics",
- "per_page": 50,
- }
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- assert response.data["data"][0]["p50(measurements.frames_frozen_rate)"] == 0.4
- def test_merge_null_unparam(self):
- self.store_transaction_metric(
- 1,
- # Transaction: unparam
- tags={
- "transaction": "<< unparameterized >>",
- },
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 2,
- # Transaction:null
- tags={},
- timestamp=self.min_ago,
- )
- query = {
- "project": [self.project.id],
- "field": [
- "transaction",
- "p50(transaction.duration)",
- ],
- "statsPeriod": "24h",
- "dataset": "metrics",
- "per_page": 50,
- }
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- assert response.data["data"][0]["p50(transaction.duration)"] == 1.5
- def test_unparam_filter(self):
- self.store_transaction_metric(
- 1,
- # Transaction: unparam
- tags={
- "transaction": "<< unparameterized >>",
- },
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 2,
- # Transaction:null
- tags={},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 3,
- tags={
- "transaction": "foo_transaction",
- },
- timestamp=self.min_ago,
- )
- query = {
- "project": [self.project.id],
- "field": [
- "transaction",
- "count()",
- ],
- "query": 'transaction:"<< unparameterized >>"',
- "statsPeriod": "24h",
- "dataset": "metrics",
- "per_page": 50,
- }
- self.wait_for_metric_count(self.project, 3)
- response = self.do_request(query)
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- assert response.data["data"][0]["transaction"] == "<< unparameterized >>"
- assert response.data["data"][0]["count()"] == 2
- def test_custom_measurements_without_function(self):
- self.store_transaction_metric(
- 33,
- metric="measurements.datacenter_memory",
- internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
- entity="metrics_distributions",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- transaction_data = load_data("transaction", timestamp=self.min_ago)
- transaction_data["measurements"]["datacenter_memory"] = {
- "value": 33,
- "unit": "petabyte",
- }
- self.store_event(transaction_data, self.project.id)
- measurement = "measurements.datacenter_memory"
- response = self.do_request(
- {
- "field": ["transaction", measurement],
- "query": "measurements.datacenter_memory:33pb",
- "dataset": "discover",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- assert len(data) == 1
- assert data[0][measurement] == 33
- meta = response.data["meta"]
- field_meta = meta["fields"]
- unit_meta = meta["units"]
- assert field_meta[measurement] == "size"
- assert unit_meta[measurement] == "petabyte"
- assert not meta["isMetricsData"]
- def test_custom_measurements_with_function(self):
- self.store_transaction_metric(
- 33,
- metric="measurements.datacenter_memory",
- internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
- entity="metrics_distributions",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- transaction_data = load_data("transaction", timestamp=self.min_ago)
- transaction_data["measurements"]["datacenter_memory"] = {
- "value": 33,
- "unit": "petabyte",
- }
- self.store_event(transaction_data, self.project.id)
- measurement = "p50(measurements.datacenter_memory)"
- response = self.do_request(
- {
- "field": ["transaction", measurement],
- "query": "measurements.datacenter_memory:33pb",
- "dataset": "discover",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- assert len(data) == 1
- assert data[0][measurement] == 33
- meta = response.data["meta"]
- field_meta = meta["fields"]
- unit_meta = meta["units"]
- assert field_meta[measurement] == "size"
- assert unit_meta[measurement] == "petabyte"
- assert not meta["isMetricsData"]
- def test_custom_measurements_equation(self):
- self.store_transaction_metric(
- 33,
- metric="measurements.datacenter_memory",
- internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
- entity="metrics_distributions",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- transaction_data = load_data("transaction", timestamp=self.min_ago)
- transaction_data["measurements"]["datacenter_memory"] = {
- "value": 33,
- "unit": "petabyte",
- }
- self.store_event(transaction_data, self.project.id)
- response = self.do_request(
- {
- "field": [
- "transaction",
- "measurements.datacenter_memory",
- "equation|measurements.datacenter_memory / 3",
- ],
- "query": "",
- "dataset": "discover",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- assert len(data) == 1
- assert data[0]["measurements.datacenter_memory"] == 33
- assert data[0]["equation|measurements.datacenter_memory / 3"] == 11
- meta = response.data["meta"]
- assert not meta["isMetricsData"]
- def test_transaction_wildcard(self):
- self.store_transaction_metric(
- 1,
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1,
- tags={"transaction": "bar_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "p90()",
- ],
- "query": "transaction:foo*",
- "dataset": "metrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- assert len(data) == 1
- assert data[0]["p90()"] == 1
- meta = response.data["meta"]
- assert meta["isMetricsData"]
- assert data[0]["transaction"] == "foo_transaction"
- def test_transaction_status_wildcard(self):
- self.store_transaction_metric(
- 1,
- tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "p90()",
- ],
- "query": "transaction.status:f*bar",
- "dataset": "metrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- assert len(data) == 1
- assert data[0]["p90()"] == 1
- meta = response.data["meta"]
- assert meta["isMetricsData"]
- def test_http_error_rate(self):
- self.store_transaction_metric(
- 1,
- tags={
- "transaction": "foo_transaction",
- "transaction.status": "foobar",
- "http.status_code": "500",
- },
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1,
- tags={"transaction": "bar_transaction", "http.status_code": "400"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "http_error_rate()",
- ],
- "dataset": "metrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- assert len(data) == 1
- assert data[0]["http_error_rate()"] == 0.5
- meta = response.data["meta"]
- assert meta["isMetricsData"]
- def test_time_spent(self):
- self.store_transaction_metric(
- 1,
- tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1,
- tags={"transaction": "bar_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "time_spent_percentage()",
- ],
- "dataset": "metrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- assert len(data) == 2
- assert data[0]["time_spent_percentage()"] == 0.5
- meta = response.data["meta"]
- assert meta["isMetricsData"]
- def test_has_filter(self):
- self.store_transaction_metric(
- 1,
- tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "p50()",
- ],
- # For the metrics dataset, has on metrics should be no-ops
- "query": "has:measurements.frames_frozen_rate",
- "dataset": "metrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- assert len(data) == 1
- assert data[0]["p50()"] == 1
- meta = response.data["meta"]
- assert meta["isMetricsData"]
- response = self.do_request(
- {
- "field": [
- "transaction",
- "p50()",
- ],
- "query": "has:transaction.status",
- "dataset": "metrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- assert len(data) == 1
- assert data[0]["p50()"] == 1
- meta = response.data["meta"]
- assert meta["isMetricsData"]
- def test_not_has_filter(self):
- self.store_transaction_metric(
- 1,
- tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "p50()",
- ],
- "query": "!has:transaction.status",
- "dataset": "metrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- assert len(data) == 0
- meta = response.data["meta"]
- assert meta["isMetricsData"]
- response = self.do_request(
- {
- "field": [
- "transaction",
- "p50()",
- ],
- # Doing !has on the metrics dataset doesn't really make sense
- "query": "!has:measurements.frames_frozen_rate",
- "dataset": "metrics",
- }
- )
- assert response.status_code == 400, response.content
- def test_p50_with_count(self):
- """Implicitly test the fact that percentiles are their own 'dataset'"""
- self.store_transaction_metric(
- 1,
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["title", "p50()", "count()"],
- "query": "event.type:transaction",
- "dataset": "metrics",
- "project": self.project.id,
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["title"] == "foo_transaction"
- assert data[0]["p50()"] == 1
- assert data[0]["count()"] == 1
- assert meta["isMetricsData"]
- assert field_meta["title"] == "string"
- assert field_meta["p50()"] == "duration"
- assert field_meta["count()"] == "integer"
- def test_p75_with_count_and_more_groupby(self):
- """Implicitly test the fact that percentiles are their own 'dataset'"""
- self.store_transaction_metric(
- 1,
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 5,
- tags={"transaction": "bar_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 5,
- tags={"transaction": "bar_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "title",
- "project",
- "p75()",
- "count()",
- ],
- "query": "event.type:transaction",
- "orderby": "count()",
- "dataset": "metrics",
- "project": self.project.id,
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 2
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["title"] == "foo_transaction"
- assert data[0]["p75()"] == 1
- assert data[0]["count()"] == 1
- assert data[1]["title"] == "bar_transaction"
- assert data[1]["p75()"] == 5
- assert data[1]["count()"] == 2
- assert meta["isMetricsData"]
- assert field_meta["title"] == "string"
- assert field_meta["p75()"] == "duration"
- assert field_meta["count()"] == "integer"
- def test_title_and_transaction_alias(self):
- # Title and transaction are aliases to the same column
- self.store_transaction_metric(
- 1,
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "title",
- "transaction",
- "p75()",
- ],
- "query": "event.type:transaction",
- "orderby": "p75()",
- "dataset": "metrics",
- "project": self.project.id,
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["title"] == "foo_transaction"
- assert data[0]["transaction"] == "foo_transaction"
- assert data[0]["p75()"] == 1
- assert meta["isMetricsData"]
- assert field_meta["title"] == "string"
- assert field_meta["transaction"] == "string"
- assert field_meta["p75()"] == "duration"
- def test_maintain_sort_order_across_datasets(self):
- self.store_transaction_metric(
- 1,
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1,
- metric="user",
- tags={"transaction": "bar_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 5,
- tags={"transaction": "bar_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 5,
- tags={"transaction": "bar_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "title",
- "project",
- "count()",
- "count_unique(user)",
- ],
- "query": "event.type:transaction",
- "orderby": "count()",
- "dataset": "metrics",
- "project": self.project.id,
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert len(data) == 2
- assert data[0]["title"] == "foo_transaction"
- assert data[0]["count()"] == 1
- assert data[0]["count_unique(user)"] == 0
- assert data[1]["title"] == "bar_transaction"
- assert data[1]["count()"] == 2
- assert data[1]["count_unique(user)"] == 1
- assert meta["isMetricsData"]
- assert field_meta["title"] == "string"
- assert field_meta["count()"] == "integer"
- assert field_meta["count_unique(user)"] == "integer"
- def test_avg_compare(self):
- self.store_transaction_metric(
- 100,
- timestamp=self.min_ago,
- tags={"release": "foo"},
- )
- self.store_transaction_metric(
- 10,
- timestamp=self.min_ago,
- tags={"release": "bar"},
- )
- for function_name in [
- "avg_compare(transaction.duration, release, foo, bar)",
- 'avg_compare(transaction.duration, release, "foo", "bar")',
- ]:
- response = self.do_request(
- {
- "field": [function_name],
- "query": "",
- "project": self.project.id,
- "dataset": "metrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0][function_name] == -0.9
- assert meta["dataset"] == "metrics"
- assert meta["fields"][function_name] == "percent_change"
- def test_avg_if(self):
- self.store_transaction_metric(
- 100,
- timestamp=self.min_ago,
- tags={"release": "foo"},
- )
- self.store_transaction_metric(
- 200,
- timestamp=self.min_ago,
- tags={"release": "foo"},
- )
- self.store_transaction_metric(
- 10,
- timestamp=self.min_ago,
- tags={"release": "bar"},
- )
- response = self.do_request(
- {
- "field": ["avg_if(transaction.duration, release, foo)"],
- "query": "",
- "project": self.project.id,
- "dataset": "metrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0]["avg_if(transaction.duration, release, foo)"] == 150
- assert meta["dataset"] == "metrics"
- assert meta["fields"]["avg_if(transaction.duration, release, foo)"] == "duration"
- def test_count_if(self):
- self.store_transaction_metric(
- 100,
- timestamp=self.min_ago,
- tags={"release": "1.0.0"},
- )
- self.store_transaction_metric(
- 200,
- timestamp=self.min_ago,
- tags={"release": "1.0.0"},
- )
- self.store_transaction_metric(
- 10,
- timestamp=self.min_ago,
- tags={"release": "2.0.0"},
- )
- countIfRelease1 = "count_if(transaction.duration,release,1.0.0)"
- countIfRelease2 = "count_if(transaction.duration,release,2.0.0)"
- response = self.do_request(
- {
- "field": [
- countIfRelease1,
- countIfRelease2,
- ],
- "query": "",
- "project": self.project.id,
- "dataset": "metrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0][countIfRelease1] == 2
- assert data[0][countIfRelease2] == 1
- assert meta["dataset"] == "metrics"
- assert meta["fields"][countIfRelease1] == "integer"
- assert meta["fields"][countIfRelease2] == "integer"
- def test_device_class(self):
- self.store_transaction_metric(
- 100,
- timestamp=self.min_ago,
- tags={"device.class": "1"},
- )
- self.store_transaction_metric(
- 200,
- timestamp=self.min_ago,
- tags={"device.class": "2"},
- )
- self.store_transaction_metric(
- 300,
- timestamp=self.min_ago,
- tags={"device.class": ""},
- )
- response = self.do_request(
- {
- "field": ["device.class", "p95()"],
- "query": "",
- "orderby": "p95()",
- "project": self.project.id,
- "dataset": "metrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 3
- # Need to actually check the dict since the level for 1 isn't guaranteed to stay `low` or `medium`
- assert data[0]["device.class"] == map_device_class_level("1")
- assert data[1]["device.class"] == map_device_class_level("2")
- assert data[2]["device.class"] == "Unknown"
- assert meta["fields"]["device.class"] == "string"
- def test_device_class_filter(self):
- self.store_transaction_metric(
- 300,
- timestamp=self.min_ago,
- tags={"device.class": "1"},
- )
- # Need to actually check the dict since the level for 1 isn't guaranteed to stay `low`
- level = map_device_class_level("1")
- response = self.do_request(
- {
- "field": ["device.class", "count()"],
- "query": f"device.class:{level}",
- "orderby": "count()",
- "project": self.project.id,
- "dataset": "metrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0]["device.class"] == level
- assert meta["fields"]["device.class"] == "string"
- def test_performance_score(self):
- self.store_transaction_metric(
- 0.03,
- metric="measurements.score.ttfb",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.30,
- metric="measurements.score.weight.ttfb",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.35,
- metric="measurements.score.fcp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.70,
- metric="measurements.score.weight.fcp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.38,
- metric="measurements.score.total",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1.00,
- metric="measurements.score.ttfb",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1.00,
- metric="measurements.score.weight.ttfb",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1.00,
- metric="measurements.score.fid",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- # These fid and ttfb scenarios shouldn't really be happening, but we can test them anyways
- self.store_transaction_metric(
- 0.00,
- metric="measurements.score.weight.fid",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.00,
- metric="measurements.score.ttfb",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.00,
- metric="measurements.score.weight.ttfb",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1.00,
- metric="measurements.score.total",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- # INP metrics
- self.store_transaction_metric(
- 0.80,
- metric="measurements.score.inp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1.00,
- metric="measurements.score.weight.inp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.80,
- metric="measurements.score.total",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "performance_score(measurements.score.fcp)",
- "performance_score(measurements.score.fid)",
- "performance_score(measurements.score.ttfb)",
- "performance_score(measurements.score.inp)",
- ],
- "query": "event.type:transaction",
- "dataset": "metrics",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["performance_score(measurements.score.fcp)"] == 0.5
- assert data[0]["performance_score(measurements.score.fid)"] == 0
- assert data[0]["performance_score(measurements.score.ttfb)"] == 0.7923076923076923
- assert data[0]["performance_score(measurements.score.inp)"] == 0.8
- assert meta["isMetricsData"]
- assert field_meta["performance_score(measurements.score.ttfb)"] == "number"
- def test_performance_score_boundaries(self):
- # Scores shouldn't exceed 1 or go below 0, but we can test these boundaries anyways
- self.store_transaction_metric(
- 0.65,
- metric="measurements.score.ttfb",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.30,
- metric="measurements.score.weight.ttfb",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- -0.35,
- metric="measurements.score.fcp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.70,
- metric="measurements.score.weight.fcp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.3,
- metric="measurements.score.total",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "performance_score(measurements.score.ttfb)",
- "performance_score(measurements.score.fcp)",
- ],
- "query": "event.type:transaction",
- "dataset": "metrics",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["performance_score(measurements.score.ttfb)"] == 1.0
- assert data[0]["performance_score(measurements.score.fcp)"] == 0.0
- assert meta["isMetricsData"]
- assert field_meta["performance_score(measurements.score.ttfb)"] == "number"
- def test_weighted_performance_score(self):
- self.store_transaction_metric(
- 0.03,
- metric="measurements.score.ttfb",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.30,
- metric="measurements.score.weight.ttfb",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.03,
- metric="measurements.score.total",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1.00,
- metric="measurements.score.ttfb",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1.00,
- metric="measurements.score.weight.ttfb",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1.00,
- metric="measurements.score.total",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.80,
- metric="measurements.score.inp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1.00,
- metric="measurements.score.weight.inp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.80,
- metric="measurements.score.total",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "weighted_performance_score(measurements.score.ttfb)",
- "weighted_performance_score(measurements.score.inp)",
- ],
- "query": "event.type:transaction",
- "dataset": "metrics",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["weighted_performance_score(measurements.score.ttfb)"] == 0.3433333333333333
- assert data[0]["weighted_performance_score(measurements.score.inp)"] == 0.26666666666666666
- assert meta["isMetricsData"]
- assert field_meta["weighted_performance_score(measurements.score.ttfb)"] == "number"
- def test_invalid_performance_score_column(self):
- self.store_transaction_metric(
- 0.03,
- metric="measurements.score.total",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "performance_score(measurements.score.fp)",
- ],
- "query": "event.type:transaction",
- "dataset": "metrics",
- "per_page": 50,
- }
- )
- assert response.status_code == 400, response.content
- def test_invalid_weighted_performance_score_column(self):
- self.store_transaction_metric(
- 0.03,
- metric="measurements.score.total",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "weighted_performance_score(measurements.score.fp)",
- ],
- "query": "event.type:transaction",
- "dataset": "metrics",
- "per_page": 50,
- }
- )
- assert response.status_code == 400, response.content
- def test_no_weighted_performance_score_column(self):
- self.store_transaction_metric(
- 0.0,
- metric="measurements.score.ttfb",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "weighted_performance_score(measurements.score.ttfb)",
- ],
- "query": "event.type:transaction",
- "dataset": "metrics",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- field_meta = meta["fields"]
- assert data[0]["weighted_performance_score(measurements.score.ttfb)"] == 0.0
- assert meta["isMetricsData"]
- assert field_meta["weighted_performance_score(measurements.score.ttfb)"] == "number"
- def test_opportunity_score(self):
- self.store_transaction_metric(
- 0.03,
- metric="measurements.score.ttfb",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.30,
- metric="measurements.score.weight.ttfb",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.40,
- metric="measurements.score.fcp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.70,
- metric="measurements.score.weight.fcp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.43,
- metric="measurements.score.total",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1.0,
- metric="measurements.score.ttfb",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1.0,
- metric="measurements.score.weight.ttfb",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1.0,
- metric="measurements.score.total",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.0,
- metric="measurements.score.total",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.80,
- metric="measurements.score.inp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1.00,
- metric="measurements.score.weight.inp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.80,
- metric="measurements.score.total",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "opportunity_score(measurements.score.ttfb)",
- "opportunity_score(measurements.score.inp)",
- "opportunity_score(measurements.score.total)",
- ],
- "query": "event.type:transaction",
- "dataset": "metrics",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["opportunity_score(measurements.score.ttfb)"] == 0.27
- # Should be 0.2. Precision issue?
- assert data[0]["opportunity_score(measurements.score.inp)"] == 0.19999999999999996
- assert data[0]["opportunity_score(measurements.score.total)"] == 1.77
- assert meta["isMetricsData"]
- def test_opportunity_score_with_fixed_weights(self):
- self.store_transaction_metric(
- 0.5,
- metric="measurements.score.inp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1.0,
- metric="measurements.score.weight.inp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.2,
- metric="measurements.score.inp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 1.0,
- metric="measurements.score.weight.inp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.2,
- metric="measurements.score.inp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.5,
- metric="measurements.score.weight.inp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.1,
- metric="measurements.score.lcp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.3,
- metric="measurements.score.weight.lcp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.2,
- metric="measurements.score.inp",
- tags={"transaction": "bar_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.5,
- metric="measurements.score.weight.inp",
- tags={"transaction": "bar_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "total_opportunity_score()",
- ],
- "query": "event.type:transaction",
- "orderby": "transaction",
- "dataset": "metrics",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 2
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["total_opportunity_score()"] == 0.029999999999999995
- assert data[1]["total_opportunity_score()"] == 0.36
- assert meta["isMetricsData"]
- def test_total_performance_score(self):
- self.store_transaction_metric(
- 0.03,
- metric="measurements.score.lcp",
- tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.30,
- metric="measurements.score.weight.lcp",
- tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.15,
- metric="measurements.score.fcp",
- tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.15,
- metric="measurements.score.weight.fcp",
- tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.10,
- metric="measurements.score.cls",
- tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.15,
- metric="measurements.score.weight.cls",
- tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.05,
- metric="measurements.score.ttfb",
- tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.10,
- metric="measurements.score.weight.ttfb",
- tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.05,
- metric="measurements.score.inp",
- tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.10,
- metric="measurements.score.weight.inp",
- tags={"transaction": "foo_transaction", "transaction.op": "pageload"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "performance_score(measurements.score.total)",
- ],
- "query": "",
- "dataset": "metrics",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["performance_score(measurements.score.total)"] == 0.48
- assert meta["isMetricsData"]
- def test_count_scores(self):
- self.store_transaction_metric(
- 0.1,
- metric="measurements.score.total",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.2,
- metric="measurements.score.total",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.3,
- metric="measurements.score.total",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.4,
- metric="measurements.score.total",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.5,
- metric="measurements.score.ttfb",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 0.8,
- metric="measurements.score.inp",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "count_scores(measurements.score.total)",
- "count_scores(measurements.score.ttfb)",
- "count_scores(measurements.score.inp)",
- ],
- "query": "event.type:transaction",
- "dataset": "metrics",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["count_scores(measurements.score.total)"] == 4
- assert data[0]["count_scores(measurements.score.ttfb)"] == 1
- assert data[0]["count_scores(measurements.score.inp)"] == 1
- assert meta["isMetricsData"]
- def test_count_starts(self):
- self.store_transaction_metric(
- 200,
- metric="measurements.app_start_warm",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 100,
- metric="measurements.app_start_warm",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 10,
- metric="measurements.app_start_cold",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "count_starts(measurements.app_start_warm)",
- "count_starts(measurements.app_start_cold)",
- ],
- "query": "event.type:transaction",
- "dataset": "metrics",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["count_starts(measurements.app_start_warm)"] == 2
- assert data[0]["count_starts(measurements.app_start_cold)"] == 1
- assert meta["isMetricsData"]
- def test_count_starts_returns_all_counts_when_no_arg_is_passed(self):
- self.store_transaction_metric(
- 200,
- metric="measurements.app_start_warm",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 100,
- metric="measurements.app_start_warm",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_transaction_metric(
- 10,
- metric="measurements.app_start_cold",
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "count_total_starts()",
- ],
- "query": "event.type:transaction",
- "dataset": "metrics",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["count_total_starts()"] == 3
- assert meta["isMetricsData"]
- def test_timestamp_groupby(self):
- self.store_transaction_metric(
- 0.03,
- tags={"transaction": "foo_transaction", "user": "foo"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "transaction",
- "timestamp",
- "count()",
- "count_unique(user)",
- ],
- "query": "event.type:transaction",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["transaction"] == "foo_transaction"
- assert meta["dataset"] == "metricsEnhanced"
- def test_on_demand_with_mep(self):
- # Store faketag as an OnDemandMetricSpec, which will put faketag into the metrics indexer
- spec = OnDemandMetricSpec(
- field="count()",
- query="user.email:blah@example.com",
- environment="prod",
- groupbys=["faketag"],
- spec_type=MetricSpecType.DYNAMIC_QUERY,
- )
- self.store_on_demand_metric(123, spec=spec)
- # This is the event that we should actually return
- transaction_data = load_data("transaction", timestamp=self.min_ago)
- transaction_data["tags"].append(("faketag", "foo"))
- self.store_event(transaction_data, self.project.id)
- with self.feature({"organizations:mep-use-default-tags": True}):
- response = self.do_request(
- {
- "field": [
- "faketag",
- "count()",
- ],
- "query": "event.type:transaction",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- }
- )
- assert response.status_code == 200, response.content
- assert len(response.data["data"]) == 1
- data = response.data["data"]
- meta = response.data["meta"]
- assert data[0]["faketag"] == "foo"
- assert not meta["isMetricsData"]
- def test_filtering_by_org_id_is_not_compatible(self):
- """Implicitly test the fact that percentiles are their own 'dataset'"""
- self.store_transaction_metric(
- 1,
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["title", "p50()", "count()"],
- "query": "event.type:transaction organization_id:2",
- "dataset": "metrics",
- "project": self.project.id,
- "per_page": 50,
- }
- )
- assert response.status_code == 400, response.content
- def test_cache_miss_rate(self):
- self.store_span_metric(
- 1,
- timestamp=self.min_ago,
- tags={"cache.hit": "true"},
- )
- self.store_span_metric(
- 1,
- timestamp=self.min_ago,
- tags={"cache.hit": "false"},
- )
- self.store_span_metric(
- 1,
- timestamp=self.min_ago,
- tags={"cache.hit": "false"},
- )
- self.store_span_metric(
- 1,
- timestamp=self.min_ago,
- tags={"cache.hit": "false"},
- )
- response = self.do_request(
- {
- "field": ["cache_miss_rate()"],
- "query": "",
- "project": self.project.id,
- "dataset": "metrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0]["cache_miss_rate()"] == 0.75
- assert meta["dataset"] == "metrics"
- assert meta["fields"]["cache_miss_rate()"] == "percentage"
- def test_http_response_rate(self):
- self.store_span_metric(
- 1,
- timestamp=self.min_ago,
- tags={"span.status_code": "200"},
- )
- self.store_span_metric(
- 3,
- timestamp=self.min_ago,
- tags={"span.status_code": "301"},
- )
- self.store_span_metric(
- 3,
- timestamp=self.min_ago,
- tags={"span.status_code": "404"},
- )
- self.store_span_metric(
- 4,
- timestamp=self.min_ago,
- tags={"span.status_code": "503"},
- )
- self.store_span_metric(
- 5,
- timestamp=self.min_ago,
- tags={"span.status_code": "501"},
- )
- response = self.do_request(
- {
- "field": [
- "http_response_rate(200)", # By exact code
- "http_response_rate(3)", # By code class
- "http_response_rate(4)",
- "http_response_rate(5)",
- ],
- "query": "",
- "project": self.project.id,
- "dataset": "metrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- assert len(data) == 1
- assert data[0]["http_response_rate(200)"] == 0.2
- assert data[0]["http_response_rate(3)"] == 0.2
- assert data[0]["http_response_rate(4)"] == 0.2
- assert data[0]["http_response_rate(5)"] == 0.4
- meta = response.data["meta"]
- assert meta["dataset"] == "metrics"
- assert meta["fields"]["http_response_rate(200)"] == "percentage"
- def test_avg_span_self_time(self):
- self.store_span_metric(
- 1,
- timestamp=self.min_ago,
- )
- self.store_span_metric(
- 3,
- timestamp=self.min_ago,
- )
- self.store_span_metric(
- 3,
- timestamp=self.min_ago,
- )
- self.store_span_metric(
- 4,
- timestamp=self.min_ago,
- )
- self.store_span_metric(
- 5,
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": [
- "avg(span.self_time)",
- ],
- "query": "",
- "project": self.project.id,
- "dataset": "metrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- assert len(data) == 1
- assert data[0]["avg(span.self_time)"] == 3.2
- def test_avg_message_receive_latency_gauge_functions(self):
- self.store_span_metric(
- {
- "min": 5,
- "max": 5,
- "sum": 5,
- "count": 1,
- "last": 5,
- },
- internal_metric=constants.SPAN_MESSAGING_LATENCY,
- timestamp=self.min_ago,
- entity="metrics_gauges",
- )
- self.store_span_metric(
- {
- "min": 15,
- "max": 15,
- "sum": 15,
- "count": 1,
- "last": 15,
- },
- internal_metric=constants.SPAN_MESSAGING_LATENCY,
- timestamp=self.min_ago,
- entity="metrics_gauges",
- )
- response = self.do_request(
- {
- "field": [
- "avg(messaging.message.receive.latency)",
- "sum(messaging.message.receive.latency)",
- "min(messaging.message.receive.latency)",
- "max(messaging.message.receive.latency)",
- ],
- "query": "",
- "project": self.project.id,
- "dataset": "metrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- assert len(data) == 1
- assert data[0]["avg(messaging.message.receive.latency)"] == 10.0
- assert data[0]["sum(messaging.message.receive.latency)"] == 20.0
- assert data[0]["min(messaging.message.receive.latency)"] == 5.0
- assert data[0]["max(messaging.message.receive.latency)"] == 15.0
- response = self.do_request(
- {
- "field": [
- "avg(g:spans/messaging.message.receive.latency@millisecond)",
- "sum(g:spans/messaging.message.receive.latency@millisecond)",
- "min(g:spans/messaging.message.receive.latency@millisecond)",
- "max(g:spans/messaging.message.receive.latency@millisecond)",
- ],
- "query": "",
- "project": self.project.id,
- "dataset": "metrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- assert len(data) == 1
- assert data[0]["avg(g:spans/messaging.message.receive.latency@millisecond)"] == 10.0
- assert data[0]["sum(g:spans/messaging.message.receive.latency@millisecond)"] == 20.0
- assert data[0]["min(g:spans/messaging.message.receive.latency@millisecond)"] == 5.0
- assert data[0]["max(g:spans/messaging.message.receive.latency@millisecond)"] == 15.0
- def test_span_module_filter(self):
- self.store_span_metric(
- 1,
- timestamp=self.min_ago,
- tags={"span.category": "db", "span.op": "db.redis"},
- )
- self.store_span_metric(
- 4,
- timestamp=self.min_ago,
- tags={"span.category": "cache"},
- )
- self.store_span_metric(
- 4,
- timestamp=self.min_ago,
- tags={"span.category": "db", "span.op": "db.sql.room"},
- )
- self.store_span_metric(
- 2,
- timestamp=self.min_ago,
- tags={"span.category": "db"},
- )
- self.store_span_metric(
- 3,
- timestamp=self.min_ago,
- tags={"span.category": "http"},
- )
- response = self.do_request(
- {
- "field": [
- "span.description",
- "span.module",
- "avg(span.self_time)",
- ],
- "orderby": "avg(span.self_time)",
- "query": "span.module:[db, cache, other]",
- "project": self.project.id,
- "dataset": "metrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- assert len(data) == 3
- assert data[0]["span.module"] == "db"
- assert data[0]["avg(span.self_time)"] == 2
- assert data[1]["span.module"] == "cache"
- assert data[1]["avg(span.self_time)"] == 2.5
- assert data[2]["span.module"] == "other"
- assert data[2]["avg(span.self_time)"] == 4
- class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithOnDemandMetrics(
- MetricsEnhancedPerformanceTestCase
- ):
- viewname = "sentry-api-0-organization-events"
- def setUp(self) -> None:
- super().setUp()
- self.url = reverse(
- self.viewname, kwargs={"organization_id_or_slug": self.organization.slug}
- )
- self.features = {"organizations:on-demand-metrics-extraction-widgets": True}
- def _create_specs(
- self, params: dict[str, Any], groupbys: list[str] | None = None
- ) -> list[OnDemandMetricSpec]:
- """Creates all specs based on the parameters that would be passed to the endpoint."""
- specs = []
- for field in params["field"]:
- spec = OnDemandMetricSpec(
- field=field,
- query=params["query"],
- environment=params.get("environment"),
- groupbys=groupbys,
- spec_type=MetricSpecType.DYNAMIC_QUERY,
- )
- specs.append(spec)
- return specs
- def _make_on_demand_request(
- self, params: dict[str, Any], extra_features: dict[str, bool] | None = None
- ) -> Response:
- """Ensures that the required parameters for an on-demand request are included."""
- # Expected parameters for this helper function
- params["dataset"] = "metricsEnhanced"
- params["useOnDemandMetrics"] = "true"
- params["onDemandType"] = "dynamic_query"
- _features = {**self.features, **(extra_features or {})}
- return self.do_request(params, features=_features)
- def _assert_on_demand_response(
- self,
- response: Response,
- expected_on_demand_query: bool | None = True,
- expected_dataset: str | None = "metricsEnhanced",
- ) -> None:
- """Basic assertions for an on-demand request."""
- assert response.status_code == 200, response.content
- meta = response.data["meta"]
- assert meta.get("isMetricsExtractedData", False) is expected_on_demand_query
- assert meta["dataset"] == expected_dataset
- def test_is_metrics_extracted_data_is_included(self) -> None:
- params = {"field": ["count()"], "query": "transaction.duration:>=91", "yAxis": "count()"}
- specs = self._create_specs(params)
- for spec in specs:
- self.store_on_demand_metric(1, spec=spec)
- response = self._make_on_demand_request(params)
- self._assert_on_demand_response(response)
- def test_on_demand_user_misery(self) -> None:
- user_misery_field = "user_misery(300)"
- query = "transaction.duration:>=100"
- # We store data for both specs, however, when the query builders try to query
- # for the data it will not query on-demand data
- for spec_version in OnDemandMetricSpecVersioning.get_spec_versions():
- spec = OnDemandMetricSpec(
- field=user_misery_field,
- query=query,
- spec_type=MetricSpecType.DYNAMIC_QUERY,
- # We only allow querying the function in the latest spec version,
- # otherwise, the data returned by the endpoint would be 0.05
- spec_version=spec_version,
- )
- tags = {"satisfaction": "miserable"}
- self.store_on_demand_metric(1, spec=spec, additional_tags=tags, timestamp=self.min_ago)
- self.store_on_demand_metric(2, spec=spec, timestamp=self.min_ago)
- params = {"field": [user_misery_field], "project": self.project.id, "query": query}
- self._create_specs(params)
- # We expect it to be False because we're not using the extra feature flag
- response = self._make_on_demand_request(params)
- self._assert_on_demand_response(response, expected_on_demand_query=False)
- # Since we're using the extra feature flag we expect user_misery to be an on-demand metric
- response = self._make_on_demand_request(params, {SPEC_VERSION_TWO_FLAG: True})
- self._assert_on_demand_response(response, expected_on_demand_query=True)
- assert response.data["data"] == [{user_misery_field: user_misery_formula(1, 2)}]
- def test_on_demand_user_misery_discover_split_with_widget_id_unsaved(self) -> None:
- user_misery_field = "user_misery(300)"
- query = "transaction.duration:>=100"
- _, widget, __ = create_widget(["count()"], "", self.project, discover_widget_split=None)
- # We store data for both specs, however, when the query builders try to query
- # for the data it will not query on-demand data
- for spec_version in OnDemandMetricSpecVersioning.get_spec_versions():
- spec = OnDemandMetricSpec(
- field=user_misery_field,
- query=query,
- spec_type=MetricSpecType.DYNAMIC_QUERY,
- # We only allow querying the function in the latest spec version,
- # otherwise, the data returned by the endpoint would be 0.05
- spec_version=spec_version,
- )
- tags = {"satisfaction": "miserable"}
- self.store_on_demand_metric(1, spec=spec, additional_tags=tags, timestamp=self.min_ago)
- self.store_on_demand_metric(2, spec=spec, timestamp=self.min_ago)
- params = {"field": [user_misery_field], "project": self.project.id, "query": query}
- self._create_specs(params)
- params["dashboardWidgetId"] = widget.id
- # Since we're using the extra feature flag we expect user_misery to be an on-demand metric
- with mock.patch.object(widget, "save") as mock_widget_save:
- response = self._make_on_demand_request(params, {SPEC_VERSION_TWO_FLAG: True})
- assert bool(mock_widget_save.assert_called_once)
- self._assert_on_demand_response(response, expected_on_demand_query=True)
- assert response.data["data"] == [{user_misery_field: user_misery_formula(1, 2)}]
- def test_on_demand_user_misery_discover_split_with_widget_id_saved(self) -> None:
- user_misery_field = "user_misery(300)"
- query = "transaction.duration:>=100"
- _, widget, __ = create_widget(
- ["count()"],
- "",
- self.project,
- discover_widget_split=DashboardWidgetTypes.TRANSACTION_LIKE, # Transactions like uses on-demand
- )
- # We store data for both specs, however, when the query builders try to query
- # for the data it will not query on-demand data
- for spec_version in OnDemandMetricSpecVersioning.get_spec_versions():
- spec = OnDemandMetricSpec(
- field=user_misery_field,
- query=query,
- spec_type=MetricSpecType.DYNAMIC_QUERY,
- # We only allow querying the function in the latest spec version,
- # otherwise, the data returned by the endpoint would be 0.05
- spec_version=spec_version,
- )
- tags = {"satisfaction": "miserable"}
- self.store_on_demand_metric(1, spec=spec, additional_tags=tags, timestamp=self.min_ago)
- self.store_on_demand_metric(2, spec=spec, timestamp=self.min_ago)
- params = {"field": [user_misery_field], "project": self.project.id, "query": query}
- self._create_specs(params)
- params["dashboardWidgetId"] = widget.id
- # Since we're using the extra feature flag we expect user_misery to be an on-demand metric
- with mock.patch.object(widget, "save") as mock_widget_save:
- response = self._make_on_demand_request(params, {SPEC_VERSION_TWO_FLAG: True})
- assert bool(mock_widget_save.assert_not_called)
- self._assert_on_demand_response(response, expected_on_demand_query=True)
- assert response.data["data"] == [{user_misery_field: user_misery_formula(1, 2)}]
- def test_on_demand_count_unique(self):
- field = "count_unique(user)"
- query = "transaction.duration:>0"
- params = {"field": [field], "query": query}
- # We do not really have to create the metrics for both specs since
- # the first API call will not query any on-demand metric
- for spec_version in OnDemandMetricSpecVersioning.get_spec_versions():
- spec = OnDemandMetricSpec(
- field=field,
- query=query,
- spec_type=MetricSpecType.DYNAMIC_QUERY,
- spec_version=spec_version,
- )
- self.store_on_demand_metric(1, spec=spec, timestamp=self.min_ago)
- self.store_on_demand_metric(2, spec=spec, timestamp=self.min_ago)
- # The first call will not be on-demand
- response = self._make_on_demand_request(params)
- self._assert_on_demand_response(response, expected_on_demand_query=False)
- # This second call will be on-demand
- response = self._make_on_demand_request(
- params, extra_features={SPEC_VERSION_TWO_FLAG: True}
- )
- self._assert_on_demand_response(response, expected_on_demand_query=True)
- assert response.data["data"] == [{"count_unique(user)": 2}]
- def test_split_decision_for_errors_widget(self):
- error_data = load_data("python", timestamp=before_now(minutes=1))
- self.store_event(
- data={
- **error_data,
- "exception": {"values": [{"type": "blah", "data": {"values": []}}]},
- },
- project_id=self.project.id,
- )
- _, widget, __ = create_widget(
- ["count()", "error.type"], "error.type:blah", self.project, discover_widget_split=None
- )
- response = self.do_request(
- {
- "field": ["count()", "error.type"],
- "query": "error.type:blah",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- "dashboardWidgetId": widget.id,
- }
- )
- assert response.status_code == 200, response.content
- assert response.data.get("meta").get(
- "discoverSplitDecision"
- ) is DashboardWidgetTypes.get_type_name(DashboardWidgetTypes.ERROR_EVENTS)
- widget.refresh_from_db()
- assert widget.discover_widget_split == DashboardWidgetTypes.ERROR_EVENTS
- assert widget.dataset_source == DatasetSourcesTypes.INFERRED.value
- def test_split_decision_for_transactions_widget(self):
- transaction_data = load_data("transaction", timestamp=before_now(minutes=1))
- self.store_event(
- data={
- **transaction_data,
- },
- project_id=self.project.id,
- )
- _, widget, __ = create_widget(
- ["count()", "transaction.name"], "", self.project, discover_widget_split=None
- )
- assert widget.discover_widget_split is None
- response = self.do_request(
- {
- "field": ["count()", "transaction.name"],
- "query": "",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- "dashboardWidgetId": widget.id,
- }
- )
- assert response.status_code == 200, response.content
- assert response.data.get("meta").get(
- "discoverSplitDecision"
- ) is DashboardWidgetTypes.get_type_name(DashboardWidgetTypes.TRANSACTION_LIKE)
- widget.refresh_from_db()
- assert widget.discover_widget_split == DashboardWidgetTypes.TRANSACTION_LIKE
- assert widget.dataset_source == DatasetSourcesTypes.INFERRED.value
- def test_split_decision_for_ambiguous_widget_without_data(self):
- _, widget, __ = create_widget(
- ["count()", "transaction.name", "error.type"],
- "",
- self.project,
- discover_widget_split=None,
- )
- assert widget.discover_widget_split is None
- response = self.do_request(
- {
- "field": ["count()", "transaction.op", "error.type"],
- "query": "",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- "dashboardWidgetId": widget.id,
- },
- features={"organizations:performance-discover-dataset-selector": True},
- )
- assert response.status_code == 200, response.content
- assert response.data.get("meta").get(
- "discoverSplitDecision"
- ) == DashboardWidgetTypes.get_type_name(DashboardWidgetTypes.ERROR_EVENTS)
- widget.refresh_from_db()
- assert widget.discover_widget_split == DashboardWidgetTypes.ERROR_EVENTS
- assert widget.dataset_source == DatasetSourcesTypes.FORCED.value
- def test_split_decision_for_ambiguous_widget_with_data(self):
- # Store a transaction
- transaction_data = load_data("transaction", timestamp=before_now(minutes=1))
- self.store_event(
- data={
- **transaction_data,
- },
- project_id=self.project.id,
- )
- # Store an event
- error_data = load_data("python", timestamp=before_now(minutes=1))
- self.store_event(
- data={
- **error_data,
- "exception": {"values": [{"type": "blah", "data": {"values": []}}]},
- },
- project_id=self.project.id,
- )
- _, widget, __ = create_widget(
- ["count()"],
- "",
- self.project,
- discover_widget_split=None,
- )
- assert widget.discover_widget_split is None
- response = self.do_request(
- {
- "field": ["count()"],
- "query": "",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- "dashboardWidgetId": widget.id,
- },
- features={"organizations:performance-discover-dataset-selector": True},
- )
- assert response.status_code == 200, response.content
- assert response.data.get("meta").get(
- "discoverSplitDecision"
- ) == DashboardWidgetTypes.get_type_name(DashboardWidgetTypes.ERROR_EVENTS)
- widget.refresh_from_db()
- assert widget.discover_widget_split == DashboardWidgetTypes.ERROR_EVENTS
- assert widget.dataset_source == DatasetSourcesTypes.FORCED.value
- @mock.patch("sentry.snuba.errors.query")
- def test_errors_request_made_for_saved_error_dashboard_widget_type(self, mock_errors_query):
- mock_errors_query.return_value = {
- "data": [],
- "meta": {},
- }
- _, widget, __ = create_widget(
- ["count()"], "", self.project, discover_widget_split=DashboardWidgetTypes.ERROR_EVENTS
- )
- response = self.do_request(
- {
- "field": [
- "count()",
- ],
- "query": "",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- "dashboardWidgetId": widget.id,
- }
- )
- assert response.status_code == 200, response.content
- mock_errors_query.assert_called_once()
- @mock.patch("sentry.snuba.metrics_enhanced_performance.query")
- def test_metrics_enhanced_request_made_for_saved_transaction_like_dashboard_widget_type(
- self, mock_mep_query
- ):
- mock_mep_query.return_value = {
- "data": [],
- "meta": {},
- }
- _, widget, __ = create_widget(
- ["count()"],
- "",
- self.project,
- discover_widget_split=DashboardWidgetTypes.TRANSACTION_LIKE,
- )
- response = self.do_request(
- {
- "field": [
- "count()",
- ],
- "query": "",
- "dataset": "metricsEnhanced",
- "per_page": 50,
- "dashboardWidgetId": widget.id,
- }
- )
- assert response.status_code == 200, response.content
- mock_mep_query.assert_called_once()
- class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithMetricLayer(
- OrganizationEventsMetricsEnhancedPerformanceEndpointTest
- ):
- def setUp(self):
- super().setUp()
- self.features["organizations:use-metrics-layer"] = True
- @pytest.mark.xfail(reason="Not supported")
- def test_time_spent(self):
- super().test_time_spent()
- @pytest.mark.xfail(reason="Not supported")
- def test_http_error_rate(self):
- super().test_http_error_rate()
- @pytest.mark.xfail(reason="Multiple aliases to same column not supported")
- def test_title_and_transaction_alias(self):
- super().test_title_and_transaction_alias()
- @pytest.mark.xfail(reason="Sort order is flaking when querying multiple datasets")
- def test_maintain_sort_order_across_datasets(self):
- """You may need to run this test a few times to get it to fail"""
- super().test_maintain_sort_order_across_datasets()
- @pytest.mark.xfail(reason="Not implemented")
- def test_avg_compare(self):
- super().test_avg_compare()
- @pytest.mark.xfail(reason="Not implemented")
- def test_avg_if(self):
- super().test_avg_if()
- @pytest.mark.xfail(reason="Not implemented")
- def test_count_if(self):
- super().test_count_if()
- @pytest.mark.xfail(reason="Not implemented")
- def test_device_class(self):
- super().test_device_class()
- @pytest.mark.xfail(reason="Not implemented")
- def test_device_class_filter(self):
- super().test_device_class_filter()
- @pytest.mark.xfail(reason="Not implemented")
- def test_performance_score(self):
- super().test_performance_score()
- @pytest.mark.xfail(reason="Not implemented")
- def test_performance_score_boundaries(self):
- super().test_performance_score_boundaries()
- @pytest.mark.xfail(reason="Not implemented")
- def test_total_performance_score(self):
- super().test_total_performance_score()
- @pytest.mark.xfail(reason="Not implemented")
- def test_weighted_performance_score(self):
- super().test_weighted_performance_score()
- @pytest.mark.xfail(reason="Not implemented")
- def test_invalid_performance_score_column(self):
- super().test_invalid_performance_score_column()
- @pytest.mark.xfail(reason="Not implemented")
- def test_invalid_weighted_performance_score_column(self):
- super().test_invalid_weighted_performance_score_column()
- @pytest.mark.xfail(reason="Not implemented")
- def test_no_weighted_performance_score_column(self):
- super().test_invalid_weighted_performance_score_column()
- @pytest.mark.xfail(reason="Not implemented")
- def test_opportunity_score(self):
- super().test_opportunity_score()
- @pytest.mark.xfail(reason="Not implemented")
- def test_opportunity_score_with_fixed_weights(self):
- super().test_opportunity_score_with_fixed_weights()
- @pytest.mark.xfail(reason="Not implemented")
- def test_count_scores(self):
- super().test_count_scores()
- @pytest.mark.xfail(reason="Not implemented")
- def test_count_starts(self):
- super().test_count_starts()
- @pytest.mark.xfail(reason="Not implemented")
- def test_count_starts_returns_all_counts_when_no_arg_is_passed(self):
- super().test_count_starts_returns_all_counts_when_no_arg_is_passed()
- @pytest.mark.xfail(reason="Not implemented")
- def test_timestamp_groupby(self):
- super().test_timestamp_groupby()
- @pytest.mark.xfail(reason="Not implemented")
- def test_on_demand_with_mep(self):
- super().test_on_demand_with_mep()
- @pytest.mark.xfail(reason="Not implemented")
- def test_cache_miss_rate(self):
- super().test_cache_miss_rate()
- @pytest.mark.xfail(reason="Not implemented")
- def test_http_response_rate(self):
- super().test_http_response_rate()
- @pytest.mark.xfail(reason="Not implemented")
- def test_avg_span_self_time(self):
- super().test_avg_span_self_time()
- @pytest.mark.xfail(reason="Not implemented")
- def test_avg_message_receive_latency_gauge_functions(self):
- super().test_avg_message_receive_latency_gauge_functions()
- @pytest.mark.xfail(reason="Not implemented")
- def test_span_module_filter(self):
- super().test_span_module_filter()
|