1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541 |
- 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 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, iso_format
- 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": iso_format(before_now(days=20)),
- "end": iso_format(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
- 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_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.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,
- )
- with self.feature({"organizations:starfish-browser-webvitals-score-computed-total": True}):
- 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_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
- 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}]
- 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_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()
- @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_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()
|