test_organization_events_mep.py 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147
  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. class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithMetricLayer(
  1907. OrganizationEventsMetricsEnhancedPerformanceEndpointTest
  1908. ):
  1909. def setUp(self):
  1910. super().setUp()
  1911. self.features["organizations:use-metrics-layer"] = True
  1912. @pytest.mark.xfail(reason="Having not supported")
  1913. def test_custom_measurement_duration_filtering(self):
  1914. super().test_custom_measurement_size_filtering()
  1915. @pytest.mark.xfail(reason="Having not supported")
  1916. def test_having_condition_not_selected(self):
  1917. super().test_having_condition_not_selected()
  1918. @pytest.mark.xfail(reason="Having not supported")
  1919. def test_custom_measurement_size_filtering(self):
  1920. super().test_custom_measurement_size_filtering()
  1921. @pytest.mark.xfail(reason="Having not supported")
  1922. def test_having_condition(self):
  1923. super().test_having_condition()