test_organization_events_mep.py 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625
  1. from unittest import mock
  2. import pytest
  3. from django.urls import reverse
  4. from sentry.api.bases.organization_events import DATASET_OPTIONS
  5. from sentry.discover.models import TeamKeyTransaction
  6. from sentry.models.projectteam import ProjectTeam
  7. from sentry.models.transaction_threshold import (
  8. ProjectTransactionThreshold,
  9. ProjectTransactionThresholdOverride,
  10. TransactionMetric,
  11. )
  12. from sentry.search.events import constants
  13. from sentry.search.utils import map_device_class_level
  14. from sentry.snuba.metrics.naming_layer.mri import TransactionMRI
  15. from sentry.snuba.metrics.naming_layer.public import TransactionMetricKey
  16. from sentry.testutils.cases import MetricsEnhancedPerformanceTestCase
  17. from sentry.testutils.helpers.datetime import before_now, iso_format
  18. from sentry.testutils.silo import region_silo_test
  19. from sentry.utils.samples import load_data
  20. pytestmark = pytest.mark.sentry_metrics
  21. @region_silo_test
  22. class OrganizationEventsMetricsEnhancedPerformanceEndpointTest(MetricsEnhancedPerformanceTestCase):
  23. viewname = "sentry-api-0-organization-events"
  24. # Poor intentionally omitted for test_measurement_rating_that_does_not_exist
  25. METRIC_STRINGS = [
  26. "foo_transaction",
  27. "bar_transaction",
  28. "baz_transaction",
  29. "staging",
  30. "measurement_rating",
  31. "good",
  32. "meh",
  33. "d:transactions/measurements.something_custom@millisecond",
  34. "d:transactions/measurements.runtime@hour",
  35. "d:transactions/measurements.bytes_transfered@byte",
  36. "d:transactions/measurements.datacenter_memory@petabyte",
  37. "d:transactions/measurements.custom.kilobyte@kilobyte",
  38. "d:transactions/measurements.longtaskcount@none",
  39. "d:transactions/measurements.percent@ratio",
  40. "d:transactions/measurements.custom_type@somethingcustom",
  41. ]
  42. def setUp(self):
  43. super().setUp()
  44. self.min_ago = before_now(minutes=1)
  45. self.two_min_ago = before_now(minutes=2)
  46. self.transaction_data = load_data("transaction", timestamp=before_now(minutes=1))
  47. self.features = {
  48. "organizations:performance-use-metrics": True,
  49. }
  50. def do_request(self, query, features=None):
  51. if features is None:
  52. features = {"organizations:discover-basic": True}
  53. features.update(self.features)
  54. self.login_as(user=self.user)
  55. url = reverse(
  56. self.viewname,
  57. kwargs={"organization_slug": self.organization.slug},
  58. )
  59. with self.feature(features):
  60. return self.client.get(url, query, format="json")
  61. def test_no_projects(self):
  62. response = self.do_request(
  63. {
  64. "dataset": "metricsEnhanced",
  65. }
  66. )
  67. assert response.status_code == 200, response.content
  68. def test_invalid_dataset(self):
  69. response = self.do_request(
  70. {
  71. "dataset": "aFakeDataset",
  72. "project": self.project.id,
  73. }
  74. )
  75. assert response.status_code == 400, response.content
  76. assert (
  77. response.data["detail"]
  78. == f"dataset must be one of: {', '.join([key for key in DATASET_OPTIONS.keys()])}"
  79. )
  80. def test_out_of_retention(self):
  81. self.create_project()
  82. with self.options({"system.event-retention-days": 10}):
  83. query = {
  84. "field": ["id", "timestamp"],
  85. "orderby": ["-timestamp", "-id"],
  86. "query": "event.type:transaction",
  87. "start": iso_format(before_now(days=20)),
  88. "end": iso_format(before_now(days=15)),
  89. "dataset": "metricsEnhanced",
  90. }
  91. response = self.do_request(query)
  92. assert response.status_code == 400, response.content
  93. assert response.data["detail"] == "Invalid date range. Please try a more recent date range."
  94. def test_invalid_search_terms(self):
  95. response = self.do_request(
  96. {
  97. "field": ["epm()"],
  98. "query": "hi \n there",
  99. "project": self.project.id,
  100. "dataset": "metricsEnhanced",
  101. }
  102. )
  103. assert response.status_code == 400, response.content
  104. assert (
  105. response.data["detail"]
  106. == "Parse error at 'hi \n ther' (column 4). This is commonly caused by unmatched parentheses. Enclose any text in double quotes."
  107. )
  108. def test_percentile_with_no_data(self):
  109. response = self.do_request(
  110. {
  111. "field": ["p50()"],
  112. "query": "",
  113. "project": self.project.id,
  114. "dataset": "metricsEnhanced",
  115. }
  116. )
  117. assert response.status_code == 200, response.content
  118. data = response.data["data"]
  119. assert len(data) == 1
  120. assert data[0]["p50()"] == 0
  121. def test_project_name(self):
  122. self.store_transaction_metric(
  123. 1,
  124. tags={"environment": "staging"},
  125. timestamp=self.min_ago,
  126. )
  127. response = self.do_request(
  128. {
  129. "field": ["project.name", "environment", "epm()"],
  130. "query": "event.type:transaction",
  131. "dataset": "metricsEnhanced",
  132. "per_page": 50,
  133. }
  134. )
  135. assert response.status_code == 200, response.content
  136. assert len(response.data["data"]) == 1
  137. data = response.data["data"]
  138. meta = response.data["meta"]
  139. field_meta = meta["fields"]
  140. assert data[0]["project.name"] == self.project.slug
  141. assert "project.id" not in data[0]
  142. assert data[0]["environment"] == "staging"
  143. assert meta["isMetricsData"]
  144. assert field_meta["project.name"] == "string"
  145. assert field_meta["environment"] == "string"
  146. assert field_meta["epm()"] == "rate"
  147. def test_project_id(self):
  148. self.store_transaction_metric(
  149. 1,
  150. tags={"environment": "staging"},
  151. timestamp=self.min_ago,
  152. )
  153. response = self.do_request(
  154. {
  155. "field": ["project_id", "environment", "epm()"],
  156. "query": "event.type:transaction",
  157. "dataset": "metrics",
  158. "per_page": 50,
  159. }
  160. )
  161. assert response.status_code == 200, response.content
  162. assert len(response.data["data"]) == 1
  163. data = response.data["data"]
  164. meta = response.data["meta"]
  165. field_meta = meta["fields"]
  166. assert data[0]["project_id"] == self.project.id
  167. assert data[0]["environment"] == "staging"
  168. assert meta["isMetricsData"]
  169. assert field_meta["project_id"] == "integer"
  170. assert field_meta["environment"] == "string"
  171. assert field_meta["epm()"] == "rate"
  172. def test_project_dot_id(self):
  173. self.store_transaction_metric(
  174. 1,
  175. tags={"environment": "staging"},
  176. timestamp=self.min_ago,
  177. )
  178. response = self.do_request(
  179. {
  180. "field": ["project.id", "environment", "epm()"],
  181. "query": "event.type:transaction",
  182. "dataset": "metrics",
  183. "per_page": 50,
  184. }
  185. )
  186. assert response.status_code == 200, response.content
  187. assert len(response.data["data"]) == 1
  188. data = response.data["data"]
  189. meta = response.data["meta"]
  190. field_meta = meta["fields"]
  191. assert data[0]["project.id"] == self.project.id
  192. assert data[0]["environment"] == "staging"
  193. assert meta["isMetricsData"]
  194. assert field_meta["project.id"] == "integer"
  195. assert field_meta["environment"] == "string"
  196. assert field_meta["epm()"] == "rate"
  197. def test_title_alias(self):
  198. """title is an alias to transaction name"""
  199. self.store_transaction_metric(
  200. 1,
  201. tags={"transaction": "foo_transaction"},
  202. timestamp=self.min_ago,
  203. )
  204. response = self.do_request(
  205. {
  206. "field": ["title", "p50()"],
  207. "query": "event.type:transaction",
  208. "dataset": "metricsEnhanced",
  209. "per_page": 50,
  210. }
  211. )
  212. assert response.status_code == 200, response.content
  213. assert len(response.data["data"]) == 1
  214. data = response.data["data"]
  215. meta = response.data["meta"]
  216. field_meta = meta["fields"]
  217. assert data[0]["title"] == "foo_transaction"
  218. assert data[0]["p50()"] == 1
  219. assert meta["isMetricsData"]
  220. assert field_meta["title"] == "string"
  221. assert field_meta["p50()"] == "duration"
  222. def test_having_condition(self):
  223. self.store_transaction_metric(
  224. 1,
  225. tags={"environment": "staging", "transaction": "foo_transaction"},
  226. timestamp=self.min_ago,
  227. )
  228. self.store_transaction_metric(
  229. # shouldn't show up
  230. 100,
  231. tags={"environment": "staging", "transaction": "bar_transaction"},
  232. timestamp=self.min_ago,
  233. )
  234. response = self.do_request(
  235. {
  236. "field": ["transaction", "project", "p50(transaction.duration)"],
  237. "query": "event.type:transaction p50(transaction.duration):<50",
  238. "dataset": "metricsEnhanced",
  239. "per_page": 50,
  240. }
  241. )
  242. assert response.status_code == 200, response.content
  243. assert len(response.data["data"]) == 1
  244. data = response.data["data"]
  245. meta = response.data["meta"]
  246. field_meta = meta["fields"]
  247. assert data[0]["transaction"] == "foo_transaction"
  248. assert data[0]["project"] == self.project.slug
  249. assert data[0]["p50(transaction.duration)"] == 1
  250. assert meta["isMetricsData"]
  251. assert field_meta["transaction"] == "string"
  252. assert field_meta["project"] == "string"
  253. assert field_meta["p50(transaction.duration)"] == "duration"
  254. def test_having_condition_with_preventing_aggregates(self):
  255. self.store_transaction_metric(
  256. 1,
  257. tags={"environment": "staging", "transaction": "foo_transaction"},
  258. timestamp=self.min_ago,
  259. )
  260. self.store_transaction_metric(
  261. 100,
  262. tags={"environment": "staging", "transaction": "bar_transaction"},
  263. timestamp=self.min_ago,
  264. )
  265. response = self.do_request(
  266. {
  267. "field": ["transaction", "project", "p50(transaction.duration)"],
  268. "query": "event.type:transaction p50(transaction.duration):<50",
  269. "dataset": "metricsEnhanced",
  270. "preventMetricAggregates": "1",
  271. "per_page": 50,
  272. }
  273. )
  274. assert response.status_code == 200, response.content
  275. assert len(response.data["data"]) == 0
  276. meta = response.data["meta"]
  277. field_meta = meta["fields"]
  278. assert not meta["isMetricsData"]
  279. assert field_meta["transaction"] == "string"
  280. assert field_meta["project"] == "string"
  281. assert field_meta["p50(transaction.duration)"] == "duration"
  282. def test_having_condition_with_preventing_aggregate_metrics_only(self):
  283. """same as the previous test, but with the dataset on explicit metrics
  284. which should throw a 400 error instead"""
  285. response = self.do_request(
  286. {
  287. "field": ["transaction", "project", "p50(transaction.duration)"],
  288. "query": "event.type:transaction p50(transaction.duration):<50",
  289. "dataset": "metrics",
  290. "preventMetricAggregates": "1",
  291. "per_page": 50,
  292. "project": self.project.id,
  293. }
  294. )
  295. assert response.status_code == 400, response.content
  296. def test_having_condition_not_selected(self):
  297. self.store_transaction_metric(
  298. 1,
  299. tags={"environment": "staging", "transaction": "foo_transaction"},
  300. timestamp=self.min_ago,
  301. )
  302. self.store_transaction_metric(
  303. # shouldn't show up
  304. 100,
  305. tags={"environment": "staging", "transaction": "bar_transaction"},
  306. timestamp=self.min_ago,
  307. )
  308. response = self.do_request(
  309. {
  310. "field": ["transaction", "project", "p50(transaction.duration)"],
  311. "query": "event.type:transaction p75(transaction.duration):<50",
  312. "dataset": "metricsEnhanced",
  313. "per_page": 50,
  314. }
  315. )
  316. assert response.status_code == 200, response.content
  317. assert len(response.data["data"]) == 1
  318. data = response.data["data"]
  319. meta = response.data["meta"]
  320. field_meta = meta["fields"]
  321. assert data[0]["transaction"] == "foo_transaction"
  322. assert data[0]["project"] == self.project.slug
  323. assert data[0]["p50(transaction.duration)"] == 1
  324. assert meta["isMetricsData"]
  325. assert field_meta["transaction"] == "string"
  326. assert field_meta["project"] == "string"
  327. assert field_meta["p50(transaction.duration)"] == "duration"
  328. def test_non_metrics_tag_with_implicit_format(self):
  329. self.store_transaction_metric(
  330. 1,
  331. tags={"environment": "staging", "transaction": "foo_transaction"},
  332. timestamp=self.min_ago,
  333. )
  334. response = self.do_request(
  335. {
  336. "field": ["test", "p50(transaction.duration)"],
  337. "query": "event.type:transaction",
  338. "dataset": "metricsEnhanced",
  339. "per_page": 50,
  340. }
  341. )
  342. assert response.status_code == 200, response.content
  343. assert len(response.data["data"]) == 0
  344. assert not response.data["meta"]["isMetricsData"]
  345. def test_non_metrics_tag_with_implicit_format_metrics_dataset(self):
  346. self.store_transaction_metric(
  347. 1,
  348. tags={"environment": "staging", "transaction": "foo_transaction"},
  349. timestamp=self.min_ago,
  350. )
  351. response = self.do_request(
  352. {
  353. "field": ["test", "p50(transaction.duration)"],
  354. "query": "event.type:transaction",
  355. "dataset": "metrics",
  356. "per_page": 50,
  357. }
  358. )
  359. assert response.status_code == 400, response.content
  360. def test_performance_homepage_query(self):
  361. self.store_transaction_metric(
  362. 1,
  363. tags={
  364. "transaction": "foo_transaction",
  365. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  366. },
  367. timestamp=self.min_ago,
  368. )
  369. self.store_transaction_metric(
  370. 1,
  371. "measurements.fcp",
  372. tags={"transaction": "foo_transaction"},
  373. timestamp=self.min_ago,
  374. )
  375. self.store_transaction_metric(
  376. 2,
  377. "measurements.lcp",
  378. tags={"transaction": "foo_transaction"},
  379. timestamp=self.min_ago,
  380. )
  381. self.store_transaction_metric(
  382. 3,
  383. "measurements.fid",
  384. tags={"transaction": "foo_transaction"},
  385. timestamp=self.min_ago,
  386. )
  387. self.store_transaction_metric(
  388. 4,
  389. "measurements.cls",
  390. tags={"transaction": "foo_transaction"},
  391. timestamp=self.min_ago,
  392. )
  393. self.store_transaction_metric(
  394. 1,
  395. "user",
  396. tags={
  397. "transaction": "foo_transaction",
  398. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_FRUSTRATED_TAG_VALUE,
  399. },
  400. timestamp=self.min_ago,
  401. )
  402. for dataset in ["metrics", "metricsEnhanced"]:
  403. response = self.do_request(
  404. {
  405. "field": [
  406. "transaction",
  407. "project",
  408. "tpm()",
  409. "p75(measurements.fcp)",
  410. "p75(measurements.lcp)",
  411. "p75(measurements.fid)",
  412. "p75(measurements.cls)",
  413. "count_unique(user)",
  414. "apdex()",
  415. "count_miserable(user)",
  416. "user_misery()",
  417. "failure_rate()",
  418. "failure_count()",
  419. ],
  420. "orderby": "tpm()",
  421. "query": "event.type:transaction",
  422. "dataset": dataset,
  423. "per_page": 50,
  424. }
  425. )
  426. assert len(response.data["data"]) == 1
  427. data = response.data["data"][0]
  428. meta = response.data["meta"]
  429. field_meta = meta["fields"]
  430. assert data["transaction"] == "foo_transaction"
  431. assert data["project"] == self.project.slug
  432. assert data["p75(measurements.fcp)"] == 1.0
  433. assert data["p75(measurements.lcp)"] == 2.0
  434. assert data["p75(measurements.fid)"] == 3.0
  435. assert data["p75(measurements.cls)"] == 4.0
  436. assert data["apdex()"] == 1.0
  437. assert data["count_miserable(user)"] == 1.0
  438. assert data["user_misery()"] == 0.058
  439. assert data["failure_rate()"] == 1
  440. assert data["failure_count()"] == 1
  441. assert meta["isMetricsData"]
  442. assert field_meta["transaction"] == "string"
  443. assert field_meta["project"] == "string"
  444. assert field_meta["p75(measurements.fcp)"] == "duration"
  445. assert field_meta["p75(measurements.lcp)"] == "duration"
  446. assert field_meta["p75(measurements.fid)"] == "duration"
  447. assert field_meta["p75(measurements.cls)"] == "number"
  448. assert field_meta["apdex()"] == "number"
  449. assert field_meta["count_miserable(user)"] == "integer"
  450. assert field_meta["user_misery()"] == "number"
  451. assert field_meta["failure_rate()"] == "percentage"
  452. assert field_meta["failure_count()"] == "integer"
  453. assert field_meta["tpm()"] == "rate"
  454. assert meta["units"]["tpm()"] == "1/minute"
  455. def test_user_misery_and_team_key_sort(self):
  456. self.store_transaction_metric(
  457. 1,
  458. tags={
  459. "transaction": "foo_transaction",
  460. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  461. },
  462. timestamp=self.min_ago,
  463. )
  464. self.store_transaction_metric(
  465. 1,
  466. "measurements.fcp",
  467. tags={"transaction": "foo_transaction"},
  468. timestamp=self.min_ago,
  469. )
  470. self.store_transaction_metric(
  471. 2,
  472. "measurements.lcp",
  473. tags={"transaction": "foo_transaction"},
  474. timestamp=self.min_ago,
  475. )
  476. self.store_transaction_metric(
  477. 3,
  478. "measurements.fid",
  479. tags={"transaction": "foo_transaction"},
  480. timestamp=self.min_ago,
  481. )
  482. self.store_transaction_metric(
  483. 4,
  484. "measurements.cls",
  485. tags={"transaction": "foo_transaction"},
  486. timestamp=self.min_ago,
  487. )
  488. self.store_transaction_metric(
  489. 1,
  490. "user",
  491. tags={
  492. "transaction": "foo_transaction",
  493. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_FRUSTRATED_TAG_VALUE,
  494. },
  495. timestamp=self.min_ago,
  496. )
  497. response = self.do_request(
  498. {
  499. "field": [
  500. "team_key_transaction",
  501. "transaction",
  502. "project",
  503. "tpm()",
  504. "p75(measurements.fcp)",
  505. "p75(measurements.lcp)",
  506. "p75(measurements.fid)",
  507. "p75(measurements.cls)",
  508. "count_unique(user)",
  509. "apdex()",
  510. "count_miserable(user)",
  511. "user_misery()",
  512. "failure_rate()",
  513. "failure_count()",
  514. ],
  515. "orderby": ["team_key_transaction", "user_misery()"],
  516. "query": "event.type:transaction",
  517. "dataset": "metrics",
  518. "per_page": 50,
  519. }
  520. )
  521. assert response.status_code == 200, response.content
  522. assert len(response.data["data"]) == 1
  523. data = response.data["data"][0]
  524. meta = response.data["meta"]
  525. field_meta = meta["fields"]
  526. assert data["transaction"] == "foo_transaction"
  527. assert data["project"] == self.project.slug
  528. assert data["p75(measurements.fcp)"] == 1.0
  529. assert data["p75(measurements.lcp)"] == 2.0
  530. assert data["p75(measurements.fid)"] == 3.0
  531. assert data["p75(measurements.cls)"] == 4.0
  532. assert data["apdex()"] == 1.0
  533. assert data["count_miserable(user)"] == 1.0
  534. assert data["user_misery()"] == 0.058
  535. assert data["failure_rate()"] == 1
  536. assert data["failure_count()"] == 1
  537. assert meta["isMetricsData"]
  538. assert field_meta["transaction"] == "string"
  539. assert field_meta["project"] == "string"
  540. assert field_meta["p75(measurements.fcp)"] == "duration"
  541. assert field_meta["p75(measurements.lcp)"] == "duration"
  542. assert field_meta["p75(measurements.fid)"] == "duration"
  543. assert field_meta["p75(measurements.cls)"] == "number"
  544. assert field_meta["apdex()"] == "number"
  545. assert field_meta["count_miserable(user)"] == "integer"
  546. assert field_meta["user_misery()"] == "number"
  547. assert field_meta["failure_rate()"] == "percentage"
  548. assert field_meta["failure_count()"] == "integer"
  549. def test_no_team_key_transactions(self):
  550. self.store_transaction_metric(
  551. 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago
  552. )
  553. self.store_transaction_metric(
  554. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  555. )
  556. query = {
  557. "team": "myteams",
  558. "project": [self.project.id],
  559. # TODO sort by transaction here once that's possible for order to match the same test without metrics
  560. "orderby": "p95()",
  561. "field": [
  562. "team_key_transaction",
  563. "transaction",
  564. "transaction.status",
  565. "project",
  566. "epm()",
  567. "failure_rate()",
  568. "p95()",
  569. ],
  570. "per_page": 50,
  571. "dataset": "metricsEnhanced",
  572. }
  573. response = self.do_request(query)
  574. assert response.status_code == 200, response.content
  575. assert len(response.data["data"]) == 2
  576. data = response.data["data"]
  577. meta = response.data["meta"]
  578. field_meta = meta["fields"]
  579. assert data[0]["team_key_transaction"] == 0
  580. assert data[0]["transaction"] == "foo_transaction"
  581. assert data[1]["team_key_transaction"] == 0
  582. assert data[1]["transaction"] == "bar_transaction"
  583. assert meta["isMetricsData"]
  584. assert field_meta["team_key_transaction"] == "boolean"
  585. assert field_meta["transaction"] == "string"
  586. def test_team_key_transactions_my_teams(self):
  587. team1 = self.create_team(organization=self.organization, name="Team A")
  588. self.create_team_membership(team1, user=self.user)
  589. self.project.add_team(team1)
  590. team2 = self.create_team(organization=self.organization, name="Team B")
  591. self.project.add_team(team2)
  592. key_transactions = [
  593. (team1, "foo_transaction"),
  594. (team2, "baz_transaction"),
  595. ]
  596. # Not a key transaction
  597. self.store_transaction_metric(
  598. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  599. )
  600. for team, transaction in key_transactions:
  601. self.store_transaction_metric(
  602. 1, tags={"transaction": transaction}, timestamp=self.min_ago
  603. )
  604. TeamKeyTransaction.objects.create(
  605. organization=self.organization,
  606. transaction=transaction,
  607. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  608. )
  609. query = {
  610. "team": "myteams",
  611. "project": [self.project.id],
  612. "field": [
  613. "team_key_transaction",
  614. "transaction",
  615. "transaction.status",
  616. "project",
  617. "epm()",
  618. "failure_rate()",
  619. "p95()",
  620. ],
  621. "per_page": 50,
  622. "dataset": "metricsEnhanced",
  623. }
  624. query["orderby"] = ["team_key_transaction", "p95()"]
  625. response = self.do_request(query)
  626. assert response.status_code == 200, response.content
  627. assert len(response.data["data"]) == 3
  628. data = response.data["data"]
  629. meta = response.data["meta"]
  630. field_meta = meta["fields"]
  631. assert data[0]["team_key_transaction"] == 0
  632. assert data[0]["transaction"] == "baz_transaction"
  633. assert data[1]["team_key_transaction"] == 0
  634. assert data[1]["transaction"] == "bar_transaction"
  635. assert data[2]["team_key_transaction"] == 1
  636. assert data[2]["transaction"] == "foo_transaction"
  637. assert meta["isMetricsData"]
  638. assert field_meta["team_key_transaction"] == "boolean"
  639. assert field_meta["transaction"] == "string"
  640. # not specifying any teams should use my teams
  641. query = {
  642. "project": [self.project.id],
  643. "field": [
  644. "team_key_transaction",
  645. "transaction",
  646. "transaction.status",
  647. "project",
  648. "epm()",
  649. "failure_rate()",
  650. "p95()",
  651. ],
  652. "per_page": 50,
  653. "dataset": "metricsEnhanced",
  654. }
  655. query["orderby"] = ["team_key_transaction", "p95()"]
  656. response = self.do_request(query)
  657. assert response.status_code == 200, response.content
  658. assert len(response.data["data"]) == 3
  659. data = response.data["data"]
  660. meta = response.data["meta"]
  661. field_meta = meta["fields"]
  662. assert data[0]["team_key_transaction"] == 0
  663. assert data[0]["transaction"] == "baz_transaction"
  664. assert data[1]["team_key_transaction"] == 0
  665. assert data[1]["transaction"] == "bar_transaction"
  666. assert data[2]["team_key_transaction"] == 1
  667. assert data[2]["transaction"] == "foo_transaction"
  668. assert meta["isMetricsData"]
  669. assert field_meta["team_key_transaction"] == "boolean"
  670. assert field_meta["transaction"] == "string"
  671. def test_team_key_transactions_orderby(self):
  672. team1 = self.create_team(organization=self.organization, name="Team A")
  673. team2 = self.create_team(organization=self.organization, name="Team B")
  674. key_transactions = [
  675. (team1, "foo_transaction", 1),
  676. (team2, "baz_transaction", 100),
  677. ]
  678. # Not a key transaction
  679. self.store_transaction_metric(
  680. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  681. )
  682. for team, transaction, value in key_transactions:
  683. self.store_transaction_metric(
  684. value, tags={"transaction": transaction}, timestamp=self.min_ago
  685. )
  686. self.create_team_membership(team, user=self.user)
  687. self.project.add_team(team)
  688. TeamKeyTransaction.objects.create(
  689. organization=self.organization,
  690. transaction=transaction,
  691. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  692. )
  693. query = {
  694. "team": "myteams",
  695. "project": [self.project.id],
  696. "field": [
  697. "team_key_transaction",
  698. "transaction",
  699. "transaction.status",
  700. "project",
  701. "epm()",
  702. "failure_rate()",
  703. "p95()",
  704. ],
  705. "per_page": 50,
  706. "dataset": "metricsEnhanced",
  707. }
  708. # test ascending order
  709. query["orderby"] = ["team_key_transaction", "p95()"]
  710. response = self.do_request(query)
  711. assert response.status_code == 200, response.content
  712. assert len(response.data["data"]) == 3
  713. data = response.data["data"]
  714. meta = response.data["meta"]
  715. field_meta = meta["fields"]
  716. assert data[0]["team_key_transaction"] == 0
  717. assert data[0]["transaction"] == "bar_transaction"
  718. assert data[1]["team_key_transaction"] == 1
  719. assert data[1]["transaction"] == "foo_transaction"
  720. assert data[2]["team_key_transaction"] == 1
  721. assert data[2]["transaction"] == "baz_transaction"
  722. assert meta["isMetricsData"]
  723. assert field_meta["team_key_transaction"] == "boolean"
  724. assert field_meta["transaction"] == "string"
  725. # test descending order
  726. query["orderby"] = ["-team_key_transaction", "p95()"]
  727. response = self.do_request(query)
  728. assert response.status_code == 200, response.content
  729. assert len(response.data["data"]) == 3
  730. data = response.data["data"]
  731. meta = response.data["meta"]
  732. field_meta = meta["fields"]
  733. assert data[0]["team_key_transaction"] == 1
  734. assert data[0]["transaction"] == "foo_transaction"
  735. assert data[1]["team_key_transaction"] == 1
  736. assert data[1]["transaction"] == "baz_transaction"
  737. assert data[2]["team_key_transaction"] == 0
  738. assert data[2]["transaction"] == "bar_transaction"
  739. assert meta["isMetricsData"]
  740. assert field_meta["team_key_transaction"] == "boolean"
  741. assert field_meta["transaction"] == "string"
  742. def test_team_key_transactions_query(self):
  743. team1 = self.create_team(organization=self.organization, name="Team A")
  744. team2 = self.create_team(organization=self.organization, name="Team B")
  745. key_transactions = [
  746. (team1, "foo_transaction", 1),
  747. (team2, "baz_transaction", 100),
  748. ]
  749. # Not a key transaction
  750. self.store_transaction_metric(
  751. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  752. )
  753. for team, transaction, value in key_transactions:
  754. self.store_transaction_metric(
  755. value, tags={"transaction": transaction}, timestamp=self.min_ago
  756. )
  757. self.create_team_membership(team, user=self.user)
  758. self.project.add_team(team)
  759. TeamKeyTransaction.objects.create(
  760. organization=self.organization,
  761. transaction=transaction,
  762. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  763. )
  764. query = {
  765. "team": "myteams",
  766. "project": [self.project.id],
  767. # use the order by to ensure the result order
  768. "orderby": "p95()",
  769. "field": [
  770. "team_key_transaction",
  771. "transaction",
  772. "transaction.status",
  773. "project",
  774. "epm()",
  775. "failure_rate()",
  776. "p95()",
  777. ],
  778. "per_page": 50,
  779. "dataset": "metricsEnhanced",
  780. }
  781. # key transactions
  782. query["query"] = "has:team_key_transaction"
  783. response = self.do_request(query)
  784. assert response.status_code == 200, response.content
  785. assert len(response.data["data"]) == 2
  786. data = response.data["data"]
  787. meta = response.data["meta"]
  788. field_meta = meta["fields"]
  789. assert data[0]["team_key_transaction"] == 1
  790. assert data[0]["transaction"] == "foo_transaction"
  791. assert data[1]["team_key_transaction"] == 1
  792. assert data[1]["transaction"] == "baz_transaction"
  793. assert meta["isMetricsData"]
  794. assert field_meta["team_key_transaction"] == "boolean"
  795. assert field_meta["transaction"] == "string"
  796. # key transactions
  797. query["query"] = "team_key_transaction:true"
  798. response = self.do_request(query)
  799. assert response.status_code == 200, response.content
  800. assert len(response.data["data"]) == 2
  801. data = response.data["data"]
  802. meta = response.data["meta"]
  803. field_meta = meta["fields"]
  804. assert data[0]["team_key_transaction"] == 1
  805. assert data[0]["transaction"] == "foo_transaction"
  806. assert data[1]["team_key_transaction"] == 1
  807. assert data[1]["transaction"] == "baz_transaction"
  808. assert meta["isMetricsData"]
  809. assert field_meta["team_key_transaction"] == "boolean"
  810. assert field_meta["transaction"] == "string"
  811. # not key transactions
  812. query["query"] = "!has:team_key_transaction"
  813. response = self.do_request(query)
  814. assert response.status_code == 200, response.content
  815. assert len(response.data["data"]) == 1
  816. data = response.data["data"]
  817. meta = response.data["meta"]
  818. field_meta = meta["fields"]
  819. assert data[0]["team_key_transaction"] == 0
  820. assert data[0]["transaction"] == "bar_transaction"
  821. assert meta["isMetricsData"]
  822. assert field_meta["team_key_transaction"] == "boolean"
  823. assert field_meta["transaction"] == "string"
  824. # not key transactions
  825. query["query"] = "team_key_transaction:false"
  826. response = self.do_request(query)
  827. assert response.status_code == 200, response.content
  828. assert len(response.data["data"]) == 1
  829. data = response.data["data"]
  830. meta = response.data["meta"]
  831. field_meta = meta["fields"]
  832. assert data[0]["team_key_transaction"] == 0
  833. assert data[0]["transaction"] == "bar_transaction"
  834. assert meta["isMetricsData"]
  835. assert field_meta["team_key_transaction"] == "boolean"
  836. assert field_meta["transaction"] == "string"
  837. def test_team_key_transaction_not_exists(self):
  838. team1 = self.create_team(organization=self.organization, name="Team A")
  839. team2 = self.create_team(organization=self.organization, name="Team B")
  840. key_transactions = [
  841. (team1, "foo_transaction", 1),
  842. (team2, "baz_transaction", 100),
  843. ]
  844. for team, transaction, value in key_transactions:
  845. self.store_transaction_metric(
  846. value, tags={"transaction": transaction}, timestamp=self.min_ago
  847. )
  848. self.create_team_membership(team, user=self.user)
  849. self.project.add_team(team)
  850. TeamKeyTransaction.objects.create(
  851. organization=self.organization,
  852. transaction=transaction,
  853. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  854. )
  855. # Don't create a metric for this one
  856. TeamKeyTransaction.objects.create(
  857. organization=self.organization,
  858. transaction="not_in_metrics",
  859. project_team=ProjectTeam.objects.get(project=self.project, team=team1),
  860. )
  861. query = {
  862. "team": "myteams",
  863. "project": [self.project.id],
  864. # use the order by to ensure the result order
  865. "orderby": "p95()",
  866. "field": [
  867. "team_key_transaction",
  868. "transaction",
  869. "transaction.status",
  870. "project",
  871. "epm()",
  872. "failure_rate()",
  873. "p95()",
  874. ],
  875. "per_page": 50,
  876. "dataset": "metricsEnhanced",
  877. }
  878. # key transactions
  879. query["query"] = "has:team_key_transaction"
  880. response = self.do_request(query)
  881. assert response.status_code == 200, response.content
  882. assert len(response.data["data"]) == 2
  883. data = response.data["data"]
  884. meta = response.data["meta"]
  885. field_meta = meta["fields"]
  886. assert data[0]["team_key_transaction"] == 1
  887. assert data[0]["transaction"] == "foo_transaction"
  888. assert data[1]["team_key_transaction"] == 1
  889. assert data[1]["transaction"] == "baz_transaction"
  890. assert meta["isMetricsData"]
  891. assert field_meta["team_key_transaction"] == "boolean"
  892. assert field_meta["transaction"] == "string"
  893. # key transactions
  894. query["query"] = "team_key_transaction:true"
  895. response = self.do_request(query)
  896. assert response.status_code == 200, response.content
  897. assert len(response.data["data"]) == 2
  898. data = response.data["data"]
  899. meta = response.data["meta"]
  900. field_meta = meta["fields"]
  901. assert data[0]["team_key_transaction"] == 1
  902. assert data[0]["transaction"] == "foo_transaction"
  903. assert data[1]["team_key_transaction"] == 1
  904. assert data[1]["transaction"] == "baz_transaction"
  905. assert meta["isMetricsData"]
  906. assert field_meta["team_key_transaction"] == "boolean"
  907. assert field_meta["transaction"] == "string"
  908. # not key transactions
  909. query["query"] = "!has:team_key_transaction"
  910. response = self.do_request(query)
  911. assert response.status_code == 200, response.content
  912. assert len(response.data["data"]) == 0
  913. data = response.data["data"]
  914. meta = response.data["meta"]
  915. field_meta = meta["fields"]
  916. assert meta["isMetricsData"]
  917. assert field_meta["team_key_transaction"] == "boolean"
  918. assert field_meta["transaction"] == "string"
  919. # not key transactions
  920. query["query"] = "team_key_transaction:false"
  921. response = self.do_request(query)
  922. assert response.status_code == 200, response.content
  923. assert len(response.data["data"]) == 0
  924. data = response.data["data"]
  925. meta = response.data["meta"]
  926. field_meta = meta["fields"]
  927. assert meta["isMetricsData"]
  928. assert field_meta["team_key_transaction"] == "boolean"
  929. assert field_meta["transaction"] == "string"
  930. def test_too_many_team_key_transactions(self):
  931. MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS = 1
  932. with mock.patch(
  933. "sentry.search.events.fields.MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS",
  934. MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS,
  935. ):
  936. team = self.create_team(organization=self.organization, name="Team A")
  937. self.create_team_membership(team, user=self.user)
  938. self.project.add_team(team)
  939. project_team = ProjectTeam.objects.get(project=self.project, team=team)
  940. transactions = ["foo_transaction", "bar_transaction", "baz_transaction"]
  941. for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1):
  942. self.store_transaction_metric(
  943. 100, tags={"transaction": transactions[i]}, timestamp=self.min_ago
  944. )
  945. TeamKeyTransaction.objects.bulk_create(
  946. [
  947. TeamKeyTransaction(
  948. organization=self.organization,
  949. project_team=project_team,
  950. transaction=transactions[i],
  951. )
  952. for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1)
  953. ]
  954. )
  955. query = {
  956. "team": "myteams",
  957. "project": [self.project.id],
  958. "orderby": "p95()",
  959. "field": [
  960. "team_key_transaction",
  961. "transaction",
  962. "transaction.status",
  963. "project",
  964. "epm()",
  965. "failure_rate()",
  966. "p95()",
  967. ],
  968. "dataset": "metricsEnhanced",
  969. "per_page": 50,
  970. }
  971. response = self.do_request(query)
  972. assert response.status_code == 200, response.content
  973. assert len(response.data["data"]) == 2
  974. data = response.data["data"]
  975. meta = response.data["meta"]
  976. assert (
  977. sum(row["team_key_transaction"] for row in data)
  978. == MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS
  979. )
  980. assert meta["isMetricsData"]
  981. def test_measurement_rating(self):
  982. self.store_transaction_metric(
  983. 50,
  984. metric="measurements.lcp",
  985. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  986. timestamp=self.min_ago,
  987. )
  988. self.store_transaction_metric(
  989. 15,
  990. metric="measurements.fp",
  991. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  992. timestamp=self.min_ago,
  993. )
  994. self.store_transaction_metric(
  995. 1500,
  996. metric="measurements.fcp",
  997. tags={"measurement_rating": "meh", "transaction": "foo_transaction"},
  998. timestamp=self.min_ago,
  999. )
  1000. self.store_transaction_metric(
  1001. 125,
  1002. metric="measurements.fid",
  1003. tags={"measurement_rating": "meh", "transaction": "foo_transaction"},
  1004. timestamp=self.min_ago,
  1005. )
  1006. self.store_transaction_metric(
  1007. 0.15,
  1008. metric="measurements.cls",
  1009. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  1010. timestamp=self.min_ago,
  1011. )
  1012. response = self.do_request(
  1013. {
  1014. "field": [
  1015. "transaction",
  1016. "count_web_vitals(measurements.lcp, good)",
  1017. "count_web_vitals(measurements.fp, good)",
  1018. "count_web_vitals(measurements.fcp, meh)",
  1019. "count_web_vitals(measurements.fid, meh)",
  1020. "count_web_vitals(measurements.cls, good)",
  1021. "count_web_vitals(measurements.lcp, any)",
  1022. ],
  1023. "query": "event.type:transaction",
  1024. "dataset": "metricsEnhanced",
  1025. "per_page": 50,
  1026. }
  1027. )
  1028. assert response.status_code == 200, response.content
  1029. assert len(response.data["data"]) == 1
  1030. data = response.data["data"]
  1031. meta = response.data["meta"]
  1032. field_meta = meta["fields"]
  1033. assert data[0]["count_web_vitals(measurements.lcp, good)"] == 1
  1034. assert data[0]["count_web_vitals(measurements.fp, good)"] == 1
  1035. assert data[0]["count_web_vitals(measurements.fcp, meh)"] == 1
  1036. assert data[0]["count_web_vitals(measurements.fid, meh)"] == 1
  1037. assert data[0]["count_web_vitals(measurements.cls, good)"] == 1
  1038. assert data[0]["count_web_vitals(measurements.lcp, any)"] == 1
  1039. assert meta["isMetricsData"]
  1040. assert field_meta["count_web_vitals(measurements.lcp, good)"] == "integer"
  1041. assert field_meta["count_web_vitals(measurements.fp, good)"] == "integer"
  1042. assert field_meta["count_web_vitals(measurements.fcp, meh)"] == "integer"
  1043. assert field_meta["count_web_vitals(measurements.fid, meh)"] == "integer"
  1044. assert field_meta["count_web_vitals(measurements.cls, good)"] == "integer"
  1045. assert field_meta["count_web_vitals(measurements.lcp, any)"] == "integer"
  1046. def test_measurement_rating_that_does_not_exist(self):
  1047. self.store_transaction_metric(
  1048. 1,
  1049. metric="measurements.lcp",
  1050. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  1051. timestamp=self.min_ago,
  1052. )
  1053. response = self.do_request(
  1054. {
  1055. "field": ["transaction", "count_web_vitals(measurements.lcp, poor)"],
  1056. "query": "event.type:transaction",
  1057. "dataset": "metricsEnhanced",
  1058. "per_page": 50,
  1059. }
  1060. )
  1061. assert response.status_code == 200, response.content
  1062. assert len(response.data["data"]) == 1
  1063. data = response.data["data"]
  1064. meta = response.data["meta"]
  1065. assert data[0]["count_web_vitals(measurements.lcp, poor)"] == 0
  1066. assert meta["isMetricsData"]
  1067. assert meta["fields"]["count_web_vitals(measurements.lcp, poor)"] == "integer"
  1068. def test_count_web_vitals_invalid_vital(self):
  1069. query = {
  1070. "field": [
  1071. "count_web_vitals(measurements.foo, poor)",
  1072. ],
  1073. "project": [self.project.id],
  1074. "dataset": "metricsEnhanced",
  1075. }
  1076. response = self.do_request(query)
  1077. assert response.status_code == 400, response.content
  1078. query = {
  1079. "field": [
  1080. "count_web_vitals(tags[lcp], poor)",
  1081. ],
  1082. "project": [self.project.id],
  1083. "dataset": "metricsEnhanced",
  1084. }
  1085. response = self.do_request(query)
  1086. assert response.status_code == 400, response.content
  1087. query = {
  1088. "field": [
  1089. "count_web_vitals(transaction.duration, poor)",
  1090. ],
  1091. "project": [self.project.id],
  1092. "dataset": "metricsEnhanced",
  1093. }
  1094. response = self.do_request(query)
  1095. assert response.status_code == 400, response.content
  1096. query = {
  1097. "field": [
  1098. "count_web_vitals(measurements.lcp, bad)",
  1099. ],
  1100. "project": [self.project.id],
  1101. "dataset": "metricsEnhanced",
  1102. }
  1103. response = self.do_request(query)
  1104. assert response.status_code == 400, response.content
  1105. def test_count_unique_user_returns_zero(self):
  1106. self.store_transaction_metric(
  1107. 50,
  1108. metric="user",
  1109. tags={"transaction": "foo_transaction"},
  1110. timestamp=self.min_ago,
  1111. )
  1112. self.store_transaction_metric(
  1113. 50,
  1114. tags={"transaction": "foo_transaction"},
  1115. timestamp=self.min_ago,
  1116. )
  1117. self.store_transaction_metric(
  1118. 100,
  1119. tags={"transaction": "bar_transaction"},
  1120. timestamp=self.min_ago,
  1121. )
  1122. query = {
  1123. "project": [self.project.id],
  1124. "orderby": "p50()",
  1125. "field": [
  1126. "transaction",
  1127. "count_unique(user)",
  1128. "p50()",
  1129. ],
  1130. "dataset": "metricsEnhanced",
  1131. "per_page": 50,
  1132. }
  1133. response = self.do_request(query)
  1134. assert response.status_code == 200, response.content
  1135. assert len(response.data["data"]) == 2
  1136. data = response.data["data"]
  1137. meta = response.data["meta"]
  1138. assert data[0]["transaction"] == "foo_transaction"
  1139. assert data[0]["count_unique(user)"] == 1
  1140. assert data[1]["transaction"] == "bar_transaction"
  1141. assert data[1]["count_unique(user)"] == 0
  1142. assert meta["isMetricsData"]
  1143. def test_sum_transaction_duration(self):
  1144. self.store_transaction_metric(
  1145. 50,
  1146. tags={"transaction": "foo_transaction"},
  1147. timestamp=self.min_ago,
  1148. )
  1149. self.store_transaction_metric(
  1150. 100,
  1151. tags={"transaction": "foo_transaction"},
  1152. timestamp=self.min_ago,
  1153. )
  1154. self.store_transaction_metric(
  1155. 150,
  1156. tags={"transaction": "foo_transaction"},
  1157. timestamp=self.min_ago,
  1158. )
  1159. query = {
  1160. "project": [self.project.id],
  1161. "orderby": "sum(transaction.duration)",
  1162. "field": [
  1163. "transaction",
  1164. "sum(transaction.duration)",
  1165. ],
  1166. "dataset": "metricsEnhanced",
  1167. "per_page": 50,
  1168. }
  1169. response = self.do_request(query)
  1170. assert response.status_code == 200, response.content
  1171. assert len(response.data["data"]) == 1
  1172. data = response.data["data"]
  1173. meta = response.data["meta"]
  1174. assert data[0]["transaction"] == "foo_transaction"
  1175. assert data[0]["sum(transaction.duration)"] == 300
  1176. assert meta["isMetricsData"]
  1177. def test_custom_measurements_simple(self):
  1178. self.store_transaction_metric(
  1179. 1,
  1180. metric="measurements.something_custom",
  1181. internal_metric="d:transactions/measurements.something_custom@millisecond",
  1182. entity="metrics_distributions",
  1183. tags={"transaction": "foo_transaction"},
  1184. timestamp=self.min_ago,
  1185. )
  1186. query = {
  1187. "project": [self.project.id],
  1188. "orderby": "p50(measurements.something_custom)",
  1189. "field": [
  1190. "transaction",
  1191. "p50(measurements.something_custom)",
  1192. ],
  1193. "statsPeriod": "24h",
  1194. "dataset": "metricsEnhanced",
  1195. "per_page": 50,
  1196. }
  1197. self.wait_for_metric_count(
  1198. self.project,
  1199. 1,
  1200. metric="measurements.something_custom",
  1201. mri="d:transactions/measurements.something_custom@millisecond",
  1202. )
  1203. response = self.do_request(query)
  1204. assert response.status_code == 200, response.content
  1205. assert len(response.data["data"]) == 1
  1206. data = response.data["data"]
  1207. meta = response.data["meta"]
  1208. assert data[0]["transaction"] == "foo_transaction"
  1209. assert data[0]["p50(measurements.something_custom)"] == 1
  1210. assert meta["isMetricsData"]
  1211. assert meta["fields"]["p50(measurements.something_custom)"] == "duration"
  1212. assert meta["units"]["p50(measurements.something_custom)"] == "millisecond"
  1213. def test_custom_measurement_size_meta_type(self):
  1214. self.store_transaction_metric(
  1215. 100,
  1216. metric="measurements.custom_type",
  1217. internal_metric="d:transactions/measurements.custom_type@somethingcustom",
  1218. entity="metrics_distributions",
  1219. tags={"transaction": "foo_transaction"},
  1220. timestamp=self.min_ago,
  1221. )
  1222. self.store_transaction_metric(
  1223. 100,
  1224. metric="measurements.percent",
  1225. internal_metric="d:transactions/measurements.percent@ratio",
  1226. entity="metrics_distributions",
  1227. tags={"transaction": "foo_transaction"},
  1228. timestamp=self.min_ago,
  1229. )
  1230. self.store_transaction_metric(
  1231. 100,
  1232. metric="measurements.longtaskcount",
  1233. internal_metric="d:transactions/measurements.longtaskcount@none",
  1234. entity="metrics_distributions",
  1235. tags={"transaction": "foo_transaction"},
  1236. timestamp=self.min_ago,
  1237. )
  1238. query = {
  1239. "project": [self.project.id],
  1240. "orderby": "p50(measurements.longtaskcount)",
  1241. "field": [
  1242. "transaction",
  1243. "p50(measurements.longtaskcount)",
  1244. "p50(measurements.percent)",
  1245. "p50(measurements.custom_type)",
  1246. ],
  1247. "statsPeriod": "24h",
  1248. "dataset": "metricsEnhanced",
  1249. "per_page": 50,
  1250. }
  1251. response = self.do_request(query)
  1252. assert response.status_code == 200, response.content
  1253. assert len(response.data["data"]) == 1
  1254. data = response.data["data"]
  1255. meta = response.data["meta"]
  1256. assert data[0]["transaction"] == "foo_transaction"
  1257. assert data[0]["p50(measurements.longtaskcount)"] == 100
  1258. assert data[0]["p50(measurements.percent)"] == 100
  1259. assert data[0]["p50(measurements.custom_type)"] == 100
  1260. assert meta["isMetricsData"]
  1261. assert meta["fields"]["p50(measurements.longtaskcount)"] == "integer"
  1262. assert meta["units"]["p50(measurements.longtaskcount)"] is None
  1263. assert meta["fields"]["p50(measurements.percent)"] == "percentage"
  1264. assert meta["units"]["p50(measurements.percent)"] is None
  1265. assert meta["fields"]["p50(measurements.custom_type)"] == "number"
  1266. assert meta["units"]["p50(measurements.custom_type)"] is None
  1267. def test_custom_measurement_none_type(self):
  1268. self.store_transaction_metric(
  1269. 1,
  1270. metric="measurements.cls",
  1271. entity="metrics_distributions",
  1272. tags={"transaction": "foo_transaction"},
  1273. timestamp=self.min_ago,
  1274. )
  1275. query = {
  1276. "project": [self.project.id],
  1277. "orderby": "p75(measurements.cls)",
  1278. "field": [
  1279. "transaction",
  1280. "p75(measurements.cls)",
  1281. "p99(measurements.cls)",
  1282. "max(measurements.cls)",
  1283. ],
  1284. "statsPeriod": "24h",
  1285. "dataset": "metricsEnhanced",
  1286. "per_page": 50,
  1287. }
  1288. self.wait_for_metric_count(
  1289. self.project,
  1290. 1,
  1291. metric=TransactionMetricKey.MEASUREMENTS_CLS.value,
  1292. mri=TransactionMRI.MEASUREMENTS_CLS.value,
  1293. )
  1294. response = self.do_request(query)
  1295. assert response.status_code == 200, response.content
  1296. assert len(response.data["data"]) == 1
  1297. data = response.data["data"]
  1298. meta = response.data["meta"]
  1299. assert data[0]["transaction"] == "foo_transaction"
  1300. assert data[0]["p75(measurements.cls)"] == 1
  1301. assert data[0]["p99(measurements.cls)"] == 1
  1302. assert data[0]["max(measurements.cls)"] == 1
  1303. assert meta["isMetricsData"]
  1304. assert meta["fields"]["p75(measurements.cls)"] == "number"
  1305. assert meta["units"]["p75(measurements.cls)"] is None
  1306. assert meta["fields"]["p99(measurements.cls)"] == "number"
  1307. assert meta["units"]["p99(measurements.cls)"] is None
  1308. assert meta["fields"]["max(measurements.cls)"] == "number"
  1309. assert meta["units"]["max(measurements.cls)"] is None
  1310. def test_custom_measurement_duration_filtering(self):
  1311. self.store_transaction_metric(
  1312. 1,
  1313. metric="measurements.runtime",
  1314. internal_metric="d:transactions/measurements.runtime@hour",
  1315. entity="metrics_distributions",
  1316. tags={"transaction": "foo_transaction"},
  1317. timestamp=self.min_ago,
  1318. )
  1319. self.store_transaction_metric(
  1320. 180,
  1321. metric="measurements.runtime",
  1322. internal_metric="d:transactions/measurements.runtime@hour",
  1323. entity="metrics_distributions",
  1324. tags={"transaction": "bar_transaction"},
  1325. timestamp=self.min_ago,
  1326. )
  1327. query = {
  1328. "project": [self.project.id],
  1329. "field": [
  1330. "transaction",
  1331. "max(measurements.runtime)",
  1332. ],
  1333. "query": "p50(measurements.runtime):>1wk",
  1334. "statsPeriod": "24h",
  1335. "dataset": "metricsEnhanced",
  1336. "per_page": 50,
  1337. }
  1338. response = self.do_request(query)
  1339. assert response.status_code == 200, response.content
  1340. assert len(response.data["data"]) == 1
  1341. data = response.data["data"]
  1342. meta = response.data["meta"]
  1343. assert data[0]["transaction"] == "bar_transaction"
  1344. assert data[0]["max(measurements.runtime)"] == 180
  1345. assert meta["isMetricsData"]
  1346. def test_custom_measurement_size_filtering(self):
  1347. self.store_transaction_metric(
  1348. 1,
  1349. metric="measurements.datacenter_memory",
  1350. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1351. entity="metrics_distributions",
  1352. tags={"transaction": "foo_transaction"},
  1353. timestamp=self.min_ago,
  1354. )
  1355. self.store_transaction_metric(
  1356. 100,
  1357. metric="measurements.datacenter_memory",
  1358. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1359. entity="metrics_distributions",
  1360. tags={"transaction": "bar_transaction"},
  1361. timestamp=self.min_ago,
  1362. )
  1363. query = {
  1364. "project": [self.project.id],
  1365. "field": [
  1366. "transaction",
  1367. "max(measurements.datacenter_memory)",
  1368. ],
  1369. "query": "p50(measurements.datacenter_memory):>5pb",
  1370. "statsPeriod": "24h",
  1371. "dataset": "metricsEnhanced",
  1372. "per_page": 50,
  1373. }
  1374. response = self.do_request(query)
  1375. assert response.status_code == 200, response.content
  1376. assert len(response.data["data"]) == 1
  1377. data = response.data["data"]
  1378. meta = response.data["meta"]
  1379. assert data[0]["transaction"] == "bar_transaction"
  1380. assert data[0]["max(measurements.datacenter_memory)"] == 100
  1381. assert meta["units"]["max(measurements.datacenter_memory)"] == "petabyte"
  1382. assert meta["fields"]["max(measurements.datacenter_memory)"] == "size"
  1383. assert meta["isMetricsData"]
  1384. def test_has_custom_measurement(self):
  1385. self.store_transaction_metric(
  1386. 33,
  1387. metric="measurements.datacenter_memory",
  1388. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1389. entity="metrics_distributions",
  1390. tags={"transaction": "foo_transaction"},
  1391. timestamp=self.min_ago,
  1392. )
  1393. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1394. transaction_data["measurements"]["datacenter_memory"] = {
  1395. "value": 33,
  1396. "unit": "petabyte",
  1397. }
  1398. self.store_event(transaction_data, self.project.id)
  1399. measurement = "measurements.datacenter_memory"
  1400. response = self.do_request(
  1401. {
  1402. "field": ["transaction", measurement],
  1403. "query": "has:measurements.datacenter_memory",
  1404. "dataset": "discover",
  1405. }
  1406. )
  1407. assert response.status_code == 200, response.content
  1408. assert len(response.data["data"]) == 1
  1409. response = self.do_request(
  1410. {
  1411. "field": ["transaction", measurement],
  1412. "query": "!has:measurements.datacenter_memory",
  1413. "dataset": "discover",
  1414. }
  1415. )
  1416. assert response.status_code == 200, response.content
  1417. assert len(response.data["data"]) == 0
  1418. def test_environment_param(self):
  1419. self.create_environment(self.project, name="staging")
  1420. self.store_transaction_metric(
  1421. 1,
  1422. tags={"transaction": "foo_transaction", "environment": "staging"},
  1423. timestamp=self.min_ago,
  1424. )
  1425. self.store_transaction_metric(
  1426. 100,
  1427. tags={"transaction": "foo_transaction"},
  1428. timestamp=self.min_ago,
  1429. )
  1430. query = {
  1431. "project": [self.project.id],
  1432. "environment": "staging",
  1433. "orderby": "p50(transaction.duration)",
  1434. "field": [
  1435. "transaction",
  1436. "environment",
  1437. "p50(transaction.duration)",
  1438. ],
  1439. "statsPeriod": "24h",
  1440. "dataset": "metricsEnhanced",
  1441. "per_page": 50,
  1442. }
  1443. response = self.do_request(query)
  1444. assert response.status_code == 200, response.content
  1445. assert len(response.data["data"]) == 1
  1446. data = response.data["data"]
  1447. meta = response.data["meta"]
  1448. assert data[0]["transaction"] == "foo_transaction"
  1449. assert data[0]["environment"] == "staging"
  1450. assert data[0]["p50(transaction.duration)"] == 1
  1451. assert meta["isMetricsData"]
  1452. @pytest.mark.xfail(reason="Started failing on ClickHouse 21.8")
  1453. def test_environment_query(self):
  1454. self.create_environment(self.project, name="staging")
  1455. self.store_transaction_metric(
  1456. 1,
  1457. tags={"transaction": "foo_transaction", "environment": "staging"},
  1458. timestamp=self.min_ago,
  1459. )
  1460. self.store_transaction_metric(
  1461. 100,
  1462. tags={"transaction": "foo_transaction"},
  1463. timestamp=self.min_ago,
  1464. )
  1465. query = {
  1466. "project": [self.project.id],
  1467. "orderby": "p50(transaction.duration)",
  1468. "field": [
  1469. "transaction",
  1470. "environment",
  1471. "p50(transaction.duration)",
  1472. ],
  1473. "query": "!has:environment",
  1474. "statsPeriod": "24h",
  1475. "dataset": "metricsEnhanced",
  1476. "per_page": 50,
  1477. }
  1478. self.wait_for_metric_count(self.project, 2)
  1479. response = self.do_request(query)
  1480. assert response.status_code == 200, response.content
  1481. assert len(response.data["data"]) == 1
  1482. data = response.data["data"]
  1483. meta = response.data["meta"]
  1484. assert data[0]["transaction"] == "foo_transaction"
  1485. assert data[0]["environment"] is None or data[0]["environment"] == ""
  1486. assert data[0]["p50(transaction.duration)"] == 100
  1487. assert meta["isMetricsData"]
  1488. def test_has_transaction(self):
  1489. self.store_transaction_metric(
  1490. 1,
  1491. tags={},
  1492. timestamp=self.min_ago,
  1493. )
  1494. self.store_transaction_metric(
  1495. 100,
  1496. tags={"transaction": "foo_transaction"},
  1497. timestamp=self.min_ago,
  1498. )
  1499. query = {
  1500. "project": [self.project.id],
  1501. "orderby": "p50(transaction.duration)",
  1502. "field": [
  1503. "transaction",
  1504. "p50(transaction.duration)",
  1505. ],
  1506. "query": "has:transaction",
  1507. "statsPeriod": "24h",
  1508. "dataset": "metricsEnhanced",
  1509. "per_page": 50,
  1510. }
  1511. self.wait_for_metric_count(self.project, 2)
  1512. response = self.do_request(query)
  1513. assert response.status_code == 200, response.content
  1514. assert len(response.data["data"]) == 2
  1515. data = response.data["data"]
  1516. meta = response.data["meta"]
  1517. assert data[0]["transaction"] == "<< unparameterized >>"
  1518. assert data[0]["p50(transaction.duration)"] == 1
  1519. assert data[1]["transaction"] == "foo_transaction"
  1520. assert data[1]["p50(transaction.duration)"] == 100
  1521. assert meta["isMetricsData"]
  1522. query = {
  1523. "project": [self.project.id],
  1524. "orderby": "p50(transaction.duration)",
  1525. "field": [
  1526. "transaction",
  1527. "p50(transaction.duration)",
  1528. ],
  1529. "query": "!has:transaction",
  1530. "statsPeriod": "24h",
  1531. "dataset": "metricsEnhanced",
  1532. "per_page": 50,
  1533. }
  1534. response = self.do_request(query)
  1535. assert response.status_code == 400, response.content
  1536. def test_apdex_transaction_threshold(self):
  1537. ProjectTransactionThresholdOverride.objects.create(
  1538. transaction="foo_transaction",
  1539. project=self.project,
  1540. organization=self.project.organization,
  1541. threshold=600,
  1542. metric=TransactionMetric.LCP.value,
  1543. )
  1544. ProjectTransactionThresholdOverride.objects.create(
  1545. transaction="bar_transaction",
  1546. project=self.project,
  1547. organization=self.project.organization,
  1548. threshold=600,
  1549. metric=TransactionMetric.LCP.value,
  1550. )
  1551. self.store_transaction_metric(
  1552. 1,
  1553. tags={
  1554. "transaction": "foo_transaction",
  1555. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1556. },
  1557. timestamp=self.min_ago,
  1558. )
  1559. self.store_transaction_metric(
  1560. 1,
  1561. "measurements.lcp",
  1562. tags={
  1563. "transaction": "bar_transaction",
  1564. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1565. },
  1566. timestamp=self.min_ago,
  1567. )
  1568. response = self.do_request(
  1569. {
  1570. "field": [
  1571. "transaction",
  1572. "apdex()",
  1573. ],
  1574. "orderby": ["apdex()"],
  1575. "query": "event.type:transaction",
  1576. "dataset": "metrics",
  1577. "per_page": 50,
  1578. }
  1579. )
  1580. assert len(response.data["data"]) == 2
  1581. data = response.data["data"]
  1582. meta = response.data["meta"]
  1583. field_meta = meta["fields"]
  1584. assert data[0]["transaction"] == "bar_transaction"
  1585. # Threshold is lcp based
  1586. assert data[0]["apdex()"] == 1
  1587. assert data[1]["transaction"] == "foo_transaction"
  1588. # Threshold is lcp based
  1589. assert data[1]["apdex()"] == 0
  1590. assert meta["isMetricsData"]
  1591. assert field_meta["transaction"] == "string"
  1592. assert field_meta["apdex()"] == "number"
  1593. def test_apdex_project_threshold(self):
  1594. ProjectTransactionThreshold.objects.create(
  1595. project=self.project,
  1596. organization=self.project.organization,
  1597. threshold=600,
  1598. metric=TransactionMetric.LCP.value,
  1599. )
  1600. self.store_transaction_metric(
  1601. 1,
  1602. tags={
  1603. "transaction": "foo_transaction",
  1604. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1605. },
  1606. timestamp=self.min_ago,
  1607. )
  1608. self.store_transaction_metric(
  1609. 1,
  1610. "measurements.lcp",
  1611. tags={
  1612. "transaction": "bar_transaction",
  1613. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1614. },
  1615. timestamp=self.min_ago,
  1616. )
  1617. response = self.do_request(
  1618. {
  1619. "field": [
  1620. "transaction",
  1621. "apdex()",
  1622. ],
  1623. "orderby": ["apdex()"],
  1624. "query": "event.type:transaction",
  1625. "dataset": "metrics",
  1626. "per_page": 50,
  1627. }
  1628. )
  1629. assert response.status_code == 200, response.content
  1630. assert len(response.data["data"]) == 2
  1631. data = response.data["data"]
  1632. meta = response.data["meta"]
  1633. field_meta = meta["fields"]
  1634. assert data[0]["transaction"] == "bar_transaction"
  1635. # Threshold is lcp based
  1636. assert data[0]["apdex()"] == 1
  1637. assert data[1]["transaction"] == "foo_transaction"
  1638. # Threshold is lcp based
  1639. assert data[1]["apdex()"] == 0
  1640. assert meta["isMetricsData"]
  1641. assert field_meta["transaction"] == "string"
  1642. assert field_meta["apdex()"] == "number"
  1643. def test_apdex_satisfaction_param(self):
  1644. for function in ["apdex(300)", "user_misery(300)", "count_miserable(user, 300)"]:
  1645. query = {
  1646. "project": [self.project.id],
  1647. "field": [
  1648. "transaction",
  1649. function,
  1650. ],
  1651. "statsPeriod": "24h",
  1652. "dataset": "metricsEnhanced",
  1653. "per_page": 50,
  1654. }
  1655. response = self.do_request(query)
  1656. assert response.status_code == 200, response.content
  1657. assert len(response.data["data"]) == 0
  1658. meta = response.data["meta"]
  1659. assert not meta["isMetricsData"], function
  1660. query = {
  1661. "project": [self.project.id],
  1662. "field": [
  1663. "transaction",
  1664. function,
  1665. ],
  1666. "statsPeriod": "24h",
  1667. "dataset": "metrics",
  1668. "per_page": 50,
  1669. }
  1670. response = self.do_request(query)
  1671. assert response.status_code == 400, function
  1672. assert b"threshold parameter" in response.content, function
  1673. def test_mobile_metrics(self):
  1674. self.store_transaction_metric(
  1675. 0.4,
  1676. "measurements.frames_frozen_rate",
  1677. tags={
  1678. "transaction": "bar_transaction",
  1679. },
  1680. timestamp=self.min_ago,
  1681. )
  1682. query = {
  1683. "project": [self.project.id],
  1684. "field": [
  1685. "transaction",
  1686. "p50(measurements.frames_frozen_rate)",
  1687. ],
  1688. "statsPeriod": "24h",
  1689. "dataset": "metrics",
  1690. "per_page": 50,
  1691. }
  1692. response = self.do_request(query)
  1693. assert response.status_code == 200, response.content
  1694. assert len(response.data["data"]) == 1
  1695. assert response.data["data"][0]["p50(measurements.frames_frozen_rate)"] == 0.4
  1696. def test_merge_null_unparam(self):
  1697. self.store_transaction_metric(
  1698. 1,
  1699. # Transaction: unparam
  1700. tags={
  1701. "transaction": "<< unparameterized >>",
  1702. },
  1703. timestamp=self.min_ago,
  1704. )
  1705. self.store_transaction_metric(
  1706. 2,
  1707. # Transaction:null
  1708. tags={},
  1709. timestamp=self.min_ago,
  1710. )
  1711. query = {
  1712. "project": [self.project.id],
  1713. "field": [
  1714. "transaction",
  1715. "p50(transaction.duration)",
  1716. ],
  1717. "statsPeriod": "24h",
  1718. "dataset": "metrics",
  1719. "per_page": 50,
  1720. }
  1721. response = self.do_request(query)
  1722. assert response.status_code == 200, response.content
  1723. assert len(response.data["data"]) == 1
  1724. assert response.data["data"][0]["p50(transaction.duration)"] == 1.5
  1725. def test_unparam_filter(self):
  1726. self.store_transaction_metric(
  1727. 1,
  1728. # Transaction: unparam
  1729. tags={
  1730. "transaction": "<< unparameterized >>",
  1731. },
  1732. timestamp=self.min_ago,
  1733. )
  1734. self.store_transaction_metric(
  1735. 2,
  1736. # Transaction:null
  1737. tags={},
  1738. timestamp=self.min_ago,
  1739. )
  1740. self.store_transaction_metric(
  1741. 3,
  1742. tags={
  1743. "transaction": "foo_transaction",
  1744. },
  1745. timestamp=self.min_ago,
  1746. )
  1747. query = {
  1748. "project": [self.project.id],
  1749. "field": [
  1750. "transaction",
  1751. "count()",
  1752. ],
  1753. "query": 'transaction:"<< unparameterized >>"',
  1754. "statsPeriod": "24h",
  1755. "dataset": "metrics",
  1756. "per_page": 50,
  1757. }
  1758. self.wait_for_metric_count(self.project, 3)
  1759. response = self.do_request(query)
  1760. assert response.status_code == 200, response.content
  1761. assert len(response.data["data"]) == 1
  1762. assert response.data["data"][0]["transaction"] == "<< unparameterized >>"
  1763. assert response.data["data"][0]["count()"] == 2
  1764. def test_custom_measurements_without_function(self):
  1765. self.store_transaction_metric(
  1766. 33,
  1767. metric="measurements.datacenter_memory",
  1768. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1769. entity="metrics_distributions",
  1770. tags={"transaction": "foo_transaction"},
  1771. timestamp=self.min_ago,
  1772. )
  1773. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1774. transaction_data["measurements"]["datacenter_memory"] = {
  1775. "value": 33,
  1776. "unit": "petabyte",
  1777. }
  1778. self.store_event(transaction_data, self.project.id)
  1779. measurement = "measurements.datacenter_memory"
  1780. response = self.do_request(
  1781. {
  1782. "field": ["transaction", measurement],
  1783. "query": "measurements.datacenter_memory:33pb",
  1784. "dataset": "discover",
  1785. }
  1786. )
  1787. assert response.status_code == 200, response.content
  1788. data = response.data["data"]
  1789. assert len(data) == 1
  1790. assert data[0][measurement] == 33
  1791. meta = response.data["meta"]
  1792. field_meta = meta["fields"]
  1793. unit_meta = meta["units"]
  1794. assert field_meta[measurement] == "size"
  1795. assert unit_meta[measurement] == "petabyte"
  1796. assert not meta["isMetricsData"]
  1797. def test_custom_measurements_with_function(self):
  1798. self.store_transaction_metric(
  1799. 33,
  1800. metric="measurements.datacenter_memory",
  1801. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1802. entity="metrics_distributions",
  1803. tags={"transaction": "foo_transaction"},
  1804. timestamp=self.min_ago,
  1805. )
  1806. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1807. transaction_data["measurements"]["datacenter_memory"] = {
  1808. "value": 33,
  1809. "unit": "petabyte",
  1810. }
  1811. self.store_event(transaction_data, self.project.id)
  1812. measurement = "p50(measurements.datacenter_memory)"
  1813. response = self.do_request(
  1814. {
  1815. "field": ["transaction", measurement],
  1816. "query": "measurements.datacenter_memory:33pb",
  1817. "dataset": "discover",
  1818. }
  1819. )
  1820. assert response.status_code == 200, response.content
  1821. data = response.data["data"]
  1822. assert len(data) == 1
  1823. assert data[0][measurement] == 33
  1824. meta = response.data["meta"]
  1825. field_meta = meta["fields"]
  1826. unit_meta = meta["units"]
  1827. assert field_meta[measurement] == "size"
  1828. assert unit_meta[measurement] == "petabyte"
  1829. assert not meta["isMetricsData"]
  1830. def test_custom_measurements_equation(self):
  1831. self.store_transaction_metric(
  1832. 33,
  1833. metric="measurements.datacenter_memory",
  1834. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1835. entity="metrics_distributions",
  1836. tags={"transaction": "foo_transaction"},
  1837. timestamp=self.min_ago,
  1838. )
  1839. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1840. transaction_data["measurements"]["datacenter_memory"] = {
  1841. "value": 33,
  1842. "unit": "petabyte",
  1843. }
  1844. self.store_event(transaction_data, self.project.id)
  1845. response = self.do_request(
  1846. {
  1847. "field": [
  1848. "transaction",
  1849. "measurements.datacenter_memory",
  1850. "equation|measurements.datacenter_memory / 3",
  1851. ],
  1852. "query": "",
  1853. "dataset": "discover",
  1854. }
  1855. )
  1856. assert response.status_code == 200, response.content
  1857. data = response.data["data"]
  1858. assert len(data) == 1
  1859. assert data[0]["measurements.datacenter_memory"] == 33
  1860. assert data[0]["equation|measurements.datacenter_memory / 3"] == 11
  1861. meta = response.data["meta"]
  1862. assert not meta["isMetricsData"]
  1863. def test_transaction_wildcard(self):
  1864. self.store_transaction_metric(
  1865. 1,
  1866. tags={"transaction": "foo_transaction"},
  1867. timestamp=self.min_ago,
  1868. )
  1869. self.store_transaction_metric(
  1870. 1,
  1871. tags={"transaction": "bar_transaction"},
  1872. timestamp=self.min_ago,
  1873. )
  1874. response = self.do_request(
  1875. {
  1876. "field": [
  1877. "transaction",
  1878. "p90()",
  1879. ],
  1880. "query": "transaction:foo*",
  1881. "dataset": "metrics",
  1882. }
  1883. )
  1884. assert response.status_code == 200, response.content
  1885. data = response.data["data"]
  1886. assert len(data) == 1
  1887. assert data[0]["p90()"] == 1
  1888. meta = response.data["meta"]
  1889. assert meta["isMetricsData"]
  1890. assert data[0]["transaction"] == "foo_transaction"
  1891. def test_transaction_status_wildcard(self):
  1892. self.store_transaction_metric(
  1893. 1,
  1894. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1895. timestamp=self.min_ago,
  1896. )
  1897. response = self.do_request(
  1898. {
  1899. "field": [
  1900. "transaction",
  1901. "p90()",
  1902. ],
  1903. "query": "transaction.status:f*bar",
  1904. "dataset": "metrics",
  1905. }
  1906. )
  1907. assert response.status_code == 200, response.content
  1908. data = response.data["data"]
  1909. assert len(data) == 1
  1910. assert data[0]["p90()"] == 1
  1911. meta = response.data["meta"]
  1912. assert meta["isMetricsData"]
  1913. def test_http_error_rate(self):
  1914. self.store_transaction_metric(
  1915. 1,
  1916. tags={
  1917. "transaction": "foo_transaction",
  1918. "transaction.status": "foobar",
  1919. "http.status_code": "500",
  1920. },
  1921. timestamp=self.min_ago,
  1922. )
  1923. self.store_transaction_metric(
  1924. 1,
  1925. tags={"transaction": "bar_transaction", "http.status_code": "400"},
  1926. timestamp=self.min_ago,
  1927. )
  1928. response = self.do_request(
  1929. {
  1930. "field": [
  1931. "http_error_rate()",
  1932. ],
  1933. "dataset": "metrics",
  1934. }
  1935. )
  1936. assert response.status_code == 200, response.content
  1937. data = response.data["data"]
  1938. assert len(data) == 1
  1939. assert data[0]["http_error_rate()"] == 0.5
  1940. meta = response.data["meta"]
  1941. assert meta["isMetricsData"]
  1942. def test_time_spent(self):
  1943. self.store_transaction_metric(
  1944. 1,
  1945. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1946. timestamp=self.min_ago,
  1947. )
  1948. self.store_transaction_metric(
  1949. 1,
  1950. tags={"transaction": "bar_transaction"},
  1951. timestamp=self.min_ago,
  1952. )
  1953. response = self.do_request(
  1954. {
  1955. "field": [
  1956. "transaction",
  1957. "time_spent_percentage()",
  1958. ],
  1959. "dataset": "metrics",
  1960. }
  1961. )
  1962. assert response.status_code == 200, response.content
  1963. data = response.data["data"]
  1964. assert len(data) == 2
  1965. assert data[0]["time_spent_percentage()"] == 0.5
  1966. meta = response.data["meta"]
  1967. assert meta["isMetricsData"]
  1968. def test_has_filter(self):
  1969. self.store_transaction_metric(
  1970. 1,
  1971. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1972. timestamp=self.min_ago,
  1973. )
  1974. response = self.do_request(
  1975. {
  1976. "field": [
  1977. "transaction",
  1978. "p50()",
  1979. ],
  1980. # For the metrics dataset, has on metrics should be no-ops
  1981. "query": "has:measurements.frames_frozen_rate",
  1982. "dataset": "metrics",
  1983. }
  1984. )
  1985. assert response.status_code == 200, response.content
  1986. data = response.data["data"]
  1987. assert len(data) == 1
  1988. assert data[0]["p50()"] == 1
  1989. meta = response.data["meta"]
  1990. assert meta["isMetricsData"]
  1991. response = self.do_request(
  1992. {
  1993. "field": [
  1994. "transaction",
  1995. "p50()",
  1996. ],
  1997. "query": "has:transaction.status",
  1998. "dataset": "metrics",
  1999. }
  2000. )
  2001. assert response.status_code == 200, response.content
  2002. data = response.data["data"]
  2003. assert len(data) == 1
  2004. assert data[0]["p50()"] == 1
  2005. meta = response.data["meta"]
  2006. assert meta["isMetricsData"]
  2007. def test_not_has_filter(self):
  2008. self.store_transaction_metric(
  2009. 1,
  2010. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  2011. timestamp=self.min_ago,
  2012. )
  2013. response = self.do_request(
  2014. {
  2015. "field": [
  2016. "transaction",
  2017. "p50()",
  2018. ],
  2019. "query": "!has:transaction.status",
  2020. "dataset": "metrics",
  2021. }
  2022. )
  2023. assert response.status_code == 200, response.content
  2024. data = response.data["data"]
  2025. assert len(data) == 0
  2026. meta = response.data["meta"]
  2027. assert meta["isMetricsData"]
  2028. response = self.do_request(
  2029. {
  2030. "field": [
  2031. "transaction",
  2032. "p50()",
  2033. ],
  2034. # Doing !has on the metrics dataset doesn't really make sense
  2035. "query": "!has:measurements.frames_frozen_rate",
  2036. "dataset": "metrics",
  2037. }
  2038. )
  2039. assert response.status_code == 400, response.content
  2040. def test_p50_with_count(self):
  2041. """Implicitly test the fact that percentiles are their own 'dataset'"""
  2042. self.store_transaction_metric(
  2043. 1,
  2044. tags={"transaction": "foo_transaction"},
  2045. timestamp=self.min_ago,
  2046. )
  2047. response = self.do_request(
  2048. {
  2049. "field": ["title", "p50()", "count()"],
  2050. "query": "event.type:transaction",
  2051. "dataset": "metrics",
  2052. "project": self.project.id,
  2053. "per_page": 50,
  2054. }
  2055. )
  2056. assert response.status_code == 200, response.content
  2057. assert len(response.data["data"]) == 1
  2058. data = response.data["data"]
  2059. meta = response.data["meta"]
  2060. field_meta = meta["fields"]
  2061. assert data[0]["title"] == "foo_transaction"
  2062. assert data[0]["p50()"] == 1
  2063. assert data[0]["count()"] == 1
  2064. assert meta["isMetricsData"]
  2065. assert field_meta["title"] == "string"
  2066. assert field_meta["p50()"] == "duration"
  2067. assert field_meta["count()"] == "integer"
  2068. def test_p75_with_count_and_more_groupby(self):
  2069. """Implicitly test the fact that percentiles are their own 'dataset'"""
  2070. self.store_transaction_metric(
  2071. 1,
  2072. tags={"transaction": "foo_transaction"},
  2073. timestamp=self.min_ago,
  2074. )
  2075. self.store_transaction_metric(
  2076. 5,
  2077. tags={"transaction": "bar_transaction"},
  2078. timestamp=self.min_ago,
  2079. )
  2080. self.store_transaction_metric(
  2081. 5,
  2082. tags={"transaction": "bar_transaction"},
  2083. timestamp=self.min_ago,
  2084. )
  2085. response = self.do_request(
  2086. {
  2087. "field": [
  2088. "title",
  2089. "project",
  2090. "p75()",
  2091. "count()",
  2092. ],
  2093. "query": "event.type:transaction",
  2094. "orderby": "count()",
  2095. "dataset": "metrics",
  2096. "project": self.project.id,
  2097. "per_page": 50,
  2098. }
  2099. )
  2100. assert response.status_code == 200, response.content
  2101. assert len(response.data["data"]) == 2
  2102. data = response.data["data"]
  2103. meta = response.data["meta"]
  2104. field_meta = meta["fields"]
  2105. assert data[0]["title"] == "foo_transaction"
  2106. assert data[0]["p75()"] == 1
  2107. assert data[0]["count()"] == 1
  2108. assert data[1]["title"] == "bar_transaction"
  2109. assert data[1]["p75()"] == 5
  2110. assert data[1]["count()"] == 2
  2111. assert meta["isMetricsData"]
  2112. assert field_meta["title"] == "string"
  2113. assert field_meta["p75()"] == "duration"
  2114. assert field_meta["count()"] == "integer"
  2115. def test_title_and_transaction_alias(self):
  2116. # Title and transaction are aliases to the same column
  2117. self.store_transaction_metric(
  2118. 1,
  2119. tags={"transaction": "foo_transaction"},
  2120. timestamp=self.min_ago,
  2121. )
  2122. response = self.do_request(
  2123. {
  2124. "field": [
  2125. "title",
  2126. "transaction",
  2127. "p75()",
  2128. ],
  2129. "query": "event.type:transaction",
  2130. "orderby": "p75()",
  2131. "dataset": "metrics",
  2132. "project": self.project.id,
  2133. "per_page": 50,
  2134. }
  2135. )
  2136. assert response.status_code == 200, response.content
  2137. assert len(response.data["data"]) == 1
  2138. data = response.data["data"]
  2139. meta = response.data["meta"]
  2140. field_meta = meta["fields"]
  2141. assert data[0]["title"] == "foo_transaction"
  2142. assert data[0]["transaction"] == "foo_transaction"
  2143. assert data[0]["p75()"] == 1
  2144. assert meta["isMetricsData"]
  2145. assert field_meta["title"] == "string"
  2146. assert field_meta["transaction"] == "string"
  2147. assert field_meta["p75()"] == "duration"
  2148. def test_maintain_sort_order_across_datasets(self):
  2149. self.store_transaction_metric(
  2150. 1,
  2151. tags={"transaction": "foo_transaction"},
  2152. timestamp=self.min_ago,
  2153. )
  2154. self.store_transaction_metric(
  2155. 1,
  2156. metric="user",
  2157. tags={"transaction": "bar_transaction"},
  2158. timestamp=self.min_ago,
  2159. )
  2160. self.store_transaction_metric(
  2161. 5,
  2162. tags={"transaction": "bar_transaction"},
  2163. timestamp=self.min_ago,
  2164. )
  2165. self.store_transaction_metric(
  2166. 5,
  2167. tags={"transaction": "bar_transaction"},
  2168. timestamp=self.min_ago,
  2169. )
  2170. response = self.do_request(
  2171. {
  2172. "field": [
  2173. "title",
  2174. "project",
  2175. "count()",
  2176. "count_unique(user)",
  2177. ],
  2178. "query": "event.type:transaction",
  2179. "orderby": "count()",
  2180. "dataset": "metrics",
  2181. "project": self.project.id,
  2182. "per_page": 50,
  2183. }
  2184. )
  2185. assert response.status_code == 200, response.content
  2186. data = response.data["data"]
  2187. meta = response.data["meta"]
  2188. field_meta = meta["fields"]
  2189. assert len(data) == 2
  2190. assert data[0]["title"] == "foo_transaction"
  2191. assert data[0]["count()"] == 1
  2192. assert data[0]["count_unique(user)"] == 0
  2193. assert data[1]["title"] == "bar_transaction"
  2194. assert data[1]["count()"] == 2
  2195. assert data[1]["count_unique(user)"] == 1
  2196. assert meta["isMetricsData"]
  2197. assert field_meta["title"] == "string"
  2198. assert field_meta["count()"] == "integer"
  2199. assert field_meta["count_unique(user)"] == "integer"
  2200. def test_avg_compare(self):
  2201. self.store_transaction_metric(
  2202. 100,
  2203. timestamp=self.min_ago,
  2204. tags={"release": "foo"},
  2205. )
  2206. self.store_transaction_metric(
  2207. 10,
  2208. timestamp=self.min_ago,
  2209. tags={"release": "bar"},
  2210. )
  2211. for function_name in [
  2212. "avg_compare(transaction.duration, release, foo, bar)",
  2213. 'avg_compare(transaction.duration, release, "foo", "bar")',
  2214. ]:
  2215. response = self.do_request(
  2216. {
  2217. "field": [function_name],
  2218. "query": "",
  2219. "project": self.project.id,
  2220. "dataset": "metrics",
  2221. }
  2222. )
  2223. assert response.status_code == 200, response.content
  2224. data = response.data["data"]
  2225. meta = response.data["meta"]
  2226. assert len(data) == 1
  2227. assert data[0][function_name] == -0.9
  2228. assert meta["dataset"] == "metrics"
  2229. assert meta["fields"][function_name] == "percent_change"
  2230. def test_avg_if(self):
  2231. self.store_transaction_metric(
  2232. 100,
  2233. timestamp=self.min_ago,
  2234. tags={"release": "foo"},
  2235. )
  2236. self.store_transaction_metric(
  2237. 200,
  2238. timestamp=self.min_ago,
  2239. tags={"release": "foo"},
  2240. )
  2241. self.store_transaction_metric(
  2242. 10,
  2243. timestamp=self.min_ago,
  2244. tags={"release": "bar"},
  2245. )
  2246. response = self.do_request(
  2247. {
  2248. "field": ["avg_if(transaction.duration, release, foo)"],
  2249. "query": "",
  2250. "project": self.project.id,
  2251. "dataset": "metrics",
  2252. }
  2253. )
  2254. assert response.status_code == 200, response.content
  2255. data = response.data["data"]
  2256. meta = response.data["meta"]
  2257. assert len(data) == 1
  2258. assert data[0]["avg_if(transaction.duration, release, foo)"] == 150
  2259. assert meta["dataset"] == "metrics"
  2260. assert meta["fields"]["avg_if(transaction.duration, release, foo)"] == "duration"
  2261. def test_device_class(self):
  2262. self.store_transaction_metric(
  2263. 100,
  2264. timestamp=self.min_ago,
  2265. tags={"device.class": "1"},
  2266. )
  2267. self.store_transaction_metric(
  2268. 200,
  2269. timestamp=self.min_ago,
  2270. tags={"device.class": "2"},
  2271. )
  2272. self.store_transaction_metric(
  2273. 300,
  2274. timestamp=self.min_ago,
  2275. tags={"device.class": ""},
  2276. )
  2277. response = self.do_request(
  2278. {
  2279. "field": ["device.class", "p95()"],
  2280. "query": "",
  2281. "orderby": "p95()",
  2282. "project": self.project.id,
  2283. "dataset": "metrics",
  2284. }
  2285. )
  2286. assert response.status_code == 200, response.content
  2287. data = response.data["data"]
  2288. meta = response.data["meta"]
  2289. assert len(data) == 3
  2290. # Need to actually check the dict since the level for 1 isn't guaranteed to stay `low` or `medium`
  2291. assert data[0]["device.class"] == map_device_class_level("1")
  2292. assert data[1]["device.class"] == map_device_class_level("2")
  2293. assert data[2]["device.class"] == "Unknown"
  2294. assert meta["fields"]["device.class"] == "string"
  2295. def test_device_class_filter(self):
  2296. self.store_transaction_metric(
  2297. 300,
  2298. timestamp=self.min_ago,
  2299. tags={"device.class": "1"},
  2300. )
  2301. # Need to actually check the dict since the level for 1 isn't guaranteed to stay `low`
  2302. level = map_device_class_level("1")
  2303. response = self.do_request(
  2304. {
  2305. "field": ["device.class", "count()"],
  2306. "query": f"device.class:{level}",
  2307. "orderby": "count()",
  2308. "project": self.project.id,
  2309. "dataset": "metrics",
  2310. }
  2311. )
  2312. assert response.status_code == 200, response.content
  2313. data = response.data["data"]
  2314. meta = response.data["meta"]
  2315. assert len(data) == 1
  2316. assert data[0]["device.class"] == level
  2317. assert meta["fields"]["device.class"] == "string"
  2318. class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithMetricLayer(
  2319. OrganizationEventsMetricsEnhancedPerformanceEndpointTest
  2320. ):
  2321. def setUp(self):
  2322. super().setUp()
  2323. self.features["organizations:use-metrics-layer"] = True
  2324. @pytest.mark.xfail(reason="Not supported")
  2325. def test_time_spent(self):
  2326. super().test_time_spent()
  2327. @pytest.mark.xfail(reason="Not supported")
  2328. def test_http_error_rate(self):
  2329. super().test_http_error_rate()
  2330. @pytest.mark.xfail(reason="Multiple aliases to same column not supported")
  2331. def test_title_and_transaction_alias(self):
  2332. super().test_title_and_transaction_alias()
  2333. @pytest.mark.xfail(reason="Sort order is flaking when querying multiple datasets")
  2334. def test_maintain_sort_order_across_datasets(self):
  2335. """You may need to run this test a few times to get it to fail"""
  2336. super().test_maintain_sort_order_across_datasets()
  2337. @pytest.mark.xfail(reason="Not implemented")
  2338. def test_avg_compare(self):
  2339. super().test_avg_compare()
  2340. @pytest.mark.xfail(reason="Not implemented")
  2341. def test_avg_if(self):
  2342. super().test_avg_if()
  2343. @pytest.mark.xfail(reason="Not implemented")
  2344. def test_device_class(self):
  2345. super().test_device_class()
  2346. @pytest.mark.xfail(reason="Not implemented")
  2347. def test_device_class_filter(self):
  2348. super().test_device_class_filter()