test_organization_events_mep.py 87 KB

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