test_organization_events_mep.py 91 KB

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