test_organization_events_mep.py 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521
  1. from typing import Any
  2. from unittest import mock
  3. import pytest
  4. from django.urls import reverse
  5. from rest_framework.response import Response
  6. from sentry.discover.models import TeamKeyTransaction
  7. from sentry.models.dashboard_widget import DashboardWidgetTypes
  8. from sentry.models.projectteam import ProjectTeam
  9. from sentry.models.transaction_threshold import (
  10. ProjectTransactionThreshold,
  11. ProjectTransactionThresholdOverride,
  12. TransactionMetric,
  13. )
  14. from sentry.search.events import constants
  15. from sentry.search.utils import map_device_class_level
  16. from sentry.snuba.metrics.extraction import (
  17. SPEC_VERSION_TWO_FLAG,
  18. MetricSpecType,
  19. OnDemandMetricSpec,
  20. OnDemandMetricSpecVersioning,
  21. )
  22. from sentry.snuba.metrics.naming_layer.mri import TransactionMRI
  23. from sentry.snuba.metrics.naming_layer.public import TransactionMetricKey
  24. from sentry.snuba.utils import DATASET_OPTIONS
  25. from sentry.testutils.cases import MetricsEnhancedPerformanceTestCase
  26. from sentry.testutils.helpers.datetime import before_now, iso_format
  27. from sentry.testutils.helpers.discover import user_misery_formula
  28. from sentry.testutils.helpers.on_demand import create_widget
  29. from sentry.utils.samples import load_data
  30. pytestmark = pytest.mark.sentry_metrics
  31. class OrganizationEventsMetricsEnhancedPerformanceEndpointTest(MetricsEnhancedPerformanceTestCase):
  32. viewname = "sentry-api-0-organization-events"
  33. # Poor intentionally omitted for test_measurement_rating_that_does_not_exist
  34. METRIC_STRINGS = [
  35. "foo_transaction",
  36. "bar_transaction",
  37. "baz_transaction",
  38. "staging",
  39. "measurement_rating",
  40. "good",
  41. "meh",
  42. "d:transactions/measurements.something_custom@millisecond",
  43. "d:transactions/measurements.runtime@hour",
  44. "d:transactions/measurements.bytes_transfered@byte",
  45. "d:transactions/measurements.datacenter_memory@petabyte",
  46. "d:transactions/measurements.custom.kilobyte@kilobyte",
  47. "d:transactions/measurements.longtaskcount@none",
  48. "d:transactions/measurements.percent@ratio",
  49. "d:transactions/measurements.custom_type@somethingcustom",
  50. ]
  51. def setUp(self):
  52. super().setUp()
  53. self.transaction_data = load_data("transaction", timestamp=before_now(minutes=1))
  54. self.features = {
  55. "organizations:performance-use-metrics": True,
  56. }
  57. def do_request(self, query, features=None):
  58. if features is None:
  59. features = {"organizations:discover-basic": True}
  60. features.update(self.features)
  61. self.login_as(user=self.user)
  62. url = reverse(
  63. self.viewname,
  64. kwargs={"organization_slug": self.organization.slug},
  65. )
  66. with self.feature(features):
  67. return self.client.get(url, query, format="json")
  68. def test_no_projects(self):
  69. response = self.do_request(
  70. {
  71. "dataset": "metricsEnhanced",
  72. }
  73. )
  74. assert response.status_code == 200, response.content
  75. def test_invalid_dataset(self):
  76. response = self.do_request(
  77. {
  78. "dataset": "aFakeDataset",
  79. "project": self.project.id,
  80. }
  81. )
  82. assert response.status_code == 400, response.content
  83. assert (
  84. response.data["detail"]
  85. == f"dataset must be one of: {', '.join([key for key in DATASET_OPTIONS.keys()])}"
  86. )
  87. def test_out_of_retention(self):
  88. self.create_project()
  89. with self.options({"system.event-retention-days": 10}):
  90. query = {
  91. "field": ["id", "timestamp"],
  92. "orderby": ["-timestamp", "-id"],
  93. "query": "event.type:transaction",
  94. "start": iso_format(before_now(days=20)),
  95. "end": iso_format(before_now(days=15)),
  96. "dataset": "metricsEnhanced",
  97. }
  98. response = self.do_request(query)
  99. assert response.status_code == 400, response.content
  100. assert response.data["detail"] == "Invalid date range. Please try a more recent date range."
  101. def test_invalid_search_terms(self):
  102. response = self.do_request(
  103. {
  104. "field": ["epm()"],
  105. "query": "hi \n there",
  106. "project": self.project.id,
  107. "dataset": "metricsEnhanced",
  108. }
  109. )
  110. assert response.status_code == 400, response.content
  111. assert (
  112. response.data["detail"]
  113. == "Parse error at 'hi \n ther' (column 4). This is commonly caused by unmatched parentheses. Enclose any text in double quotes."
  114. )
  115. def test_percentile_with_no_data(self):
  116. response = self.do_request(
  117. {
  118. "field": ["p50()"],
  119. "query": "",
  120. "project": self.project.id,
  121. "dataset": "metricsEnhanced",
  122. }
  123. )
  124. assert response.status_code == 200, response.content
  125. data = response.data["data"]
  126. assert len(data) == 1
  127. assert data[0]["p50()"] == 0
  128. def test_project_name(self):
  129. self.store_transaction_metric(
  130. 1,
  131. tags={"environment": "staging"},
  132. timestamp=self.min_ago,
  133. )
  134. response = self.do_request(
  135. {
  136. "field": ["project.name", "environment", "epm()"],
  137. "query": "event.type:transaction",
  138. "dataset": "metricsEnhanced",
  139. "per_page": 50,
  140. }
  141. )
  142. assert response.status_code == 200, response.content
  143. assert len(response.data["data"]) == 1
  144. data = response.data["data"]
  145. meta = response.data["meta"]
  146. field_meta = meta["fields"]
  147. assert data[0]["project.name"] == self.project.slug
  148. assert "project.id" not in data[0]
  149. assert data[0]["environment"] == "staging"
  150. assert meta["isMetricsData"]
  151. assert field_meta["project.name"] == "string"
  152. assert field_meta["environment"] == "string"
  153. assert field_meta["epm()"] == "rate"
  154. def test_project_id(self):
  155. self.store_transaction_metric(
  156. 1,
  157. tags={"environment": "staging"},
  158. timestamp=self.min_ago,
  159. )
  160. response = self.do_request(
  161. {
  162. "field": ["project_id", "environment", "epm()"],
  163. "query": "event.type:transaction",
  164. "dataset": "metrics",
  165. "per_page": 50,
  166. }
  167. )
  168. assert response.status_code == 200, response.content
  169. assert len(response.data["data"]) == 1
  170. data = response.data["data"]
  171. meta = response.data["meta"]
  172. field_meta = meta["fields"]
  173. assert data[0]["project_id"] == self.project.id
  174. assert data[0]["environment"] == "staging"
  175. assert meta["isMetricsData"]
  176. assert field_meta["project_id"] == "integer"
  177. assert field_meta["environment"] == "string"
  178. assert field_meta["epm()"] == "rate"
  179. def test_project_dot_id(self):
  180. self.store_transaction_metric(
  181. 1,
  182. tags={"environment": "staging"},
  183. timestamp=self.min_ago,
  184. )
  185. response = self.do_request(
  186. {
  187. "field": ["project.id", "environment", "epm()"],
  188. "query": "event.type:transaction",
  189. "dataset": "metrics",
  190. "per_page": 50,
  191. }
  192. )
  193. assert response.status_code == 200, response.content
  194. assert len(response.data["data"]) == 1
  195. data = response.data["data"]
  196. meta = response.data["meta"]
  197. field_meta = meta["fields"]
  198. assert data[0]["project.id"] == self.project.id
  199. assert data[0]["environment"] == "staging"
  200. assert meta["isMetricsData"]
  201. assert field_meta["project.id"] == "integer"
  202. assert field_meta["environment"] == "string"
  203. assert field_meta["epm()"] == "rate"
  204. def test_title_alias(self):
  205. """title is an alias to transaction name"""
  206. self.store_transaction_metric(
  207. 1,
  208. tags={"transaction": "foo_transaction"},
  209. timestamp=self.min_ago,
  210. )
  211. response = self.do_request(
  212. {
  213. "field": ["title", "p50()"],
  214. "query": "event.type:transaction",
  215. "dataset": "metricsEnhanced",
  216. "per_page": 50,
  217. }
  218. )
  219. assert response.status_code == 200, response.content
  220. assert len(response.data["data"]) == 1
  221. data = response.data["data"]
  222. meta = response.data["meta"]
  223. field_meta = meta["fields"]
  224. assert data[0]["title"] == "foo_transaction"
  225. assert data[0]["p50()"] == 1
  226. assert meta["isMetricsData"]
  227. assert field_meta["title"] == "string"
  228. assert field_meta["p50()"] == "duration"
  229. def test_having_condition(self):
  230. self.store_transaction_metric(
  231. 1,
  232. tags={"environment": "staging", "transaction": "foo_transaction"},
  233. timestamp=self.min_ago,
  234. )
  235. self.store_transaction_metric(
  236. # shouldn't show up
  237. 100,
  238. tags={"environment": "staging", "transaction": "bar_transaction"},
  239. timestamp=self.min_ago,
  240. )
  241. response = self.do_request(
  242. {
  243. "field": ["transaction", "project", "p50(transaction.duration)"],
  244. "query": "event.type:transaction p50(transaction.duration):<50",
  245. "dataset": "metricsEnhanced",
  246. "per_page": 50,
  247. }
  248. )
  249. assert response.status_code == 200, response.content
  250. assert len(response.data["data"]) == 1
  251. data = response.data["data"]
  252. meta = response.data["meta"]
  253. field_meta = meta["fields"]
  254. assert data[0]["transaction"] == "foo_transaction"
  255. assert data[0]["project"] == self.project.slug
  256. assert data[0]["p50(transaction.duration)"] == 1
  257. assert meta["isMetricsData"]
  258. assert field_meta["transaction"] == "string"
  259. assert field_meta["project"] == "string"
  260. assert field_meta["p50(transaction.duration)"] == "duration"
  261. def test_having_condition_with_preventing_aggregates(self):
  262. self.store_transaction_metric(
  263. 1,
  264. tags={"environment": "staging", "transaction": "foo_transaction"},
  265. timestamp=self.min_ago,
  266. )
  267. self.store_transaction_metric(
  268. 100,
  269. tags={"environment": "staging", "transaction": "bar_transaction"},
  270. timestamp=self.min_ago,
  271. )
  272. response = self.do_request(
  273. {
  274. "field": ["transaction", "project", "p50(transaction.duration)"],
  275. "query": "event.type:transaction p50(transaction.duration):<50",
  276. "dataset": "metricsEnhanced",
  277. "preventMetricAggregates": "1",
  278. "per_page": 50,
  279. }
  280. )
  281. assert response.status_code == 200, response.content
  282. assert len(response.data["data"]) == 0
  283. meta = response.data["meta"]
  284. field_meta = meta["fields"]
  285. assert not meta["isMetricsData"]
  286. assert field_meta["transaction"] == "string"
  287. assert field_meta["project"] == "string"
  288. assert field_meta["p50(transaction.duration)"] == "duration"
  289. def test_having_condition_with_preventing_aggregate_metrics_only(self):
  290. """same as the previous test, but with the dataset on explicit metrics
  291. which should throw a 400 error instead"""
  292. response = self.do_request(
  293. {
  294. "field": ["transaction", "project", "p50(transaction.duration)"],
  295. "query": "event.type:transaction p50(transaction.duration):<50",
  296. "dataset": "metrics",
  297. "preventMetricAggregates": "1",
  298. "per_page": 50,
  299. "project": self.project.id,
  300. }
  301. )
  302. assert response.status_code == 400, response.content
  303. def test_having_condition_not_selected(self):
  304. self.store_transaction_metric(
  305. 1,
  306. tags={"environment": "staging", "transaction": "foo_transaction"},
  307. timestamp=self.min_ago,
  308. )
  309. self.store_transaction_metric(
  310. # shouldn't show up
  311. 100,
  312. tags={"environment": "staging", "transaction": "bar_transaction"},
  313. timestamp=self.min_ago,
  314. )
  315. response = self.do_request(
  316. {
  317. "field": ["transaction", "project", "p50(transaction.duration)"],
  318. "query": "event.type:transaction p75(transaction.duration):<50",
  319. "dataset": "metricsEnhanced",
  320. "per_page": 50,
  321. }
  322. )
  323. assert response.status_code == 200, response.content
  324. assert len(response.data["data"]) == 1
  325. data = response.data["data"]
  326. meta = response.data["meta"]
  327. field_meta = meta["fields"]
  328. assert data[0]["transaction"] == "foo_transaction"
  329. assert data[0]["project"] == self.project.slug
  330. assert data[0]["p50(transaction.duration)"] == 1
  331. assert meta["isMetricsData"]
  332. assert field_meta["transaction"] == "string"
  333. assert field_meta["project"] == "string"
  334. assert field_meta["p50(transaction.duration)"] == "duration"
  335. def test_non_metrics_tag_with_implicit_format(self):
  336. self.store_transaction_metric(
  337. 1,
  338. tags={"environment": "staging", "transaction": "foo_transaction"},
  339. timestamp=self.min_ago,
  340. )
  341. response = self.do_request(
  342. {
  343. "field": ["test", "p50(transaction.duration)"],
  344. "query": "event.type:transaction",
  345. "dataset": "metricsEnhanced",
  346. "per_page": 50,
  347. }
  348. )
  349. assert response.status_code == 200, response.content
  350. assert len(response.data["data"]) == 0
  351. assert not response.data["meta"]["isMetricsData"]
  352. def test_non_metrics_tag_with_implicit_format_metrics_dataset(self):
  353. self.store_transaction_metric(
  354. 1,
  355. tags={"environment": "staging", "transaction": "foo_transaction"},
  356. timestamp=self.min_ago,
  357. )
  358. response = self.do_request(
  359. {
  360. "field": ["test", "p50(transaction.duration)"],
  361. "query": "event.type:transaction",
  362. "dataset": "metrics",
  363. "per_page": 50,
  364. }
  365. )
  366. assert response.status_code == 400, response.content
  367. def test_performance_homepage_query(self):
  368. self.store_transaction_metric(
  369. 1,
  370. tags={
  371. "transaction": "foo_transaction",
  372. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  373. },
  374. timestamp=self.min_ago,
  375. )
  376. self.store_transaction_metric(
  377. 1,
  378. "measurements.fcp",
  379. tags={"transaction": "foo_transaction"},
  380. timestamp=self.min_ago,
  381. )
  382. self.store_transaction_metric(
  383. 2,
  384. "measurements.lcp",
  385. tags={"transaction": "foo_transaction"},
  386. timestamp=self.min_ago,
  387. )
  388. self.store_transaction_metric(
  389. 3,
  390. "measurements.fid",
  391. tags={"transaction": "foo_transaction"},
  392. timestamp=self.min_ago,
  393. )
  394. self.store_transaction_metric(
  395. 4,
  396. "measurements.cls",
  397. tags={"transaction": "foo_transaction"},
  398. timestamp=self.min_ago,
  399. )
  400. self.store_transaction_metric(
  401. 1,
  402. "user",
  403. tags={
  404. "transaction": "foo_transaction",
  405. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_FRUSTRATED_TAG_VALUE,
  406. },
  407. timestamp=self.min_ago,
  408. )
  409. for dataset in ["metrics", "metricsEnhanced"]:
  410. response = self.do_request(
  411. {
  412. "field": [
  413. "transaction",
  414. "project",
  415. "tpm()",
  416. "p75(measurements.fcp)",
  417. "p75(measurements.lcp)",
  418. "p75(measurements.fid)",
  419. "p75(measurements.cls)",
  420. "count_unique(user)",
  421. "apdex()",
  422. "count_miserable(user)",
  423. "user_misery()",
  424. "failure_rate()",
  425. "failure_count()",
  426. ],
  427. "orderby": "tpm()",
  428. "query": "event.type:transaction",
  429. "dataset": dataset,
  430. "per_page": 50,
  431. }
  432. )
  433. assert len(response.data["data"]) == 1
  434. data = response.data["data"][0]
  435. meta = response.data["meta"]
  436. field_meta = meta["fields"]
  437. assert data["transaction"] == "foo_transaction"
  438. assert data["project"] == self.project.slug
  439. assert data["p75(measurements.fcp)"] == 1.0
  440. assert data["p75(measurements.lcp)"] == 2.0
  441. assert data["p75(measurements.fid)"] == 3.0
  442. assert data["p75(measurements.cls)"] == 4.0
  443. assert data["apdex()"] == 1.0
  444. assert data["count_miserable(user)"] == 1.0
  445. assert data["user_misery()"] == 0.058
  446. assert data["failure_rate()"] == 1
  447. assert data["failure_count()"] == 1
  448. assert meta["isMetricsData"]
  449. assert field_meta["transaction"] == "string"
  450. assert field_meta["project"] == "string"
  451. assert field_meta["p75(measurements.fcp)"] == "duration"
  452. assert field_meta["p75(measurements.lcp)"] == "duration"
  453. assert field_meta["p75(measurements.fid)"] == "duration"
  454. assert field_meta["p75(measurements.cls)"] == "number"
  455. assert field_meta["apdex()"] == "number"
  456. assert field_meta["count_miserable(user)"] == "integer"
  457. assert field_meta["user_misery()"] == "number"
  458. assert field_meta["failure_rate()"] == "percentage"
  459. assert field_meta["failure_count()"] == "integer"
  460. assert field_meta["tpm()"] == "rate"
  461. assert meta["units"]["tpm()"] == "1/minute"
  462. def test_user_misery_and_team_key_sort(self):
  463. self.store_transaction_metric(
  464. 1,
  465. tags={
  466. "transaction": "foo_transaction",
  467. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  468. },
  469. timestamp=self.min_ago,
  470. )
  471. self.store_transaction_metric(
  472. 1,
  473. "measurements.fcp",
  474. tags={"transaction": "foo_transaction"},
  475. timestamp=self.min_ago,
  476. )
  477. self.store_transaction_metric(
  478. 2,
  479. "measurements.lcp",
  480. tags={"transaction": "foo_transaction"},
  481. timestamp=self.min_ago,
  482. )
  483. self.store_transaction_metric(
  484. 3,
  485. "measurements.fid",
  486. tags={"transaction": "foo_transaction"},
  487. timestamp=self.min_ago,
  488. )
  489. self.store_transaction_metric(
  490. 4,
  491. "measurements.cls",
  492. tags={"transaction": "foo_transaction"},
  493. timestamp=self.min_ago,
  494. )
  495. self.store_transaction_metric(
  496. 1,
  497. "user",
  498. tags={
  499. "transaction": "foo_transaction",
  500. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_FRUSTRATED_TAG_VALUE,
  501. },
  502. timestamp=self.min_ago,
  503. )
  504. response = self.do_request(
  505. {
  506. "field": [
  507. "team_key_transaction",
  508. "transaction",
  509. "project",
  510. "tpm()",
  511. "p75(measurements.fcp)",
  512. "p75(measurements.lcp)",
  513. "p75(measurements.fid)",
  514. "p75(measurements.cls)",
  515. "count_unique(user)",
  516. "apdex()",
  517. "count_miserable(user)",
  518. "user_misery()",
  519. "failure_rate()",
  520. "failure_count()",
  521. ],
  522. "orderby": ["team_key_transaction", "user_misery()"],
  523. "query": "event.type:transaction",
  524. "dataset": "metrics",
  525. "per_page": 50,
  526. }
  527. )
  528. assert response.status_code == 200, response.content
  529. assert len(response.data["data"]) == 1
  530. data = response.data["data"][0]
  531. meta = response.data["meta"]
  532. field_meta = meta["fields"]
  533. assert data["transaction"] == "foo_transaction"
  534. assert data["project"] == self.project.slug
  535. assert data["p75(measurements.fcp)"] == 1.0
  536. assert data["p75(measurements.lcp)"] == 2.0
  537. assert data["p75(measurements.fid)"] == 3.0
  538. assert data["p75(measurements.cls)"] == 4.0
  539. assert data["apdex()"] == 1.0
  540. assert data["count_miserable(user)"] == 1.0
  541. assert data["user_misery()"] == 0.058
  542. assert data["failure_rate()"] == 1
  543. assert data["failure_count()"] == 1
  544. assert meta["isMetricsData"]
  545. assert field_meta["transaction"] == "string"
  546. assert field_meta["project"] == "string"
  547. assert field_meta["p75(measurements.fcp)"] == "duration"
  548. assert field_meta["p75(measurements.lcp)"] == "duration"
  549. assert field_meta["p75(measurements.fid)"] == "duration"
  550. assert field_meta["p75(measurements.cls)"] == "number"
  551. assert field_meta["apdex()"] == "number"
  552. assert field_meta["count_miserable(user)"] == "integer"
  553. assert field_meta["user_misery()"] == "number"
  554. assert field_meta["failure_rate()"] == "percentage"
  555. assert field_meta["failure_count()"] == "integer"
  556. def test_no_team_key_transactions(self):
  557. self.store_transaction_metric(
  558. 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago
  559. )
  560. self.store_transaction_metric(
  561. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  562. )
  563. query = {
  564. "team": "myteams",
  565. "project": [self.project.id],
  566. # TODO sort by transaction here once that's possible for order to match the same test without metrics
  567. "orderby": "p95()",
  568. "field": [
  569. "team_key_transaction",
  570. "transaction",
  571. "transaction.status",
  572. "project",
  573. "epm()",
  574. "failure_rate()",
  575. "p95()",
  576. ],
  577. "per_page": 50,
  578. "dataset": "metricsEnhanced",
  579. }
  580. response = self.do_request(query)
  581. assert response.status_code == 200, response.content
  582. assert len(response.data["data"]) == 2
  583. data = response.data["data"]
  584. meta = response.data["meta"]
  585. field_meta = meta["fields"]
  586. assert data[0]["team_key_transaction"] == 0
  587. assert data[0]["transaction"] == "foo_transaction"
  588. assert data[1]["team_key_transaction"] == 0
  589. assert data[1]["transaction"] == "bar_transaction"
  590. assert meta["isMetricsData"]
  591. assert field_meta["team_key_transaction"] == "boolean"
  592. assert field_meta["transaction"] == "string"
  593. def test_team_key_transactions_my_teams(self):
  594. team1 = self.create_team(organization=self.organization, name="Team A")
  595. self.create_team_membership(team1, user=self.user)
  596. self.project.add_team(team1)
  597. team2 = self.create_team(organization=self.organization, name="Team B")
  598. self.project.add_team(team2)
  599. key_transactions = [
  600. (team1, "foo_transaction"),
  601. (team2, "baz_transaction"),
  602. ]
  603. # Not a key transaction
  604. self.store_transaction_metric(
  605. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  606. )
  607. for team, transaction in key_transactions:
  608. self.store_transaction_metric(
  609. 1, tags={"transaction": transaction}, timestamp=self.min_ago
  610. )
  611. TeamKeyTransaction.objects.create(
  612. organization=self.organization,
  613. transaction=transaction,
  614. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  615. )
  616. query = {
  617. "team": "myteams",
  618. "project": [self.project.id],
  619. "field": [
  620. "team_key_transaction",
  621. "transaction",
  622. "transaction.status",
  623. "project",
  624. "epm()",
  625. "failure_rate()",
  626. "p95()",
  627. ],
  628. "per_page": 50,
  629. "dataset": "metricsEnhanced",
  630. }
  631. query["orderby"] = ["team_key_transaction", "p95()"]
  632. response = self.do_request(query)
  633. assert response.status_code == 200, response.content
  634. assert len(response.data["data"]) == 3
  635. data = response.data["data"]
  636. meta = response.data["meta"]
  637. field_meta = meta["fields"]
  638. assert data[0]["team_key_transaction"] == 0
  639. assert data[0]["transaction"] == "baz_transaction"
  640. assert data[1]["team_key_transaction"] == 0
  641. assert data[1]["transaction"] == "bar_transaction"
  642. assert data[2]["team_key_transaction"] == 1
  643. assert data[2]["transaction"] == "foo_transaction"
  644. assert meta["isMetricsData"]
  645. assert field_meta["team_key_transaction"] == "boolean"
  646. assert field_meta["transaction"] == "string"
  647. # not specifying any teams should use my teams
  648. query = {
  649. "project": [self.project.id],
  650. "field": [
  651. "team_key_transaction",
  652. "transaction",
  653. "transaction.status",
  654. "project",
  655. "epm()",
  656. "failure_rate()",
  657. "p95()",
  658. ],
  659. "per_page": 50,
  660. "dataset": "metricsEnhanced",
  661. }
  662. query["orderby"] = ["team_key_transaction", "p95()"]
  663. response = self.do_request(query)
  664. assert response.status_code == 200, response.content
  665. assert len(response.data["data"]) == 3
  666. data = response.data["data"]
  667. meta = response.data["meta"]
  668. field_meta = meta["fields"]
  669. assert data[0]["team_key_transaction"] == 0
  670. assert data[0]["transaction"] == "baz_transaction"
  671. assert data[1]["team_key_transaction"] == 0
  672. assert data[1]["transaction"] == "bar_transaction"
  673. assert data[2]["team_key_transaction"] == 1
  674. assert data[2]["transaction"] == "foo_transaction"
  675. assert meta["isMetricsData"]
  676. assert field_meta["team_key_transaction"] == "boolean"
  677. assert field_meta["transaction"] == "string"
  678. def test_team_key_transactions_orderby(self):
  679. team1 = self.create_team(organization=self.organization, name="Team A")
  680. team2 = self.create_team(organization=self.organization, name="Team B")
  681. key_transactions = [
  682. (team1, "foo_transaction", 1),
  683. (team2, "baz_transaction", 100),
  684. ]
  685. # Not a key transaction
  686. self.store_transaction_metric(
  687. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  688. )
  689. for team, transaction, value in key_transactions:
  690. self.store_transaction_metric(
  691. value, tags={"transaction": transaction}, timestamp=self.min_ago
  692. )
  693. self.create_team_membership(team, user=self.user)
  694. self.project.add_team(team)
  695. TeamKeyTransaction.objects.create(
  696. organization=self.organization,
  697. transaction=transaction,
  698. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  699. )
  700. query = {
  701. "team": "myteams",
  702. "project": [self.project.id],
  703. "field": [
  704. "team_key_transaction",
  705. "transaction",
  706. "transaction.status",
  707. "project",
  708. "epm()",
  709. "failure_rate()",
  710. "p95()",
  711. ],
  712. "per_page": 50,
  713. "dataset": "metricsEnhanced",
  714. }
  715. # test ascending order
  716. query["orderby"] = ["team_key_transaction", "p95()"]
  717. response = self.do_request(query)
  718. assert response.status_code == 200, response.content
  719. assert len(response.data["data"]) == 3
  720. data = response.data["data"]
  721. meta = response.data["meta"]
  722. field_meta = meta["fields"]
  723. assert data[0]["team_key_transaction"] == 0
  724. assert data[0]["transaction"] == "bar_transaction"
  725. assert data[1]["team_key_transaction"] == 1
  726. assert data[1]["transaction"] == "foo_transaction"
  727. assert data[2]["team_key_transaction"] == 1
  728. assert data[2]["transaction"] == "baz_transaction"
  729. assert meta["isMetricsData"]
  730. assert field_meta["team_key_transaction"] == "boolean"
  731. assert field_meta["transaction"] == "string"
  732. # test descending order
  733. query["orderby"] = ["-team_key_transaction", "p95()"]
  734. response = self.do_request(query)
  735. assert response.status_code == 200, response.content
  736. assert len(response.data["data"]) == 3
  737. data = response.data["data"]
  738. meta = response.data["meta"]
  739. field_meta = meta["fields"]
  740. assert data[0]["team_key_transaction"] == 1
  741. assert data[0]["transaction"] == "foo_transaction"
  742. assert data[1]["team_key_transaction"] == 1
  743. assert data[1]["transaction"] == "baz_transaction"
  744. assert data[2]["team_key_transaction"] == 0
  745. assert data[2]["transaction"] == "bar_transaction"
  746. assert meta["isMetricsData"]
  747. assert field_meta["team_key_transaction"] == "boolean"
  748. assert field_meta["transaction"] == "string"
  749. def test_team_key_transactions_query(self):
  750. team1 = self.create_team(organization=self.organization, name="Team A")
  751. team2 = self.create_team(organization=self.organization, name="Team B")
  752. key_transactions = [
  753. (team1, "foo_transaction", 1),
  754. (team2, "baz_transaction", 100),
  755. ]
  756. # Not a key transaction
  757. self.store_transaction_metric(
  758. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  759. )
  760. for team, transaction, value in key_transactions:
  761. self.store_transaction_metric(
  762. value, tags={"transaction": transaction}, timestamp=self.min_ago
  763. )
  764. self.create_team_membership(team, user=self.user)
  765. self.project.add_team(team)
  766. TeamKeyTransaction.objects.create(
  767. organization=self.organization,
  768. transaction=transaction,
  769. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  770. )
  771. query = {
  772. "team": "myteams",
  773. "project": [self.project.id],
  774. # use the order by to ensure the result order
  775. "orderby": "p95()",
  776. "field": [
  777. "team_key_transaction",
  778. "transaction",
  779. "transaction.status",
  780. "project",
  781. "epm()",
  782. "failure_rate()",
  783. "p95()",
  784. ],
  785. "per_page": 50,
  786. "dataset": "metricsEnhanced",
  787. }
  788. # key transactions
  789. query["query"] = "has:team_key_transaction"
  790. response = self.do_request(query)
  791. assert response.status_code == 200, response.content
  792. assert len(response.data["data"]) == 2
  793. data = response.data["data"]
  794. meta = response.data["meta"]
  795. field_meta = meta["fields"]
  796. assert data[0]["team_key_transaction"] == 1
  797. assert data[0]["transaction"] == "foo_transaction"
  798. assert data[1]["team_key_transaction"] == 1
  799. assert data[1]["transaction"] == "baz_transaction"
  800. assert meta["isMetricsData"]
  801. assert field_meta["team_key_transaction"] == "boolean"
  802. assert field_meta["transaction"] == "string"
  803. # key transactions
  804. query["query"] = "team_key_transaction:true"
  805. response = self.do_request(query)
  806. assert response.status_code == 200, response.content
  807. assert len(response.data["data"]) == 2
  808. data = response.data["data"]
  809. meta = response.data["meta"]
  810. field_meta = meta["fields"]
  811. assert data[0]["team_key_transaction"] == 1
  812. assert data[0]["transaction"] == "foo_transaction"
  813. assert data[1]["team_key_transaction"] == 1
  814. assert data[1]["transaction"] == "baz_transaction"
  815. assert meta["isMetricsData"]
  816. assert field_meta["team_key_transaction"] == "boolean"
  817. assert field_meta["transaction"] == "string"
  818. # not key transactions
  819. query["query"] = "!has:team_key_transaction"
  820. response = self.do_request(query)
  821. assert response.status_code == 200, response.content
  822. assert len(response.data["data"]) == 1
  823. data = response.data["data"]
  824. meta = response.data["meta"]
  825. field_meta = meta["fields"]
  826. assert data[0]["team_key_transaction"] == 0
  827. assert data[0]["transaction"] == "bar_transaction"
  828. assert meta["isMetricsData"]
  829. assert field_meta["team_key_transaction"] == "boolean"
  830. assert field_meta["transaction"] == "string"
  831. # not key transactions
  832. query["query"] = "team_key_transaction:false"
  833. response = self.do_request(query)
  834. assert response.status_code == 200, response.content
  835. assert len(response.data["data"]) == 1
  836. data = response.data["data"]
  837. meta = response.data["meta"]
  838. field_meta = meta["fields"]
  839. assert data[0]["team_key_transaction"] == 0
  840. assert data[0]["transaction"] == "bar_transaction"
  841. assert meta["isMetricsData"]
  842. assert field_meta["team_key_transaction"] == "boolean"
  843. assert field_meta["transaction"] == "string"
  844. def test_team_key_transaction_not_exists(self):
  845. team1 = self.create_team(organization=self.organization, name="Team A")
  846. team2 = self.create_team(organization=self.organization, name="Team B")
  847. key_transactions = [
  848. (team1, "foo_transaction", 1),
  849. (team2, "baz_transaction", 100),
  850. ]
  851. for team, transaction, value in key_transactions:
  852. self.store_transaction_metric(
  853. value, tags={"transaction": transaction}, timestamp=self.min_ago
  854. )
  855. self.create_team_membership(team, user=self.user)
  856. self.project.add_team(team)
  857. TeamKeyTransaction.objects.create(
  858. organization=self.organization,
  859. transaction=transaction,
  860. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  861. )
  862. # Don't create a metric for this one
  863. TeamKeyTransaction.objects.create(
  864. organization=self.organization,
  865. transaction="not_in_metrics",
  866. project_team=ProjectTeam.objects.get(project=self.project, team=team1),
  867. )
  868. query = {
  869. "team": "myteams",
  870. "project": [self.project.id],
  871. # use the order by to ensure the result order
  872. "orderby": "p95()",
  873. "field": [
  874. "team_key_transaction",
  875. "transaction",
  876. "transaction.status",
  877. "project",
  878. "epm()",
  879. "failure_rate()",
  880. "p95()",
  881. ],
  882. "per_page": 50,
  883. "dataset": "metricsEnhanced",
  884. }
  885. # key transactions
  886. query["query"] = "has:team_key_transaction"
  887. response = self.do_request(query)
  888. assert response.status_code == 200, response.content
  889. assert len(response.data["data"]) == 2
  890. data = response.data["data"]
  891. meta = response.data["meta"]
  892. field_meta = meta["fields"]
  893. assert data[0]["team_key_transaction"] == 1
  894. assert data[0]["transaction"] == "foo_transaction"
  895. assert data[1]["team_key_transaction"] == 1
  896. assert data[1]["transaction"] == "baz_transaction"
  897. assert meta["isMetricsData"]
  898. assert field_meta["team_key_transaction"] == "boolean"
  899. assert field_meta["transaction"] == "string"
  900. # key transactions
  901. query["query"] = "team_key_transaction:true"
  902. response = self.do_request(query)
  903. assert response.status_code == 200, response.content
  904. assert len(response.data["data"]) == 2
  905. data = response.data["data"]
  906. meta = response.data["meta"]
  907. field_meta = meta["fields"]
  908. assert data[0]["team_key_transaction"] == 1
  909. assert data[0]["transaction"] == "foo_transaction"
  910. assert data[1]["team_key_transaction"] == 1
  911. assert data[1]["transaction"] == "baz_transaction"
  912. assert meta["isMetricsData"]
  913. assert field_meta["team_key_transaction"] == "boolean"
  914. assert field_meta["transaction"] == "string"
  915. # not key transactions
  916. query["query"] = "!has:team_key_transaction"
  917. response = self.do_request(query)
  918. assert response.status_code == 200, response.content
  919. assert len(response.data["data"]) == 0
  920. data = response.data["data"]
  921. meta = response.data["meta"]
  922. field_meta = meta["fields"]
  923. assert meta["isMetricsData"]
  924. assert field_meta["team_key_transaction"] == "boolean"
  925. assert field_meta["transaction"] == "string"
  926. # not key transactions
  927. query["query"] = "team_key_transaction:false"
  928. response = self.do_request(query)
  929. assert response.status_code == 200, response.content
  930. assert len(response.data["data"]) == 0
  931. data = response.data["data"]
  932. meta = response.data["meta"]
  933. field_meta = meta["fields"]
  934. assert meta["isMetricsData"]
  935. assert field_meta["team_key_transaction"] == "boolean"
  936. assert field_meta["transaction"] == "string"
  937. def test_too_many_team_key_transactions(self):
  938. MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS = 1
  939. with mock.patch(
  940. "sentry.search.events.fields.MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS",
  941. MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS,
  942. ):
  943. team = self.create_team(organization=self.organization, name="Team A")
  944. self.create_team_membership(team, user=self.user)
  945. self.project.add_team(team)
  946. project_team = ProjectTeam.objects.get(project=self.project, team=team)
  947. transactions = ["foo_transaction", "bar_transaction", "baz_transaction"]
  948. for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1):
  949. self.store_transaction_metric(
  950. 100, tags={"transaction": transactions[i]}, timestamp=self.min_ago
  951. )
  952. TeamKeyTransaction.objects.bulk_create(
  953. [
  954. TeamKeyTransaction(
  955. organization=self.organization,
  956. project_team=project_team,
  957. transaction=transactions[i],
  958. )
  959. for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1)
  960. ]
  961. )
  962. query = {
  963. "team": "myteams",
  964. "project": [self.project.id],
  965. "orderby": "p95()",
  966. "field": [
  967. "team_key_transaction",
  968. "transaction",
  969. "transaction.status",
  970. "project",
  971. "epm()",
  972. "failure_rate()",
  973. "p95()",
  974. ],
  975. "dataset": "metricsEnhanced",
  976. "per_page": 50,
  977. }
  978. response = self.do_request(query)
  979. assert response.status_code == 200, response.content
  980. assert len(response.data["data"]) == 2
  981. data = response.data["data"]
  982. meta = response.data["meta"]
  983. assert (
  984. sum(row["team_key_transaction"] for row in data)
  985. == MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS
  986. )
  987. assert meta["isMetricsData"]
  988. def test_measurement_rating(self):
  989. self.store_transaction_metric(
  990. 50,
  991. metric="measurements.lcp",
  992. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  993. timestamp=self.min_ago,
  994. )
  995. self.store_transaction_metric(
  996. 15,
  997. metric="measurements.fp",
  998. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  999. timestamp=self.min_ago,
  1000. )
  1001. self.store_transaction_metric(
  1002. 1500,
  1003. metric="measurements.fcp",
  1004. tags={"measurement_rating": "meh", "transaction": "foo_transaction"},
  1005. timestamp=self.min_ago,
  1006. )
  1007. self.store_transaction_metric(
  1008. 125,
  1009. metric="measurements.fid",
  1010. tags={"measurement_rating": "meh", "transaction": "foo_transaction"},
  1011. timestamp=self.min_ago,
  1012. )
  1013. self.store_transaction_metric(
  1014. 0.15,
  1015. metric="measurements.cls",
  1016. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  1017. timestamp=self.min_ago,
  1018. )
  1019. response = self.do_request(
  1020. {
  1021. "field": [
  1022. "transaction",
  1023. "count_web_vitals(measurements.lcp, good)",
  1024. "count_web_vitals(measurements.fp, good)",
  1025. "count_web_vitals(measurements.fcp, meh)",
  1026. "count_web_vitals(measurements.fid, meh)",
  1027. "count_web_vitals(measurements.cls, good)",
  1028. "count_web_vitals(measurements.lcp, any)",
  1029. ],
  1030. "query": "event.type:transaction",
  1031. "dataset": "metricsEnhanced",
  1032. "per_page": 50,
  1033. }
  1034. )
  1035. assert response.status_code == 200, response.content
  1036. assert len(response.data["data"]) == 1
  1037. data = response.data["data"]
  1038. meta = response.data["meta"]
  1039. field_meta = meta["fields"]
  1040. assert data[0]["count_web_vitals(measurements.lcp, good)"] == 1
  1041. assert data[0]["count_web_vitals(measurements.fp, good)"] == 1
  1042. assert data[0]["count_web_vitals(measurements.fcp, meh)"] == 1
  1043. assert data[0]["count_web_vitals(measurements.fid, meh)"] == 1
  1044. assert data[0]["count_web_vitals(measurements.cls, good)"] == 1
  1045. assert data[0]["count_web_vitals(measurements.lcp, any)"] == 1
  1046. assert meta["isMetricsData"]
  1047. assert field_meta["count_web_vitals(measurements.lcp, good)"] == "integer"
  1048. assert field_meta["count_web_vitals(measurements.fp, good)"] == "integer"
  1049. assert field_meta["count_web_vitals(measurements.fcp, meh)"] == "integer"
  1050. assert field_meta["count_web_vitals(measurements.fid, meh)"] == "integer"
  1051. assert field_meta["count_web_vitals(measurements.cls, good)"] == "integer"
  1052. assert field_meta["count_web_vitals(measurements.lcp, any)"] == "integer"
  1053. def test_measurement_rating_that_does_not_exist(self):
  1054. self.store_transaction_metric(
  1055. 1,
  1056. metric="measurements.lcp",
  1057. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  1058. timestamp=self.min_ago,
  1059. )
  1060. response = self.do_request(
  1061. {
  1062. "field": ["transaction", "count_web_vitals(measurements.lcp, poor)"],
  1063. "query": "event.type:transaction",
  1064. "dataset": "metricsEnhanced",
  1065. "per_page": 50,
  1066. }
  1067. )
  1068. assert response.status_code == 200, response.content
  1069. assert len(response.data["data"]) == 1
  1070. data = response.data["data"]
  1071. meta = response.data["meta"]
  1072. assert data[0]["count_web_vitals(measurements.lcp, poor)"] == 0
  1073. assert meta["isMetricsData"]
  1074. assert meta["fields"]["count_web_vitals(measurements.lcp, poor)"] == "integer"
  1075. def test_count_web_vitals_invalid_vital(self):
  1076. query = {
  1077. "field": [
  1078. "count_web_vitals(measurements.foo, poor)",
  1079. ],
  1080. "project": [self.project.id],
  1081. "dataset": "metricsEnhanced",
  1082. }
  1083. response = self.do_request(query)
  1084. assert response.status_code == 400, response.content
  1085. query = {
  1086. "field": [
  1087. "count_web_vitals(tags[lcp], poor)",
  1088. ],
  1089. "project": [self.project.id],
  1090. "dataset": "metricsEnhanced",
  1091. }
  1092. response = self.do_request(query)
  1093. assert response.status_code == 400, response.content
  1094. query = {
  1095. "field": [
  1096. "count_web_vitals(transaction.duration, poor)",
  1097. ],
  1098. "project": [self.project.id],
  1099. "dataset": "metricsEnhanced",
  1100. }
  1101. response = self.do_request(query)
  1102. assert response.status_code == 400, response.content
  1103. query = {
  1104. "field": [
  1105. "count_web_vitals(measurements.lcp, bad)",
  1106. ],
  1107. "project": [self.project.id],
  1108. "dataset": "metricsEnhanced",
  1109. }
  1110. response = self.do_request(query)
  1111. assert response.status_code == 400, response.content
  1112. def test_count_unique_user_returns_zero(self):
  1113. self.store_transaction_metric(
  1114. 50,
  1115. metric="user",
  1116. tags={"transaction": "foo_transaction"},
  1117. timestamp=self.min_ago,
  1118. )
  1119. self.store_transaction_metric(
  1120. 50,
  1121. tags={"transaction": "foo_transaction"},
  1122. timestamp=self.min_ago,
  1123. )
  1124. self.store_transaction_metric(
  1125. 100,
  1126. tags={"transaction": "bar_transaction"},
  1127. timestamp=self.min_ago,
  1128. )
  1129. query = {
  1130. "project": [self.project.id],
  1131. "orderby": "p50()",
  1132. "field": [
  1133. "transaction",
  1134. "count_unique(user)",
  1135. "p50()",
  1136. ],
  1137. "dataset": "metricsEnhanced",
  1138. "per_page": 50,
  1139. }
  1140. response = self.do_request(query)
  1141. assert response.status_code == 200, response.content
  1142. assert len(response.data["data"]) == 2
  1143. data = response.data["data"]
  1144. meta = response.data["meta"]
  1145. assert data[0]["transaction"] == "foo_transaction"
  1146. assert data[0]["count_unique(user)"] == 1
  1147. assert data[1]["transaction"] == "bar_transaction"
  1148. assert data[1]["count_unique(user)"] == 0
  1149. assert meta["isMetricsData"]
  1150. def test_sum_transaction_duration(self):
  1151. self.store_transaction_metric(
  1152. 50,
  1153. tags={"transaction": "foo_transaction"},
  1154. timestamp=self.min_ago,
  1155. )
  1156. self.store_transaction_metric(
  1157. 100,
  1158. tags={"transaction": "foo_transaction"},
  1159. timestamp=self.min_ago,
  1160. )
  1161. self.store_transaction_metric(
  1162. 150,
  1163. tags={"transaction": "foo_transaction"},
  1164. timestamp=self.min_ago,
  1165. )
  1166. query = {
  1167. "project": [self.project.id],
  1168. "orderby": "sum(transaction.duration)",
  1169. "field": [
  1170. "transaction",
  1171. "sum(transaction.duration)",
  1172. ],
  1173. "dataset": "metricsEnhanced",
  1174. "per_page": 50,
  1175. }
  1176. response = self.do_request(query)
  1177. assert response.status_code == 200, response.content
  1178. assert len(response.data["data"]) == 1
  1179. data = response.data["data"]
  1180. meta = response.data["meta"]
  1181. assert data[0]["transaction"] == "foo_transaction"
  1182. assert data[0]["sum(transaction.duration)"] == 300
  1183. assert meta["isMetricsData"]
  1184. def test_custom_measurements_simple(self):
  1185. self.store_transaction_metric(
  1186. 1,
  1187. metric="measurements.something_custom",
  1188. internal_metric="d:transactions/measurements.something_custom@millisecond",
  1189. entity="metrics_distributions",
  1190. tags={"transaction": "foo_transaction"},
  1191. timestamp=self.min_ago,
  1192. )
  1193. query = {
  1194. "project": [self.project.id],
  1195. "orderby": "p50(measurements.something_custom)",
  1196. "field": [
  1197. "transaction",
  1198. "p50(measurements.something_custom)",
  1199. ],
  1200. "statsPeriod": "24h",
  1201. "dataset": "metricsEnhanced",
  1202. "per_page": 50,
  1203. }
  1204. self.wait_for_metric_count(
  1205. self.project,
  1206. 1,
  1207. metric="measurements.something_custom",
  1208. mri="d:transactions/measurements.something_custom@millisecond",
  1209. )
  1210. response = self.do_request(query)
  1211. assert response.status_code == 200, response.content
  1212. assert len(response.data["data"]) == 1
  1213. data = response.data["data"]
  1214. meta = response.data["meta"]
  1215. assert data[0]["transaction"] == "foo_transaction"
  1216. assert data[0]["p50(measurements.something_custom)"] == 1
  1217. assert meta["isMetricsData"]
  1218. assert meta["fields"]["p50(measurements.something_custom)"] == "duration"
  1219. assert meta["units"]["p50(measurements.something_custom)"] == "millisecond"
  1220. def test_custom_measurement_size_meta_type(self):
  1221. self.store_transaction_metric(
  1222. 100,
  1223. metric="measurements.custom_type",
  1224. internal_metric="d:transactions/measurements.custom_type@somethingcustom",
  1225. entity="metrics_distributions",
  1226. tags={"transaction": "foo_transaction"},
  1227. timestamp=self.min_ago,
  1228. )
  1229. self.store_transaction_metric(
  1230. 100,
  1231. metric="measurements.percent",
  1232. internal_metric="d:transactions/measurements.percent@ratio",
  1233. entity="metrics_distributions",
  1234. tags={"transaction": "foo_transaction"},
  1235. timestamp=self.min_ago,
  1236. )
  1237. self.store_transaction_metric(
  1238. 100,
  1239. metric="measurements.longtaskcount",
  1240. internal_metric="d:transactions/measurements.longtaskcount@none",
  1241. entity="metrics_distributions",
  1242. tags={"transaction": "foo_transaction"},
  1243. timestamp=self.min_ago,
  1244. )
  1245. query = {
  1246. "project": [self.project.id],
  1247. "orderby": "p50(measurements.longtaskcount)",
  1248. "field": [
  1249. "transaction",
  1250. "p50(measurements.longtaskcount)",
  1251. "p50(measurements.percent)",
  1252. "p50(measurements.custom_type)",
  1253. ],
  1254. "statsPeriod": "24h",
  1255. "dataset": "metricsEnhanced",
  1256. "per_page": 50,
  1257. }
  1258. response = self.do_request(query)
  1259. assert response.status_code == 200, response.content
  1260. assert len(response.data["data"]) == 1
  1261. data = response.data["data"]
  1262. meta = response.data["meta"]
  1263. assert data[0]["transaction"] == "foo_transaction"
  1264. assert data[0]["p50(measurements.longtaskcount)"] == 100
  1265. assert data[0]["p50(measurements.percent)"] == 100
  1266. assert data[0]["p50(measurements.custom_type)"] == 100
  1267. assert meta["isMetricsData"]
  1268. assert meta["fields"]["p50(measurements.longtaskcount)"] == "integer"
  1269. assert meta["units"]["p50(measurements.longtaskcount)"] is None
  1270. assert meta["fields"]["p50(measurements.percent)"] == "percentage"
  1271. assert meta["units"]["p50(measurements.percent)"] is None
  1272. assert meta["fields"]["p50(measurements.custom_type)"] == "number"
  1273. assert meta["units"]["p50(measurements.custom_type)"] is None
  1274. def test_custom_measurement_none_type(self):
  1275. self.store_transaction_metric(
  1276. 1,
  1277. metric="measurements.cls",
  1278. entity="metrics_distributions",
  1279. tags={"transaction": "foo_transaction"},
  1280. timestamp=self.min_ago,
  1281. )
  1282. query = {
  1283. "project": [self.project.id],
  1284. "orderby": "p75(measurements.cls)",
  1285. "field": [
  1286. "transaction",
  1287. "p75(measurements.cls)",
  1288. "p99(measurements.cls)",
  1289. "max(measurements.cls)",
  1290. ],
  1291. "statsPeriod": "24h",
  1292. "dataset": "metricsEnhanced",
  1293. "per_page": 50,
  1294. }
  1295. self.wait_for_metric_count(
  1296. self.project,
  1297. 1,
  1298. metric=TransactionMetricKey.MEASUREMENTS_CLS.value,
  1299. mri=TransactionMRI.MEASUREMENTS_CLS.value,
  1300. )
  1301. response = self.do_request(query)
  1302. assert response.status_code == 200, response.content
  1303. assert len(response.data["data"]) == 1
  1304. data = response.data["data"]
  1305. meta = response.data["meta"]
  1306. assert data[0]["transaction"] == "foo_transaction"
  1307. assert data[0]["p75(measurements.cls)"] == 1
  1308. assert data[0]["p99(measurements.cls)"] == 1
  1309. assert data[0]["max(measurements.cls)"] == 1
  1310. assert meta["isMetricsData"]
  1311. assert meta["fields"]["p75(measurements.cls)"] == "number"
  1312. assert meta["units"]["p75(measurements.cls)"] is None
  1313. assert meta["fields"]["p99(measurements.cls)"] == "number"
  1314. assert meta["units"]["p99(measurements.cls)"] is None
  1315. assert meta["fields"]["max(measurements.cls)"] == "number"
  1316. assert meta["units"]["max(measurements.cls)"] is None
  1317. def test_custom_measurement_duration_filtering(self):
  1318. self.store_transaction_metric(
  1319. 1,
  1320. metric="measurements.runtime",
  1321. internal_metric="d:transactions/measurements.runtime@hour",
  1322. entity="metrics_distributions",
  1323. tags={"transaction": "foo_transaction"},
  1324. timestamp=self.min_ago,
  1325. )
  1326. self.store_transaction_metric(
  1327. 180,
  1328. metric="measurements.runtime",
  1329. internal_metric="d:transactions/measurements.runtime@hour",
  1330. entity="metrics_distributions",
  1331. tags={"transaction": "bar_transaction"},
  1332. timestamp=self.min_ago,
  1333. )
  1334. query = {
  1335. "project": [self.project.id],
  1336. "field": [
  1337. "transaction",
  1338. "max(measurements.runtime)",
  1339. ],
  1340. "query": "p50(measurements.runtime):>1wk",
  1341. "statsPeriod": "24h",
  1342. "dataset": "metricsEnhanced",
  1343. "per_page": 50,
  1344. }
  1345. response = self.do_request(query)
  1346. assert response.status_code == 200, response.content
  1347. assert len(response.data["data"]) == 1
  1348. data = response.data["data"]
  1349. meta = response.data["meta"]
  1350. assert data[0]["transaction"] == "bar_transaction"
  1351. assert data[0]["max(measurements.runtime)"] == 180
  1352. assert meta["isMetricsData"]
  1353. def test_custom_measurement_size_filtering(self):
  1354. self.store_transaction_metric(
  1355. 1,
  1356. metric="measurements.datacenter_memory",
  1357. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1358. entity="metrics_distributions",
  1359. tags={"transaction": "foo_transaction"},
  1360. timestamp=self.min_ago,
  1361. )
  1362. self.store_transaction_metric(
  1363. 100,
  1364. metric="measurements.datacenter_memory",
  1365. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1366. entity="metrics_distributions",
  1367. tags={"transaction": "bar_transaction"},
  1368. timestamp=self.min_ago,
  1369. )
  1370. query = {
  1371. "project": [self.project.id],
  1372. "field": [
  1373. "transaction",
  1374. "max(measurements.datacenter_memory)",
  1375. ],
  1376. "query": "p50(measurements.datacenter_memory):>5pb",
  1377. "statsPeriod": "24h",
  1378. "dataset": "metricsEnhanced",
  1379. "per_page": 50,
  1380. }
  1381. response = self.do_request(query)
  1382. assert response.status_code == 200, response.content
  1383. assert len(response.data["data"]) == 1
  1384. data = response.data["data"]
  1385. meta = response.data["meta"]
  1386. assert data[0]["transaction"] == "bar_transaction"
  1387. assert data[0]["max(measurements.datacenter_memory)"] == 100
  1388. assert meta["units"]["max(measurements.datacenter_memory)"] == "petabyte"
  1389. assert meta["fields"]["max(measurements.datacenter_memory)"] == "size"
  1390. assert meta["isMetricsData"]
  1391. def test_has_custom_measurement(self):
  1392. self.store_transaction_metric(
  1393. 33,
  1394. metric="measurements.datacenter_memory",
  1395. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1396. entity="metrics_distributions",
  1397. tags={"transaction": "foo_transaction"},
  1398. timestamp=self.min_ago,
  1399. )
  1400. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1401. transaction_data["measurements"]["datacenter_memory"] = {
  1402. "value": 33,
  1403. "unit": "petabyte",
  1404. }
  1405. self.store_event(transaction_data, self.project.id)
  1406. measurement = "measurements.datacenter_memory"
  1407. response = self.do_request(
  1408. {
  1409. "field": ["transaction", measurement],
  1410. "query": "has:measurements.datacenter_memory",
  1411. "dataset": "discover",
  1412. }
  1413. )
  1414. assert response.status_code == 200, response.content
  1415. assert len(response.data["data"]) == 1
  1416. response = self.do_request(
  1417. {
  1418. "field": ["transaction", measurement],
  1419. "query": "!has:measurements.datacenter_memory",
  1420. "dataset": "discover",
  1421. }
  1422. )
  1423. assert response.status_code == 200, response.content
  1424. assert len(response.data["data"]) == 0
  1425. def test_environment_param(self):
  1426. self.create_environment(self.project, name="staging")
  1427. self.store_transaction_metric(
  1428. 1,
  1429. tags={"transaction": "foo_transaction", "environment": "staging"},
  1430. timestamp=self.min_ago,
  1431. )
  1432. self.store_transaction_metric(
  1433. 100,
  1434. tags={"transaction": "foo_transaction"},
  1435. timestamp=self.min_ago,
  1436. )
  1437. query = {
  1438. "project": [self.project.id],
  1439. "environment": "staging",
  1440. "orderby": "p50(transaction.duration)",
  1441. "field": [
  1442. "transaction",
  1443. "environment",
  1444. "p50(transaction.duration)",
  1445. ],
  1446. "statsPeriod": "24h",
  1447. "dataset": "metricsEnhanced",
  1448. "per_page": 50,
  1449. }
  1450. response = self.do_request(query)
  1451. assert response.status_code == 200, response.content
  1452. assert len(response.data["data"]) == 1
  1453. data = response.data["data"]
  1454. meta = response.data["meta"]
  1455. assert data[0]["transaction"] == "foo_transaction"
  1456. assert data[0]["environment"] == "staging"
  1457. assert data[0]["p50(transaction.duration)"] == 1
  1458. assert meta["isMetricsData"]
  1459. @pytest.mark.xfail(reason="Started failing on ClickHouse 21.8")
  1460. def test_environment_query(self):
  1461. self.create_environment(self.project, name="staging")
  1462. self.store_transaction_metric(
  1463. 1,
  1464. tags={"transaction": "foo_transaction", "environment": "staging"},
  1465. timestamp=self.min_ago,
  1466. )
  1467. self.store_transaction_metric(
  1468. 100,
  1469. tags={"transaction": "foo_transaction"},
  1470. timestamp=self.min_ago,
  1471. )
  1472. query = {
  1473. "project": [self.project.id],
  1474. "orderby": "p50(transaction.duration)",
  1475. "field": [
  1476. "transaction",
  1477. "environment",
  1478. "p50(transaction.duration)",
  1479. ],
  1480. "query": "!has:environment",
  1481. "statsPeriod": "24h",
  1482. "dataset": "metricsEnhanced",
  1483. "per_page": 50,
  1484. }
  1485. self.wait_for_metric_count(self.project, 2)
  1486. response = self.do_request(query)
  1487. assert response.status_code == 200, response.content
  1488. assert len(response.data["data"]) == 1
  1489. data = response.data["data"]
  1490. meta = response.data["meta"]
  1491. assert data[0]["transaction"] == "foo_transaction"
  1492. assert data[0]["environment"] is None or data[0]["environment"] == ""
  1493. assert data[0]["p50(transaction.duration)"] == 100
  1494. assert meta["isMetricsData"]
  1495. def test_has_transaction(self):
  1496. self.store_transaction_metric(
  1497. 1,
  1498. tags={},
  1499. timestamp=self.min_ago,
  1500. )
  1501. self.store_transaction_metric(
  1502. 100,
  1503. tags={"transaction": "foo_transaction"},
  1504. timestamp=self.min_ago,
  1505. )
  1506. query = {
  1507. "project": [self.project.id],
  1508. "orderby": "p50(transaction.duration)",
  1509. "field": [
  1510. "transaction",
  1511. "p50(transaction.duration)",
  1512. ],
  1513. "query": "has:transaction",
  1514. "statsPeriod": "24h",
  1515. "dataset": "metricsEnhanced",
  1516. "per_page": 50,
  1517. }
  1518. self.wait_for_metric_count(self.project, 2)
  1519. response = self.do_request(query)
  1520. assert response.status_code == 200, response.content
  1521. assert len(response.data["data"]) == 2
  1522. data = response.data["data"]
  1523. meta = response.data["meta"]
  1524. assert data[0]["transaction"] == "<< unparameterized >>"
  1525. assert data[0]["p50(transaction.duration)"] == 1
  1526. assert data[1]["transaction"] == "foo_transaction"
  1527. assert data[1]["p50(transaction.duration)"] == 100
  1528. assert meta["isMetricsData"]
  1529. query = {
  1530. "project": [self.project.id],
  1531. "orderby": "p50(transaction.duration)",
  1532. "field": [
  1533. "transaction",
  1534. "p50(transaction.duration)",
  1535. ],
  1536. "query": "!has:transaction",
  1537. "statsPeriod": "24h",
  1538. "dataset": "metricsEnhanced",
  1539. "per_page": 50,
  1540. }
  1541. response = self.do_request(query)
  1542. assert response.status_code == 400, response.content
  1543. def test_apdex_transaction_threshold(self):
  1544. ProjectTransactionThresholdOverride.objects.create(
  1545. transaction="foo_transaction",
  1546. project=self.project,
  1547. organization=self.project.organization,
  1548. threshold=600,
  1549. metric=TransactionMetric.LCP.value,
  1550. )
  1551. ProjectTransactionThresholdOverride.objects.create(
  1552. transaction="bar_transaction",
  1553. project=self.project,
  1554. organization=self.project.organization,
  1555. threshold=600,
  1556. metric=TransactionMetric.LCP.value,
  1557. )
  1558. self.store_transaction_metric(
  1559. 1,
  1560. tags={
  1561. "transaction": "foo_transaction",
  1562. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1563. },
  1564. timestamp=self.min_ago,
  1565. )
  1566. self.store_transaction_metric(
  1567. 1,
  1568. "measurements.lcp",
  1569. tags={
  1570. "transaction": "bar_transaction",
  1571. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1572. },
  1573. timestamp=self.min_ago,
  1574. )
  1575. response = self.do_request(
  1576. {
  1577. "field": [
  1578. "transaction",
  1579. "apdex()",
  1580. ],
  1581. "orderby": ["apdex()"],
  1582. "query": "event.type:transaction",
  1583. "dataset": "metrics",
  1584. "per_page": 50,
  1585. }
  1586. )
  1587. assert len(response.data["data"]) == 2
  1588. data = response.data["data"]
  1589. meta = response.data["meta"]
  1590. field_meta = meta["fields"]
  1591. assert data[0]["transaction"] == "bar_transaction"
  1592. # Threshold is lcp based
  1593. assert data[0]["apdex()"] == 1
  1594. assert data[1]["transaction"] == "foo_transaction"
  1595. # Threshold is lcp based
  1596. assert data[1]["apdex()"] == 0
  1597. assert meta["isMetricsData"]
  1598. assert field_meta["transaction"] == "string"
  1599. assert field_meta["apdex()"] == "number"
  1600. def test_apdex_project_threshold(self):
  1601. ProjectTransactionThreshold.objects.create(
  1602. project=self.project,
  1603. organization=self.project.organization,
  1604. threshold=600,
  1605. metric=TransactionMetric.LCP.value,
  1606. )
  1607. self.store_transaction_metric(
  1608. 1,
  1609. tags={
  1610. "transaction": "foo_transaction",
  1611. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1612. },
  1613. timestamp=self.min_ago,
  1614. )
  1615. self.store_transaction_metric(
  1616. 1,
  1617. "measurements.lcp",
  1618. tags={
  1619. "transaction": "bar_transaction",
  1620. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1621. },
  1622. timestamp=self.min_ago,
  1623. )
  1624. response = self.do_request(
  1625. {
  1626. "field": [
  1627. "transaction",
  1628. "apdex()",
  1629. ],
  1630. "orderby": ["apdex()"],
  1631. "query": "event.type:transaction",
  1632. "dataset": "metrics",
  1633. "per_page": 50,
  1634. }
  1635. )
  1636. assert response.status_code == 200, response.content
  1637. assert len(response.data["data"]) == 2
  1638. data = response.data["data"]
  1639. meta = response.data["meta"]
  1640. field_meta = meta["fields"]
  1641. assert data[0]["transaction"] == "bar_transaction"
  1642. # Threshold is lcp based
  1643. assert data[0]["apdex()"] == 1
  1644. assert data[1]["transaction"] == "foo_transaction"
  1645. # Threshold is lcp based
  1646. assert data[1]["apdex()"] == 0
  1647. assert meta["isMetricsData"]
  1648. assert field_meta["transaction"] == "string"
  1649. assert field_meta["apdex()"] == "number"
  1650. def test_apdex_satisfaction_param(self):
  1651. for function in ["apdex(300)", "user_misery(300)", "count_miserable(user, 300)"]:
  1652. query = {
  1653. "project": [self.project.id],
  1654. "field": [
  1655. "transaction",
  1656. function,
  1657. ],
  1658. "statsPeriod": "24h",
  1659. "dataset": "metricsEnhanced",
  1660. "per_page": 50,
  1661. }
  1662. response = self.do_request(query)
  1663. assert response.status_code == 200, response.content
  1664. assert len(response.data["data"]) == 0
  1665. meta = response.data["meta"]
  1666. assert not meta["isMetricsData"], function
  1667. query = {
  1668. "project": [self.project.id],
  1669. "field": [
  1670. "transaction",
  1671. function,
  1672. ],
  1673. "statsPeriod": "24h",
  1674. "dataset": "metrics",
  1675. "per_page": 50,
  1676. }
  1677. response = self.do_request(query)
  1678. assert response.status_code == 400, function
  1679. assert b"threshold parameter" in response.content, function
  1680. def test_mobile_metrics(self):
  1681. self.store_transaction_metric(
  1682. 0.4,
  1683. "measurements.frames_frozen_rate",
  1684. tags={
  1685. "transaction": "bar_transaction",
  1686. },
  1687. timestamp=self.min_ago,
  1688. )
  1689. query = {
  1690. "project": [self.project.id],
  1691. "field": [
  1692. "transaction",
  1693. "p50(measurements.frames_frozen_rate)",
  1694. ],
  1695. "statsPeriod": "24h",
  1696. "dataset": "metrics",
  1697. "per_page": 50,
  1698. }
  1699. response = self.do_request(query)
  1700. assert response.status_code == 200, response.content
  1701. assert len(response.data["data"]) == 1
  1702. assert response.data["data"][0]["p50(measurements.frames_frozen_rate)"] == 0.4
  1703. def test_merge_null_unparam(self):
  1704. self.store_transaction_metric(
  1705. 1,
  1706. # Transaction: unparam
  1707. tags={
  1708. "transaction": "<< unparameterized >>",
  1709. },
  1710. timestamp=self.min_ago,
  1711. )
  1712. self.store_transaction_metric(
  1713. 2,
  1714. # Transaction:null
  1715. tags={},
  1716. timestamp=self.min_ago,
  1717. )
  1718. query = {
  1719. "project": [self.project.id],
  1720. "field": [
  1721. "transaction",
  1722. "p50(transaction.duration)",
  1723. ],
  1724. "statsPeriod": "24h",
  1725. "dataset": "metrics",
  1726. "per_page": 50,
  1727. }
  1728. response = self.do_request(query)
  1729. assert response.status_code == 200, response.content
  1730. assert len(response.data["data"]) == 1
  1731. assert response.data["data"][0]["p50(transaction.duration)"] == 1.5
  1732. def test_unparam_filter(self):
  1733. self.store_transaction_metric(
  1734. 1,
  1735. # Transaction: unparam
  1736. tags={
  1737. "transaction": "<< unparameterized >>",
  1738. },
  1739. timestamp=self.min_ago,
  1740. )
  1741. self.store_transaction_metric(
  1742. 2,
  1743. # Transaction:null
  1744. tags={},
  1745. timestamp=self.min_ago,
  1746. )
  1747. self.store_transaction_metric(
  1748. 3,
  1749. tags={
  1750. "transaction": "foo_transaction",
  1751. },
  1752. timestamp=self.min_ago,
  1753. )
  1754. query = {
  1755. "project": [self.project.id],
  1756. "field": [
  1757. "transaction",
  1758. "count()",
  1759. ],
  1760. "query": 'transaction:"<< unparameterized >>"',
  1761. "statsPeriod": "24h",
  1762. "dataset": "metrics",
  1763. "per_page": 50,
  1764. }
  1765. self.wait_for_metric_count(self.project, 3)
  1766. response = self.do_request(query)
  1767. assert response.status_code == 200, response.content
  1768. assert len(response.data["data"]) == 1
  1769. assert response.data["data"][0]["transaction"] == "<< unparameterized >>"
  1770. assert response.data["data"][0]["count()"] == 2
  1771. def test_custom_measurements_without_function(self):
  1772. self.store_transaction_metric(
  1773. 33,
  1774. metric="measurements.datacenter_memory",
  1775. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1776. entity="metrics_distributions",
  1777. tags={"transaction": "foo_transaction"},
  1778. timestamp=self.min_ago,
  1779. )
  1780. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1781. transaction_data["measurements"]["datacenter_memory"] = {
  1782. "value": 33,
  1783. "unit": "petabyte",
  1784. }
  1785. self.store_event(transaction_data, self.project.id)
  1786. measurement = "measurements.datacenter_memory"
  1787. response = self.do_request(
  1788. {
  1789. "field": ["transaction", measurement],
  1790. "query": "measurements.datacenter_memory:33pb",
  1791. "dataset": "discover",
  1792. }
  1793. )
  1794. assert response.status_code == 200, response.content
  1795. data = response.data["data"]
  1796. assert len(data) == 1
  1797. assert data[0][measurement] == 33
  1798. meta = response.data["meta"]
  1799. field_meta = meta["fields"]
  1800. unit_meta = meta["units"]
  1801. assert field_meta[measurement] == "size"
  1802. assert unit_meta[measurement] == "petabyte"
  1803. assert not meta["isMetricsData"]
  1804. def test_custom_measurements_with_function(self):
  1805. self.store_transaction_metric(
  1806. 33,
  1807. metric="measurements.datacenter_memory",
  1808. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1809. entity="metrics_distributions",
  1810. tags={"transaction": "foo_transaction"},
  1811. timestamp=self.min_ago,
  1812. )
  1813. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1814. transaction_data["measurements"]["datacenter_memory"] = {
  1815. "value": 33,
  1816. "unit": "petabyte",
  1817. }
  1818. self.store_event(transaction_data, self.project.id)
  1819. measurement = "p50(measurements.datacenter_memory)"
  1820. response = self.do_request(
  1821. {
  1822. "field": ["transaction", measurement],
  1823. "query": "measurements.datacenter_memory:33pb",
  1824. "dataset": "discover",
  1825. }
  1826. )
  1827. assert response.status_code == 200, response.content
  1828. data = response.data["data"]
  1829. assert len(data) == 1
  1830. assert data[0][measurement] == 33
  1831. meta = response.data["meta"]
  1832. field_meta = meta["fields"]
  1833. unit_meta = meta["units"]
  1834. assert field_meta[measurement] == "size"
  1835. assert unit_meta[measurement] == "petabyte"
  1836. assert not meta["isMetricsData"]
  1837. def test_custom_measurements_equation(self):
  1838. self.store_transaction_metric(
  1839. 33,
  1840. metric="measurements.datacenter_memory",
  1841. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1842. entity="metrics_distributions",
  1843. tags={"transaction": "foo_transaction"},
  1844. timestamp=self.min_ago,
  1845. )
  1846. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1847. transaction_data["measurements"]["datacenter_memory"] = {
  1848. "value": 33,
  1849. "unit": "petabyte",
  1850. }
  1851. self.store_event(transaction_data, self.project.id)
  1852. response = self.do_request(
  1853. {
  1854. "field": [
  1855. "transaction",
  1856. "measurements.datacenter_memory",
  1857. "equation|measurements.datacenter_memory / 3",
  1858. ],
  1859. "query": "",
  1860. "dataset": "discover",
  1861. }
  1862. )
  1863. assert response.status_code == 200, response.content
  1864. data = response.data["data"]
  1865. assert len(data) == 1
  1866. assert data[0]["measurements.datacenter_memory"] == 33
  1867. assert data[0]["equation|measurements.datacenter_memory / 3"] == 11
  1868. meta = response.data["meta"]
  1869. assert not meta["isMetricsData"]
  1870. def test_transaction_wildcard(self):
  1871. self.store_transaction_metric(
  1872. 1,
  1873. tags={"transaction": "foo_transaction"},
  1874. timestamp=self.min_ago,
  1875. )
  1876. self.store_transaction_metric(
  1877. 1,
  1878. tags={"transaction": "bar_transaction"},
  1879. timestamp=self.min_ago,
  1880. )
  1881. response = self.do_request(
  1882. {
  1883. "field": [
  1884. "transaction",
  1885. "p90()",
  1886. ],
  1887. "query": "transaction:foo*",
  1888. "dataset": "metrics",
  1889. }
  1890. )
  1891. assert response.status_code == 200, response.content
  1892. data = response.data["data"]
  1893. assert len(data) == 1
  1894. assert data[0]["p90()"] == 1
  1895. meta = response.data["meta"]
  1896. assert meta["isMetricsData"]
  1897. assert data[0]["transaction"] == "foo_transaction"
  1898. def test_transaction_status_wildcard(self):
  1899. self.store_transaction_metric(
  1900. 1,
  1901. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1902. timestamp=self.min_ago,
  1903. )
  1904. response = self.do_request(
  1905. {
  1906. "field": [
  1907. "transaction",
  1908. "p90()",
  1909. ],
  1910. "query": "transaction.status:f*bar",
  1911. "dataset": "metrics",
  1912. }
  1913. )
  1914. assert response.status_code == 200, response.content
  1915. data = response.data["data"]
  1916. assert len(data) == 1
  1917. assert data[0]["p90()"] == 1
  1918. meta = response.data["meta"]
  1919. assert meta["isMetricsData"]
  1920. def test_http_error_rate(self):
  1921. self.store_transaction_metric(
  1922. 1,
  1923. tags={
  1924. "transaction": "foo_transaction",
  1925. "transaction.status": "foobar",
  1926. "http.status_code": "500",
  1927. },
  1928. timestamp=self.min_ago,
  1929. )
  1930. self.store_transaction_metric(
  1931. 1,
  1932. tags={"transaction": "bar_transaction", "http.status_code": "400"},
  1933. timestamp=self.min_ago,
  1934. )
  1935. response = self.do_request(
  1936. {
  1937. "field": [
  1938. "http_error_rate()",
  1939. ],
  1940. "dataset": "metrics",
  1941. }
  1942. )
  1943. assert response.status_code == 200, response.content
  1944. data = response.data["data"]
  1945. assert len(data) == 1
  1946. assert data[0]["http_error_rate()"] == 0.5
  1947. meta = response.data["meta"]
  1948. assert meta["isMetricsData"]
  1949. def test_time_spent(self):
  1950. self.store_transaction_metric(
  1951. 1,
  1952. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1953. timestamp=self.min_ago,
  1954. )
  1955. self.store_transaction_metric(
  1956. 1,
  1957. tags={"transaction": "bar_transaction"},
  1958. timestamp=self.min_ago,
  1959. )
  1960. response = self.do_request(
  1961. {
  1962. "field": [
  1963. "transaction",
  1964. "time_spent_percentage()",
  1965. ],
  1966. "dataset": "metrics",
  1967. }
  1968. )
  1969. assert response.status_code == 200, response.content
  1970. data = response.data["data"]
  1971. assert len(data) == 2
  1972. assert data[0]["time_spent_percentage()"] == 0.5
  1973. meta = response.data["meta"]
  1974. assert meta["isMetricsData"]
  1975. def test_has_filter(self):
  1976. self.store_transaction_metric(
  1977. 1,
  1978. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1979. timestamp=self.min_ago,
  1980. )
  1981. response = self.do_request(
  1982. {
  1983. "field": [
  1984. "transaction",
  1985. "p50()",
  1986. ],
  1987. # For the metrics dataset, has on metrics should be no-ops
  1988. "query": "has:measurements.frames_frozen_rate",
  1989. "dataset": "metrics",
  1990. }
  1991. )
  1992. assert response.status_code == 200, response.content
  1993. data = response.data["data"]
  1994. assert len(data) == 1
  1995. assert data[0]["p50()"] == 1
  1996. meta = response.data["meta"]
  1997. assert meta["isMetricsData"]
  1998. response = self.do_request(
  1999. {
  2000. "field": [
  2001. "transaction",
  2002. "p50()",
  2003. ],
  2004. "query": "has:transaction.status",
  2005. "dataset": "metrics",
  2006. }
  2007. )
  2008. assert response.status_code == 200, response.content
  2009. data = response.data["data"]
  2010. assert len(data) == 1
  2011. assert data[0]["p50()"] == 1
  2012. meta = response.data["meta"]
  2013. assert meta["isMetricsData"]
  2014. def test_not_has_filter(self):
  2015. self.store_transaction_metric(
  2016. 1,
  2017. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  2018. timestamp=self.min_ago,
  2019. )
  2020. response = self.do_request(
  2021. {
  2022. "field": [
  2023. "transaction",
  2024. "p50()",
  2025. ],
  2026. "query": "!has:transaction.status",
  2027. "dataset": "metrics",
  2028. }
  2029. )
  2030. assert response.status_code == 200, response.content
  2031. data = response.data["data"]
  2032. assert len(data) == 0
  2033. meta = response.data["meta"]
  2034. assert meta["isMetricsData"]
  2035. response = self.do_request(
  2036. {
  2037. "field": [
  2038. "transaction",
  2039. "p50()",
  2040. ],
  2041. # Doing !has on the metrics dataset doesn't really make sense
  2042. "query": "!has:measurements.frames_frozen_rate",
  2043. "dataset": "metrics",
  2044. }
  2045. )
  2046. assert response.status_code == 400, response.content
  2047. def test_p50_with_count(self):
  2048. """Implicitly test the fact that percentiles are their own 'dataset'"""
  2049. self.store_transaction_metric(
  2050. 1,
  2051. tags={"transaction": "foo_transaction"},
  2052. timestamp=self.min_ago,
  2053. )
  2054. response = self.do_request(
  2055. {
  2056. "field": ["title", "p50()", "count()"],
  2057. "query": "event.type:transaction",
  2058. "dataset": "metrics",
  2059. "project": self.project.id,
  2060. "per_page": 50,
  2061. }
  2062. )
  2063. assert response.status_code == 200, response.content
  2064. assert len(response.data["data"]) == 1
  2065. data = response.data["data"]
  2066. meta = response.data["meta"]
  2067. field_meta = meta["fields"]
  2068. assert data[0]["title"] == "foo_transaction"
  2069. assert data[0]["p50()"] == 1
  2070. assert data[0]["count()"] == 1
  2071. assert meta["isMetricsData"]
  2072. assert field_meta["title"] == "string"
  2073. assert field_meta["p50()"] == "duration"
  2074. assert field_meta["count()"] == "integer"
  2075. def test_p75_with_count_and_more_groupby(self):
  2076. """Implicitly test the fact that percentiles are their own 'dataset'"""
  2077. self.store_transaction_metric(
  2078. 1,
  2079. tags={"transaction": "foo_transaction"},
  2080. timestamp=self.min_ago,
  2081. )
  2082. self.store_transaction_metric(
  2083. 5,
  2084. tags={"transaction": "bar_transaction"},
  2085. timestamp=self.min_ago,
  2086. )
  2087. self.store_transaction_metric(
  2088. 5,
  2089. tags={"transaction": "bar_transaction"},
  2090. timestamp=self.min_ago,
  2091. )
  2092. response = self.do_request(
  2093. {
  2094. "field": [
  2095. "title",
  2096. "project",
  2097. "p75()",
  2098. "count()",
  2099. ],
  2100. "query": "event.type:transaction",
  2101. "orderby": "count()",
  2102. "dataset": "metrics",
  2103. "project": self.project.id,
  2104. "per_page": 50,
  2105. }
  2106. )
  2107. assert response.status_code == 200, response.content
  2108. assert len(response.data["data"]) == 2
  2109. data = response.data["data"]
  2110. meta = response.data["meta"]
  2111. field_meta = meta["fields"]
  2112. assert data[0]["title"] == "foo_transaction"
  2113. assert data[0]["p75()"] == 1
  2114. assert data[0]["count()"] == 1
  2115. assert data[1]["title"] == "bar_transaction"
  2116. assert data[1]["p75()"] == 5
  2117. assert data[1]["count()"] == 2
  2118. assert meta["isMetricsData"]
  2119. assert field_meta["title"] == "string"
  2120. assert field_meta["p75()"] == "duration"
  2121. assert field_meta["count()"] == "integer"
  2122. def test_title_and_transaction_alias(self):
  2123. # Title and transaction are aliases to the same column
  2124. self.store_transaction_metric(
  2125. 1,
  2126. tags={"transaction": "foo_transaction"},
  2127. timestamp=self.min_ago,
  2128. )
  2129. response = self.do_request(
  2130. {
  2131. "field": [
  2132. "title",
  2133. "transaction",
  2134. "p75()",
  2135. ],
  2136. "query": "event.type:transaction",
  2137. "orderby": "p75()",
  2138. "dataset": "metrics",
  2139. "project": self.project.id,
  2140. "per_page": 50,
  2141. }
  2142. )
  2143. assert response.status_code == 200, response.content
  2144. assert len(response.data["data"]) == 1
  2145. data = response.data["data"]
  2146. meta = response.data["meta"]
  2147. field_meta = meta["fields"]
  2148. assert data[0]["title"] == "foo_transaction"
  2149. assert data[0]["transaction"] == "foo_transaction"
  2150. assert data[0]["p75()"] == 1
  2151. assert meta["isMetricsData"]
  2152. assert field_meta["title"] == "string"
  2153. assert field_meta["transaction"] == "string"
  2154. assert field_meta["p75()"] == "duration"
  2155. def test_maintain_sort_order_across_datasets(self):
  2156. self.store_transaction_metric(
  2157. 1,
  2158. tags={"transaction": "foo_transaction"},
  2159. timestamp=self.min_ago,
  2160. )
  2161. self.store_transaction_metric(
  2162. 1,
  2163. metric="user",
  2164. tags={"transaction": "bar_transaction"},
  2165. timestamp=self.min_ago,
  2166. )
  2167. self.store_transaction_metric(
  2168. 5,
  2169. tags={"transaction": "bar_transaction"},
  2170. timestamp=self.min_ago,
  2171. )
  2172. self.store_transaction_metric(
  2173. 5,
  2174. tags={"transaction": "bar_transaction"},
  2175. timestamp=self.min_ago,
  2176. )
  2177. response = self.do_request(
  2178. {
  2179. "field": [
  2180. "title",
  2181. "project",
  2182. "count()",
  2183. "count_unique(user)",
  2184. ],
  2185. "query": "event.type:transaction",
  2186. "orderby": "count()",
  2187. "dataset": "metrics",
  2188. "project": self.project.id,
  2189. "per_page": 50,
  2190. }
  2191. )
  2192. assert response.status_code == 200, response.content
  2193. data = response.data["data"]
  2194. meta = response.data["meta"]
  2195. field_meta = meta["fields"]
  2196. assert len(data) == 2
  2197. assert data[0]["title"] == "foo_transaction"
  2198. assert data[0]["count()"] == 1
  2199. assert data[0]["count_unique(user)"] == 0
  2200. assert data[1]["title"] == "bar_transaction"
  2201. assert data[1]["count()"] == 2
  2202. assert data[1]["count_unique(user)"] == 1
  2203. assert meta["isMetricsData"]
  2204. assert field_meta["title"] == "string"
  2205. assert field_meta["count()"] == "integer"
  2206. assert field_meta["count_unique(user)"] == "integer"
  2207. def test_avg_compare(self):
  2208. self.store_transaction_metric(
  2209. 100,
  2210. timestamp=self.min_ago,
  2211. tags={"release": "foo"},
  2212. )
  2213. self.store_transaction_metric(
  2214. 10,
  2215. timestamp=self.min_ago,
  2216. tags={"release": "bar"},
  2217. )
  2218. for function_name in [
  2219. "avg_compare(transaction.duration, release, foo, bar)",
  2220. 'avg_compare(transaction.duration, release, "foo", "bar")',
  2221. ]:
  2222. response = self.do_request(
  2223. {
  2224. "field": [function_name],
  2225. "query": "",
  2226. "project": self.project.id,
  2227. "dataset": "metrics",
  2228. }
  2229. )
  2230. assert response.status_code == 200, response.content
  2231. data = response.data["data"]
  2232. meta = response.data["meta"]
  2233. assert len(data) == 1
  2234. assert data[0][function_name] == -0.9
  2235. assert meta["dataset"] == "metrics"
  2236. assert meta["fields"][function_name] == "percent_change"
  2237. def test_avg_if(self):
  2238. self.store_transaction_metric(
  2239. 100,
  2240. timestamp=self.min_ago,
  2241. tags={"release": "foo"},
  2242. )
  2243. self.store_transaction_metric(
  2244. 200,
  2245. timestamp=self.min_ago,
  2246. tags={"release": "foo"},
  2247. )
  2248. self.store_transaction_metric(
  2249. 10,
  2250. timestamp=self.min_ago,
  2251. tags={"release": "bar"},
  2252. )
  2253. response = self.do_request(
  2254. {
  2255. "field": ["avg_if(transaction.duration, release, foo)"],
  2256. "query": "",
  2257. "project": self.project.id,
  2258. "dataset": "metrics",
  2259. }
  2260. )
  2261. assert response.status_code == 200, response.content
  2262. data = response.data["data"]
  2263. meta = response.data["meta"]
  2264. assert len(data) == 1
  2265. assert data[0]["avg_if(transaction.duration, release, foo)"] == 150
  2266. assert meta["dataset"] == "metrics"
  2267. assert meta["fields"]["avg_if(transaction.duration, release, foo)"] == "duration"
  2268. def test_device_class(self):
  2269. self.store_transaction_metric(
  2270. 100,
  2271. timestamp=self.min_ago,
  2272. tags={"device.class": "1"},
  2273. )
  2274. self.store_transaction_metric(
  2275. 200,
  2276. timestamp=self.min_ago,
  2277. tags={"device.class": "2"},
  2278. )
  2279. self.store_transaction_metric(
  2280. 300,
  2281. timestamp=self.min_ago,
  2282. tags={"device.class": ""},
  2283. )
  2284. response = self.do_request(
  2285. {
  2286. "field": ["device.class", "p95()"],
  2287. "query": "",
  2288. "orderby": "p95()",
  2289. "project": self.project.id,
  2290. "dataset": "metrics",
  2291. }
  2292. )
  2293. assert response.status_code == 200, response.content
  2294. data = response.data["data"]
  2295. meta = response.data["meta"]
  2296. assert len(data) == 3
  2297. # Need to actually check the dict since the level for 1 isn't guaranteed to stay `low` or `medium`
  2298. assert data[0]["device.class"] == map_device_class_level("1")
  2299. assert data[1]["device.class"] == map_device_class_level("2")
  2300. assert data[2]["device.class"] == "Unknown"
  2301. assert meta["fields"]["device.class"] == "string"
  2302. def test_device_class_filter(self):
  2303. self.store_transaction_metric(
  2304. 300,
  2305. timestamp=self.min_ago,
  2306. tags={"device.class": "1"},
  2307. )
  2308. # Need to actually check the dict since the level for 1 isn't guaranteed to stay `low`
  2309. level = map_device_class_level("1")
  2310. response = self.do_request(
  2311. {
  2312. "field": ["device.class", "count()"],
  2313. "query": f"device.class:{level}",
  2314. "orderby": "count()",
  2315. "project": self.project.id,
  2316. "dataset": "metrics",
  2317. }
  2318. )
  2319. assert response.status_code == 200, response.content
  2320. data = response.data["data"]
  2321. meta = response.data["meta"]
  2322. assert len(data) == 1
  2323. assert data[0]["device.class"] == level
  2324. assert meta["fields"]["device.class"] == "string"
  2325. def test_performance_score(self):
  2326. self.store_transaction_metric(
  2327. 0.03,
  2328. metric="measurements.score.lcp",
  2329. tags={"transaction": "foo_transaction"},
  2330. timestamp=self.min_ago,
  2331. )
  2332. self.store_transaction_metric(
  2333. 0.30,
  2334. metric="measurements.score.weight.lcp",
  2335. tags={"transaction": "foo_transaction"},
  2336. timestamp=self.min_ago,
  2337. )
  2338. self.store_transaction_metric(
  2339. 0.35,
  2340. metric="measurements.score.fcp",
  2341. tags={"transaction": "foo_transaction"},
  2342. timestamp=self.min_ago,
  2343. )
  2344. self.store_transaction_metric(
  2345. 0.70,
  2346. metric="measurements.score.weight.fcp",
  2347. tags={"transaction": "foo_transaction"},
  2348. timestamp=self.min_ago,
  2349. )
  2350. self.store_transaction_metric(
  2351. 0.38,
  2352. metric="measurements.score.total",
  2353. tags={"transaction": "foo_transaction"},
  2354. timestamp=self.min_ago,
  2355. )
  2356. self.store_transaction_metric(
  2357. 1.00,
  2358. metric="measurements.score.lcp",
  2359. tags={"transaction": "foo_transaction"},
  2360. timestamp=self.min_ago,
  2361. )
  2362. self.store_transaction_metric(
  2363. 1.00,
  2364. metric="measurements.score.weight.lcp",
  2365. tags={"transaction": "foo_transaction"},
  2366. timestamp=self.min_ago,
  2367. )
  2368. self.store_transaction_metric(
  2369. 0.00,
  2370. metric="measurements.score.fid",
  2371. tags={"transaction": "foo_transaction"},
  2372. timestamp=self.min_ago,
  2373. )
  2374. # These fid and ttfb scenarios shouldn't really be happening, but we can test them anyways
  2375. self.store_transaction_metric(
  2376. 0.00,
  2377. metric="measurements.score.weight.fid",
  2378. tags={"transaction": "foo_transaction"},
  2379. timestamp=self.min_ago,
  2380. )
  2381. self.store_transaction_metric(
  2382. 1.00,
  2383. metric="measurements.score.ttfb",
  2384. tags={"transaction": "foo_transaction"},
  2385. timestamp=self.min_ago,
  2386. )
  2387. self.store_transaction_metric(
  2388. 0.00,
  2389. metric="measurements.score.weight.ttfb",
  2390. tags={"transaction": "foo_transaction"},
  2391. timestamp=self.min_ago,
  2392. )
  2393. self.store_transaction_metric(
  2394. 1.00,
  2395. metric="measurements.score.total",
  2396. tags={"transaction": "foo_transaction"},
  2397. timestamp=self.min_ago,
  2398. )
  2399. # INP metrics
  2400. self.store_transaction_metric(
  2401. 0.80,
  2402. metric="measurements.score.inp",
  2403. tags={"transaction": "foo_transaction"},
  2404. timestamp=self.min_ago,
  2405. )
  2406. self.store_transaction_metric(
  2407. 1.00,
  2408. metric="measurements.score.weight.inp",
  2409. tags={"transaction": "foo_transaction"},
  2410. timestamp=self.min_ago,
  2411. )
  2412. self.store_transaction_metric(
  2413. 0.80,
  2414. metric="measurements.score.total",
  2415. tags={"transaction": "foo_transaction"},
  2416. timestamp=self.min_ago,
  2417. )
  2418. response = self.do_request(
  2419. {
  2420. "field": [
  2421. "transaction",
  2422. "performance_score(measurements.score.lcp)",
  2423. "performance_score(measurements.score.fcp)",
  2424. "performance_score(measurements.score.fid)",
  2425. "performance_score(measurements.score.ttfb)",
  2426. "performance_score(measurements.score.inp)",
  2427. ],
  2428. "query": "event.type:transaction",
  2429. "dataset": "metrics",
  2430. "per_page": 50,
  2431. }
  2432. )
  2433. assert response.status_code == 200, response.content
  2434. assert len(response.data["data"]) == 1
  2435. data = response.data["data"]
  2436. meta = response.data["meta"]
  2437. field_meta = meta["fields"]
  2438. assert data[0]["performance_score(measurements.score.lcp)"] == 0.7923076923076923
  2439. assert data[0]["performance_score(measurements.score.fcp)"] == 0.5
  2440. assert data[0]["performance_score(measurements.score.fid)"] == 0
  2441. assert data[0]["performance_score(measurements.score.ttfb)"] == 0
  2442. assert data[0]["performance_score(measurements.score.inp)"] == 0.8
  2443. assert meta["isMetricsData"]
  2444. assert field_meta["performance_score(measurements.score.lcp)"] == "number"
  2445. def test_performance_score_boundaries(self):
  2446. # Scores shouldn't exceed 1 or go below 0, but we can test these boundaries anyways
  2447. self.store_transaction_metric(
  2448. 0.65,
  2449. metric="measurements.score.lcp",
  2450. tags={"transaction": "foo_transaction"},
  2451. timestamp=self.min_ago,
  2452. )
  2453. self.store_transaction_metric(
  2454. 0.30,
  2455. metric="measurements.score.weight.lcp",
  2456. tags={"transaction": "foo_transaction"},
  2457. timestamp=self.min_ago,
  2458. )
  2459. self.store_transaction_metric(
  2460. -0.35,
  2461. metric="measurements.score.fcp",
  2462. tags={"transaction": "foo_transaction"},
  2463. timestamp=self.min_ago,
  2464. )
  2465. self.store_transaction_metric(
  2466. 0.70,
  2467. metric="measurements.score.weight.fcp",
  2468. tags={"transaction": "foo_transaction"},
  2469. timestamp=self.min_ago,
  2470. )
  2471. self.store_transaction_metric(
  2472. 0.3,
  2473. metric="measurements.score.total",
  2474. tags={"transaction": "foo_transaction"},
  2475. timestamp=self.min_ago,
  2476. )
  2477. response = self.do_request(
  2478. {
  2479. "field": [
  2480. "transaction",
  2481. "performance_score(measurements.score.lcp)",
  2482. "performance_score(measurements.score.fcp)",
  2483. ],
  2484. "query": "event.type:transaction",
  2485. "dataset": "metrics",
  2486. "per_page": 50,
  2487. }
  2488. )
  2489. assert response.status_code == 200, response.content
  2490. assert len(response.data["data"]) == 1
  2491. data = response.data["data"]
  2492. meta = response.data["meta"]
  2493. field_meta = meta["fields"]
  2494. assert data[0]["performance_score(measurements.score.lcp)"] == 1.0
  2495. assert data[0]["performance_score(measurements.score.fcp)"] == 0.0
  2496. assert meta["isMetricsData"]
  2497. assert field_meta["performance_score(measurements.score.lcp)"] == "number"
  2498. def test_weighted_performance_score(self):
  2499. self.store_transaction_metric(
  2500. 0.03,
  2501. metric="measurements.score.lcp",
  2502. tags={"transaction": "foo_transaction"},
  2503. timestamp=self.min_ago,
  2504. )
  2505. self.store_transaction_metric(
  2506. 0.30,
  2507. metric="measurements.score.weight.lcp",
  2508. tags={"transaction": "foo_transaction"},
  2509. timestamp=self.min_ago,
  2510. )
  2511. self.store_transaction_metric(
  2512. 0.03,
  2513. metric="measurements.score.total",
  2514. tags={"transaction": "foo_transaction"},
  2515. timestamp=self.min_ago,
  2516. )
  2517. self.store_transaction_metric(
  2518. 1.00,
  2519. metric="measurements.score.lcp",
  2520. tags={"transaction": "foo_transaction"},
  2521. timestamp=self.min_ago,
  2522. )
  2523. self.store_transaction_metric(
  2524. 1.00,
  2525. metric="measurements.score.weight.lcp",
  2526. tags={"transaction": "foo_transaction"},
  2527. timestamp=self.min_ago,
  2528. )
  2529. self.store_transaction_metric(
  2530. 1.00,
  2531. metric="measurements.score.total",
  2532. tags={"transaction": "foo_transaction"},
  2533. timestamp=self.min_ago,
  2534. )
  2535. self.store_transaction_metric(
  2536. 0.00,
  2537. metric="measurements.score.total",
  2538. tags={"transaction": "foo_transaction"},
  2539. timestamp=self.min_ago,
  2540. )
  2541. self.store_transaction_metric(
  2542. 0.80,
  2543. metric="measurements.score.inp",
  2544. tags={"transaction": "foo_transaction"},
  2545. timestamp=self.min_ago,
  2546. )
  2547. self.store_transaction_metric(
  2548. 1.00,
  2549. metric="measurements.score.weight.lcp",
  2550. tags={"transaction": "foo_transaction"},
  2551. timestamp=self.min_ago,
  2552. )
  2553. self.store_transaction_metric(
  2554. 0.80,
  2555. metric="measurements.score.total",
  2556. tags={"transaction": "foo_transaction"},
  2557. timestamp=self.min_ago,
  2558. )
  2559. response = self.do_request(
  2560. {
  2561. "field": [
  2562. "transaction",
  2563. "weighted_performance_score(measurements.score.lcp)",
  2564. "weighted_performance_score(measurements.score.inp)",
  2565. ],
  2566. "query": "event.type:transaction",
  2567. "dataset": "metrics",
  2568. "per_page": 50,
  2569. }
  2570. )
  2571. assert response.status_code == 200, response.content
  2572. assert len(response.data["data"]) == 1
  2573. data = response.data["data"]
  2574. meta = response.data["meta"]
  2575. field_meta = meta["fields"]
  2576. assert data[0]["weighted_performance_score(measurements.score.lcp)"] == 0.2575
  2577. assert data[0]["weighted_performance_score(measurements.score.inp)"] == 0.2
  2578. assert meta["isMetricsData"]
  2579. assert field_meta["weighted_performance_score(measurements.score.lcp)"] == "number"
  2580. def test_invalid_performance_score_column(self):
  2581. self.store_transaction_metric(
  2582. 0.03,
  2583. metric="measurements.score.total",
  2584. tags={"transaction": "foo_transaction"},
  2585. timestamp=self.min_ago,
  2586. )
  2587. response = self.do_request(
  2588. {
  2589. "field": [
  2590. "transaction",
  2591. "performance_score(measurements.score.fp)",
  2592. ],
  2593. "query": "event.type:transaction",
  2594. "dataset": "metrics",
  2595. "per_page": 50,
  2596. }
  2597. )
  2598. assert response.status_code == 400, response.content
  2599. def test_invalid_weighted_performance_score_column(self):
  2600. self.store_transaction_metric(
  2601. 0.03,
  2602. metric="measurements.score.total",
  2603. tags={"transaction": "foo_transaction"},
  2604. timestamp=self.min_ago,
  2605. )
  2606. response = self.do_request(
  2607. {
  2608. "field": [
  2609. "transaction",
  2610. "weighted_performance_score(measurements.score.fp)",
  2611. ],
  2612. "query": "event.type:transaction",
  2613. "dataset": "metrics",
  2614. "per_page": 50,
  2615. }
  2616. )
  2617. assert response.status_code == 400, response.content
  2618. def test_no_weighted_performance_score_column(self):
  2619. self.store_transaction_metric(
  2620. 0.0,
  2621. metric="measurements.score.lcp",
  2622. tags={"transaction": "foo_transaction"},
  2623. timestamp=self.min_ago,
  2624. )
  2625. response = self.do_request(
  2626. {
  2627. "field": [
  2628. "transaction",
  2629. "weighted_performance_score(measurements.score.lcp)",
  2630. ],
  2631. "query": "event.type:transaction",
  2632. "dataset": "metrics",
  2633. "per_page": 50,
  2634. }
  2635. )
  2636. assert response.status_code == 200, response.content
  2637. assert len(response.data["data"]) == 1
  2638. data = response.data["data"]
  2639. meta = response.data["meta"]
  2640. field_meta = meta["fields"]
  2641. assert data[0]["weighted_performance_score(measurements.score.lcp)"] == 0.0
  2642. assert meta["isMetricsData"]
  2643. assert field_meta["weighted_performance_score(measurements.score.lcp)"] == "number"
  2644. def test_opportunity_score(self):
  2645. self.store_transaction_metric(
  2646. 0.03,
  2647. metric="measurements.score.lcp",
  2648. tags={"transaction": "foo_transaction"},
  2649. timestamp=self.min_ago,
  2650. )
  2651. self.store_transaction_metric(
  2652. 0.30,
  2653. metric="measurements.score.weight.lcp",
  2654. tags={"transaction": "foo_transaction"},
  2655. timestamp=self.min_ago,
  2656. )
  2657. self.store_transaction_metric(
  2658. 0.40,
  2659. metric="measurements.score.fcp",
  2660. tags={"transaction": "foo_transaction"},
  2661. timestamp=self.min_ago,
  2662. )
  2663. self.store_transaction_metric(
  2664. 0.70,
  2665. metric="measurements.score.weight.fcp",
  2666. tags={"transaction": "foo_transaction"},
  2667. timestamp=self.min_ago,
  2668. )
  2669. self.store_transaction_metric(
  2670. 0.43,
  2671. metric="measurements.score.total",
  2672. tags={"transaction": "foo_transaction"},
  2673. timestamp=self.min_ago,
  2674. )
  2675. self.store_transaction_metric(
  2676. 1.0,
  2677. metric="measurements.score.lcp",
  2678. tags={"transaction": "foo_transaction"},
  2679. timestamp=self.min_ago,
  2680. )
  2681. self.store_transaction_metric(
  2682. 1.0,
  2683. metric="measurements.score.weight.lcp",
  2684. tags={"transaction": "foo_transaction"},
  2685. timestamp=self.min_ago,
  2686. )
  2687. self.store_transaction_metric(
  2688. 1.0,
  2689. metric="measurements.score.total",
  2690. tags={"transaction": "foo_transaction"},
  2691. timestamp=self.min_ago,
  2692. )
  2693. self.store_transaction_metric(
  2694. 0.0,
  2695. metric="measurements.score.total",
  2696. tags={"transaction": "foo_transaction"},
  2697. timestamp=self.min_ago,
  2698. )
  2699. self.store_transaction_metric(
  2700. 0.80,
  2701. metric="measurements.score.inp",
  2702. tags={"transaction": "foo_transaction"},
  2703. timestamp=self.min_ago,
  2704. )
  2705. self.store_transaction_metric(
  2706. 1.00,
  2707. metric="measurements.score.weight.inp",
  2708. tags={"transaction": "foo_transaction"},
  2709. timestamp=self.min_ago,
  2710. )
  2711. self.store_transaction_metric(
  2712. 0.80,
  2713. metric="measurements.score.total",
  2714. tags={"transaction": "foo_transaction"},
  2715. timestamp=self.min_ago,
  2716. )
  2717. response = self.do_request(
  2718. {
  2719. "field": [
  2720. "transaction",
  2721. "opportunity_score(measurements.score.lcp)",
  2722. "opportunity_score(measurements.score.inp)",
  2723. "opportunity_score(measurements.score.total)",
  2724. ],
  2725. "query": "event.type:transaction",
  2726. "dataset": "metrics",
  2727. "per_page": 50,
  2728. }
  2729. )
  2730. assert response.status_code == 200, response.content
  2731. assert len(response.data["data"]) == 1
  2732. data = response.data["data"]
  2733. meta = response.data["meta"]
  2734. assert data[0]["opportunity_score(measurements.score.lcp)"] == 0.27
  2735. # Should be 0.2. Precision issue?
  2736. assert data[0]["opportunity_score(measurements.score.inp)"] == 0.19999999999999996
  2737. assert data[0]["opportunity_score(measurements.score.total)"] == 1.77
  2738. assert meta["isMetricsData"]
  2739. def test_count_scores(self):
  2740. self.store_transaction_metric(
  2741. 0.1,
  2742. metric="measurements.score.total",
  2743. tags={"transaction": "foo_transaction"},
  2744. timestamp=self.min_ago,
  2745. )
  2746. self.store_transaction_metric(
  2747. 0.2,
  2748. metric="measurements.score.total",
  2749. tags={"transaction": "foo_transaction"},
  2750. timestamp=self.min_ago,
  2751. )
  2752. self.store_transaction_metric(
  2753. 0.3,
  2754. metric="measurements.score.total",
  2755. tags={"transaction": "foo_transaction"},
  2756. timestamp=self.min_ago,
  2757. )
  2758. self.store_transaction_metric(
  2759. 0.4,
  2760. metric="measurements.score.total",
  2761. tags={"transaction": "foo_transaction"},
  2762. timestamp=self.min_ago,
  2763. )
  2764. self.store_transaction_metric(
  2765. 0.5,
  2766. metric="measurements.score.lcp",
  2767. tags={"transaction": "foo_transaction"},
  2768. timestamp=self.min_ago,
  2769. )
  2770. self.store_transaction_metric(
  2771. 0.8,
  2772. metric="measurements.score.inp",
  2773. tags={"transaction": "foo_transaction"},
  2774. timestamp=self.min_ago,
  2775. )
  2776. response = self.do_request(
  2777. {
  2778. "field": [
  2779. "transaction",
  2780. "count_scores(measurements.score.total)",
  2781. "count_scores(measurements.score.lcp)",
  2782. "count_scores(measurements.score.inp)",
  2783. ],
  2784. "query": "event.type:transaction",
  2785. "dataset": "metrics",
  2786. "per_page": 50,
  2787. }
  2788. )
  2789. assert response.status_code == 200, response.content
  2790. assert len(response.data["data"]) == 1
  2791. data = response.data["data"]
  2792. meta = response.data["meta"]
  2793. assert data[0]["count_scores(measurements.score.total)"] == 4
  2794. assert data[0]["count_scores(measurements.score.lcp)"] == 1
  2795. assert data[0]["count_scores(measurements.score.inp)"] == 1
  2796. assert meta["isMetricsData"]
  2797. def test_count_starts(self):
  2798. self.store_transaction_metric(
  2799. 200,
  2800. metric="measurements.app_start_warm",
  2801. tags={"transaction": "foo_transaction"},
  2802. timestamp=self.min_ago,
  2803. )
  2804. self.store_transaction_metric(
  2805. 100,
  2806. metric="measurements.app_start_warm",
  2807. tags={"transaction": "foo_transaction"},
  2808. timestamp=self.min_ago,
  2809. )
  2810. self.store_transaction_metric(
  2811. 10,
  2812. metric="measurements.app_start_cold",
  2813. tags={"transaction": "foo_transaction"},
  2814. timestamp=self.min_ago,
  2815. )
  2816. response = self.do_request(
  2817. {
  2818. "field": [
  2819. "transaction",
  2820. "count_starts(measurements.app_start_warm)",
  2821. "count_starts(measurements.app_start_cold)",
  2822. ],
  2823. "query": "event.type:transaction",
  2824. "dataset": "metrics",
  2825. "per_page": 50,
  2826. }
  2827. )
  2828. assert response.status_code == 200, response.content
  2829. assert len(response.data["data"]) == 1
  2830. data = response.data["data"]
  2831. meta = response.data["meta"]
  2832. assert data[0]["count_starts(measurements.app_start_warm)"] == 2
  2833. assert data[0]["count_starts(measurements.app_start_cold)"] == 1
  2834. assert meta["isMetricsData"]
  2835. def test_count_starts_returns_all_counts_when_no_arg_is_passed(self):
  2836. self.store_transaction_metric(
  2837. 200,
  2838. metric="measurements.app_start_warm",
  2839. tags={"transaction": "foo_transaction"},
  2840. timestamp=self.min_ago,
  2841. )
  2842. self.store_transaction_metric(
  2843. 100,
  2844. metric="measurements.app_start_warm",
  2845. tags={"transaction": "foo_transaction"},
  2846. timestamp=self.min_ago,
  2847. )
  2848. self.store_transaction_metric(
  2849. 10,
  2850. metric="measurements.app_start_cold",
  2851. tags={"transaction": "foo_transaction"},
  2852. timestamp=self.min_ago,
  2853. )
  2854. response = self.do_request(
  2855. {
  2856. "field": [
  2857. "transaction",
  2858. "count_total_starts()",
  2859. ],
  2860. "query": "event.type:transaction",
  2861. "dataset": "metrics",
  2862. "per_page": 50,
  2863. }
  2864. )
  2865. assert response.status_code == 200, response.content
  2866. assert len(response.data["data"]) == 1
  2867. data = response.data["data"]
  2868. meta = response.data["meta"]
  2869. assert data[0]["count_total_starts()"] == 3
  2870. assert meta["isMetricsData"]
  2871. def test_timestamp_groupby(self):
  2872. self.store_transaction_metric(
  2873. 0.03,
  2874. tags={"transaction": "foo_transaction", "user": "foo"},
  2875. timestamp=self.min_ago,
  2876. )
  2877. response = self.do_request(
  2878. {
  2879. "field": [
  2880. "transaction",
  2881. "timestamp",
  2882. "count()",
  2883. "count_unique(user)",
  2884. ],
  2885. "query": "event.type:transaction",
  2886. "dataset": "metricsEnhanced",
  2887. "per_page": 50,
  2888. }
  2889. )
  2890. assert response.status_code == 200, response.content
  2891. assert len(response.data["data"]) == 1
  2892. data = response.data["data"]
  2893. meta = response.data["meta"]
  2894. assert data[0]["transaction"] == "foo_transaction"
  2895. assert meta["dataset"] == "metricsEnhanced"
  2896. def test_on_demand_with_mep(self):
  2897. # Store faketag as an OnDemandMetricSpec, which will put faketag into the metrics indexer
  2898. spec = OnDemandMetricSpec(
  2899. field="count()",
  2900. query="user.email:blah@example.com",
  2901. environment="prod",
  2902. groupbys=["faketag"],
  2903. spec_type=MetricSpecType.DYNAMIC_QUERY,
  2904. )
  2905. self.store_on_demand_metric(123, spec=spec)
  2906. # This is the event that we should actually return
  2907. transaction_data = load_data("transaction", timestamp=self.min_ago)
  2908. transaction_data["tags"].append(("faketag", "foo"))
  2909. self.store_event(transaction_data, self.project.id)
  2910. with self.feature({"organizations:mep-use-default-tags": True}):
  2911. response = self.do_request(
  2912. {
  2913. "field": [
  2914. "faketag",
  2915. "count()",
  2916. ],
  2917. "query": "event.type:transaction",
  2918. "dataset": "metricsEnhanced",
  2919. "per_page": 50,
  2920. }
  2921. )
  2922. assert response.status_code == 200, response.content
  2923. assert len(response.data["data"]) == 1
  2924. data = response.data["data"]
  2925. meta = response.data["meta"]
  2926. assert data[0]["faketag"] == "foo"
  2927. assert not meta["isMetricsData"]
  2928. class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithOnDemandMetrics(
  2929. MetricsEnhancedPerformanceTestCase
  2930. ):
  2931. viewname = "sentry-api-0-organization-events"
  2932. def setUp(self) -> None:
  2933. super().setUp()
  2934. self.url = reverse(self.viewname, kwargs={"organization_slug": self.organization.slug})
  2935. self.features = {"organizations:on-demand-metrics-extraction-widgets": True}
  2936. def _create_specs(
  2937. self, params: dict[str, Any], groupbys: list[str] | None = None
  2938. ) -> list[OnDemandMetricSpec]:
  2939. """Creates all specs based on the parameters that would be passed to the endpoint."""
  2940. specs = []
  2941. for field in params["field"]:
  2942. spec = OnDemandMetricSpec(
  2943. field=field,
  2944. query=params["query"],
  2945. environment=params.get("environment"),
  2946. groupbys=groupbys,
  2947. spec_type=MetricSpecType.DYNAMIC_QUERY,
  2948. )
  2949. specs.append(spec)
  2950. return specs
  2951. def _make_on_demand_request(
  2952. self, params: dict[str, Any], extra_features: dict[str, bool] | None = None
  2953. ) -> Response:
  2954. """Ensures that the required parameters for an on-demand request are included."""
  2955. # Expected parameters for this helper function
  2956. params["dataset"] = "metricsEnhanced"
  2957. params["useOnDemandMetrics"] = "true"
  2958. params["onDemandType"] = "dynamic_query"
  2959. _features = {**self.features, **(extra_features or {})}
  2960. return self.do_request(params, features=_features)
  2961. def _assert_on_demand_response(
  2962. self,
  2963. response: Response,
  2964. expected_on_demand_query: bool | None = True,
  2965. expected_dataset: str | None = "metricsEnhanced",
  2966. ) -> None:
  2967. """Basic assertions for an on-demand request."""
  2968. assert response.status_code == 200, response.content
  2969. meta = response.data["meta"]
  2970. assert meta.get("isMetricsExtractedData", False) is expected_on_demand_query
  2971. assert meta["dataset"] == expected_dataset
  2972. def test_is_metrics_extracted_data_is_included(self) -> None:
  2973. params = {"field": ["count()"], "query": "transaction.duration:>=91", "yAxis": "count()"}
  2974. specs = self._create_specs(params)
  2975. for spec in specs:
  2976. self.store_on_demand_metric(1, spec=spec)
  2977. response = self._make_on_demand_request(params)
  2978. self._assert_on_demand_response(response)
  2979. def test_on_demand_user_misery(self) -> None:
  2980. user_misery_field = "user_misery(300)"
  2981. query = "transaction.duration:>=100"
  2982. # We store data for both specs, however, when the query builders try to query
  2983. # for the data it will not query on-demand data
  2984. for spec_version in OnDemandMetricSpecVersioning.get_spec_versions():
  2985. spec = OnDemandMetricSpec(
  2986. field=user_misery_field,
  2987. query=query,
  2988. spec_type=MetricSpecType.DYNAMIC_QUERY,
  2989. # We only allow querying the function in the latest spec version,
  2990. # otherwise, the data returned by the endpoint would be 0.05
  2991. spec_version=spec_version,
  2992. )
  2993. tags = {"satisfaction": "miserable"}
  2994. self.store_on_demand_metric(1, spec=spec, additional_tags=tags, timestamp=self.min_ago)
  2995. self.store_on_demand_metric(2, spec=spec, timestamp=self.min_ago)
  2996. params = {"field": [user_misery_field], "project": self.project.id, "query": query}
  2997. self._create_specs(params)
  2998. # We expect it to be False because we're not using the extra feature flag
  2999. response = self._make_on_demand_request(params)
  3000. self._assert_on_demand_response(response, expected_on_demand_query=False)
  3001. # Since we're using the extra feature flag we expect user_misery to be an on-demand metric
  3002. response = self._make_on_demand_request(params, {SPEC_VERSION_TWO_FLAG: True})
  3003. self._assert_on_demand_response(response, expected_on_demand_query=True)
  3004. assert response.data["data"] == [{user_misery_field: user_misery_formula(1, 2)}]
  3005. def test_on_demand_user_misery_discover_split_with_widget_id_unsaved(self) -> None:
  3006. user_misery_field = "user_misery(300)"
  3007. query = "transaction.duration:>=100"
  3008. _, widget, __ = create_widget(["count()"], "", self.project, discover_widget_split=None)
  3009. # We store data for both specs, however, when the query builders try to query
  3010. # for the data it will not query on-demand data
  3011. for spec_version in OnDemandMetricSpecVersioning.get_spec_versions():
  3012. spec = OnDemandMetricSpec(
  3013. field=user_misery_field,
  3014. query=query,
  3015. spec_type=MetricSpecType.DYNAMIC_QUERY,
  3016. # We only allow querying the function in the latest spec version,
  3017. # otherwise, the data returned by the endpoint would be 0.05
  3018. spec_version=spec_version,
  3019. )
  3020. tags = {"satisfaction": "miserable"}
  3021. self.store_on_demand_metric(1, spec=spec, additional_tags=tags, timestamp=self.min_ago)
  3022. self.store_on_demand_metric(2, spec=spec, timestamp=self.min_ago)
  3023. params = {"field": [user_misery_field], "project": self.project.id, "query": query}
  3024. self._create_specs(params)
  3025. params["dashboardWidgetId"] = widget.id
  3026. # Since we're using the extra feature flag we expect user_misery to be an on-demand metric
  3027. with mock.patch.object(widget, "save") as mock_widget_save:
  3028. response = self._make_on_demand_request(params, {SPEC_VERSION_TWO_FLAG: True})
  3029. assert bool(mock_widget_save.assert_called_once)
  3030. self._assert_on_demand_response(response, expected_on_demand_query=True)
  3031. assert response.data["data"] == [{user_misery_field: user_misery_formula(1, 2)}]
  3032. def test_on_demand_user_misery_discover_split_with_widget_id_saved(self) -> None:
  3033. user_misery_field = "user_misery(300)"
  3034. query = "transaction.duration:>=100"
  3035. _, widget, __ = create_widget(
  3036. ["count()"],
  3037. "",
  3038. self.project,
  3039. discover_widget_split=DashboardWidgetTypes.TRANSACTION_LIKE, # Transactions like uses on-demand
  3040. )
  3041. # We store data for both specs, however, when the query builders try to query
  3042. # for the data it will not query on-demand data
  3043. for spec_version in OnDemandMetricSpecVersioning.get_spec_versions():
  3044. spec = OnDemandMetricSpec(
  3045. field=user_misery_field,
  3046. query=query,
  3047. spec_type=MetricSpecType.DYNAMIC_QUERY,
  3048. # We only allow querying the function in the latest spec version,
  3049. # otherwise, the data returned by the endpoint would be 0.05
  3050. spec_version=spec_version,
  3051. )
  3052. tags = {"satisfaction": "miserable"}
  3053. self.store_on_demand_metric(1, spec=spec, additional_tags=tags, timestamp=self.min_ago)
  3054. self.store_on_demand_metric(2, spec=spec, timestamp=self.min_ago)
  3055. params = {"field": [user_misery_field], "project": self.project.id, "query": query}
  3056. self._create_specs(params)
  3057. params["dashboardWidgetId"] = widget.id
  3058. # Since we're using the extra feature flag we expect user_misery to be an on-demand metric
  3059. with mock.patch.object(widget, "save") as mock_widget_save:
  3060. response = self._make_on_demand_request(params, {SPEC_VERSION_TWO_FLAG: True})
  3061. assert bool(mock_widget_save.assert_not_called)
  3062. self._assert_on_demand_response(response, expected_on_demand_query=True)
  3063. assert response.data["data"] == [{user_misery_field: user_misery_formula(1, 2)}]
  3064. def test_on_demand_count_unique(self):
  3065. field = "count_unique(user)"
  3066. query = "transaction.duration:>0"
  3067. params = {"field": [field], "query": query}
  3068. # We do not really have to create the metrics for both specs since
  3069. # the first API call will not query any on-demand metric
  3070. for spec_version in OnDemandMetricSpecVersioning.get_spec_versions():
  3071. spec = OnDemandMetricSpec(
  3072. field=field,
  3073. query=query,
  3074. spec_type=MetricSpecType.DYNAMIC_QUERY,
  3075. spec_version=spec_version,
  3076. )
  3077. self.store_on_demand_metric(1, spec=spec, timestamp=self.min_ago)
  3078. self.store_on_demand_metric(2, spec=spec, timestamp=self.min_ago)
  3079. # The first call will not be on-demand
  3080. response = self._make_on_demand_request(params)
  3081. self._assert_on_demand_response(response, expected_on_demand_query=False)
  3082. # This second call will be on-demand
  3083. response = self._make_on_demand_request(
  3084. params, extra_features={SPEC_VERSION_TWO_FLAG: True}
  3085. )
  3086. self._assert_on_demand_response(response, expected_on_demand_query=True)
  3087. assert response.data["data"] == [{"count_unique(user)": 2}]
  3088. class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithMetricLayer(
  3089. OrganizationEventsMetricsEnhancedPerformanceEndpointTest
  3090. ):
  3091. def setUp(self):
  3092. super().setUp()
  3093. self.features["organizations:use-metrics-layer"] = True
  3094. @pytest.mark.xfail(reason="Not supported")
  3095. def test_time_spent(self):
  3096. super().test_time_spent()
  3097. @pytest.mark.xfail(reason="Not supported")
  3098. def test_http_error_rate(self):
  3099. super().test_http_error_rate()
  3100. @pytest.mark.xfail(reason="Multiple aliases to same column not supported")
  3101. def test_title_and_transaction_alias(self):
  3102. super().test_title_and_transaction_alias()
  3103. @pytest.mark.xfail(reason="Sort order is flaking when querying multiple datasets")
  3104. def test_maintain_sort_order_across_datasets(self):
  3105. """You may need to run this test a few times to get it to fail"""
  3106. super().test_maintain_sort_order_across_datasets()
  3107. @pytest.mark.xfail(reason="Not implemented")
  3108. def test_avg_compare(self):
  3109. super().test_avg_compare()
  3110. @pytest.mark.xfail(reason="Not implemented")
  3111. def test_avg_if(self):
  3112. super().test_avg_if()
  3113. @pytest.mark.xfail(reason="Not implemented")
  3114. def test_device_class(self):
  3115. super().test_device_class()
  3116. @pytest.mark.xfail(reason="Not implemented")
  3117. def test_device_class_filter(self):
  3118. super().test_device_class_filter()
  3119. @pytest.mark.xfail(reason="Not implemented")
  3120. def test_performance_score(self):
  3121. super().test_performance_score()
  3122. @pytest.mark.xfail(reason="Not implemented")
  3123. def test_performance_score_boundaries(self):
  3124. super().test_performance_score()
  3125. @pytest.mark.xfail(reason="Not implemented")
  3126. def test_weighted_performance_score(self):
  3127. super().test_weighted_performance_score()
  3128. @pytest.mark.xfail(reason="Not implemented")
  3129. def test_invalid_performance_score_column(self):
  3130. super().test_invalid_performance_score_column()
  3131. @pytest.mark.xfail(reason="Not implemented")
  3132. def test_invalid_weighted_performance_score_column(self):
  3133. super().test_invalid_weighted_performance_score_column()
  3134. @pytest.mark.xfail(reason="Not implemented")
  3135. def test_no_weighted_performance_score_column(self):
  3136. super().test_invalid_weighted_performance_score_column()
  3137. @pytest.mark.xfail(reason="Not implemented")
  3138. def test_opportunity_score(self):
  3139. super().test_opportunity_score()
  3140. @pytest.mark.xfail(reason="Not implemented")
  3141. def test_count_scores(self):
  3142. super().test_count_scores()
  3143. @pytest.mark.xfail(reason="Not implemented")
  3144. def test_count_starts(self):
  3145. super().test_count_starts()
  3146. @pytest.mark.xfail(reason="Not implemented")
  3147. def test_count_starts_returns_all_counts_when_no_arg_is_passed(self):
  3148. super().test_count_starts_returns_all_counts_when_no_arg_is_passed()
  3149. @pytest.mark.xfail(reason="Not implemented")
  3150. def test_timestamp_groupby(self):
  3151. super().test_timestamp_groupby()
  3152. @pytest.mark.xfail(reason="Not implemented")
  3153. def test_on_demand_with_mep(self):
  3154. super().test_on_demand_with_mep()