test_organization_events_stats_mep.py 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  1. from __future__ import annotations
  2. from datetime import timedelta
  3. from typing import Any
  4. from unittest import mock
  5. import pytest
  6. from django.urls import reverse
  7. from sentry.models.environment import Environment
  8. from sentry.sentry_metrics.use_case_id_registry import UseCaseID
  9. from sentry.snuba.metrics.extraction import MetricSpecType, OnDemandMetricSpec
  10. from sentry.testutils.cases import MetricsEnhancedPerformanceTestCase
  11. from sentry.testutils.helpers.datetime import before_now, iso_format
  12. from sentry.testutils.silo import region_silo_test
  13. pytestmark = pytest.mark.sentry_metrics
  14. @region_silo_test
  15. class OrganizationEventsStatsMetricsEnhancedPerformanceEndpointTest(
  16. MetricsEnhancedPerformanceTestCase
  17. ):
  18. endpoint = "sentry-api-0-organization-events-stats"
  19. METRIC_STRINGS = [
  20. "foo_transaction",
  21. "d:transactions/measurements.datacenter_memory@pebibyte",
  22. ]
  23. def setUp(self):
  24. super().setUp()
  25. self.login_as(user=self.user)
  26. self.day_ago = before_now(days=1).replace(hour=10, minute=0, second=0, microsecond=0)
  27. self.DEFAULT_METRIC_TIMESTAMP = self.day_ago
  28. self.url = reverse(
  29. "sentry-api-0-organization-events-stats",
  30. kwargs={"organization_slug": self.project.organization.slug},
  31. )
  32. self.features = {
  33. "organizations:performance-use-metrics": True,
  34. }
  35. self.additional_params = dict()
  36. def do_request(self, data, url=None, features=None):
  37. if features is None:
  38. features = {"organizations:discover-basic": True}
  39. features.update(self.features)
  40. with self.feature(features):
  41. return self.client.get(self.url if url is None else url, data=data, format="json")
  42. # These throughput tests should roughly match the ones in OrganizationEventsStatsEndpointTest
  43. def test_throughput_epm_hour_rollup(self):
  44. # Each of these denotes how many events to create in each hour
  45. event_counts = [6, 0, 6, 3, 0, 3]
  46. for hour, count in enumerate(event_counts):
  47. for minute in range(count):
  48. self.store_transaction_metric(
  49. 1, timestamp=self.day_ago + timedelta(hours=hour, minutes=minute)
  50. )
  51. for axis in ["epm()", "tpm()"]:
  52. response = self.do_request(
  53. data={
  54. "start": iso_format(self.day_ago),
  55. "end": iso_format(self.day_ago + timedelta(hours=6)),
  56. "interval": "1h",
  57. "yAxis": axis,
  58. "project": self.project.id,
  59. "dataset": "metricsEnhanced",
  60. **self.additional_params,
  61. },
  62. )
  63. assert response.status_code == 200, response.content
  64. data = response.data["data"]
  65. assert len(data) == 6
  66. assert response.data["isMetricsData"]
  67. rows = data[0:6]
  68. for test in zip(event_counts, rows):
  69. assert test[1][1][0]["count"] == test[0] / (3600.0 / 60.0)
  70. def test_throughput_epm_day_rollup(self):
  71. # Each of these denotes how many events to create in each minute
  72. event_counts = [6, 0, 6, 3, 0, 3]
  73. for hour, count in enumerate(event_counts):
  74. for minute in range(count):
  75. self.store_transaction_metric(
  76. 1, timestamp=self.day_ago + timedelta(hours=hour, minutes=minute)
  77. )
  78. for axis in ["epm()", "tpm()"]:
  79. response = self.do_request(
  80. data={
  81. "start": iso_format(self.day_ago),
  82. "end": iso_format(self.day_ago + timedelta(hours=24)),
  83. "interval": "24h",
  84. "yAxis": axis,
  85. "project": self.project.id,
  86. "dataset": "metricsEnhanced",
  87. **self.additional_params,
  88. },
  89. )
  90. assert response.status_code == 200, response.content
  91. data = response.data["data"]
  92. assert len(data) == 2
  93. assert response.data["isMetricsData"]
  94. assert data[0][1][0]["count"] == sum(event_counts) / (86400.0 / 60.0)
  95. def test_throughput_epm_hour_rollup_offset_of_hour(self):
  96. # Each of these denotes how many events to create in each hour
  97. event_counts = [6, 0, 6, 3, 0, 3]
  98. for hour, count in enumerate(event_counts):
  99. for minute in range(count):
  100. self.store_transaction_metric(
  101. 1, timestamp=self.day_ago + timedelta(hours=hour, minutes=minute + 30)
  102. )
  103. for axis in ["tpm()", "epm()"]:
  104. response = self.do_request(
  105. data={
  106. "start": iso_format(self.day_ago + timedelta(minutes=30)),
  107. "end": iso_format(self.day_ago + timedelta(hours=6, minutes=30)),
  108. "interval": "1h",
  109. "yAxis": axis,
  110. "project": self.project.id,
  111. "dataset": "metricsEnhanced",
  112. **self.additional_params,
  113. },
  114. )
  115. assert response.status_code == 200, response.content
  116. data = response.data["data"]
  117. assert len(data) == 6
  118. assert response.data["isMetricsData"]
  119. rows = data[0:6]
  120. for test in zip(event_counts, rows):
  121. assert test[1][1][0]["count"] == test[0] / (3600.0 / 60.0)
  122. def test_throughput_eps_minute_rollup(self):
  123. # Each of these denotes how many events to create in each minute
  124. event_counts = [6, 0, 6, 3, 0, 3]
  125. for minute, count in enumerate(event_counts):
  126. for second in range(count):
  127. self.store_transaction_metric(
  128. 1, timestamp=self.day_ago + timedelta(minutes=minute, seconds=second)
  129. )
  130. for axis in ["eps()", "tps()"]:
  131. response = self.do_request(
  132. data={
  133. "start": iso_format(self.day_ago),
  134. "end": iso_format(self.day_ago + timedelta(minutes=6)),
  135. "interval": "1m",
  136. "yAxis": axis,
  137. "project": self.project.id,
  138. "dataset": "metricsEnhanced",
  139. **self.additional_params,
  140. },
  141. )
  142. assert response.status_code == 200, response.content
  143. data = response.data["data"]
  144. assert len(data) == 6
  145. assert response.data["isMetricsData"]
  146. rows = data[0:6]
  147. for test in zip(event_counts, rows):
  148. assert test[1][1][0]["count"] == test[0] / 60.0
  149. def test_failure_rate(self):
  150. for hour in range(6):
  151. timestamp = self.day_ago + timedelta(hours=hour, minutes=30)
  152. self.store_transaction_metric(1, tags={"transaction.status": "ok"}, timestamp=timestamp)
  153. if hour < 3:
  154. self.store_transaction_metric(
  155. 1, tags={"transaction.status": "internal_error"}, timestamp=timestamp
  156. )
  157. response = self.do_request(
  158. data={
  159. "start": iso_format(self.day_ago),
  160. "end": iso_format(self.day_ago + timedelta(hours=6)),
  161. "interval": "1h",
  162. "yAxis": ["failure_rate()"],
  163. "project": self.project.id,
  164. "dataset": "metricsEnhanced",
  165. **self.additional_params,
  166. },
  167. )
  168. assert response.status_code == 200, response.content
  169. data = response.data["data"]
  170. assert len(data) == 6
  171. assert response.data["isMetricsData"]
  172. assert [attrs for time, attrs in response.data["data"]] == [
  173. [{"count": 0.5}],
  174. [{"count": 0.5}],
  175. [{"count": 0.5}],
  176. [{"count": 0}],
  177. [{"count": 0}],
  178. [{"count": 0}],
  179. ]
  180. def test_percentiles_multi_axis(self):
  181. for hour in range(6):
  182. timestamp = self.day_ago + timedelta(hours=hour, minutes=30)
  183. self.store_transaction_metric(111, timestamp=timestamp)
  184. self.store_transaction_metric(222, metric="measurements.lcp", timestamp=timestamp)
  185. response = self.do_request(
  186. data={
  187. "start": iso_format(self.day_ago),
  188. "end": iso_format(self.day_ago + timedelta(hours=6)),
  189. "interval": "1h",
  190. "yAxis": ["p75(measurements.lcp)", "p75(transaction.duration)"],
  191. "project": self.project.id,
  192. "dataset": "metricsEnhanced",
  193. **self.additional_params,
  194. },
  195. )
  196. assert response.status_code == 200, response.content
  197. lcp = response.data["p75(measurements.lcp)"]
  198. duration = response.data["p75(transaction.duration)"]
  199. assert len(duration["data"]) == 6
  200. assert duration["isMetricsData"]
  201. assert len(lcp["data"]) == 6
  202. assert lcp["isMetricsData"]
  203. for item in duration["data"]:
  204. assert item[1][0]["count"] == 111
  205. for item in lcp["data"]:
  206. assert item[1][0]["count"] == 222
  207. @mock.patch("sentry.snuba.metrics_enhanced_performance.timeseries_query", return_value={})
  208. def test_multiple_yaxis_only_one_query(self, mock_query):
  209. self.do_request(
  210. data={
  211. "project": self.project.id,
  212. "start": iso_format(self.day_ago),
  213. "end": iso_format(self.day_ago + timedelta(hours=2)),
  214. "interval": "1h",
  215. "yAxis": ["epm()", "eps()", "tpm()", "p50(transaction.duration)"],
  216. "dataset": "metricsEnhanced",
  217. **self.additional_params,
  218. },
  219. )
  220. assert mock_query.call_count == 1
  221. def test_aggregate_function_user_count(self):
  222. self.store_transaction_metric(
  223. 1, metric="user", timestamp=self.day_ago + timedelta(minutes=30)
  224. )
  225. self.store_transaction_metric(
  226. 1, metric="user", timestamp=self.day_ago + timedelta(hours=1, minutes=30)
  227. )
  228. response = self.do_request(
  229. data={
  230. "start": iso_format(self.day_ago),
  231. "end": iso_format(self.day_ago + timedelta(hours=2)),
  232. "interval": "1h",
  233. "yAxis": "count_unique(user)",
  234. "dataset": "metricsEnhanced",
  235. **self.additional_params,
  236. },
  237. )
  238. assert response.status_code == 200, response.content
  239. assert response.data["isMetricsData"]
  240. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 1}], [{"count": 1}]]
  241. meta = response.data["meta"]
  242. assert meta["isMetricsData"] == response.data["isMetricsData"]
  243. def test_non_mep_query_fallsback(self):
  244. def get_mep(query):
  245. response = self.do_request(
  246. data={
  247. "project": self.project.id,
  248. "start": iso_format(self.day_ago),
  249. "end": iso_format(self.day_ago + timedelta(hours=2)),
  250. "interval": "1h",
  251. "query": query,
  252. "yAxis": ["epm()"],
  253. "dataset": "metricsEnhanced",
  254. **self.additional_params,
  255. },
  256. )
  257. assert response.status_code == 200, response.content
  258. return response.data["isMetricsData"]
  259. assert get_mep(""), "empty query"
  260. assert get_mep("event.type:transaction"), "event type transaction"
  261. assert not get_mep("event.type:error"), "event type error"
  262. assert not get_mep("transaction.duration:<15min"), "outlier filter"
  263. assert get_mep("epm():>0.01"), "throughput filter"
  264. assert not get_mep(
  265. "event.type:transaction OR event.type:error"
  266. ), "boolean with non-mep filter"
  267. assert get_mep(
  268. "event.type:transaction OR transaction:foo_transaction"
  269. ), "boolean with mep filter"
  270. def test_having_condition_with_preventing_aggregates(self):
  271. response = self.do_request(
  272. data={
  273. "project": self.project.id,
  274. "start": iso_format(self.day_ago),
  275. "end": iso_format(self.day_ago + timedelta(hours=2)),
  276. "interval": "1h",
  277. "query": "p95():<5s",
  278. "yAxis": ["epm()"],
  279. "dataset": "metricsEnhanced",
  280. "preventMetricAggregates": "1",
  281. **self.additional_params,
  282. },
  283. )
  284. assert response.status_code == 200, response.content
  285. assert not response.data["isMetricsData"]
  286. meta = response.data["meta"]
  287. assert meta["isMetricsData"] == response.data["isMetricsData"]
  288. def test_explicit_not_mep(self):
  289. response = self.do_request(
  290. data={
  291. "project": self.project.id,
  292. "start": iso_format(self.day_ago),
  293. "end": iso_format(self.day_ago + timedelta(hours=2)),
  294. "interval": "1h",
  295. # Should be a mep able query
  296. "query": "",
  297. "yAxis": ["epm()"],
  298. "metricsEnhanced": "0",
  299. **self.additional_params,
  300. },
  301. )
  302. assert response.status_code == 200, response.content
  303. assert not response.data["isMetricsData"]
  304. meta = response.data["meta"]
  305. assert meta["isMetricsData"] == response.data["isMetricsData"]
  306. def test_sum_transaction_duration(self):
  307. self.store_transaction_metric(123, timestamp=self.day_ago + timedelta(minutes=30))
  308. self.store_transaction_metric(456, timestamp=self.day_ago + timedelta(hours=1, minutes=30))
  309. self.store_transaction_metric(789, timestamp=self.day_ago + timedelta(hours=1, minutes=30))
  310. response = self.do_request(
  311. data={
  312. "start": iso_format(self.day_ago),
  313. "end": iso_format(self.day_ago + timedelta(hours=2)),
  314. "interval": "1h",
  315. "yAxis": "sum(transaction.duration)",
  316. "dataset": "metricsEnhanced",
  317. **self.additional_params,
  318. },
  319. )
  320. assert response.status_code == 200, response.content
  321. assert response.data["isMetricsData"]
  322. assert [attrs for time, attrs in response.data["data"]] == [
  323. [{"count": 123}],
  324. [{"count": 1245}],
  325. ]
  326. meta = response.data["meta"]
  327. assert meta["isMetricsData"] == response.data["isMetricsData"]
  328. assert meta["fields"] == {"time": "date", "sum_transaction_duration": "duration"}
  329. assert meta["units"] == {"time": None, "sum_transaction_duration": "millisecond"}
  330. def test_sum_transaction_duration_with_comparison(self):
  331. # We store the data for the previous day (in order to have values for the comparison).
  332. self.store_transaction_metric(
  333. 1, timestamp=self.day_ago - timedelta(days=1) + timedelta(minutes=30)
  334. )
  335. self.store_transaction_metric(
  336. 2, timestamp=self.day_ago - timedelta(days=1) + timedelta(minutes=30)
  337. )
  338. # We store the data for today.
  339. self.store_transaction_metric(123, timestamp=self.day_ago + timedelta(minutes=30))
  340. self.store_transaction_metric(456, timestamp=self.day_ago + timedelta(minutes=30))
  341. response = self.do_request(
  342. data={
  343. "start": iso_format(self.day_ago),
  344. "end": iso_format(self.day_ago + timedelta(days=1)),
  345. "interval": "1d",
  346. "yAxis": "sum(transaction.duration)",
  347. "comparisonDelta": 86400,
  348. "dataset": "metricsEnhanced",
  349. **self.additional_params,
  350. },
  351. )
  352. assert response.status_code == 200, response.content
  353. assert response.data["isMetricsData"]
  354. # For some reason, if all tests run, there is some shared state that makes this test have data in the second
  355. # time bucket, which is filled automatically by the zerofilling. In order to avoid this flaky failure, we will
  356. # only check that the first bucket contains the actual data.
  357. assert [attrs for time, attrs in response.data["data"]][0] == [
  358. {"comparisonCount": 3.0, "count": 579.0}
  359. ]
  360. meta = response.data["meta"]
  361. assert meta["isMetricsData"] == response.data["isMetricsData"]
  362. assert meta["fields"] == {"time": "date", "sum_transaction_duration": "duration"}
  363. assert meta["units"] == {"time": None, "sum_transaction_duration": "millisecond"}
  364. def test_custom_measurement(self):
  365. self.store_transaction_metric(
  366. 123,
  367. metric="measurements.bytes_transfered",
  368. internal_metric="d:transactions/measurements.datacenter_memory@pebibyte",
  369. entity="metrics_distributions",
  370. tags={"transaction": "foo_transaction"},
  371. timestamp=self.day_ago + timedelta(minutes=30),
  372. )
  373. self.store_transaction_metric(
  374. 456,
  375. metric="measurements.bytes_transfered",
  376. internal_metric="d:transactions/measurements.datacenter_memory@pebibyte",
  377. entity="metrics_distributions",
  378. tags={"transaction": "foo_transaction"},
  379. timestamp=self.day_ago + timedelta(hours=1, minutes=30),
  380. )
  381. self.store_transaction_metric(
  382. 789,
  383. metric="measurements.bytes_transfered",
  384. internal_metric="d:transactions/measurements.datacenter_memory@pebibyte",
  385. entity="metrics_distributions",
  386. tags={"transaction": "foo_transaction"},
  387. timestamp=self.day_ago + timedelta(hours=1, minutes=30),
  388. )
  389. response = self.do_request(
  390. data={
  391. "start": iso_format(self.day_ago),
  392. "end": iso_format(self.day_ago + timedelta(hours=2)),
  393. "interval": "1h",
  394. "yAxis": "sum(measurements.datacenter_memory)",
  395. "dataset": "metricsEnhanced",
  396. **self.additional_params,
  397. },
  398. )
  399. assert response.status_code == 200, response.content
  400. assert response.data["isMetricsData"]
  401. assert [attrs for time, attrs in response.data["data"]] == [
  402. [{"count": 123}],
  403. [{"count": 1245}],
  404. ]
  405. meta = response.data["meta"]
  406. assert meta["isMetricsData"] == response.data["isMetricsData"]
  407. assert meta["fields"] == {"time": "date", "sum_measurements_datacenter_memory": "size"}
  408. assert meta["units"] == {"time": None, "sum_measurements_datacenter_memory": "pebibyte"}
  409. def test_does_not_fallback_if_custom_metric_is_out_of_request_time_range(self):
  410. self.store_transaction_metric(
  411. 123,
  412. timestamp=self.day_ago + timedelta(hours=1),
  413. internal_metric="d:transactions/measurements.custom@kibibyte",
  414. entity="metrics_distributions",
  415. )
  416. response = self.do_request(
  417. data={
  418. "start": iso_format(self.day_ago),
  419. "end": iso_format(self.day_ago + timedelta(hours=2)),
  420. "interval": "1h",
  421. "yAxis": "p99(measurements.custom)",
  422. "dataset": "metricsEnhanced",
  423. **self.additional_params,
  424. },
  425. )
  426. meta = response.data["meta"]
  427. assert response.status_code == 200, response.content
  428. assert response.data["isMetricsData"]
  429. assert meta["isMetricsData"]
  430. assert meta["fields"] == {"time": "date", "p99_measurements_custom": "size"}
  431. assert meta["units"] == {"time": None, "p99_measurements_custom": "kibibyte"}
  432. def test_multi_yaxis_custom_measurement(self):
  433. self.store_transaction_metric(
  434. 123,
  435. metric="measurements.bytes_transfered",
  436. internal_metric="d:transactions/measurements.datacenter_memory@pebibyte",
  437. entity="metrics_distributions",
  438. tags={"transaction": "foo_transaction"},
  439. timestamp=self.day_ago + timedelta(minutes=30),
  440. )
  441. self.store_transaction_metric(
  442. 456,
  443. metric="measurements.bytes_transfered",
  444. internal_metric="d:transactions/measurements.datacenter_memory@pebibyte",
  445. entity="metrics_distributions",
  446. tags={"transaction": "foo_transaction"},
  447. timestamp=self.day_ago + timedelta(hours=1, minutes=30),
  448. )
  449. self.store_transaction_metric(
  450. 789,
  451. metric="measurements.bytes_transfered",
  452. internal_metric="d:transactions/measurements.datacenter_memory@pebibyte",
  453. entity="metrics_distributions",
  454. tags={"transaction": "foo_transaction"},
  455. timestamp=self.day_ago + timedelta(hours=1, minutes=30),
  456. )
  457. response = self.do_request(
  458. data={
  459. "start": iso_format(self.day_ago),
  460. "end": iso_format(self.day_ago + timedelta(hours=2)),
  461. "interval": "1h",
  462. "yAxis": [
  463. "sum(measurements.datacenter_memory)",
  464. "p50(measurements.datacenter_memory)",
  465. ],
  466. "dataset": "metricsEnhanced",
  467. **self.additional_params,
  468. },
  469. )
  470. assert response.status_code == 200, response.content
  471. sum_data = response.data["sum(measurements.datacenter_memory)"]
  472. p50_data = response.data["p50(measurements.datacenter_memory)"]
  473. assert sum_data["isMetricsData"]
  474. assert p50_data["isMetricsData"]
  475. assert [attrs for time, attrs in sum_data["data"]] == [
  476. [{"count": 123}],
  477. [{"count": 1245}],
  478. ]
  479. assert [attrs for time, attrs in p50_data["data"]] == [
  480. [{"count": 123}],
  481. [{"count": 622.5}],
  482. ]
  483. sum_meta = sum_data["meta"]
  484. assert sum_meta["isMetricsData"] == sum_data["isMetricsData"]
  485. assert sum_meta["fields"] == {
  486. "time": "date",
  487. "sum_measurements_datacenter_memory": "size",
  488. "p50_measurements_datacenter_memory": "size",
  489. }
  490. assert sum_meta["units"] == {
  491. "time": None,
  492. "sum_measurements_datacenter_memory": "pebibyte",
  493. "p50_measurements_datacenter_memory": "pebibyte",
  494. }
  495. p50_meta = p50_data["meta"]
  496. assert p50_meta["isMetricsData"] == p50_data["isMetricsData"]
  497. assert p50_meta["fields"] == {
  498. "time": "date",
  499. "sum_measurements_datacenter_memory": "size",
  500. "p50_measurements_datacenter_memory": "size",
  501. }
  502. assert p50_meta["units"] == {
  503. "time": None,
  504. "sum_measurements_datacenter_memory": "pebibyte",
  505. "p50_measurements_datacenter_memory": "pebibyte",
  506. }
  507. def test_dataset_metrics_does_not_fallback(self):
  508. self.store_transaction_metric(123, timestamp=self.day_ago + timedelta(minutes=30))
  509. self.store_transaction_metric(456, timestamp=self.day_ago + timedelta(hours=1, minutes=30))
  510. self.store_transaction_metric(789, timestamp=self.day_ago + timedelta(hours=1, minutes=30))
  511. response = self.do_request(
  512. data={
  513. "start": iso_format(self.day_ago),
  514. "end": iso_format(self.day_ago + timedelta(hours=2)),
  515. "interval": "1h",
  516. "query": "transaction.duration:<5s",
  517. "yAxis": "sum(transaction.duration)",
  518. "dataset": "metrics",
  519. **self.additional_params,
  520. },
  521. )
  522. assert response.status_code == 400, response.content
  523. def test_title_filter(self):
  524. self.store_transaction_metric(
  525. 123,
  526. tags={"transaction": "foo_transaction"},
  527. timestamp=self.day_ago + timedelta(minutes=30),
  528. )
  529. response = self.do_request(
  530. data={
  531. "start": iso_format(self.day_ago),
  532. "end": iso_format(self.day_ago + timedelta(hours=2)),
  533. "interval": "1h",
  534. "query": "title:foo_transaction",
  535. "yAxis": [
  536. "sum(transaction.duration)",
  537. ],
  538. "dataset": "metricsEnhanced",
  539. **self.additional_params,
  540. },
  541. )
  542. assert response.status_code == 200, response.content
  543. data = response.data["data"]
  544. assert [attrs for time, attrs in data] == [
  545. [{"count": 123}],
  546. [{"count": 0}],
  547. ]
  548. def test_transaction_status_unknown_error(self):
  549. self.store_transaction_metric(
  550. 123,
  551. tags={"transaction.status": "unknown"},
  552. timestamp=self.day_ago + timedelta(minutes=30),
  553. )
  554. response = self.do_request(
  555. data={
  556. "start": iso_format(self.day_ago),
  557. "end": iso_format(self.day_ago + timedelta(hours=2)),
  558. "interval": "1h",
  559. "query": "transaction.status:unknown_error",
  560. "yAxis": [
  561. "sum(transaction.duration)",
  562. ],
  563. "dataset": "metricsEnhanced",
  564. **self.additional_params,
  565. },
  566. )
  567. assert response.status_code == 200, response.content
  568. data = response.data["data"]
  569. assert [attrs for time, attrs in data] == [
  570. [{"count": 123}],
  571. [{"count": 0}],
  572. ]
  573. def test_custom_performance_metric_meta_contains_field_and_unit_data(self):
  574. self.store_transaction_metric(
  575. 123,
  576. timestamp=self.day_ago + timedelta(hours=1),
  577. internal_metric="d:transactions/measurements.custom@kibibyte",
  578. entity="metrics_distributions",
  579. )
  580. response = self.do_request(
  581. data={
  582. "start": iso_format(self.day_ago),
  583. "end": iso_format(self.day_ago + timedelta(hours=2)),
  584. "interval": "1h",
  585. "yAxis": "p99(measurements.custom)",
  586. "query": "",
  587. **self.additional_params,
  588. },
  589. )
  590. assert response.status_code == 200
  591. meta = response.data["meta"]
  592. assert meta["fields"] == {"time": "date", "p99_measurements_custom": "size"}
  593. assert meta["units"] == {"time": None, "p99_measurements_custom": "kibibyte"}
  594. def test_multi_series_custom_performance_metric_meta_contains_field_and_unit_data(self):
  595. self.store_transaction_metric(
  596. 123,
  597. timestamp=self.day_ago + timedelta(hours=1),
  598. internal_metric="d:transactions/measurements.custom@kibibyte",
  599. entity="metrics_distributions",
  600. )
  601. self.store_transaction_metric(
  602. 123,
  603. timestamp=self.day_ago + timedelta(hours=1),
  604. internal_metric="d:transactions/measurements.another.custom@pebibyte",
  605. entity="metrics_distributions",
  606. )
  607. response = self.do_request(
  608. data={
  609. "start": iso_format(self.day_ago),
  610. "end": iso_format(self.day_ago + timedelta(hours=2)),
  611. "interval": "1h",
  612. "yAxis": [
  613. "p95(measurements.custom)",
  614. "p99(measurements.custom)",
  615. "p99(measurements.another.custom)",
  616. ],
  617. "query": "",
  618. **self.additional_params,
  619. },
  620. )
  621. assert response.status_code == 200
  622. meta = response.data["p95(measurements.custom)"]["meta"]
  623. assert meta["fields"] == {
  624. "time": "date",
  625. "p95_measurements_custom": "size",
  626. "p99_measurements_custom": "size",
  627. "p99_measurements_another_custom": "size",
  628. }
  629. assert meta["units"] == {
  630. "time": None,
  631. "p95_measurements_custom": "kibibyte",
  632. "p99_measurements_custom": "kibibyte",
  633. "p99_measurements_another_custom": "pebibyte",
  634. }
  635. assert meta == response.data["p99(measurements.custom)"]["meta"]
  636. assert meta == response.data["p99(measurements.another.custom)"]["meta"]
  637. def test_no_top_events_with_project_field(self):
  638. project = self.create_project()
  639. response = self.do_request(
  640. data={
  641. # make sure to query the project with 0 events
  642. "project": project.id,
  643. "start": iso_format(self.day_ago),
  644. "end": iso_format(self.day_ago + timedelta(hours=2)),
  645. "interval": "1h",
  646. "yAxis": "count()",
  647. "orderby": ["-count()"],
  648. "field": ["count()", "project"],
  649. "topEvents": 5,
  650. "dataset": "metrics",
  651. **self.additional_params,
  652. },
  653. )
  654. assert response.status_code == 200, response.content
  655. # When there are no top events, we do not return an empty dict.
  656. # Instead, we return a single zero-filled series for an empty graph.
  657. data = response.data["data"]
  658. assert [attrs for time, attrs in data] == [[{"count": 0}], [{"count": 0}]]
  659. def test_top_events_with_transaction(self):
  660. transaction_spec = [("foo", 100), ("bar", 200), ("baz", 300)]
  661. for offset in range(5):
  662. for transaction, duration in transaction_spec:
  663. self.store_transaction_metric(
  664. duration,
  665. tags={"transaction": f"{transaction}_transaction"},
  666. timestamp=self.day_ago + timedelta(hours=offset, minutes=30),
  667. )
  668. response = self.do_request(
  669. data={
  670. # make sure to query the project with 0 events
  671. "project": self.project.id,
  672. "start": iso_format(self.day_ago),
  673. "end": iso_format(self.day_ago + timedelta(hours=5)),
  674. "interval": "1h",
  675. "yAxis": "p75(transaction.duration)",
  676. "orderby": ["-p75(transaction.duration)"],
  677. "field": ["p75(transaction.duration)", "transaction"],
  678. "topEvents": 5,
  679. "dataset": "metrics",
  680. **self.additional_params,
  681. },
  682. )
  683. assert response.status_code == 200, response.content
  684. for position, (transaction, duration) in enumerate(transaction_spec):
  685. data = response.data[f"{transaction}_transaction"]
  686. chart_data = data["data"]
  687. assert data["order"] == 2 - position
  688. assert [attrs for time, attrs in chart_data] == [[{"count": duration}]] * 5
  689. def test_top_events_with_project(self):
  690. self.store_transaction_metric(
  691. 100,
  692. timestamp=self.day_ago + timedelta(hours=1, minutes=30),
  693. )
  694. response = self.do_request(
  695. data={
  696. # make sure to query the project with 0 events
  697. "project": self.project.id,
  698. "start": iso_format(self.day_ago),
  699. "end": iso_format(self.day_ago + timedelta(hours=5)),
  700. "interval": "1h",
  701. "yAxis": "p75(transaction.duration)",
  702. "orderby": ["-p75(transaction.duration)"],
  703. "field": ["p75(transaction.duration)", "project"],
  704. "topEvents": 5,
  705. "dataset": "metrics",
  706. **self.additional_params,
  707. },
  708. )
  709. assert response.status_code == 200, response.content
  710. data = response.data[f"{self.project.slug}"]
  711. assert data["order"] == 0
  712. class OrganizationEventsStatsMetricsEnhancedPerformanceEndpointTestWithMetricLayer(
  713. OrganizationEventsStatsMetricsEnhancedPerformanceEndpointTest
  714. ):
  715. def setUp(self):
  716. super().setUp()
  717. self.features["organizations:use-metrics-layer"] = True
  718. self.additional_params = {"forceMetricsLayer": "true"}
  719. def test_counter_standard_metric(self):
  720. mri = "c:transactions/usage@none"
  721. for index, value in enumerate((10, 20, 30, 40, 50, 60)):
  722. self.store_transaction_metric(
  723. value,
  724. metric=mri,
  725. internal_metric=mri,
  726. entity="metrics_counters",
  727. timestamp=self.day_ago + timedelta(minutes=index),
  728. use_case_id=UseCaseID.CUSTOM,
  729. )
  730. response = self.do_request(
  731. data={
  732. "start": iso_format(self.day_ago),
  733. "end": iso_format(self.day_ago + timedelta(hours=6)),
  734. "interval": "1m",
  735. "yAxis": [f"sum({mri})"],
  736. "project": self.project.id,
  737. "dataset": "metricsEnhanced",
  738. **self.additional_params,
  739. },
  740. )
  741. assert response.status_code == 200, response.content
  742. data = response.data["data"]
  743. for (_, value), expected_value in zip(data, [10, 20, 30, 40, 50, 60]):
  744. assert value[0]["count"] == expected_value # type:ignore
  745. def test_counter_custom_metric(self):
  746. mri = "c:custom/sentry.process_profile.track_outcome@second"
  747. for index, value in enumerate((10, 20, 30, 40, 50, 60)):
  748. self.store_transaction_metric(
  749. value,
  750. metric=mri,
  751. internal_metric=mri,
  752. entity="metrics_counters",
  753. timestamp=self.day_ago + timedelta(hours=index),
  754. use_case_id=UseCaseID.CUSTOM,
  755. )
  756. response = self.do_request(
  757. data={
  758. "start": iso_format(self.day_ago),
  759. "end": iso_format(self.day_ago + timedelta(hours=6)),
  760. "interval": "1h",
  761. "yAxis": [f"sum({mri})"],
  762. "project": self.project.id,
  763. "dataset": "metricsEnhanced",
  764. **self.additional_params,
  765. },
  766. )
  767. assert response.status_code == 200, response.content
  768. data = response.data["data"]
  769. for (_, value), expected_value in zip(data, [10, 20, 30, 40, 50, 60]):
  770. assert value[0]["count"] == expected_value # type:ignore
  771. def test_distribution_custom_metric(self):
  772. mri = "d:custom/sentry.process_profile.track_outcome@second"
  773. for index, value in enumerate((10, 20, 30, 40, 50, 60)):
  774. for multiplier in (1, 2, 3):
  775. self.store_transaction_metric(
  776. value * multiplier,
  777. metric=mri,
  778. internal_metric=mri,
  779. entity="metrics_distributions",
  780. timestamp=self.day_ago + timedelta(hours=index),
  781. use_case_id=UseCaseID.CUSTOM,
  782. )
  783. response = self.do_request(
  784. data={
  785. "start": iso_format(self.day_ago),
  786. "end": iso_format(self.day_ago + timedelta(hours=6)),
  787. "interval": "1h",
  788. "yAxis": [f"min({mri})", f"max({mri})", f"p90({mri})"],
  789. "project": self.project.id,
  790. "dataset": "metricsEnhanced",
  791. **self.additional_params,
  792. },
  793. )
  794. assert response.status_code == 200, response.content
  795. data = response.data
  796. min = data[f"min({mri})"]["data"]
  797. for (_, value), expected_value in zip(min, [10.0, 20.0, 30.0, 40.0, 50.0, 60.0]):
  798. assert value[0]["count"] == expected_value # type:ignore
  799. max = data[f"max({mri})"]["data"]
  800. for (_, value), expected_value in zip(max, [30.0, 60.0, 90.0, 120.0, 150.0, 180.0]):
  801. assert value[0]["count"] == expected_value # type:ignore
  802. p90 = data[f"p90({mri})"]["data"]
  803. for (_, value), expected_value in zip(p90, [28.0, 56.0, 84.0, 112.0, 140.0, 168.0]):
  804. assert value[0]["count"] == expected_value # type:ignore
  805. def test_set_custom_metric(self):
  806. mri = "s:custom/sentry.process_profile.track_outcome@second"
  807. for index, value in enumerate((10, 20, 30, 40, 50, 60)):
  808. # We store each value a second time, since we want to check the de-duplication of sets.
  809. for i in range(0, 2):
  810. self.store_transaction_metric(
  811. value,
  812. metric=mri,
  813. internal_metric=mri,
  814. entity="metrics_sets",
  815. timestamp=self.day_ago + timedelta(hours=index),
  816. use_case_id=UseCaseID.CUSTOM,
  817. )
  818. response = self.do_request(
  819. data={
  820. "start": iso_format(self.day_ago),
  821. "end": iso_format(self.day_ago + timedelta(hours=6)),
  822. "interval": "1h",
  823. "yAxis": [f"count_unique({mri})"],
  824. "project": self.project.id,
  825. "dataset": "metricsEnhanced",
  826. **self.additional_params,
  827. },
  828. )
  829. assert response.status_code == 200, response.content
  830. data = response.data["data"]
  831. for (_, value), expected_value in zip(data, [1, 1, 1, 1, 1, 1]):
  832. assert value[0]["count"] == expected_value # type:ignore
  833. def test_gauge_custom_metric(self):
  834. mri = "g:custom/sentry.process_profile.track_outcome@second"
  835. for index, value in enumerate((10, 20, 30, 40, 50, 60)):
  836. for multiplier in (1, 3):
  837. self.store_transaction_metric(
  838. value * multiplier,
  839. metric=mri,
  840. internal_metric=mri,
  841. entity="metrics_gauges",
  842. # When multiple gauges are merged, in order to make the `last` merge work deterministically it's
  843. # better to have the gauges with different timestamps so that the last value is always the same.
  844. timestamp=self.day_ago + timedelta(hours=index, minutes=multiplier),
  845. use_case_id=UseCaseID.CUSTOM,
  846. )
  847. response = self.do_request(
  848. data={
  849. "start": iso_format(self.day_ago),
  850. "end": iso_format(self.day_ago + timedelta(hours=6)),
  851. "interval": "1h",
  852. "yAxis": [
  853. f"min({mri})",
  854. f"max({mri})",
  855. f"last({mri})",
  856. f"sum({mri})",
  857. f"count({mri})",
  858. ],
  859. "project": self.project.id,
  860. "dataset": "metricsEnhanced",
  861. **self.additional_params,
  862. },
  863. )
  864. assert response.status_code == 200, response.content
  865. data = response.data
  866. min = data[f"min({mri})"]["data"]
  867. for (_, value), expected_value in zip(min, [10.0, 20.0, 30.0, 40.0, 50.0, 60.0]):
  868. assert value[0]["count"] == expected_value # type:ignore
  869. max = data[f"max({mri})"]["data"]
  870. for (_, value), expected_value in zip(max, [30.0, 60.0, 90.0, 120.0, 150.0, 180.0]):
  871. assert value[0]["count"] == expected_value # type:ignore
  872. last = data[f"last({mri})"]["data"]
  873. for (_, value), expected_value in zip(last, [30.0, 60.0, 90.0, 120.0, 150.0, 180.0]):
  874. assert value[0]["count"] == expected_value # type:ignore
  875. sum = data[f"sum({mri})"]["data"]
  876. for (_, value), expected_value in zip(sum, [40.0, 80.0, 120.0, 160.0, 200.0, 240.0]):
  877. assert value[0]["count"] == expected_value # type:ignore
  878. count = data[f"count({mri})"]["data"]
  879. for (_, value), expected_value in zip(count, [40, 80, 120, 160, 200, 240]):
  880. assert value[0]["count"] == expected_value # type:ignore
  881. @region_silo_test
  882. class OrganizationEventsStatsMetricsEnhancedPerformanceEndpointTestWithOnDemandWidgets(
  883. MetricsEnhancedPerformanceTestCase
  884. ):
  885. endpoint = "sentry-api-0-organization-events-stats"
  886. def setUp(self):
  887. super().setUp()
  888. self.login_as(user=self.user)
  889. self.day_ago = before_now(days=1).replace(hour=10, minute=0, second=0, microsecond=0)
  890. self.DEFAULT_METRIC_TIMESTAMP = self.day_ago
  891. Environment.get_or_create(self.project, "production")
  892. self.url = reverse(
  893. "sentry-api-0-organization-events-stats",
  894. kwargs={"organization_slug": self.project.organization.slug},
  895. )
  896. self.features = {"organizations:on-demand-metrics-extraction-widgets": True}
  897. def do_request(self, data, url=None, features=None):
  898. if features is None:
  899. features = {"organizations:discover-basic": True}
  900. features.update(self.features)
  901. with self.feature(features):
  902. return self.client.get(self.url if url is None else url, data=data, format="json")
  903. def test_top_events_wrong_on_demand_type(self):
  904. query = "transaction.duration:>=100"
  905. yAxis = ["count()", "count_web_vitals(measurements.lcp, good)"]
  906. response = self.do_request(
  907. data={
  908. "project": self.project.id,
  909. "start": iso_format(self.day_ago),
  910. "end": iso_format(self.day_ago + timedelta(hours=2)),
  911. "interval": "1h",
  912. "orderby": ["-count()"],
  913. "environment": "production",
  914. "query": query,
  915. "yAxis": yAxis,
  916. "field": [
  917. "count()",
  918. ],
  919. "topEvents": 5,
  920. "dataset": "metrics",
  921. "useOnDemandMetrics": "true",
  922. "onDemandType": "not_real",
  923. },
  924. )
  925. assert response.status_code == 400, response.content
  926. def test_top_events_works_without_on_demand_type(self):
  927. query = "transaction.duration:>=100"
  928. yAxis = ["count()", "count_web_vitals(measurements.lcp, good)"]
  929. response = self.do_request(
  930. data={
  931. "project": self.project.id,
  932. "start": iso_format(self.day_ago),
  933. "end": iso_format(self.day_ago + timedelta(hours=2)),
  934. "interval": "1h",
  935. "orderby": ["-count()"],
  936. "environment": "production",
  937. "query": query,
  938. "yAxis": yAxis,
  939. "field": [
  940. "count()",
  941. ],
  942. "topEvents": 5,
  943. "dataset": "metrics",
  944. "useOnDemandMetrics": "true",
  945. },
  946. )
  947. assert response.status_code == 200, response.content
  948. def test_top_events_with_transaction_on_demand(self):
  949. field = "count()"
  950. field_two = "count_web_vitals(measurements.lcp, good)"
  951. groupbys = ["customtag1", "customtag2"]
  952. query = "transaction.duration:>=100"
  953. spec = OnDemandMetricSpec(
  954. field=field, groupbys=groupbys, query=query, spec_type=MetricSpecType.DYNAMIC_QUERY
  955. )
  956. spec_two = OnDemandMetricSpec(
  957. field=field_two, groupbys=groupbys, query=query, spec_type=MetricSpecType.DYNAMIC_QUERY
  958. )
  959. for hour in range(0, 5):
  960. self.store_on_demand_metric(
  961. hour * 62 * 24,
  962. spec=spec,
  963. additional_tags={
  964. "customtag1": "foo",
  965. "customtag2": "red",
  966. "environment": "production",
  967. },
  968. timestamp=self.day_ago + timedelta(hours=hour),
  969. )
  970. self.store_on_demand_metric(
  971. hour * 60 * 24,
  972. spec=spec_two,
  973. additional_tags={
  974. "customtag1": "bar",
  975. "customtag2": "blue",
  976. "environment": "production",
  977. },
  978. timestamp=self.day_ago + timedelta(hours=hour),
  979. )
  980. yAxis = ["count()", "count_web_vitals(measurements.lcp, good)"]
  981. response = self.do_request(
  982. data={
  983. "project": self.project.id,
  984. "start": iso_format(self.day_ago),
  985. "end": iso_format(self.day_ago + timedelta(hours=2)),
  986. "interval": "1h",
  987. "orderby": ["-count()"],
  988. "environment": "production",
  989. "query": query,
  990. "yAxis": yAxis,
  991. "field": [
  992. "count()",
  993. "count_web_vitals(measurements.lcp, good)",
  994. "customtag1",
  995. "customtag2",
  996. ],
  997. "topEvents": 5,
  998. "dataset": "metricsEnhanced",
  999. "useOnDemandMetrics": "true",
  1000. "onDemandType": "dynamic_query",
  1001. },
  1002. )
  1003. assert response.status_code == 200, response.content
  1004. groups = [
  1005. ("foo,red", "count()", 0.0, 1488.0),
  1006. ("foo,red", "count_web_vitals(measurements.lcp, good)", 0.0, 0.0),
  1007. ("bar,blue", "count()", 0.0, 0.0),
  1008. ("bar,blue", "count_web_vitals(measurements.lcp, good)", 0.0, 1440.0),
  1009. ]
  1010. assert len(response.data.keys()) == 2
  1011. for group_count in groups:
  1012. group, agg, row1, row2 = group_count
  1013. row_data = response.data[group][agg]["data"][:2]
  1014. assert [attrs for _, attrs in row_data] == [[{"count": row1}], [{"count": row2}]]
  1015. assert response.data[group][agg]["meta"]["isMetricsExtractedData"]
  1016. assert response.data[group]["isMetricsExtractedData"]
  1017. def test_top_events_with_transaction_on_demand_and_no_environment(self):
  1018. field = "count()"
  1019. field_two = "count_web_vitals(measurements.lcp, good)"
  1020. groupbys = ["customtag1", "customtag2"]
  1021. query = "transaction.duration:>=100"
  1022. spec = OnDemandMetricSpec(
  1023. field=field, groupbys=groupbys, query=query, spec_type=MetricSpecType.DYNAMIC_QUERY
  1024. )
  1025. spec_two = OnDemandMetricSpec(
  1026. field=field_two, groupbys=groupbys, query=query, spec_type=MetricSpecType.DYNAMIC_QUERY
  1027. )
  1028. for hour in range(0, 5):
  1029. self.store_on_demand_metric(
  1030. hour * 62 * 24,
  1031. spec=spec,
  1032. additional_tags={
  1033. "customtag1": "foo",
  1034. "customtag2": "red",
  1035. "environment": "production",
  1036. },
  1037. timestamp=self.day_ago + timedelta(hours=hour),
  1038. )
  1039. self.store_on_demand_metric(
  1040. hour * 60 * 24,
  1041. spec=spec_two,
  1042. additional_tags={
  1043. "customtag1": "bar",
  1044. "customtag2": "blue",
  1045. "environment": "production",
  1046. },
  1047. timestamp=self.day_ago + timedelta(hours=hour),
  1048. )
  1049. yAxis = ["count()", "count_web_vitals(measurements.lcp, good)"]
  1050. response = self.do_request(
  1051. data={
  1052. "project": self.project.id,
  1053. "start": iso_format(self.day_ago),
  1054. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1055. "interval": "1h",
  1056. "orderby": ["-count()"],
  1057. "query": query,
  1058. "yAxis": yAxis,
  1059. "field": [
  1060. "count()",
  1061. "count_web_vitals(measurements.lcp, good)",
  1062. "customtag1",
  1063. "customtag2",
  1064. ],
  1065. "topEvents": 5,
  1066. "dataset": "metricsEnhanced",
  1067. "useOnDemandMetrics": "true",
  1068. "onDemandType": "dynamic_query",
  1069. },
  1070. )
  1071. assert response.status_code == 200, response.content
  1072. groups = [
  1073. ("foo,red", "count()", 0.0, 1488.0),
  1074. ("foo,red", "count_web_vitals(measurements.lcp, good)", 0.0, 0.0),
  1075. ("bar,blue", "count()", 0.0, 0.0),
  1076. ("bar,blue", "count_web_vitals(measurements.lcp, good)", 0.0, 1440.0),
  1077. ]
  1078. assert len(response.data.keys()) == 2
  1079. for group_count in groups:
  1080. group, agg, row1, row2 = group_count
  1081. row_data = response.data[group][agg]["data"][:2]
  1082. assert [attrs for time, attrs in row_data] == [[{"count": row1}], [{"count": row2}]]
  1083. assert response.data[group][agg]["meta"]["isMetricsExtractedData"]
  1084. assert response.data[group]["isMetricsExtractedData"]
  1085. def test_timeseries_on_demand_with_multiple_percentiles(self):
  1086. field = "p75(measurements.fcp)"
  1087. field_two = "p75(measurements.lcp)"
  1088. query = "transaction.duration:>=100"
  1089. spec = OnDemandMetricSpec(field=field, query=query, spec_type=MetricSpecType.DYNAMIC_QUERY)
  1090. spec_two = OnDemandMetricSpec(
  1091. field=field_two, query=query, spec_type=MetricSpecType.DYNAMIC_QUERY
  1092. )
  1093. assert (
  1094. spec._query_str_for_hash
  1095. == "event.measurements.fcp.value;{'name': 'event.duration', 'op': 'gte', 'value': 100.0}"
  1096. )
  1097. assert (
  1098. spec_two._query_str_for_hash
  1099. == "event.measurements.lcp.value;{'name': 'event.duration', 'op': 'gte', 'value': 100.0}"
  1100. )
  1101. for count in range(0, 4):
  1102. self.store_on_demand_metric(
  1103. count * 100,
  1104. spec=spec,
  1105. timestamp=self.day_ago + timedelta(hours=1),
  1106. )
  1107. self.store_on_demand_metric(
  1108. count * 200.0,
  1109. spec=spec_two,
  1110. timestamp=self.day_ago + timedelta(hours=1),
  1111. )
  1112. yAxis = [field, field_two]
  1113. response = self.do_request(
  1114. data={
  1115. "project": self.project.id,
  1116. "start": iso_format(self.day_ago),
  1117. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1118. "interval": "1h",
  1119. "orderby": [field],
  1120. "query": query,
  1121. "yAxis": yAxis,
  1122. "dataset": "metricsEnhanced",
  1123. "useOnDemandMetrics": "true",
  1124. "onDemandType": "dynamic_query",
  1125. },
  1126. )
  1127. assert response.status_code == 200, response.content
  1128. assert response.data["p75(measurements.fcp)"]["meta"]["isMetricsExtractedData"]
  1129. assert response.data["p75(measurements.lcp)"]["meta"]["isMetricsData"]
  1130. assert [attrs for time, attrs in response.data["p75(measurements.fcp)"]["data"]] == [
  1131. [{"count": 0}],
  1132. [{"count": 225.0}],
  1133. ]
  1134. assert response.data["p75(measurements.lcp)"]["meta"]["isMetricsExtractedData"]
  1135. assert response.data["p75(measurements.lcp)"]["meta"]["isMetricsData"]
  1136. assert [attrs for time, attrs in response.data["p75(measurements.lcp)"]["data"]] == [
  1137. [{"count": 0}],
  1138. [{"count": 450.0}],
  1139. ]
  1140. def test_apdex_issue(self):
  1141. field = "apdex(300)"
  1142. groupbys = ["group_tag"]
  1143. query = "transaction.duration:>=100"
  1144. spec = OnDemandMetricSpec(
  1145. field=field,
  1146. groupbys=groupbys,
  1147. query=query,
  1148. spec_type=MetricSpecType.DYNAMIC_QUERY,
  1149. )
  1150. for hour in range(0, 5):
  1151. self.store_on_demand_metric(
  1152. 1,
  1153. spec=spec,
  1154. additional_tags={
  1155. "group_tag": "group_one",
  1156. "environment": "production",
  1157. "satisfaction": "tolerable",
  1158. },
  1159. timestamp=self.day_ago + timedelta(hours=hour),
  1160. )
  1161. self.store_on_demand_metric(
  1162. 1,
  1163. spec=spec,
  1164. additional_tags={
  1165. "group_tag": "group_two",
  1166. "environment": "production",
  1167. "satisfaction": "satisfactory",
  1168. },
  1169. timestamp=self.day_ago + timedelta(hours=hour),
  1170. )
  1171. response = self.do_request(
  1172. data={
  1173. "dataset": "metricsEnhanced",
  1174. "environment": "production",
  1175. "excludeOther": 1,
  1176. "field": [field, "group_tag"],
  1177. "start": iso_format(self.day_ago),
  1178. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1179. "interval": "1h",
  1180. "orderby": f"-{field}",
  1181. "partial": 1,
  1182. "project": self.project.id,
  1183. "query": query,
  1184. "topEvents": 5,
  1185. "yAxis": field,
  1186. "onDemandType": "dynamic_query",
  1187. "useOnDemandMetrics": "true",
  1188. },
  1189. )
  1190. assert response.status_code == 200, response.content
  1191. assert response.data["group_one"]["meta"]["isMetricsExtractedData"] is True
  1192. assert [attrs for time, attrs in response.data["group_one"]["data"]] == [
  1193. [{"count": 0.5}],
  1194. [{"count": 0.5}],
  1195. ]
  1196. def test_glob_http_referer_on_demand(self):
  1197. agg = "count()"
  1198. network_id_tag = "networkId"
  1199. url = "https://sentry.io"
  1200. query = f'http.url:{url}/*/foo/bar/* http.referer:"{url}/*/bar/*" event.type:transaction'
  1201. spec = OnDemandMetricSpec(
  1202. field=agg,
  1203. groupbys=[network_id_tag],
  1204. query=query,
  1205. spec_type=MetricSpecType.DYNAMIC_QUERY,
  1206. )
  1207. assert spec.to_metric_spec(self.project) == {
  1208. "category": "transaction",
  1209. "mri": "c:transactions/on_demand@none",
  1210. "field": None,
  1211. "tags": [
  1212. {"key": "query_hash", "value": "ac241f56"},
  1213. {"key": "networkId", "field": "event.tags.networkId"},
  1214. {"key": "environment", "field": "event.environment"},
  1215. ],
  1216. "condition": {
  1217. "op": "and",
  1218. "inner": [
  1219. {
  1220. "op": "glob",
  1221. "name": "event.request.url",
  1222. "value": ["https://sentry.io/*/foo/bar/*"],
  1223. },
  1224. {
  1225. "op": "glob",
  1226. "name": "event.request.headers.Referer",
  1227. "value": ["https://sentry.io/*/bar/*"],
  1228. },
  1229. ],
  1230. },
  1231. }
  1232. for hour in range(0, 5):
  1233. self.store_on_demand_metric(
  1234. 1,
  1235. spec=spec,
  1236. additional_tags={network_id_tag: "1234"},
  1237. timestamp=self.day_ago + timedelta(hours=hour),
  1238. )
  1239. self.store_on_demand_metric(
  1240. 1,
  1241. spec=spec,
  1242. additional_tags={network_id_tag: "5678"},
  1243. timestamp=self.day_ago + timedelta(hours=hour),
  1244. )
  1245. response = self.do_request(
  1246. data={
  1247. "dataset": "metricsEnhanced",
  1248. "field": [network_id_tag, agg],
  1249. "start": iso_format(self.day_ago),
  1250. "end": iso_format(self.day_ago + timedelta(hours=5)),
  1251. "onDemandType": "dynamic_query",
  1252. "orderby": f"-{agg}",
  1253. "interval": "1d",
  1254. "partial": 1,
  1255. "query": query,
  1256. "referrer": "api.dashboards.widget.bar-chart",
  1257. "project": self.project.id,
  1258. "topEvents": 2,
  1259. "useOnDemandMetrics": "true",
  1260. "yAxis": agg,
  1261. },
  1262. )
  1263. assert response.status_code == 200, response.content
  1264. for datum in response.data.values():
  1265. assert datum["meta"] == {
  1266. "dataset": "metricsEnhanced",
  1267. "datasetReason": "unchanged",
  1268. "fields": {},
  1269. "isMetricsData": False,
  1270. "isMetricsExtractedData": True,
  1271. "tips": {},
  1272. "units": {},
  1273. }
  1274. def _test_is_metrics_extracted_data(
  1275. self, params: dict[str, Any], expected_on_demand_query: bool, dataset: str
  1276. ) -> None:
  1277. features = {"organizations:on-demand-metrics-extraction": True}
  1278. spec = OnDemandMetricSpec(
  1279. field="count()",
  1280. query="transaction.duration:>1s",
  1281. spec_type=MetricSpecType.DYNAMIC_QUERY,
  1282. )
  1283. self.store_on_demand_metric(1, spec=spec)
  1284. response = self.do_request(params, features=features)
  1285. assert response.status_code == 200, response.content
  1286. meta = response.data["meta"]
  1287. # This is the main thing we want to test for
  1288. assert meta.get("isMetricsExtractedData", False) is expected_on_demand_query
  1289. assert meta["dataset"] == dataset
  1290. return meta
  1291. def test_is_metrics_extracted_data_is_included(self):
  1292. self._test_is_metrics_extracted_data(
  1293. {
  1294. "dataset": "metricsEnhanced",
  1295. "query": "transaction.duration:>=91",
  1296. "useOnDemandMetrics": "true",
  1297. "yAxis": "count()",
  1298. },
  1299. expected_on_demand_query=True,
  1300. dataset="metricsEnhanced",
  1301. )
  1302. def test_group_by_transaction(self):
  1303. field = "count()"
  1304. groupbys = ["transaction"]
  1305. query = "transaction.duration:>=100"
  1306. spec = OnDemandMetricSpec(
  1307. field=field,
  1308. groupbys=groupbys,
  1309. query=query,
  1310. spec_type=MetricSpecType.DYNAMIC_QUERY,
  1311. )
  1312. for hour in range(0, 2):
  1313. self.store_on_demand_metric(
  1314. (hour + 1) * 5,
  1315. spec=spec,
  1316. additional_tags={
  1317. "transaction": "/performance",
  1318. "environment": "production",
  1319. },
  1320. timestamp=self.day_ago + timedelta(hours=hour),
  1321. )
  1322. response = self.do_request(
  1323. data={
  1324. "dataset": "metricsEnhanced",
  1325. "environment": "production",
  1326. "excludeOther": 1,
  1327. "field": [field, "transaction"],
  1328. "start": iso_format(self.day_ago),
  1329. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1330. "interval": "1h",
  1331. "orderby": f"-{field}",
  1332. "partial": 1,
  1333. "project": self.project.id,
  1334. "query": query,
  1335. "topEvents": 5,
  1336. "yAxis": field,
  1337. "onDemandType": "dynamic_query",
  1338. "useOnDemandMetrics": "true",
  1339. },
  1340. )
  1341. assert response.status_code == 200, response.content
  1342. assert response.data["/performance"]["meta"]["isMetricsExtractedData"] is True
  1343. assert [attrs for time, attrs in response.data["/performance"]["data"]] == [
  1344. [{"count": 5.0}],
  1345. [{"count": 10.0}],
  1346. ]