test_organization_events_mep.py 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622
  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. ],
  1022. "query": "event.type:transaction",
  1023. "dataset": "metricsEnhanced",
  1024. "per_page": 50,
  1025. }
  1026. )
  1027. assert response.status_code == 200, response.content
  1028. assert len(response.data["data"]) == 1
  1029. data = response.data["data"]
  1030. meta = response.data["meta"]
  1031. field_meta = meta["fields"]
  1032. assert data[0]["count_web_vitals(measurements.lcp, good)"] == 1
  1033. assert data[0]["count_web_vitals(measurements.fp, good)"] == 1
  1034. assert data[0]["count_web_vitals(measurements.fcp, meh)"] == 1
  1035. assert data[0]["count_web_vitals(measurements.fid, meh)"] == 1
  1036. assert data[0]["count_web_vitals(measurements.cls, good)"] == 1
  1037. assert meta["isMetricsData"]
  1038. assert field_meta["count_web_vitals(measurements.lcp, good)"] == "integer"
  1039. assert field_meta["count_web_vitals(measurements.fp, good)"] == "integer"
  1040. assert field_meta["count_web_vitals(measurements.fcp, meh)"] == "integer"
  1041. assert field_meta["count_web_vitals(measurements.fid, meh)"] == "integer"
  1042. assert field_meta["count_web_vitals(measurements.cls, good)"] == "integer"
  1043. def test_measurement_rating_that_does_not_exist(self):
  1044. self.store_transaction_metric(
  1045. 1,
  1046. metric="measurements.lcp",
  1047. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  1048. timestamp=self.min_ago,
  1049. )
  1050. response = self.do_request(
  1051. {
  1052. "field": ["transaction", "count_web_vitals(measurements.lcp, poor)"],
  1053. "query": "event.type:transaction",
  1054. "dataset": "metricsEnhanced",
  1055. "per_page": 50,
  1056. }
  1057. )
  1058. assert response.status_code == 200, response.content
  1059. assert len(response.data["data"]) == 1
  1060. data = response.data["data"]
  1061. meta = response.data["meta"]
  1062. assert data[0]["count_web_vitals(measurements.lcp, poor)"] == 0
  1063. assert meta["isMetricsData"]
  1064. assert meta["fields"]["count_web_vitals(measurements.lcp, poor)"] == "integer"
  1065. def test_count_web_vitals_invalid_vital(self):
  1066. query = {
  1067. "field": [
  1068. "count_web_vitals(measurements.foo, poor)",
  1069. ],
  1070. "project": [self.project.id],
  1071. "dataset": "metricsEnhanced",
  1072. }
  1073. response = self.do_request(query)
  1074. assert response.status_code == 400, response.content
  1075. query = {
  1076. "field": [
  1077. "count_web_vitals(tags[lcp], poor)",
  1078. ],
  1079. "project": [self.project.id],
  1080. "dataset": "metricsEnhanced",
  1081. }
  1082. response = self.do_request(query)
  1083. assert response.status_code == 400, response.content
  1084. query = {
  1085. "field": [
  1086. "count_web_vitals(transaction.duration, poor)",
  1087. ],
  1088. "project": [self.project.id],
  1089. "dataset": "metricsEnhanced",
  1090. }
  1091. response = self.do_request(query)
  1092. assert response.status_code == 400, response.content
  1093. query = {
  1094. "field": [
  1095. "count_web_vitals(measurements.lcp, bad)",
  1096. ],
  1097. "project": [self.project.id],
  1098. "dataset": "metricsEnhanced",
  1099. }
  1100. response = self.do_request(query)
  1101. assert response.status_code == 400, response.content
  1102. def test_count_unique_user_returns_zero(self):
  1103. self.store_transaction_metric(
  1104. 50,
  1105. metric="user",
  1106. tags={"transaction": "foo_transaction"},
  1107. timestamp=self.min_ago,
  1108. )
  1109. self.store_transaction_metric(
  1110. 50,
  1111. tags={"transaction": "foo_transaction"},
  1112. timestamp=self.min_ago,
  1113. )
  1114. self.store_transaction_metric(
  1115. 100,
  1116. tags={"transaction": "bar_transaction"},
  1117. timestamp=self.min_ago,
  1118. )
  1119. query = {
  1120. "project": [self.project.id],
  1121. "orderby": "p50()",
  1122. "field": [
  1123. "transaction",
  1124. "count_unique(user)",
  1125. "p50()",
  1126. ],
  1127. "dataset": "metricsEnhanced",
  1128. "per_page": 50,
  1129. }
  1130. response = self.do_request(query)
  1131. assert response.status_code == 200, response.content
  1132. assert len(response.data["data"]) == 2
  1133. data = response.data["data"]
  1134. meta = response.data["meta"]
  1135. assert data[0]["transaction"] == "foo_transaction"
  1136. assert data[0]["count_unique(user)"] == 1
  1137. assert data[1]["transaction"] == "bar_transaction"
  1138. assert data[1]["count_unique(user)"] == 0
  1139. assert meta["isMetricsData"]
  1140. def test_sum_transaction_duration(self):
  1141. self.store_transaction_metric(
  1142. 50,
  1143. tags={"transaction": "foo_transaction"},
  1144. timestamp=self.min_ago,
  1145. )
  1146. self.store_transaction_metric(
  1147. 100,
  1148. tags={"transaction": "foo_transaction"},
  1149. timestamp=self.min_ago,
  1150. )
  1151. self.store_transaction_metric(
  1152. 150,
  1153. tags={"transaction": "foo_transaction"},
  1154. timestamp=self.min_ago,
  1155. )
  1156. query = {
  1157. "project": [self.project.id],
  1158. "orderby": "sum(transaction.duration)",
  1159. "field": [
  1160. "transaction",
  1161. "sum(transaction.duration)",
  1162. ],
  1163. "dataset": "metricsEnhanced",
  1164. "per_page": 50,
  1165. }
  1166. response = self.do_request(query)
  1167. assert response.status_code == 200, response.content
  1168. assert len(response.data["data"]) == 1
  1169. data = response.data["data"]
  1170. meta = response.data["meta"]
  1171. assert data[0]["transaction"] == "foo_transaction"
  1172. assert data[0]["sum(transaction.duration)"] == 300
  1173. assert meta["isMetricsData"]
  1174. def test_custom_measurements_simple(self):
  1175. self.store_transaction_metric(
  1176. 1,
  1177. metric="measurements.something_custom",
  1178. internal_metric="d:transactions/measurements.something_custom@millisecond",
  1179. entity="metrics_distributions",
  1180. tags={"transaction": "foo_transaction"},
  1181. timestamp=self.min_ago,
  1182. )
  1183. query = {
  1184. "project": [self.project.id],
  1185. "orderby": "p50(measurements.something_custom)",
  1186. "field": [
  1187. "transaction",
  1188. "p50(measurements.something_custom)",
  1189. ],
  1190. "statsPeriod": "24h",
  1191. "dataset": "metricsEnhanced",
  1192. "per_page": 50,
  1193. }
  1194. self.wait_for_metric_count(
  1195. self.project,
  1196. 1,
  1197. metric="measurements.something_custom",
  1198. mri="d:transactions/measurements.something_custom@millisecond",
  1199. )
  1200. response = self.do_request(query)
  1201. assert response.status_code == 200, response.content
  1202. assert len(response.data["data"]) == 1
  1203. data = response.data["data"]
  1204. meta = response.data["meta"]
  1205. assert data[0]["transaction"] == "foo_transaction"
  1206. assert data[0]["p50(measurements.something_custom)"] == 1
  1207. assert meta["isMetricsData"]
  1208. assert meta["fields"]["p50(measurements.something_custom)"] == "duration"
  1209. assert meta["units"]["p50(measurements.something_custom)"] == "millisecond"
  1210. def test_custom_measurement_size_meta_type(self):
  1211. self.store_transaction_metric(
  1212. 100,
  1213. metric="measurements.custom_type",
  1214. internal_metric="d:transactions/measurements.custom_type@somethingcustom",
  1215. entity="metrics_distributions",
  1216. tags={"transaction": "foo_transaction"},
  1217. timestamp=self.min_ago,
  1218. )
  1219. self.store_transaction_metric(
  1220. 100,
  1221. metric="measurements.percent",
  1222. internal_metric="d:transactions/measurements.percent@ratio",
  1223. entity="metrics_distributions",
  1224. tags={"transaction": "foo_transaction"},
  1225. timestamp=self.min_ago,
  1226. )
  1227. self.store_transaction_metric(
  1228. 100,
  1229. metric="measurements.longtaskcount",
  1230. internal_metric="d:transactions/measurements.longtaskcount@none",
  1231. entity="metrics_distributions",
  1232. tags={"transaction": "foo_transaction"},
  1233. timestamp=self.min_ago,
  1234. )
  1235. query = {
  1236. "project": [self.project.id],
  1237. "orderby": "p50(measurements.longtaskcount)",
  1238. "field": [
  1239. "transaction",
  1240. "p50(measurements.longtaskcount)",
  1241. "p50(measurements.percent)",
  1242. "p50(measurements.custom_type)",
  1243. ],
  1244. "statsPeriod": "24h",
  1245. "dataset": "metricsEnhanced",
  1246. "per_page": 50,
  1247. }
  1248. response = self.do_request(query)
  1249. assert response.status_code == 200, response.content
  1250. assert len(response.data["data"]) == 1
  1251. data = response.data["data"]
  1252. meta = response.data["meta"]
  1253. assert data[0]["transaction"] == "foo_transaction"
  1254. assert data[0]["p50(measurements.longtaskcount)"] == 100
  1255. assert data[0]["p50(measurements.percent)"] == 100
  1256. assert data[0]["p50(measurements.custom_type)"] == 100
  1257. assert meta["isMetricsData"]
  1258. assert meta["fields"]["p50(measurements.longtaskcount)"] == "integer"
  1259. assert meta["units"]["p50(measurements.longtaskcount)"] is None
  1260. assert meta["fields"]["p50(measurements.percent)"] == "percentage"
  1261. assert meta["units"]["p50(measurements.percent)"] is None
  1262. assert meta["fields"]["p50(measurements.custom_type)"] == "number"
  1263. assert meta["units"]["p50(measurements.custom_type)"] is None
  1264. def test_custom_measurement_none_type(self):
  1265. self.store_transaction_metric(
  1266. 1,
  1267. metric="measurements.cls",
  1268. entity="metrics_distributions",
  1269. tags={"transaction": "foo_transaction"},
  1270. timestamp=self.min_ago,
  1271. )
  1272. query = {
  1273. "project": [self.project.id],
  1274. "orderby": "p75(measurements.cls)",
  1275. "field": [
  1276. "transaction",
  1277. "p75(measurements.cls)",
  1278. "p99(measurements.cls)",
  1279. "max(measurements.cls)",
  1280. ],
  1281. "statsPeriod": "24h",
  1282. "dataset": "metricsEnhanced",
  1283. "per_page": 50,
  1284. }
  1285. self.wait_for_metric_count(
  1286. self.project,
  1287. 1,
  1288. metric=TransactionMetricKey.MEASUREMENTS_CLS.value,
  1289. mri=TransactionMRI.MEASUREMENTS_CLS.value,
  1290. )
  1291. response = self.do_request(query)
  1292. assert response.status_code == 200, response.content
  1293. assert len(response.data["data"]) == 1
  1294. data = response.data["data"]
  1295. meta = response.data["meta"]
  1296. assert data[0]["transaction"] == "foo_transaction"
  1297. assert data[0]["p75(measurements.cls)"] == 1
  1298. assert data[0]["p99(measurements.cls)"] == 1
  1299. assert data[0]["max(measurements.cls)"] == 1
  1300. assert meta["isMetricsData"]
  1301. assert meta["fields"]["p75(measurements.cls)"] == "number"
  1302. assert meta["units"]["p75(measurements.cls)"] is None
  1303. assert meta["fields"]["p99(measurements.cls)"] == "number"
  1304. assert meta["units"]["p99(measurements.cls)"] is None
  1305. assert meta["fields"]["max(measurements.cls)"] == "number"
  1306. assert meta["units"]["max(measurements.cls)"] is None
  1307. def test_custom_measurement_duration_filtering(self):
  1308. self.store_transaction_metric(
  1309. 1,
  1310. metric="measurements.runtime",
  1311. internal_metric="d:transactions/measurements.runtime@hour",
  1312. entity="metrics_distributions",
  1313. tags={"transaction": "foo_transaction"},
  1314. timestamp=self.min_ago,
  1315. )
  1316. self.store_transaction_metric(
  1317. 180,
  1318. metric="measurements.runtime",
  1319. internal_metric="d:transactions/measurements.runtime@hour",
  1320. entity="metrics_distributions",
  1321. tags={"transaction": "bar_transaction"},
  1322. timestamp=self.min_ago,
  1323. )
  1324. query = {
  1325. "project": [self.project.id],
  1326. "field": [
  1327. "transaction",
  1328. "max(measurements.runtime)",
  1329. ],
  1330. "query": "p50(measurements.runtime):>1wk",
  1331. "statsPeriod": "24h",
  1332. "dataset": "metricsEnhanced",
  1333. "per_page": 50,
  1334. }
  1335. response = self.do_request(query)
  1336. assert response.status_code == 200, response.content
  1337. assert len(response.data["data"]) == 1
  1338. data = response.data["data"]
  1339. meta = response.data["meta"]
  1340. assert data[0]["transaction"] == "bar_transaction"
  1341. assert data[0]["max(measurements.runtime)"] == 180
  1342. assert meta["isMetricsData"]
  1343. def test_custom_measurement_size_filtering(self):
  1344. self.store_transaction_metric(
  1345. 1,
  1346. metric="measurements.datacenter_memory",
  1347. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1348. entity="metrics_distributions",
  1349. tags={"transaction": "foo_transaction"},
  1350. timestamp=self.min_ago,
  1351. )
  1352. self.store_transaction_metric(
  1353. 100,
  1354. metric="measurements.datacenter_memory",
  1355. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1356. entity="metrics_distributions",
  1357. tags={"transaction": "bar_transaction"},
  1358. timestamp=self.min_ago,
  1359. )
  1360. query = {
  1361. "project": [self.project.id],
  1362. "field": [
  1363. "transaction",
  1364. "max(measurements.datacenter_memory)",
  1365. ],
  1366. "query": "p50(measurements.datacenter_memory):>5pb",
  1367. "statsPeriod": "24h",
  1368. "dataset": "metricsEnhanced",
  1369. "per_page": 50,
  1370. }
  1371. response = self.do_request(query)
  1372. assert response.status_code == 200, response.content
  1373. assert len(response.data["data"]) == 1
  1374. data = response.data["data"]
  1375. meta = response.data["meta"]
  1376. assert data[0]["transaction"] == "bar_transaction"
  1377. assert data[0]["max(measurements.datacenter_memory)"] == 100
  1378. assert meta["units"]["max(measurements.datacenter_memory)"] == "petabyte"
  1379. assert meta["fields"]["max(measurements.datacenter_memory)"] == "size"
  1380. assert meta["isMetricsData"]
  1381. def test_has_custom_measurement(self):
  1382. self.store_transaction_metric(
  1383. 33,
  1384. metric="measurements.datacenter_memory",
  1385. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1386. entity="metrics_distributions",
  1387. tags={"transaction": "foo_transaction"},
  1388. timestamp=self.min_ago,
  1389. )
  1390. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1391. transaction_data["measurements"]["datacenter_memory"] = {
  1392. "value": 33,
  1393. "unit": "petabyte",
  1394. }
  1395. self.store_event(transaction_data, self.project.id)
  1396. measurement = "measurements.datacenter_memory"
  1397. response = self.do_request(
  1398. {
  1399. "field": ["transaction", measurement],
  1400. "query": "has:measurements.datacenter_memory",
  1401. "dataset": "discover",
  1402. }
  1403. )
  1404. assert response.status_code == 200, response.content
  1405. assert len(response.data["data"]) == 1
  1406. response = self.do_request(
  1407. {
  1408. "field": ["transaction", measurement],
  1409. "query": "!has:measurements.datacenter_memory",
  1410. "dataset": "discover",
  1411. }
  1412. )
  1413. assert response.status_code == 200, response.content
  1414. assert len(response.data["data"]) == 0
  1415. def test_environment_param(self):
  1416. self.create_environment(self.project, name="staging")
  1417. self.store_transaction_metric(
  1418. 1,
  1419. tags={"transaction": "foo_transaction", "environment": "staging"},
  1420. timestamp=self.min_ago,
  1421. )
  1422. self.store_transaction_metric(
  1423. 100,
  1424. tags={"transaction": "foo_transaction"},
  1425. timestamp=self.min_ago,
  1426. )
  1427. query = {
  1428. "project": [self.project.id],
  1429. "environment": "staging",
  1430. "orderby": "p50(transaction.duration)",
  1431. "field": [
  1432. "transaction",
  1433. "environment",
  1434. "p50(transaction.duration)",
  1435. ],
  1436. "statsPeriod": "24h",
  1437. "dataset": "metricsEnhanced",
  1438. "per_page": 50,
  1439. }
  1440. response = self.do_request(query)
  1441. assert response.status_code == 200, response.content
  1442. assert len(response.data["data"]) == 1
  1443. data = response.data["data"]
  1444. meta = response.data["meta"]
  1445. assert data[0]["transaction"] == "foo_transaction"
  1446. assert data[0]["environment"] == "staging"
  1447. assert data[0]["p50(transaction.duration)"] == 1
  1448. assert meta["isMetricsData"]
  1449. @pytest.mark.xfail(reason="Started failing on ClickHouse 21.8")
  1450. def test_environment_query(self):
  1451. self.create_environment(self.project, name="staging")
  1452. self.store_transaction_metric(
  1453. 1,
  1454. tags={"transaction": "foo_transaction", "environment": "staging"},
  1455. timestamp=self.min_ago,
  1456. )
  1457. self.store_transaction_metric(
  1458. 100,
  1459. tags={"transaction": "foo_transaction"},
  1460. timestamp=self.min_ago,
  1461. )
  1462. query = {
  1463. "project": [self.project.id],
  1464. "orderby": "p50(transaction.duration)",
  1465. "field": [
  1466. "transaction",
  1467. "environment",
  1468. "p50(transaction.duration)",
  1469. ],
  1470. "query": "!has:environment",
  1471. "statsPeriod": "24h",
  1472. "dataset": "metricsEnhanced",
  1473. "per_page": 50,
  1474. }
  1475. self.wait_for_metric_count(self.project, 2)
  1476. response = self.do_request(query)
  1477. assert response.status_code == 200, response.content
  1478. assert len(response.data["data"]) == 1
  1479. data = response.data["data"]
  1480. meta = response.data["meta"]
  1481. assert data[0]["transaction"] == "foo_transaction"
  1482. assert data[0]["environment"] is None or data[0]["environment"] == ""
  1483. assert data[0]["p50(transaction.duration)"] == 100
  1484. assert meta["isMetricsData"]
  1485. def test_has_transaction(self):
  1486. self.store_transaction_metric(
  1487. 1,
  1488. tags={},
  1489. timestamp=self.min_ago,
  1490. )
  1491. self.store_transaction_metric(
  1492. 100,
  1493. tags={"transaction": "foo_transaction"},
  1494. timestamp=self.min_ago,
  1495. )
  1496. query = {
  1497. "project": [self.project.id],
  1498. "orderby": "p50(transaction.duration)",
  1499. "field": [
  1500. "transaction",
  1501. "p50(transaction.duration)",
  1502. ],
  1503. "query": "has:transaction",
  1504. "statsPeriod": "24h",
  1505. "dataset": "metricsEnhanced",
  1506. "per_page": 50,
  1507. }
  1508. self.wait_for_metric_count(self.project, 2)
  1509. response = self.do_request(query)
  1510. assert response.status_code == 200, response.content
  1511. assert len(response.data["data"]) == 2
  1512. data = response.data["data"]
  1513. meta = response.data["meta"]
  1514. assert data[0]["transaction"] == "<< unparameterized >>"
  1515. assert data[0]["p50(transaction.duration)"] == 1
  1516. assert data[1]["transaction"] == "foo_transaction"
  1517. assert data[1]["p50(transaction.duration)"] == 100
  1518. assert meta["isMetricsData"]
  1519. query = {
  1520. "project": [self.project.id],
  1521. "orderby": "p50(transaction.duration)",
  1522. "field": [
  1523. "transaction",
  1524. "p50(transaction.duration)",
  1525. ],
  1526. "query": "!has:transaction",
  1527. "statsPeriod": "24h",
  1528. "dataset": "metricsEnhanced",
  1529. "per_page": 50,
  1530. }
  1531. response = self.do_request(query)
  1532. assert response.status_code == 400, response.content
  1533. def test_apdex_transaction_threshold(self):
  1534. ProjectTransactionThresholdOverride.objects.create(
  1535. transaction="foo_transaction",
  1536. project=self.project,
  1537. organization=self.project.organization,
  1538. threshold=600,
  1539. metric=TransactionMetric.LCP.value,
  1540. )
  1541. ProjectTransactionThresholdOverride.objects.create(
  1542. transaction="bar_transaction",
  1543. project=self.project,
  1544. organization=self.project.organization,
  1545. threshold=600,
  1546. metric=TransactionMetric.LCP.value,
  1547. )
  1548. self.store_transaction_metric(
  1549. 1,
  1550. tags={
  1551. "transaction": "foo_transaction",
  1552. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1553. },
  1554. timestamp=self.min_ago,
  1555. )
  1556. self.store_transaction_metric(
  1557. 1,
  1558. "measurements.lcp",
  1559. tags={
  1560. "transaction": "bar_transaction",
  1561. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1562. },
  1563. timestamp=self.min_ago,
  1564. )
  1565. response = self.do_request(
  1566. {
  1567. "field": [
  1568. "transaction",
  1569. "apdex()",
  1570. ],
  1571. "orderby": ["apdex()"],
  1572. "query": "event.type:transaction",
  1573. "dataset": "metrics",
  1574. "per_page": 50,
  1575. }
  1576. )
  1577. assert len(response.data["data"]) == 2
  1578. data = response.data["data"]
  1579. meta = response.data["meta"]
  1580. field_meta = meta["fields"]
  1581. assert data[0]["transaction"] == "bar_transaction"
  1582. # Threshold is lcp based
  1583. assert data[0]["apdex()"] == 1
  1584. assert data[1]["transaction"] == "foo_transaction"
  1585. # Threshold is lcp based
  1586. assert data[1]["apdex()"] == 0
  1587. assert meta["isMetricsData"]
  1588. assert field_meta["transaction"] == "string"
  1589. assert field_meta["apdex()"] == "number"
  1590. def test_apdex_project_threshold(self):
  1591. ProjectTransactionThreshold.objects.create(
  1592. project=self.project,
  1593. organization=self.project.organization,
  1594. threshold=600,
  1595. metric=TransactionMetric.LCP.value,
  1596. )
  1597. self.store_transaction_metric(
  1598. 1,
  1599. tags={
  1600. "transaction": "foo_transaction",
  1601. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1602. },
  1603. timestamp=self.min_ago,
  1604. )
  1605. self.store_transaction_metric(
  1606. 1,
  1607. "measurements.lcp",
  1608. tags={
  1609. "transaction": "bar_transaction",
  1610. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1611. },
  1612. timestamp=self.min_ago,
  1613. )
  1614. response = self.do_request(
  1615. {
  1616. "field": [
  1617. "transaction",
  1618. "apdex()",
  1619. ],
  1620. "orderby": ["apdex()"],
  1621. "query": "event.type:transaction",
  1622. "dataset": "metrics",
  1623. "per_page": 50,
  1624. }
  1625. )
  1626. assert response.status_code == 200, response.content
  1627. assert len(response.data["data"]) == 2
  1628. data = response.data["data"]
  1629. meta = response.data["meta"]
  1630. field_meta = meta["fields"]
  1631. assert data[0]["transaction"] == "bar_transaction"
  1632. # Threshold is lcp based
  1633. assert data[0]["apdex()"] == 1
  1634. assert data[1]["transaction"] == "foo_transaction"
  1635. # Threshold is lcp based
  1636. assert data[1]["apdex()"] == 0
  1637. assert meta["isMetricsData"]
  1638. assert field_meta["transaction"] == "string"
  1639. assert field_meta["apdex()"] == "number"
  1640. def test_apdex_satisfaction_param(self):
  1641. for function in ["apdex(300)", "user_misery(300)", "count_miserable(user, 300)"]:
  1642. query = {
  1643. "project": [self.project.id],
  1644. "field": [
  1645. "transaction",
  1646. function,
  1647. ],
  1648. "statsPeriod": "24h",
  1649. "dataset": "metricsEnhanced",
  1650. "per_page": 50,
  1651. }
  1652. response = self.do_request(query)
  1653. assert response.status_code == 200, response.content
  1654. assert len(response.data["data"]) == 0
  1655. meta = response.data["meta"]
  1656. assert not meta["isMetricsData"], function
  1657. query = {
  1658. "project": [self.project.id],
  1659. "field": [
  1660. "transaction",
  1661. function,
  1662. ],
  1663. "statsPeriod": "24h",
  1664. "dataset": "metrics",
  1665. "per_page": 50,
  1666. }
  1667. response = self.do_request(query)
  1668. assert response.status_code == 400, function
  1669. assert b"threshold parameter" in response.content, function
  1670. def test_mobile_metrics(self):
  1671. self.store_transaction_metric(
  1672. 0.4,
  1673. "measurements.frames_frozen_rate",
  1674. tags={
  1675. "transaction": "bar_transaction",
  1676. },
  1677. timestamp=self.min_ago,
  1678. )
  1679. query = {
  1680. "project": [self.project.id],
  1681. "field": [
  1682. "transaction",
  1683. "p50(measurements.frames_frozen_rate)",
  1684. ],
  1685. "statsPeriod": "24h",
  1686. "dataset": "metrics",
  1687. "per_page": 50,
  1688. }
  1689. response = self.do_request(query)
  1690. assert response.status_code == 200, response.content
  1691. assert len(response.data["data"]) == 1
  1692. assert response.data["data"][0]["p50(measurements.frames_frozen_rate)"] == 0.4
  1693. def test_merge_null_unparam(self):
  1694. self.store_transaction_metric(
  1695. 1,
  1696. # Transaction: unparam
  1697. tags={
  1698. "transaction": "<< unparameterized >>",
  1699. },
  1700. timestamp=self.min_ago,
  1701. )
  1702. self.store_transaction_metric(
  1703. 2,
  1704. # Transaction:null
  1705. tags={},
  1706. timestamp=self.min_ago,
  1707. )
  1708. query = {
  1709. "project": [self.project.id],
  1710. "field": [
  1711. "transaction",
  1712. "p50(transaction.duration)",
  1713. ],
  1714. "statsPeriod": "24h",
  1715. "dataset": "metrics",
  1716. "per_page": 50,
  1717. }
  1718. response = self.do_request(query)
  1719. assert response.status_code == 200, response.content
  1720. assert len(response.data["data"]) == 1
  1721. assert response.data["data"][0]["p50(transaction.duration)"] == 1.5
  1722. def test_unparam_filter(self):
  1723. self.store_transaction_metric(
  1724. 1,
  1725. # Transaction: unparam
  1726. tags={
  1727. "transaction": "<< unparameterized >>",
  1728. },
  1729. timestamp=self.min_ago,
  1730. )
  1731. self.store_transaction_metric(
  1732. 2,
  1733. # Transaction:null
  1734. tags={},
  1735. timestamp=self.min_ago,
  1736. )
  1737. self.store_transaction_metric(
  1738. 3,
  1739. tags={
  1740. "transaction": "foo_transaction",
  1741. },
  1742. timestamp=self.min_ago,
  1743. )
  1744. query = {
  1745. "project": [self.project.id],
  1746. "field": [
  1747. "transaction",
  1748. "count()",
  1749. ],
  1750. "query": 'transaction:"<< unparameterized >>"',
  1751. "statsPeriod": "24h",
  1752. "dataset": "metrics",
  1753. "per_page": 50,
  1754. }
  1755. self.wait_for_metric_count(self.project, 3)
  1756. response = self.do_request(query)
  1757. assert response.status_code == 200, response.content
  1758. assert len(response.data["data"]) == 1
  1759. assert response.data["data"][0]["transaction"] == "<< unparameterized >>"
  1760. assert response.data["data"][0]["count()"] == 2
  1761. def test_custom_measurements_without_function(self):
  1762. self.store_transaction_metric(
  1763. 33,
  1764. metric="measurements.datacenter_memory",
  1765. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1766. entity="metrics_distributions",
  1767. tags={"transaction": "foo_transaction"},
  1768. timestamp=self.min_ago,
  1769. )
  1770. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1771. transaction_data["measurements"]["datacenter_memory"] = {
  1772. "value": 33,
  1773. "unit": "petabyte",
  1774. }
  1775. self.store_event(transaction_data, self.project.id)
  1776. measurement = "measurements.datacenter_memory"
  1777. response = self.do_request(
  1778. {
  1779. "field": ["transaction", measurement],
  1780. "query": "measurements.datacenter_memory:33pb",
  1781. "dataset": "discover",
  1782. }
  1783. )
  1784. assert response.status_code == 200, response.content
  1785. data = response.data["data"]
  1786. assert len(data) == 1
  1787. assert data[0][measurement] == 33
  1788. meta = response.data["meta"]
  1789. field_meta = meta["fields"]
  1790. unit_meta = meta["units"]
  1791. assert field_meta[measurement] == "size"
  1792. assert unit_meta[measurement] == "petabyte"
  1793. assert not meta["isMetricsData"]
  1794. def test_custom_measurements_with_function(self):
  1795. self.store_transaction_metric(
  1796. 33,
  1797. metric="measurements.datacenter_memory",
  1798. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1799. entity="metrics_distributions",
  1800. tags={"transaction": "foo_transaction"},
  1801. timestamp=self.min_ago,
  1802. )
  1803. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1804. transaction_data["measurements"]["datacenter_memory"] = {
  1805. "value": 33,
  1806. "unit": "petabyte",
  1807. }
  1808. self.store_event(transaction_data, self.project.id)
  1809. measurement = "p50(measurements.datacenter_memory)"
  1810. response = self.do_request(
  1811. {
  1812. "field": ["transaction", measurement],
  1813. "query": "measurements.datacenter_memory:33pb",
  1814. "dataset": "discover",
  1815. }
  1816. )
  1817. assert response.status_code == 200, response.content
  1818. data = response.data["data"]
  1819. assert len(data) == 1
  1820. assert data[0][measurement] == 33
  1821. meta = response.data["meta"]
  1822. field_meta = meta["fields"]
  1823. unit_meta = meta["units"]
  1824. assert field_meta[measurement] == "size"
  1825. assert unit_meta[measurement] == "petabyte"
  1826. assert not meta["isMetricsData"]
  1827. def test_custom_measurements_equation(self):
  1828. self.store_transaction_metric(
  1829. 33,
  1830. metric="measurements.datacenter_memory",
  1831. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1832. entity="metrics_distributions",
  1833. tags={"transaction": "foo_transaction"},
  1834. timestamp=self.min_ago,
  1835. )
  1836. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1837. transaction_data["measurements"]["datacenter_memory"] = {
  1838. "value": 33,
  1839. "unit": "petabyte",
  1840. }
  1841. self.store_event(transaction_data, self.project.id)
  1842. response = self.do_request(
  1843. {
  1844. "field": [
  1845. "transaction",
  1846. "measurements.datacenter_memory",
  1847. "equation|measurements.datacenter_memory / 3",
  1848. ],
  1849. "query": "",
  1850. "dataset": "discover",
  1851. }
  1852. )
  1853. assert response.status_code == 200, response.content
  1854. data = response.data["data"]
  1855. assert len(data) == 1
  1856. assert data[0]["measurements.datacenter_memory"] == 33
  1857. assert data[0]["equation|measurements.datacenter_memory / 3"] == 11
  1858. meta = response.data["meta"]
  1859. assert not meta["isMetricsData"]
  1860. def test_transaction_wildcard(self):
  1861. self.store_transaction_metric(
  1862. 1,
  1863. tags={"transaction": "foo_transaction"},
  1864. timestamp=self.min_ago,
  1865. )
  1866. self.store_transaction_metric(
  1867. 1,
  1868. tags={"transaction": "bar_transaction"},
  1869. timestamp=self.min_ago,
  1870. )
  1871. response = self.do_request(
  1872. {
  1873. "field": [
  1874. "transaction",
  1875. "p90()",
  1876. ],
  1877. "query": "transaction:foo*",
  1878. "dataset": "metrics",
  1879. }
  1880. )
  1881. assert response.status_code == 200, response.content
  1882. data = response.data["data"]
  1883. assert len(data) == 1
  1884. assert data[0]["p90()"] == 1
  1885. meta = response.data["meta"]
  1886. assert meta["isMetricsData"]
  1887. assert data[0]["transaction"] == "foo_transaction"
  1888. def test_transaction_status_wildcard(self):
  1889. self.store_transaction_metric(
  1890. 1,
  1891. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1892. timestamp=self.min_ago,
  1893. )
  1894. response = self.do_request(
  1895. {
  1896. "field": [
  1897. "transaction",
  1898. "p90()",
  1899. ],
  1900. "query": "transaction.status:f*bar",
  1901. "dataset": "metrics",
  1902. }
  1903. )
  1904. assert response.status_code == 200, response.content
  1905. data = response.data["data"]
  1906. assert len(data) == 1
  1907. assert data[0]["p90()"] == 1
  1908. meta = response.data["meta"]
  1909. assert meta["isMetricsData"]
  1910. def test_http_error_rate(self):
  1911. self.store_transaction_metric(
  1912. 1,
  1913. tags={
  1914. "transaction": "foo_transaction",
  1915. "transaction.status": "foobar",
  1916. "http.status_code": "500",
  1917. },
  1918. timestamp=self.min_ago,
  1919. )
  1920. self.store_transaction_metric(
  1921. 1,
  1922. tags={"transaction": "bar_transaction", "http.status_code": "400"},
  1923. timestamp=self.min_ago,
  1924. )
  1925. response = self.do_request(
  1926. {
  1927. "field": [
  1928. "http_error_rate()",
  1929. ],
  1930. "dataset": "metrics",
  1931. }
  1932. )
  1933. assert response.status_code == 200, response.content
  1934. data = response.data["data"]
  1935. assert len(data) == 1
  1936. assert data[0]["http_error_rate()"] == 0.5
  1937. meta = response.data["meta"]
  1938. assert meta["isMetricsData"]
  1939. def test_time_spent(self):
  1940. self.store_transaction_metric(
  1941. 1,
  1942. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1943. timestamp=self.min_ago,
  1944. )
  1945. self.store_transaction_metric(
  1946. 1,
  1947. tags={"transaction": "bar_transaction"},
  1948. timestamp=self.min_ago,
  1949. )
  1950. response = self.do_request(
  1951. {
  1952. "field": [
  1953. "transaction",
  1954. "time_spent_percentage()",
  1955. ],
  1956. "dataset": "metrics",
  1957. }
  1958. )
  1959. assert response.status_code == 200, response.content
  1960. data = response.data["data"]
  1961. assert len(data) == 2
  1962. assert data[0]["time_spent_percentage()"] == 0.5
  1963. meta = response.data["meta"]
  1964. assert meta["isMetricsData"]
  1965. def test_has_filter(self):
  1966. self.store_transaction_metric(
  1967. 1,
  1968. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1969. timestamp=self.min_ago,
  1970. )
  1971. response = self.do_request(
  1972. {
  1973. "field": [
  1974. "transaction",
  1975. "p50()",
  1976. ],
  1977. # For the metrics dataset, has on metrics should be no-ops
  1978. "query": "has:measurements.frames_frozen_rate",
  1979. "dataset": "metrics",
  1980. }
  1981. )
  1982. assert response.status_code == 200, response.content
  1983. data = response.data["data"]
  1984. assert len(data) == 1
  1985. assert data[0]["p50()"] == 1
  1986. meta = response.data["meta"]
  1987. assert meta["isMetricsData"]
  1988. response = self.do_request(
  1989. {
  1990. "field": [
  1991. "transaction",
  1992. "p50()",
  1993. ],
  1994. "query": "has:transaction.status",
  1995. "dataset": "metrics",
  1996. }
  1997. )
  1998. assert response.status_code == 200, response.content
  1999. data = response.data["data"]
  2000. assert len(data) == 1
  2001. assert data[0]["p50()"] == 1
  2002. meta = response.data["meta"]
  2003. assert meta["isMetricsData"]
  2004. def test_not_has_filter(self):
  2005. self.store_transaction_metric(
  2006. 1,
  2007. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  2008. timestamp=self.min_ago,
  2009. )
  2010. response = self.do_request(
  2011. {
  2012. "field": [
  2013. "transaction",
  2014. "p50()",
  2015. ],
  2016. "query": "!has:transaction.status",
  2017. "dataset": "metrics",
  2018. }
  2019. )
  2020. assert response.status_code == 200, response.content
  2021. data = response.data["data"]
  2022. assert len(data) == 0
  2023. meta = response.data["meta"]
  2024. assert meta["isMetricsData"]
  2025. response = self.do_request(
  2026. {
  2027. "field": [
  2028. "transaction",
  2029. "p50()",
  2030. ],
  2031. # Doing !has on the metrics dataset doesn't really make sense
  2032. "query": "!has:measurements.frames_frozen_rate",
  2033. "dataset": "metrics",
  2034. }
  2035. )
  2036. assert response.status_code == 400, response.content
  2037. def test_p50_with_count(self):
  2038. """Implicitly test the fact that percentiles are their own 'dataset'"""
  2039. self.store_transaction_metric(
  2040. 1,
  2041. tags={"transaction": "foo_transaction"},
  2042. timestamp=self.min_ago,
  2043. )
  2044. response = self.do_request(
  2045. {
  2046. "field": ["title", "p50()", "count()"],
  2047. "query": "event.type:transaction",
  2048. "dataset": "metrics",
  2049. "project": self.project.id,
  2050. "per_page": 50,
  2051. }
  2052. )
  2053. assert response.status_code == 200, response.content
  2054. assert len(response.data["data"]) == 1
  2055. data = response.data["data"]
  2056. meta = response.data["meta"]
  2057. field_meta = meta["fields"]
  2058. assert data[0]["title"] == "foo_transaction"
  2059. assert data[0]["p50()"] == 1
  2060. assert data[0]["count()"] == 1
  2061. assert meta["isMetricsData"]
  2062. assert field_meta["title"] == "string"
  2063. assert field_meta["p50()"] == "duration"
  2064. assert field_meta["count()"] == "integer"
  2065. def test_p75_with_count_and_more_groupby(self):
  2066. """Implicitly test the fact that percentiles are their own 'dataset'"""
  2067. self.store_transaction_metric(
  2068. 1,
  2069. tags={"transaction": "foo_transaction"},
  2070. timestamp=self.min_ago,
  2071. )
  2072. self.store_transaction_metric(
  2073. 5,
  2074. tags={"transaction": "bar_transaction"},
  2075. timestamp=self.min_ago,
  2076. )
  2077. self.store_transaction_metric(
  2078. 5,
  2079. tags={"transaction": "bar_transaction"},
  2080. timestamp=self.min_ago,
  2081. )
  2082. response = self.do_request(
  2083. {
  2084. "field": [
  2085. "title",
  2086. "project",
  2087. "p75()",
  2088. "count()",
  2089. ],
  2090. "query": "event.type:transaction",
  2091. "orderby": "count()",
  2092. "dataset": "metrics",
  2093. "project": self.project.id,
  2094. "per_page": 50,
  2095. }
  2096. )
  2097. assert response.status_code == 200, response.content
  2098. assert len(response.data["data"]) == 2
  2099. data = response.data["data"]
  2100. meta = response.data["meta"]
  2101. field_meta = meta["fields"]
  2102. assert data[0]["title"] == "foo_transaction"
  2103. assert data[0]["p75()"] == 1
  2104. assert data[0]["count()"] == 1
  2105. assert data[1]["title"] == "bar_transaction"
  2106. assert data[1]["p75()"] == 5
  2107. assert data[1]["count()"] == 2
  2108. assert meta["isMetricsData"]
  2109. assert field_meta["title"] == "string"
  2110. assert field_meta["p75()"] == "duration"
  2111. assert field_meta["count()"] == "integer"
  2112. def test_title_and_transaction_alias(self):
  2113. # Title and transaction are aliases to the same column
  2114. self.store_transaction_metric(
  2115. 1,
  2116. tags={"transaction": "foo_transaction"},
  2117. timestamp=self.min_ago,
  2118. )
  2119. response = self.do_request(
  2120. {
  2121. "field": [
  2122. "title",
  2123. "transaction",
  2124. "p75()",
  2125. ],
  2126. "query": "event.type:transaction",
  2127. "orderby": "p75()",
  2128. "dataset": "metrics",
  2129. "project": self.project.id,
  2130. "per_page": 50,
  2131. }
  2132. )
  2133. assert response.status_code == 200, response.content
  2134. assert len(response.data["data"]) == 1
  2135. data = response.data["data"]
  2136. meta = response.data["meta"]
  2137. field_meta = meta["fields"]
  2138. assert data[0]["title"] == "foo_transaction"
  2139. assert data[0]["transaction"] == "foo_transaction"
  2140. assert data[0]["p75()"] == 1
  2141. assert meta["isMetricsData"]
  2142. assert field_meta["title"] == "string"
  2143. assert field_meta["transaction"] == "string"
  2144. assert field_meta["p75()"] == "duration"
  2145. def test_maintain_sort_order_across_datasets(self):
  2146. self.store_transaction_metric(
  2147. 1,
  2148. tags={"transaction": "foo_transaction"},
  2149. timestamp=self.min_ago,
  2150. )
  2151. self.store_transaction_metric(
  2152. 1,
  2153. metric="user",
  2154. tags={"transaction": "bar_transaction"},
  2155. timestamp=self.min_ago,
  2156. )
  2157. self.store_transaction_metric(
  2158. 5,
  2159. tags={"transaction": "bar_transaction"},
  2160. timestamp=self.min_ago,
  2161. )
  2162. self.store_transaction_metric(
  2163. 5,
  2164. tags={"transaction": "bar_transaction"},
  2165. timestamp=self.min_ago,
  2166. )
  2167. response = self.do_request(
  2168. {
  2169. "field": [
  2170. "title",
  2171. "project",
  2172. "count()",
  2173. "count_unique(user)",
  2174. ],
  2175. "query": "event.type:transaction",
  2176. "orderby": "count()",
  2177. "dataset": "metrics",
  2178. "project": self.project.id,
  2179. "per_page": 50,
  2180. }
  2181. )
  2182. assert response.status_code == 200, response.content
  2183. data = response.data["data"]
  2184. meta = response.data["meta"]
  2185. field_meta = meta["fields"]
  2186. assert len(data) == 2
  2187. assert data[0]["title"] == "foo_transaction"
  2188. assert data[0]["count()"] == 1
  2189. assert data[0]["count_unique(user)"] == 0
  2190. assert data[1]["title"] == "bar_transaction"
  2191. assert data[1]["count()"] == 2
  2192. assert data[1]["count_unique(user)"] == 1
  2193. assert meta["isMetricsData"]
  2194. assert field_meta["title"] == "string"
  2195. assert field_meta["count()"] == "integer"
  2196. assert field_meta["count_unique(user)"] == "integer"
  2197. def test_avg_compare(self):
  2198. self.store_transaction_metric(
  2199. 100,
  2200. timestamp=self.min_ago,
  2201. tags={"release": "foo"},
  2202. )
  2203. self.store_transaction_metric(
  2204. 10,
  2205. timestamp=self.min_ago,
  2206. tags={"release": "bar"},
  2207. )
  2208. for function_name in [
  2209. "avg_compare(transaction.duration, release, foo, bar)",
  2210. 'avg_compare(transaction.duration, release, "foo", "bar")',
  2211. ]:
  2212. response = self.do_request(
  2213. {
  2214. "field": [function_name],
  2215. "query": "",
  2216. "project": self.project.id,
  2217. "dataset": "metrics",
  2218. }
  2219. )
  2220. assert response.status_code == 200, response.content
  2221. data = response.data["data"]
  2222. meta = response.data["meta"]
  2223. assert len(data) == 1
  2224. assert data[0][function_name] == -0.9
  2225. assert meta["dataset"] == "metrics"
  2226. assert meta["fields"][function_name] == "percent_change"
  2227. def test_avg_if(self):
  2228. self.store_transaction_metric(
  2229. 100,
  2230. timestamp=self.min_ago,
  2231. tags={"release": "foo"},
  2232. )
  2233. self.store_transaction_metric(
  2234. 200,
  2235. timestamp=self.min_ago,
  2236. tags={"release": "foo"},
  2237. )
  2238. self.store_transaction_metric(
  2239. 10,
  2240. timestamp=self.min_ago,
  2241. tags={"release": "bar"},
  2242. )
  2243. response = self.do_request(
  2244. {
  2245. "field": ["avg_if(transaction.duration, release, foo)"],
  2246. "query": "",
  2247. "project": self.project.id,
  2248. "dataset": "metrics",
  2249. }
  2250. )
  2251. assert response.status_code == 200, response.content
  2252. data = response.data["data"]
  2253. meta = response.data["meta"]
  2254. assert len(data) == 1
  2255. assert data[0]["avg_if(transaction.duration, release, foo)"] == 150
  2256. assert meta["dataset"] == "metrics"
  2257. assert meta["fields"]["avg_if(transaction.duration, release, foo)"] == "duration"
  2258. def test_device_class(self):
  2259. self.store_transaction_metric(
  2260. 100,
  2261. timestamp=self.min_ago,
  2262. tags={"device.class": "1"},
  2263. )
  2264. self.store_transaction_metric(
  2265. 200,
  2266. timestamp=self.min_ago,
  2267. tags={"device.class": "2"},
  2268. )
  2269. self.store_transaction_metric(
  2270. 300,
  2271. timestamp=self.min_ago,
  2272. tags={"device.class": ""},
  2273. )
  2274. response = self.do_request(
  2275. {
  2276. "field": ["device.class", "p95()"],
  2277. "query": "",
  2278. "orderby": "p95()",
  2279. "project": self.project.id,
  2280. "dataset": "metrics",
  2281. }
  2282. )
  2283. assert response.status_code == 200, response.content
  2284. data = response.data["data"]
  2285. meta = response.data["meta"]
  2286. assert len(data) == 3
  2287. # Need to actually check the dict since the level for 1 isn't guaranteed to stay `low` or `medium`
  2288. assert data[0]["device.class"] == map_device_class_level("1")
  2289. assert data[1]["device.class"] == map_device_class_level("2")
  2290. assert data[2]["device.class"] == "Unknown"
  2291. assert meta["fields"]["device.class"] == "string"
  2292. def test_device_class_filter(self):
  2293. self.store_transaction_metric(
  2294. 300,
  2295. timestamp=self.min_ago,
  2296. tags={"device.class": "1"},
  2297. )
  2298. # Need to actually check the dict since the level for 1 isn't guaranteed to stay `low`
  2299. level = map_device_class_level("1")
  2300. response = self.do_request(
  2301. {
  2302. "field": ["device.class", "count()"],
  2303. "query": f"device.class:{level}",
  2304. "orderby": "count()",
  2305. "project": self.project.id,
  2306. "dataset": "metrics",
  2307. }
  2308. )
  2309. assert response.status_code == 200, response.content
  2310. data = response.data["data"]
  2311. meta = response.data["meta"]
  2312. assert len(data) == 1
  2313. assert data[0]["device.class"] == level
  2314. assert meta["fields"]["device.class"] == "string"
  2315. class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithMetricLayer(
  2316. OrganizationEventsMetricsEnhancedPerformanceEndpointTest
  2317. ):
  2318. def setUp(self):
  2319. super().setUp()
  2320. self.features["organizations:use-metrics-layer"] = True
  2321. @pytest.mark.xfail(reason="Not supported")
  2322. def test_time_spent(self):
  2323. super().test_time_spent()
  2324. @pytest.mark.xfail(reason="Not supported")
  2325. def test_http_error_rate(self):
  2326. super().test_http_error_rate()
  2327. @pytest.mark.xfail(reason="Multiple aliases to same column not supported")
  2328. def test_title_and_transaction_alias(self):
  2329. super().test_title_and_transaction_alias()
  2330. @pytest.mark.xfail(reason="Sort order is flaking when querying multiple datasets")
  2331. def test_maintain_sort_order_across_datasets(self):
  2332. """You may need to run this test a few times to get it to fail"""
  2333. super().test_maintain_sort_order_across_datasets()
  2334. @pytest.mark.xfail(reason="Not implemented")
  2335. def test_avg_compare(self):
  2336. super().test_avg_compare()
  2337. @pytest.mark.xfail(reason="Not implemented")
  2338. def test_avg_if(self):
  2339. super().test_avg_if()
  2340. @pytest.mark.xfail(reason="Not implemented")
  2341. def test_device_class(self):
  2342. super().test_device_class()
  2343. @pytest.mark.xfail(reason="Not implemented")
  2344. def test_device_class_filter(self):
  2345. super().test_device_class_filter()