test_organization_events_mep.py 80 KB

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