test_organization_events_mep.py 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259
  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 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 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()"] == "number"
  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()"] == "number"
  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()"] == "number"
  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. def test_user_misery_and_team_key_sort(self):
  453. self.store_transaction_metric(
  454. 1,
  455. tags={
  456. "transaction": "foo_transaction",
  457. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  458. },
  459. timestamp=self.min_ago,
  460. )
  461. self.store_transaction_metric(
  462. 1,
  463. "measurements.fcp",
  464. tags={"transaction": "foo_transaction"},
  465. timestamp=self.min_ago,
  466. )
  467. self.store_transaction_metric(
  468. 2,
  469. "measurements.lcp",
  470. tags={"transaction": "foo_transaction"},
  471. timestamp=self.min_ago,
  472. )
  473. self.store_transaction_metric(
  474. 3,
  475. "measurements.fid",
  476. tags={"transaction": "foo_transaction"},
  477. timestamp=self.min_ago,
  478. )
  479. self.store_transaction_metric(
  480. 4,
  481. "measurements.cls",
  482. tags={"transaction": "foo_transaction"},
  483. timestamp=self.min_ago,
  484. )
  485. self.store_transaction_metric(
  486. 1,
  487. "user",
  488. tags={
  489. "transaction": "foo_transaction",
  490. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_FRUSTRATED_TAG_VALUE,
  491. },
  492. timestamp=self.min_ago,
  493. )
  494. response = self.do_request(
  495. {
  496. "field": [
  497. "team_key_transaction",
  498. "transaction",
  499. "project",
  500. "tpm()",
  501. "p75(measurements.fcp)",
  502. "p75(measurements.lcp)",
  503. "p75(measurements.fid)",
  504. "p75(measurements.cls)",
  505. "count_unique(user)",
  506. "apdex()",
  507. "count_miserable(user)",
  508. "user_misery()",
  509. "failure_rate()",
  510. "failure_count()",
  511. ],
  512. "orderby": ["team_key_transaction", "user_misery()"],
  513. "query": "event.type:transaction",
  514. "dataset": "metrics",
  515. "per_page": 50,
  516. }
  517. )
  518. assert response.status_code == 200, response.content
  519. assert len(response.data["data"]) == 1
  520. data = response.data["data"][0]
  521. meta = response.data["meta"]
  522. field_meta = meta["fields"]
  523. assert data["transaction"] == "foo_transaction"
  524. assert data["project"] == self.project.slug
  525. assert data["p75(measurements.fcp)"] == 1.0
  526. assert data["p75(measurements.lcp)"] == 2.0
  527. assert data["p75(measurements.fid)"] == 3.0
  528. assert data["p75(measurements.cls)"] == 4.0
  529. assert data["apdex()"] == 1.0
  530. assert data["count_miserable(user)"] == 1.0
  531. assert data["user_misery()"] == 0.058
  532. assert data["failure_rate()"] == 1
  533. assert data["failure_count()"] == 1
  534. assert meta["isMetricsData"]
  535. assert field_meta["transaction"] == "string"
  536. assert field_meta["project"] == "string"
  537. assert field_meta["p75(measurements.fcp)"] == "duration"
  538. assert field_meta["p75(measurements.lcp)"] == "duration"
  539. assert field_meta["p75(measurements.fid)"] == "duration"
  540. assert field_meta["p75(measurements.cls)"] == "number"
  541. assert field_meta["apdex()"] == "number"
  542. assert field_meta["count_miserable(user)"] == "integer"
  543. assert field_meta["user_misery()"] == "number"
  544. assert field_meta["failure_rate()"] == "percentage"
  545. assert field_meta["failure_count()"] == "integer"
  546. def test_no_team_key_transactions(self):
  547. self.store_transaction_metric(
  548. 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago
  549. )
  550. self.store_transaction_metric(
  551. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  552. )
  553. query = {
  554. "team": "myteams",
  555. "project": [self.project.id],
  556. # TODO sort by transaction here once that's possible for order to match the same test without metrics
  557. "orderby": "p95()",
  558. "field": [
  559. "team_key_transaction",
  560. "transaction",
  561. "transaction.status",
  562. "project",
  563. "epm()",
  564. "failure_rate()",
  565. "p95()",
  566. ],
  567. "per_page": 50,
  568. "dataset": "metricsEnhanced",
  569. }
  570. response = self.do_request(query)
  571. assert response.status_code == 200, response.content
  572. assert len(response.data["data"]) == 2
  573. data = response.data["data"]
  574. meta = response.data["meta"]
  575. field_meta = meta["fields"]
  576. assert data[0]["team_key_transaction"] == 0
  577. assert data[0]["transaction"] == "foo_transaction"
  578. assert data[1]["team_key_transaction"] == 0
  579. assert data[1]["transaction"] == "bar_transaction"
  580. assert meta["isMetricsData"]
  581. assert field_meta["team_key_transaction"] == "boolean"
  582. assert field_meta["transaction"] == "string"
  583. def test_team_key_transactions_my_teams(self):
  584. team1 = self.create_team(organization=self.organization, name="Team A")
  585. self.create_team_membership(team1, user=self.user)
  586. self.project.add_team(team1)
  587. team2 = self.create_team(organization=self.organization, name="Team B")
  588. self.project.add_team(team2)
  589. key_transactions = [
  590. (team1, "foo_transaction"),
  591. (team2, "baz_transaction"),
  592. ]
  593. # Not a key transaction
  594. self.store_transaction_metric(
  595. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  596. )
  597. for team, transaction in key_transactions:
  598. self.store_transaction_metric(
  599. 1, tags={"transaction": transaction}, timestamp=self.min_ago
  600. )
  601. TeamKeyTransaction.objects.create(
  602. organization=self.organization,
  603. transaction=transaction,
  604. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  605. )
  606. query = {
  607. "team": "myteams",
  608. "project": [self.project.id],
  609. "field": [
  610. "team_key_transaction",
  611. "transaction",
  612. "transaction.status",
  613. "project",
  614. "epm()",
  615. "failure_rate()",
  616. "p95()",
  617. ],
  618. "per_page": 50,
  619. "dataset": "metricsEnhanced",
  620. }
  621. query["orderby"] = ["team_key_transaction", "p95()"]
  622. response = self.do_request(query)
  623. assert response.status_code == 200, response.content
  624. assert len(response.data["data"]) == 3
  625. data = response.data["data"]
  626. meta = response.data["meta"]
  627. field_meta = meta["fields"]
  628. assert data[0]["team_key_transaction"] == 0
  629. assert data[0]["transaction"] == "baz_transaction"
  630. assert data[1]["team_key_transaction"] == 0
  631. assert data[1]["transaction"] == "bar_transaction"
  632. assert data[2]["team_key_transaction"] == 1
  633. assert data[2]["transaction"] == "foo_transaction"
  634. assert meta["isMetricsData"]
  635. assert field_meta["team_key_transaction"] == "boolean"
  636. assert field_meta["transaction"] == "string"
  637. # not specifying any teams should use my teams
  638. query = {
  639. "project": [self.project.id],
  640. "field": [
  641. "team_key_transaction",
  642. "transaction",
  643. "transaction.status",
  644. "project",
  645. "epm()",
  646. "failure_rate()",
  647. "p95()",
  648. ],
  649. "per_page": 50,
  650. "dataset": "metricsEnhanced",
  651. }
  652. query["orderby"] = ["team_key_transaction", "p95()"]
  653. response = self.do_request(query)
  654. assert response.status_code == 200, response.content
  655. assert len(response.data["data"]) == 3
  656. data = response.data["data"]
  657. meta = response.data["meta"]
  658. field_meta = meta["fields"]
  659. assert data[0]["team_key_transaction"] == 0
  660. assert data[0]["transaction"] == "baz_transaction"
  661. assert data[1]["team_key_transaction"] == 0
  662. assert data[1]["transaction"] == "bar_transaction"
  663. assert data[2]["team_key_transaction"] == 1
  664. assert data[2]["transaction"] == "foo_transaction"
  665. assert meta["isMetricsData"]
  666. assert field_meta["team_key_transaction"] == "boolean"
  667. assert field_meta["transaction"] == "string"
  668. def test_team_key_transactions_orderby(self):
  669. team1 = self.create_team(organization=self.organization, name="Team A")
  670. team2 = self.create_team(organization=self.organization, name="Team B")
  671. key_transactions = [
  672. (team1, "foo_transaction", 1),
  673. (team2, "baz_transaction", 100),
  674. ]
  675. # Not a key transaction
  676. self.store_transaction_metric(
  677. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  678. )
  679. for team, transaction, value in key_transactions:
  680. self.store_transaction_metric(
  681. value, tags={"transaction": transaction}, timestamp=self.min_ago
  682. )
  683. self.create_team_membership(team, user=self.user)
  684. self.project.add_team(team)
  685. TeamKeyTransaction.objects.create(
  686. organization=self.organization,
  687. transaction=transaction,
  688. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  689. )
  690. query = {
  691. "team": "myteams",
  692. "project": [self.project.id],
  693. "field": [
  694. "team_key_transaction",
  695. "transaction",
  696. "transaction.status",
  697. "project",
  698. "epm()",
  699. "failure_rate()",
  700. "p95()",
  701. ],
  702. "per_page": 50,
  703. "dataset": "metricsEnhanced",
  704. }
  705. # test ascending order
  706. query["orderby"] = ["team_key_transaction", "p95()"]
  707. response = self.do_request(query)
  708. assert response.status_code == 200, response.content
  709. assert len(response.data["data"]) == 3
  710. data = response.data["data"]
  711. meta = response.data["meta"]
  712. field_meta = meta["fields"]
  713. assert data[0]["team_key_transaction"] == 0
  714. assert data[0]["transaction"] == "bar_transaction"
  715. assert data[1]["team_key_transaction"] == 1
  716. assert data[1]["transaction"] == "foo_transaction"
  717. assert data[2]["team_key_transaction"] == 1
  718. assert data[2]["transaction"] == "baz_transaction"
  719. assert meta["isMetricsData"]
  720. assert field_meta["team_key_transaction"] == "boolean"
  721. assert field_meta["transaction"] == "string"
  722. # test descending order
  723. query["orderby"] = ["-team_key_transaction", "p95()"]
  724. response = self.do_request(query)
  725. assert response.status_code == 200, response.content
  726. assert len(response.data["data"]) == 3
  727. data = response.data["data"]
  728. meta = response.data["meta"]
  729. field_meta = meta["fields"]
  730. assert data[0]["team_key_transaction"] == 1
  731. assert data[0]["transaction"] == "foo_transaction"
  732. assert data[1]["team_key_transaction"] == 1
  733. assert data[1]["transaction"] == "baz_transaction"
  734. assert data[2]["team_key_transaction"] == 0
  735. assert data[2]["transaction"] == "bar_transaction"
  736. assert meta["isMetricsData"]
  737. assert field_meta["team_key_transaction"] == "boolean"
  738. assert field_meta["transaction"] == "string"
  739. def test_team_key_transactions_query(self):
  740. team1 = self.create_team(organization=self.organization, name="Team A")
  741. team2 = self.create_team(organization=self.organization, name="Team B")
  742. key_transactions = [
  743. (team1, "foo_transaction", 1),
  744. (team2, "baz_transaction", 100),
  745. ]
  746. # Not a key transaction
  747. self.store_transaction_metric(
  748. 100, tags={"transaction": "bar_transaction"}, timestamp=self.min_ago
  749. )
  750. for team, transaction, value in key_transactions:
  751. self.store_transaction_metric(
  752. value, tags={"transaction": transaction}, timestamp=self.min_ago
  753. )
  754. self.create_team_membership(team, user=self.user)
  755. self.project.add_team(team)
  756. TeamKeyTransaction.objects.create(
  757. organization=self.organization,
  758. transaction=transaction,
  759. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  760. )
  761. query = {
  762. "team": "myteams",
  763. "project": [self.project.id],
  764. # use the order by to ensure the result order
  765. "orderby": "p95()",
  766. "field": [
  767. "team_key_transaction",
  768. "transaction",
  769. "transaction.status",
  770. "project",
  771. "epm()",
  772. "failure_rate()",
  773. "p95()",
  774. ],
  775. "per_page": 50,
  776. "dataset": "metricsEnhanced",
  777. }
  778. # key transactions
  779. query["query"] = "has:team_key_transaction"
  780. response = self.do_request(query)
  781. assert response.status_code == 200, response.content
  782. assert len(response.data["data"]) == 2
  783. data = response.data["data"]
  784. meta = response.data["meta"]
  785. field_meta = meta["fields"]
  786. assert data[0]["team_key_transaction"] == 1
  787. assert data[0]["transaction"] == "foo_transaction"
  788. assert data[1]["team_key_transaction"] == 1
  789. assert data[1]["transaction"] == "baz_transaction"
  790. assert meta["isMetricsData"]
  791. assert field_meta["team_key_transaction"] == "boolean"
  792. assert field_meta["transaction"] == "string"
  793. # key transactions
  794. query["query"] = "team_key_transaction:true"
  795. response = self.do_request(query)
  796. assert response.status_code == 200, response.content
  797. assert len(response.data["data"]) == 2
  798. data = response.data["data"]
  799. meta = response.data["meta"]
  800. field_meta = meta["fields"]
  801. assert data[0]["team_key_transaction"] == 1
  802. assert data[0]["transaction"] == "foo_transaction"
  803. assert data[1]["team_key_transaction"] == 1
  804. assert data[1]["transaction"] == "baz_transaction"
  805. assert meta["isMetricsData"]
  806. assert field_meta["team_key_transaction"] == "boolean"
  807. assert field_meta["transaction"] == "string"
  808. # not key transactions
  809. query["query"] = "!has:team_key_transaction"
  810. response = self.do_request(query)
  811. assert response.status_code == 200, response.content
  812. assert len(response.data["data"]) == 1
  813. data = response.data["data"]
  814. meta = response.data["meta"]
  815. field_meta = meta["fields"]
  816. assert data[0]["team_key_transaction"] == 0
  817. assert data[0]["transaction"] == "bar_transaction"
  818. assert meta["isMetricsData"]
  819. assert field_meta["team_key_transaction"] == "boolean"
  820. assert field_meta["transaction"] == "string"
  821. # not key transactions
  822. query["query"] = "team_key_transaction:false"
  823. response = self.do_request(query)
  824. assert response.status_code == 200, response.content
  825. assert len(response.data["data"]) == 1
  826. data = response.data["data"]
  827. meta = response.data["meta"]
  828. field_meta = meta["fields"]
  829. assert data[0]["team_key_transaction"] == 0
  830. assert data[0]["transaction"] == "bar_transaction"
  831. assert meta["isMetricsData"]
  832. assert field_meta["team_key_transaction"] == "boolean"
  833. assert field_meta["transaction"] == "string"
  834. def test_team_key_transaction_not_exists(self):
  835. team1 = self.create_team(organization=self.organization, name="Team A")
  836. team2 = self.create_team(organization=self.organization, name="Team B")
  837. key_transactions = [
  838. (team1, "foo_transaction", 1),
  839. (team2, "baz_transaction", 100),
  840. ]
  841. for team, transaction, value in key_transactions:
  842. self.store_transaction_metric(
  843. value, tags={"transaction": transaction}, timestamp=self.min_ago
  844. )
  845. self.create_team_membership(team, user=self.user)
  846. self.project.add_team(team)
  847. TeamKeyTransaction.objects.create(
  848. organization=self.organization,
  849. transaction=transaction,
  850. project_team=ProjectTeam.objects.get(project=self.project, team=team),
  851. )
  852. # Don't create a metric for this one
  853. TeamKeyTransaction.objects.create(
  854. organization=self.organization,
  855. transaction="not_in_metrics",
  856. project_team=ProjectTeam.objects.get(project=self.project, team=team1),
  857. )
  858. query = {
  859. "team": "myteams",
  860. "project": [self.project.id],
  861. # use the order by to ensure the result order
  862. "orderby": "p95()",
  863. "field": [
  864. "team_key_transaction",
  865. "transaction",
  866. "transaction.status",
  867. "project",
  868. "epm()",
  869. "failure_rate()",
  870. "p95()",
  871. ],
  872. "per_page": 50,
  873. "dataset": "metricsEnhanced",
  874. }
  875. # key transactions
  876. query["query"] = "has:team_key_transaction"
  877. response = self.do_request(query)
  878. assert response.status_code == 200, response.content
  879. assert len(response.data["data"]) == 2
  880. data = response.data["data"]
  881. meta = response.data["meta"]
  882. field_meta = meta["fields"]
  883. assert data[0]["team_key_transaction"] == 1
  884. assert data[0]["transaction"] == "foo_transaction"
  885. assert data[1]["team_key_transaction"] == 1
  886. assert data[1]["transaction"] == "baz_transaction"
  887. assert meta["isMetricsData"]
  888. assert field_meta["team_key_transaction"] == "boolean"
  889. assert field_meta["transaction"] == "string"
  890. # key transactions
  891. query["query"] = "team_key_transaction:true"
  892. response = self.do_request(query)
  893. assert response.status_code == 200, response.content
  894. assert len(response.data["data"]) == 2
  895. data = response.data["data"]
  896. meta = response.data["meta"]
  897. field_meta = meta["fields"]
  898. assert data[0]["team_key_transaction"] == 1
  899. assert data[0]["transaction"] == "foo_transaction"
  900. assert data[1]["team_key_transaction"] == 1
  901. assert data[1]["transaction"] == "baz_transaction"
  902. assert meta["isMetricsData"]
  903. assert field_meta["team_key_transaction"] == "boolean"
  904. assert field_meta["transaction"] == "string"
  905. # not key transactions
  906. query["query"] = "!has:team_key_transaction"
  907. response = self.do_request(query)
  908. assert response.status_code == 200, response.content
  909. assert len(response.data["data"]) == 0
  910. data = response.data["data"]
  911. meta = response.data["meta"]
  912. field_meta = meta["fields"]
  913. assert meta["isMetricsData"]
  914. assert field_meta["team_key_transaction"] == "boolean"
  915. assert field_meta["transaction"] == "string"
  916. # not key transactions
  917. query["query"] = "team_key_transaction:false"
  918. response = self.do_request(query)
  919. assert response.status_code == 200, response.content
  920. assert len(response.data["data"]) == 0
  921. data = response.data["data"]
  922. meta = response.data["meta"]
  923. field_meta = meta["fields"]
  924. assert meta["isMetricsData"]
  925. assert field_meta["team_key_transaction"] == "boolean"
  926. assert field_meta["transaction"] == "string"
  927. def test_too_many_team_key_transactions(self):
  928. MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS = 1
  929. with mock.patch(
  930. "sentry.search.events.fields.MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS",
  931. MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS,
  932. ):
  933. team = self.create_team(organization=self.organization, name="Team A")
  934. self.create_team_membership(team, user=self.user)
  935. self.project.add_team(team)
  936. project_team = ProjectTeam.objects.get(project=self.project, team=team)
  937. transactions = ["foo_transaction", "bar_transaction", "baz_transaction"]
  938. for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1):
  939. self.store_transaction_metric(
  940. 100, tags={"transaction": transactions[i]}, timestamp=self.min_ago
  941. )
  942. TeamKeyTransaction.objects.bulk_create(
  943. [
  944. TeamKeyTransaction(
  945. organization=self.organization,
  946. project_team=project_team,
  947. transaction=transactions[i],
  948. )
  949. for i in range(MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS + 1)
  950. ]
  951. )
  952. query = {
  953. "team": "myteams",
  954. "project": [self.project.id],
  955. "orderby": "p95()",
  956. "field": [
  957. "team_key_transaction",
  958. "transaction",
  959. "transaction.status",
  960. "project",
  961. "epm()",
  962. "failure_rate()",
  963. "p95()",
  964. ],
  965. "dataset": "metricsEnhanced",
  966. "per_page": 50,
  967. }
  968. response = self.do_request(query)
  969. assert response.status_code == 200, response.content
  970. assert len(response.data["data"]) == 2
  971. data = response.data["data"]
  972. meta = response.data["meta"]
  973. assert (
  974. sum(row["team_key_transaction"] for row in data)
  975. == MAX_QUERYABLE_TEAM_KEY_TRANSACTIONS
  976. )
  977. assert meta["isMetricsData"]
  978. def test_measurement_rating(self):
  979. self.store_transaction_metric(
  980. 50,
  981. metric="measurements.lcp",
  982. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  983. timestamp=self.min_ago,
  984. )
  985. self.store_transaction_metric(
  986. 15,
  987. metric="measurements.fp",
  988. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  989. timestamp=self.min_ago,
  990. )
  991. self.store_transaction_metric(
  992. 1500,
  993. metric="measurements.fcp",
  994. tags={"measurement_rating": "meh", "transaction": "foo_transaction"},
  995. timestamp=self.min_ago,
  996. )
  997. self.store_transaction_metric(
  998. 125,
  999. metric="measurements.fid",
  1000. tags={"measurement_rating": "meh", "transaction": "foo_transaction"},
  1001. timestamp=self.min_ago,
  1002. )
  1003. self.store_transaction_metric(
  1004. 0.15,
  1005. metric="measurements.cls",
  1006. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  1007. timestamp=self.min_ago,
  1008. )
  1009. response = self.do_request(
  1010. {
  1011. "field": [
  1012. "transaction",
  1013. "count_web_vitals(measurements.lcp, good)",
  1014. "count_web_vitals(measurements.fp, good)",
  1015. "count_web_vitals(measurements.fcp, meh)",
  1016. "count_web_vitals(measurements.fid, meh)",
  1017. "count_web_vitals(measurements.cls, good)",
  1018. ],
  1019. "query": "event.type:transaction",
  1020. "dataset": "metricsEnhanced",
  1021. "per_page": 50,
  1022. }
  1023. )
  1024. assert response.status_code == 200, response.content
  1025. assert len(response.data["data"]) == 1
  1026. data = response.data["data"]
  1027. meta = response.data["meta"]
  1028. field_meta = meta["fields"]
  1029. assert data[0]["count_web_vitals(measurements.lcp, good)"] == 1
  1030. assert data[0]["count_web_vitals(measurements.fp, good)"] == 1
  1031. assert data[0]["count_web_vitals(measurements.fcp, meh)"] == 1
  1032. assert data[0]["count_web_vitals(measurements.fid, meh)"] == 1
  1033. assert data[0]["count_web_vitals(measurements.cls, good)"] == 1
  1034. assert meta["isMetricsData"]
  1035. assert field_meta["count_web_vitals(measurements.lcp, good)"] == "integer"
  1036. assert field_meta["count_web_vitals(measurements.fp, good)"] == "integer"
  1037. assert field_meta["count_web_vitals(measurements.fcp, meh)"] == "integer"
  1038. assert field_meta["count_web_vitals(measurements.fid, meh)"] == "integer"
  1039. assert field_meta["count_web_vitals(measurements.cls, good)"] == "integer"
  1040. def test_measurement_rating_that_does_not_exist(self):
  1041. self.store_transaction_metric(
  1042. 1,
  1043. metric="measurements.lcp",
  1044. tags={"measurement_rating": "good", "transaction": "foo_transaction"},
  1045. timestamp=self.min_ago,
  1046. )
  1047. response = self.do_request(
  1048. {
  1049. "field": ["transaction", "count_web_vitals(measurements.lcp, poor)"],
  1050. "query": "event.type:transaction",
  1051. "dataset": "metricsEnhanced",
  1052. "per_page": 50,
  1053. }
  1054. )
  1055. assert response.status_code == 200, response.content
  1056. assert len(response.data["data"]) == 1
  1057. data = response.data["data"]
  1058. meta = response.data["meta"]
  1059. assert data[0]["count_web_vitals(measurements.lcp, poor)"] == 0
  1060. assert meta["isMetricsData"]
  1061. assert meta["fields"]["count_web_vitals(measurements.lcp, poor)"] == "integer"
  1062. def test_count_web_vitals_invalid_vital(self):
  1063. query = {
  1064. "field": [
  1065. "count_web_vitals(measurements.foo, poor)",
  1066. ],
  1067. "project": [self.project.id],
  1068. "dataset": "metricsEnhanced",
  1069. }
  1070. response = self.do_request(query)
  1071. assert response.status_code == 400, response.content
  1072. query = {
  1073. "field": [
  1074. "count_web_vitals(tags[lcp], poor)",
  1075. ],
  1076. "project": [self.project.id],
  1077. "dataset": "metricsEnhanced",
  1078. }
  1079. response = self.do_request(query)
  1080. assert response.status_code == 400, response.content
  1081. query = {
  1082. "field": [
  1083. "count_web_vitals(transaction.duration, poor)",
  1084. ],
  1085. "project": [self.project.id],
  1086. "dataset": "metricsEnhanced",
  1087. }
  1088. response = self.do_request(query)
  1089. assert response.status_code == 400, response.content
  1090. query = {
  1091. "field": [
  1092. "count_web_vitals(measurements.lcp, bad)",
  1093. ],
  1094. "project": [self.project.id],
  1095. "dataset": "metricsEnhanced",
  1096. }
  1097. response = self.do_request(query)
  1098. assert response.status_code == 400, response.content
  1099. def test_count_unique_user_returns_zero(self):
  1100. self.store_transaction_metric(
  1101. 50,
  1102. metric="user",
  1103. tags={"transaction": "foo_transaction"},
  1104. timestamp=self.min_ago,
  1105. )
  1106. self.store_transaction_metric(
  1107. 50,
  1108. tags={"transaction": "foo_transaction"},
  1109. timestamp=self.min_ago,
  1110. )
  1111. self.store_transaction_metric(
  1112. 100,
  1113. tags={"transaction": "bar_transaction"},
  1114. timestamp=self.min_ago,
  1115. )
  1116. query = {
  1117. "project": [self.project.id],
  1118. "orderby": "p50()",
  1119. "field": [
  1120. "transaction",
  1121. "count_unique(user)",
  1122. "p50()",
  1123. ],
  1124. "dataset": "metricsEnhanced",
  1125. "per_page": 50,
  1126. }
  1127. response = self.do_request(query)
  1128. assert response.status_code == 200, response.content
  1129. assert len(response.data["data"]) == 2
  1130. data = response.data["data"]
  1131. meta = response.data["meta"]
  1132. assert data[0]["transaction"] == "foo_transaction"
  1133. assert data[0]["count_unique(user)"] == 1
  1134. assert data[1]["transaction"] == "bar_transaction"
  1135. assert data[1]["count_unique(user)"] == 0
  1136. assert meta["isMetricsData"]
  1137. def test_sum_transaction_duration(self):
  1138. self.store_transaction_metric(
  1139. 50,
  1140. tags={"transaction": "foo_transaction"},
  1141. timestamp=self.min_ago,
  1142. )
  1143. self.store_transaction_metric(
  1144. 100,
  1145. tags={"transaction": "foo_transaction"},
  1146. timestamp=self.min_ago,
  1147. )
  1148. self.store_transaction_metric(
  1149. 150,
  1150. tags={"transaction": "foo_transaction"},
  1151. timestamp=self.min_ago,
  1152. )
  1153. query = {
  1154. "project": [self.project.id],
  1155. "orderby": "sum(transaction.duration)",
  1156. "field": [
  1157. "transaction",
  1158. "sum(transaction.duration)",
  1159. ],
  1160. "dataset": "metricsEnhanced",
  1161. "per_page": 50,
  1162. }
  1163. response = self.do_request(query)
  1164. assert response.status_code == 200, response.content
  1165. assert len(response.data["data"]) == 1
  1166. data = response.data["data"]
  1167. meta = response.data["meta"]
  1168. assert data[0]["transaction"] == "foo_transaction"
  1169. assert data[0]["sum(transaction.duration)"] == 300
  1170. assert meta["isMetricsData"]
  1171. def test_custom_measurements_simple(self):
  1172. self.store_transaction_metric(
  1173. 1,
  1174. metric="measurements.something_custom",
  1175. internal_metric="d:transactions/measurements.something_custom@millisecond",
  1176. entity="metrics_distributions",
  1177. tags={"transaction": "foo_transaction"},
  1178. timestamp=self.min_ago,
  1179. )
  1180. query = {
  1181. "project": [self.project.id],
  1182. "orderby": "p50(measurements.something_custom)",
  1183. "field": [
  1184. "transaction",
  1185. "p50(measurements.something_custom)",
  1186. ],
  1187. "statsPeriod": "24h",
  1188. "dataset": "metricsEnhanced",
  1189. "per_page": 50,
  1190. }
  1191. self.wait_for_metric_count(
  1192. self.project,
  1193. 1,
  1194. metric="measurements.something_custom",
  1195. mri="d:transactions/measurements.something_custom@millisecond",
  1196. )
  1197. response = self.do_request(query)
  1198. assert response.status_code == 200, response.content
  1199. assert len(response.data["data"]) == 1
  1200. data = response.data["data"]
  1201. meta = response.data["meta"]
  1202. assert data[0]["transaction"] == "foo_transaction"
  1203. assert data[0]["p50(measurements.something_custom)"] == 1
  1204. assert meta["isMetricsData"]
  1205. assert meta["fields"]["p50(measurements.something_custom)"] == "duration"
  1206. assert meta["units"]["p50(measurements.something_custom)"] == "millisecond"
  1207. def test_custom_measurement_size_meta_type(self):
  1208. self.store_transaction_metric(
  1209. 100,
  1210. metric="measurements.custom_type",
  1211. internal_metric="d:transactions/measurements.custom_type@somethingcustom",
  1212. entity="metrics_distributions",
  1213. tags={"transaction": "foo_transaction"},
  1214. timestamp=self.min_ago,
  1215. )
  1216. self.store_transaction_metric(
  1217. 100,
  1218. metric="measurements.percent",
  1219. internal_metric="d:transactions/measurements.percent@ratio",
  1220. entity="metrics_distributions",
  1221. tags={"transaction": "foo_transaction"},
  1222. timestamp=self.min_ago,
  1223. )
  1224. self.store_transaction_metric(
  1225. 100,
  1226. metric="measurements.longtaskcount",
  1227. internal_metric="d:transactions/measurements.longtaskcount@none",
  1228. entity="metrics_distributions",
  1229. tags={"transaction": "foo_transaction"},
  1230. timestamp=self.min_ago,
  1231. )
  1232. query = {
  1233. "project": [self.project.id],
  1234. "orderby": "p50(measurements.longtaskcount)",
  1235. "field": [
  1236. "transaction",
  1237. "p50(measurements.longtaskcount)",
  1238. "p50(measurements.percent)",
  1239. "p50(measurements.custom_type)",
  1240. ],
  1241. "statsPeriod": "24h",
  1242. "dataset": "metricsEnhanced",
  1243. "per_page": 50,
  1244. }
  1245. response = self.do_request(query)
  1246. assert response.status_code == 200, response.content
  1247. assert len(response.data["data"]) == 1
  1248. data = response.data["data"]
  1249. meta = response.data["meta"]
  1250. assert data[0]["transaction"] == "foo_transaction"
  1251. assert data[0]["p50(measurements.longtaskcount)"] == 100
  1252. assert data[0]["p50(measurements.percent)"] == 100
  1253. assert data[0]["p50(measurements.custom_type)"] == 100
  1254. assert meta["isMetricsData"]
  1255. assert meta["fields"]["p50(measurements.longtaskcount)"] == "integer"
  1256. assert meta["units"]["p50(measurements.longtaskcount)"] is None
  1257. assert meta["fields"]["p50(measurements.percent)"] == "percentage"
  1258. assert meta["units"]["p50(measurements.percent)"] is None
  1259. assert meta["fields"]["p50(measurements.custom_type)"] == "number"
  1260. assert meta["units"]["p50(measurements.custom_type)"] is None
  1261. def test_custom_measurement_none_type(self):
  1262. self.store_transaction_metric(
  1263. 1,
  1264. metric="measurements.cls",
  1265. entity="metrics_distributions",
  1266. tags={"transaction": "foo_transaction"},
  1267. timestamp=self.min_ago,
  1268. )
  1269. query = {
  1270. "project": [self.project.id],
  1271. "orderby": "p75(measurements.cls)",
  1272. "field": [
  1273. "transaction",
  1274. "p75(measurements.cls)",
  1275. "p99(measurements.cls)",
  1276. "max(measurements.cls)",
  1277. ],
  1278. "statsPeriod": "24h",
  1279. "dataset": "metricsEnhanced",
  1280. "per_page": 50,
  1281. }
  1282. self.wait_for_metric_count(
  1283. self.project,
  1284. 1,
  1285. metric=TransactionMetricKey.MEASUREMENTS_CLS.value,
  1286. mri=TransactionMRI.MEASUREMENTS_CLS.value,
  1287. )
  1288. response = self.do_request(query)
  1289. assert response.status_code == 200, response.content
  1290. assert len(response.data["data"]) == 1
  1291. data = response.data["data"]
  1292. meta = response.data["meta"]
  1293. assert data[0]["transaction"] == "foo_transaction"
  1294. assert data[0]["p75(measurements.cls)"] == 1
  1295. assert data[0]["p99(measurements.cls)"] == 1
  1296. assert data[0]["max(measurements.cls)"] == 1
  1297. assert meta["isMetricsData"]
  1298. assert meta["fields"]["p75(measurements.cls)"] == "number"
  1299. assert meta["units"]["p75(measurements.cls)"] is None
  1300. assert meta["fields"]["p99(measurements.cls)"] == "number"
  1301. assert meta["units"]["p99(measurements.cls)"] is None
  1302. assert meta["fields"]["max(measurements.cls)"] == "number"
  1303. assert meta["units"]["max(measurements.cls)"] is None
  1304. def test_custom_measurement_duration_filtering(self):
  1305. self.store_transaction_metric(
  1306. 1,
  1307. metric="measurements.runtime",
  1308. internal_metric="d:transactions/measurements.runtime@hour",
  1309. entity="metrics_distributions",
  1310. tags={"transaction": "foo_transaction"},
  1311. timestamp=self.min_ago,
  1312. )
  1313. self.store_transaction_metric(
  1314. 180,
  1315. metric="measurements.runtime",
  1316. internal_metric="d:transactions/measurements.runtime@hour",
  1317. entity="metrics_distributions",
  1318. tags={"transaction": "bar_transaction"},
  1319. timestamp=self.min_ago,
  1320. )
  1321. query = {
  1322. "project": [self.project.id],
  1323. "field": [
  1324. "transaction",
  1325. "max(measurements.runtime)",
  1326. ],
  1327. "query": "p50(measurements.runtime):>1wk",
  1328. "statsPeriod": "24h",
  1329. "dataset": "metricsEnhanced",
  1330. "per_page": 50,
  1331. }
  1332. response = self.do_request(query)
  1333. assert response.status_code == 200, response.content
  1334. assert len(response.data["data"]) == 1
  1335. data = response.data["data"]
  1336. meta = response.data["meta"]
  1337. assert data[0]["transaction"] == "bar_transaction"
  1338. assert data[0]["max(measurements.runtime)"] == 180
  1339. assert meta["isMetricsData"]
  1340. def test_custom_measurement_size_filtering(self):
  1341. self.store_transaction_metric(
  1342. 1,
  1343. metric="measurements.datacenter_memory",
  1344. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1345. entity="metrics_distributions",
  1346. tags={"transaction": "foo_transaction"},
  1347. timestamp=self.min_ago,
  1348. )
  1349. self.store_transaction_metric(
  1350. 100,
  1351. metric="measurements.datacenter_memory",
  1352. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1353. entity="metrics_distributions",
  1354. tags={"transaction": "bar_transaction"},
  1355. timestamp=self.min_ago,
  1356. )
  1357. query = {
  1358. "project": [self.project.id],
  1359. "field": [
  1360. "transaction",
  1361. "max(measurements.datacenter_memory)",
  1362. ],
  1363. "query": "p50(measurements.datacenter_memory):>5pb",
  1364. "statsPeriod": "24h",
  1365. "dataset": "metricsEnhanced",
  1366. "per_page": 50,
  1367. }
  1368. response = self.do_request(query)
  1369. assert response.status_code == 200, response.content
  1370. assert len(response.data["data"]) == 1
  1371. data = response.data["data"]
  1372. meta = response.data["meta"]
  1373. assert data[0]["transaction"] == "bar_transaction"
  1374. assert data[0]["max(measurements.datacenter_memory)"] == 100
  1375. assert meta["units"]["max(measurements.datacenter_memory)"] == "petabyte"
  1376. assert meta["fields"]["max(measurements.datacenter_memory)"] == "size"
  1377. assert meta["isMetricsData"]
  1378. def test_has_custom_measurement(self):
  1379. self.store_transaction_metric(
  1380. 33,
  1381. metric="measurements.datacenter_memory",
  1382. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1383. entity="metrics_distributions",
  1384. tags={"transaction": "foo_transaction"},
  1385. timestamp=self.min_ago,
  1386. )
  1387. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1388. transaction_data["measurements"]["datacenter_memory"] = {
  1389. "value": 33,
  1390. "unit": "petabyte",
  1391. }
  1392. self.store_event(transaction_data, self.project.id)
  1393. measurement = "measurements.datacenter_memory"
  1394. response = self.do_request(
  1395. {
  1396. "field": ["transaction", measurement],
  1397. "query": "has:measurements.datacenter_memory",
  1398. "dataset": "discover",
  1399. }
  1400. )
  1401. assert response.status_code == 200, response.content
  1402. assert len(response.data["data"]) == 1
  1403. response = self.do_request(
  1404. {
  1405. "field": ["transaction", measurement],
  1406. "query": "!has:measurements.datacenter_memory",
  1407. "dataset": "discover",
  1408. }
  1409. )
  1410. assert response.status_code == 200, response.content
  1411. assert len(response.data["data"]) == 0
  1412. def test_environment_param(self):
  1413. self.create_environment(self.project, name="staging")
  1414. self.store_transaction_metric(
  1415. 1,
  1416. tags={"transaction": "foo_transaction", "environment": "staging"},
  1417. timestamp=self.min_ago,
  1418. )
  1419. self.store_transaction_metric(
  1420. 100,
  1421. tags={"transaction": "foo_transaction"},
  1422. timestamp=self.min_ago,
  1423. )
  1424. query = {
  1425. "project": [self.project.id],
  1426. "environment": "staging",
  1427. "orderby": "p50(transaction.duration)",
  1428. "field": [
  1429. "transaction",
  1430. "environment",
  1431. "p50(transaction.duration)",
  1432. ],
  1433. "statsPeriod": "24h",
  1434. "dataset": "metricsEnhanced",
  1435. "per_page": 50,
  1436. }
  1437. response = self.do_request(query)
  1438. assert response.status_code == 200, response.content
  1439. assert len(response.data["data"]) == 1
  1440. data = response.data["data"]
  1441. meta = response.data["meta"]
  1442. assert data[0]["transaction"] == "foo_transaction"
  1443. assert data[0]["environment"] == "staging"
  1444. assert data[0]["p50(transaction.duration)"] == 1
  1445. assert meta["isMetricsData"]
  1446. def test_environment_query(self):
  1447. self.create_environment(self.project, name="staging")
  1448. self.store_transaction_metric(
  1449. 1,
  1450. tags={"transaction": "foo_transaction", "environment": "staging"},
  1451. timestamp=self.min_ago,
  1452. )
  1453. self.store_transaction_metric(
  1454. 100,
  1455. tags={"transaction": "foo_transaction"},
  1456. timestamp=self.min_ago,
  1457. )
  1458. query = {
  1459. "project": [self.project.id],
  1460. "orderby": "p50(transaction.duration)",
  1461. "field": [
  1462. "transaction",
  1463. "environment",
  1464. "p50(transaction.duration)",
  1465. ],
  1466. "query": "!has:environment",
  1467. "statsPeriod": "24h",
  1468. "dataset": "metricsEnhanced",
  1469. "per_page": 50,
  1470. }
  1471. self.wait_for_metric_count(self.project, 2)
  1472. response = self.do_request(query)
  1473. assert response.status_code == 200, response.content
  1474. assert len(response.data["data"]) == 1
  1475. data = response.data["data"]
  1476. meta = response.data["meta"]
  1477. assert data[0]["transaction"] == "foo_transaction"
  1478. assert data[0]["environment"] is None or data[0]["environment"] == ""
  1479. assert data[0]["p50(transaction.duration)"] == 100
  1480. assert meta["isMetricsData"]
  1481. def test_has_transaction(self):
  1482. self.store_transaction_metric(
  1483. 1,
  1484. tags={},
  1485. timestamp=self.min_ago,
  1486. )
  1487. self.store_transaction_metric(
  1488. 100,
  1489. tags={"transaction": "foo_transaction"},
  1490. timestamp=self.min_ago,
  1491. )
  1492. query = {
  1493. "project": [self.project.id],
  1494. "orderby": "p50(transaction.duration)",
  1495. "field": [
  1496. "transaction",
  1497. "p50(transaction.duration)",
  1498. ],
  1499. "query": "has:transaction",
  1500. "statsPeriod": "24h",
  1501. "dataset": "metricsEnhanced",
  1502. "per_page": 50,
  1503. }
  1504. self.wait_for_metric_count(self.project, 2)
  1505. response = self.do_request(query)
  1506. assert response.status_code == 200, response.content
  1507. assert len(response.data["data"]) == 2
  1508. data = response.data["data"]
  1509. meta = response.data["meta"]
  1510. assert data[0]["transaction"] == "<< unparameterized >>"
  1511. assert data[0]["p50(transaction.duration)"] == 1
  1512. assert data[1]["transaction"] == "foo_transaction"
  1513. assert data[1]["p50(transaction.duration)"] == 100
  1514. assert meta["isMetricsData"]
  1515. query = {
  1516. "project": [self.project.id],
  1517. "orderby": "p50(transaction.duration)",
  1518. "field": [
  1519. "transaction",
  1520. "p50(transaction.duration)",
  1521. ],
  1522. "query": "!has:transaction",
  1523. "statsPeriod": "24h",
  1524. "dataset": "metricsEnhanced",
  1525. "per_page": 50,
  1526. }
  1527. response = self.do_request(query)
  1528. assert response.status_code == 400, response.content
  1529. def test_apdex_transaction_threshold(self):
  1530. ProjectTransactionThresholdOverride.objects.create(
  1531. transaction="foo_transaction",
  1532. project=self.project,
  1533. organization=self.project.organization,
  1534. threshold=600,
  1535. metric=TransactionMetric.LCP.value,
  1536. )
  1537. ProjectTransactionThresholdOverride.objects.create(
  1538. transaction="bar_transaction",
  1539. project=self.project,
  1540. organization=self.project.organization,
  1541. threshold=600,
  1542. metric=TransactionMetric.LCP.value,
  1543. )
  1544. self.store_transaction_metric(
  1545. 1,
  1546. tags={
  1547. "transaction": "foo_transaction",
  1548. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1549. },
  1550. timestamp=self.min_ago,
  1551. )
  1552. self.store_transaction_metric(
  1553. 1,
  1554. "measurements.lcp",
  1555. tags={
  1556. "transaction": "bar_transaction",
  1557. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1558. },
  1559. timestamp=self.min_ago,
  1560. )
  1561. response = self.do_request(
  1562. {
  1563. "field": [
  1564. "transaction",
  1565. "apdex()",
  1566. ],
  1567. "orderby": ["apdex()"],
  1568. "query": "event.type:transaction",
  1569. "dataset": "metrics",
  1570. "per_page": 50,
  1571. }
  1572. )
  1573. assert len(response.data["data"]) == 2
  1574. data = response.data["data"]
  1575. meta = response.data["meta"]
  1576. field_meta = meta["fields"]
  1577. assert data[0]["transaction"] == "bar_transaction"
  1578. # Threshold is lcp based
  1579. assert data[0]["apdex()"] == 1
  1580. assert data[1]["transaction"] == "foo_transaction"
  1581. # Threshold is lcp based
  1582. assert data[1]["apdex()"] == 0
  1583. assert meta["isMetricsData"]
  1584. assert field_meta["transaction"] == "string"
  1585. assert field_meta["apdex()"] == "number"
  1586. def test_apdex_project_threshold(self):
  1587. ProjectTransactionThreshold.objects.create(
  1588. project=self.project,
  1589. organization=self.project.organization,
  1590. threshold=600,
  1591. metric=TransactionMetric.LCP.value,
  1592. )
  1593. self.store_transaction_metric(
  1594. 1,
  1595. tags={
  1596. "transaction": "foo_transaction",
  1597. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1598. },
  1599. timestamp=self.min_ago,
  1600. )
  1601. self.store_transaction_metric(
  1602. 1,
  1603. "measurements.lcp",
  1604. tags={
  1605. "transaction": "bar_transaction",
  1606. constants.METRIC_SATISFACTION_TAG_KEY: constants.METRIC_SATISFIED_TAG_VALUE,
  1607. },
  1608. timestamp=self.min_ago,
  1609. )
  1610. response = self.do_request(
  1611. {
  1612. "field": [
  1613. "transaction",
  1614. "apdex()",
  1615. ],
  1616. "orderby": ["apdex()"],
  1617. "query": "event.type:transaction",
  1618. "dataset": "metrics",
  1619. "per_page": 50,
  1620. }
  1621. )
  1622. assert response.status_code == 200, response.content
  1623. assert len(response.data["data"]) == 2
  1624. data = response.data["data"]
  1625. meta = response.data["meta"]
  1626. field_meta = meta["fields"]
  1627. assert data[0]["transaction"] == "bar_transaction"
  1628. # Threshold is lcp based
  1629. assert data[0]["apdex()"] == 1
  1630. assert data[1]["transaction"] == "foo_transaction"
  1631. # Threshold is lcp based
  1632. assert data[1]["apdex()"] == 0
  1633. assert meta["isMetricsData"]
  1634. assert field_meta["transaction"] == "string"
  1635. assert field_meta["apdex()"] == "number"
  1636. def test_apdex_satisfaction_param(self):
  1637. for function in ["apdex(300)", "user_misery(300)", "count_miserable(user, 300)"]:
  1638. query = {
  1639. "project": [self.project.id],
  1640. "field": [
  1641. "transaction",
  1642. function,
  1643. ],
  1644. "statsPeriod": "24h",
  1645. "dataset": "metricsEnhanced",
  1646. "per_page": 50,
  1647. }
  1648. response = self.do_request(query)
  1649. assert response.status_code == 200, response.content
  1650. assert len(response.data["data"]) == 0
  1651. meta = response.data["meta"]
  1652. assert not meta["isMetricsData"], function
  1653. query = {
  1654. "project": [self.project.id],
  1655. "field": [
  1656. "transaction",
  1657. function,
  1658. ],
  1659. "statsPeriod": "24h",
  1660. "dataset": "metrics",
  1661. "per_page": 50,
  1662. }
  1663. response = self.do_request(query)
  1664. assert response.status_code == 400, function
  1665. assert b"threshold parameter" in response.content, function
  1666. def test_mobile_metrics(self):
  1667. self.store_transaction_metric(
  1668. 0.4,
  1669. "measurements.frames_frozen_rate",
  1670. tags={
  1671. "transaction": "bar_transaction",
  1672. },
  1673. timestamp=self.min_ago,
  1674. )
  1675. query = {
  1676. "project": [self.project.id],
  1677. "field": [
  1678. "transaction",
  1679. "p50(measurements.frames_frozen_rate)",
  1680. ],
  1681. "statsPeriod": "24h",
  1682. "dataset": "metrics",
  1683. "per_page": 50,
  1684. }
  1685. response = self.do_request(query)
  1686. assert response.status_code == 200, response.content
  1687. assert len(response.data["data"]) == 1
  1688. assert response.data["data"][0]["p50(measurements.frames_frozen_rate)"] == 0.4
  1689. def test_merge_null_unparam(self):
  1690. self.store_transaction_metric(
  1691. 1,
  1692. # Transaction: unparam
  1693. tags={
  1694. "transaction": "<< unparameterized >>",
  1695. },
  1696. timestamp=self.min_ago,
  1697. )
  1698. self.store_transaction_metric(
  1699. 2,
  1700. # Transaction:null
  1701. tags={},
  1702. timestamp=self.min_ago,
  1703. )
  1704. query = {
  1705. "project": [self.project.id],
  1706. "field": [
  1707. "transaction",
  1708. "p50(transaction.duration)",
  1709. ],
  1710. "statsPeriod": "24h",
  1711. "dataset": "metrics",
  1712. "per_page": 50,
  1713. }
  1714. response = self.do_request(query)
  1715. assert response.status_code == 200, response.content
  1716. assert len(response.data["data"]) == 1
  1717. assert response.data["data"][0]["p50(transaction.duration)"] == 1.5
  1718. def test_unparam_filter(self):
  1719. self.store_transaction_metric(
  1720. 1,
  1721. # Transaction: unparam
  1722. tags={
  1723. "transaction": "<< unparameterized >>",
  1724. },
  1725. timestamp=self.min_ago,
  1726. )
  1727. self.store_transaction_metric(
  1728. 2,
  1729. # Transaction:null
  1730. tags={},
  1731. timestamp=self.min_ago,
  1732. )
  1733. self.store_transaction_metric(
  1734. 3,
  1735. tags={
  1736. "transaction": "foo_transaction",
  1737. },
  1738. timestamp=self.min_ago,
  1739. )
  1740. query = {
  1741. "project": [self.project.id],
  1742. "field": [
  1743. "transaction",
  1744. "count()",
  1745. ],
  1746. "query": 'transaction:"<< unparameterized >>"',
  1747. "statsPeriod": "24h",
  1748. "dataset": "metrics",
  1749. "per_page": 50,
  1750. }
  1751. self.wait_for_metric_count(self.project, 3)
  1752. response = self.do_request(query)
  1753. assert response.status_code == 200, response.content
  1754. assert len(response.data["data"]) == 1
  1755. assert response.data["data"][0]["transaction"] == "<< unparameterized >>"
  1756. assert response.data["data"][0]["count()"] == 2
  1757. def test_custom_measurements_without_function(self):
  1758. self.store_transaction_metric(
  1759. 33,
  1760. metric="measurements.datacenter_memory",
  1761. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1762. entity="metrics_distributions",
  1763. tags={"transaction": "foo_transaction"},
  1764. timestamp=self.min_ago,
  1765. )
  1766. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1767. transaction_data["measurements"]["datacenter_memory"] = {
  1768. "value": 33,
  1769. "unit": "petabyte",
  1770. }
  1771. self.store_event(transaction_data, self.project.id)
  1772. measurement = "measurements.datacenter_memory"
  1773. response = self.do_request(
  1774. {
  1775. "field": ["transaction", measurement],
  1776. "query": "measurements.datacenter_memory:33pb",
  1777. "dataset": "discover",
  1778. }
  1779. )
  1780. assert response.status_code == 200, response.content
  1781. data = response.data["data"]
  1782. assert len(data) == 1
  1783. assert data[0][measurement] == 33
  1784. meta = response.data["meta"]
  1785. field_meta = meta["fields"]
  1786. unit_meta = meta["units"]
  1787. assert field_meta[measurement] == "size"
  1788. assert unit_meta[measurement] == "petabyte"
  1789. assert not meta["isMetricsData"]
  1790. def test_custom_measurements_with_function(self):
  1791. self.store_transaction_metric(
  1792. 33,
  1793. metric="measurements.datacenter_memory",
  1794. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1795. entity="metrics_distributions",
  1796. tags={"transaction": "foo_transaction"},
  1797. timestamp=self.min_ago,
  1798. )
  1799. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1800. transaction_data["measurements"]["datacenter_memory"] = {
  1801. "value": 33,
  1802. "unit": "petabyte",
  1803. }
  1804. self.store_event(transaction_data, self.project.id)
  1805. measurement = "p50(measurements.datacenter_memory)"
  1806. response = self.do_request(
  1807. {
  1808. "field": ["transaction", measurement],
  1809. "query": "measurements.datacenter_memory:33pb",
  1810. "dataset": "discover",
  1811. }
  1812. )
  1813. assert response.status_code == 200, response.content
  1814. data = response.data["data"]
  1815. assert len(data) == 1
  1816. assert data[0][measurement] == 33
  1817. meta = response.data["meta"]
  1818. field_meta = meta["fields"]
  1819. unit_meta = meta["units"]
  1820. assert field_meta[measurement] == "size"
  1821. assert unit_meta[measurement] == "petabyte"
  1822. assert not meta["isMetricsData"]
  1823. def test_custom_measurements_equation(self):
  1824. self.store_transaction_metric(
  1825. 33,
  1826. metric="measurements.datacenter_memory",
  1827. internal_metric="d:transactions/measurements.datacenter_memory@petabyte",
  1828. entity="metrics_distributions",
  1829. tags={"transaction": "foo_transaction"},
  1830. timestamp=self.min_ago,
  1831. )
  1832. transaction_data = load_data("transaction", timestamp=self.min_ago)
  1833. transaction_data["measurements"]["datacenter_memory"] = {
  1834. "value": 33,
  1835. "unit": "petabyte",
  1836. }
  1837. self.store_event(transaction_data, self.project.id)
  1838. response = self.do_request(
  1839. {
  1840. "field": [
  1841. "transaction",
  1842. "measurements.datacenter_memory",
  1843. "equation|measurements.datacenter_memory / 3",
  1844. ],
  1845. "query": "",
  1846. "dataset": "discover",
  1847. }
  1848. )
  1849. assert response.status_code == 200, response.content
  1850. data = response.data["data"]
  1851. assert len(data) == 1
  1852. assert data[0]["measurements.datacenter_memory"] == 33
  1853. assert data[0]["equation|measurements.datacenter_memory / 3"] == 11
  1854. meta = response.data["meta"]
  1855. assert not meta["isMetricsData"]
  1856. def test_transaction_wildcard(self):
  1857. self.store_transaction_metric(
  1858. 1,
  1859. tags={"transaction": "foo_transaction"},
  1860. timestamp=self.min_ago,
  1861. )
  1862. self.store_transaction_metric(
  1863. 1,
  1864. tags={"transaction": "bar_transaction"},
  1865. timestamp=self.min_ago,
  1866. )
  1867. response = self.do_request(
  1868. {
  1869. "field": [
  1870. "transaction",
  1871. "p90()",
  1872. ],
  1873. "query": "transaction:foo*",
  1874. "dataset": "metrics",
  1875. }
  1876. )
  1877. assert response.status_code == 200, response.content
  1878. data = response.data["data"]
  1879. assert len(data) == 1
  1880. assert data[0]["p90()"] == 1
  1881. meta = response.data["meta"]
  1882. assert meta["isMetricsData"]
  1883. assert data[0]["transaction"] == "foo_transaction"
  1884. def test_transaction_status_wildcard(self):
  1885. self.store_transaction_metric(
  1886. 1,
  1887. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1888. timestamp=self.min_ago,
  1889. )
  1890. response = self.do_request(
  1891. {
  1892. "field": [
  1893. "transaction",
  1894. "p90()",
  1895. ],
  1896. "query": "transaction.status:f*bar",
  1897. "dataset": "metrics",
  1898. }
  1899. )
  1900. assert response.status_code == 200, response.content
  1901. data = response.data["data"]
  1902. assert len(data) == 1
  1903. assert data[0]["p90()"] == 1
  1904. meta = response.data["meta"]
  1905. assert meta["isMetricsData"]
  1906. def test_os_name_incompatible(self):
  1907. self.store_transaction_metric(
  1908. 1,
  1909. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1910. timestamp=self.min_ago,
  1911. )
  1912. response = self.do_request(
  1913. {
  1914. "field": [
  1915. "os.name",
  1916. "p90()",
  1917. ],
  1918. "query": "transaction:foo_transaction",
  1919. "dataset": "metrics",
  1920. }
  1921. )
  1922. assert response.status_code == 400, response.content
  1923. assert response.data["detail"] == "os.name is unavailable"
  1924. def test_os_name_falls_back(self):
  1925. self.store_transaction_metric(
  1926. 1,
  1927. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1928. timestamp=self.min_ago,
  1929. )
  1930. response = self.do_request(
  1931. {
  1932. "field": [
  1933. "os.name",
  1934. "p75()",
  1935. ],
  1936. "query": "transaction:foo_transaction",
  1937. "dataset": "metricsEnhanced",
  1938. }
  1939. )
  1940. assert response.status_code == 200, response.content
  1941. meta = response.data["meta"]
  1942. assert not meta["isMetricsData"]
  1943. def test_http_error_rate(self):
  1944. self.store_transaction_metric(
  1945. 1,
  1946. tags={
  1947. "transaction": "foo_transaction",
  1948. "transaction.status": "foobar",
  1949. "http.status_code": "500",
  1950. },
  1951. timestamp=self.min_ago,
  1952. )
  1953. self.store_transaction_metric(
  1954. 1,
  1955. tags={"transaction": "bar_transaction", "http.status_code": "400"},
  1956. timestamp=self.min_ago,
  1957. )
  1958. response = self.do_request(
  1959. {
  1960. "field": [
  1961. "http_error_rate()",
  1962. ],
  1963. "dataset": "metrics",
  1964. }
  1965. )
  1966. assert response.status_code == 200, response.content
  1967. data = response.data["data"]
  1968. assert len(data) == 1
  1969. assert data[0]["http_error_rate()"] == 0.5
  1970. meta = response.data["meta"]
  1971. assert meta["isMetricsData"]
  1972. def test_time_spent(self):
  1973. self.store_transaction_metric(
  1974. 1,
  1975. tags={"transaction": "foo_transaction", "transaction.status": "foobar"},
  1976. timestamp=self.min_ago,
  1977. )
  1978. self.store_transaction_metric(
  1979. 1,
  1980. tags={"transaction": "bar_transaction"},
  1981. timestamp=self.min_ago,
  1982. )
  1983. response = self.do_request(
  1984. {
  1985. "field": [
  1986. "transaction",
  1987. "time_spent_percentage()",
  1988. ],
  1989. "dataset": "metrics",
  1990. }
  1991. )
  1992. assert response.status_code == 200, response.content
  1993. data = response.data["data"]
  1994. assert len(data) == 2
  1995. assert data[0]["time_spent_percentage()"] == 0.5
  1996. meta = response.data["meta"]
  1997. assert meta["isMetricsData"]
  1998. class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithMetricLayer(
  1999. OrganizationEventsMetricsEnhancedPerformanceEndpointTest
  2000. ):
  2001. def setUp(self):
  2002. super().setUp()
  2003. self.features["organizations:use-metrics-layer"] = True
  2004. @pytest.mark.xfail(reason="Having not supported")
  2005. def test_custom_measurement_duration_filtering(self):
  2006. super().test_custom_measurement_size_filtering()
  2007. @pytest.mark.xfail(reason="Having not supported")
  2008. def test_having_condition_not_selected(self):
  2009. super().test_having_condition_not_selected()
  2010. @pytest.mark.xfail(reason="Having not supported")
  2011. def test_custom_measurement_size_filtering(self):
  2012. super().test_custom_measurement_size_filtering()
  2013. @pytest.mark.xfail(reason="Having not supported")
  2014. def test_having_condition(self):
  2015. super().test_having_condition()
  2016. @pytest.mark.xfail(reason="Not supported")
  2017. def test_time_spent(self):
  2018. super().test_custom_measurement_size_filtering()
  2019. @pytest.mark.xfail(reason="Not supported")
  2020. def test_http_error_rate(self):
  2021. super().test_having_condition()