test_metrics_layer.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. from datetime import datetime, timedelta, timezone
  2. from typing import Literal, Mapping
  3. import pytest
  4. from snuba_sdk import (
  5. Column,
  6. Condition,
  7. Direction,
  8. Metric,
  9. MetricsQuery,
  10. MetricsScope,
  11. Op,
  12. Request,
  13. Rollup,
  14. Timeseries,
  15. )
  16. from sentry.api.utils import InvalidParams
  17. from sentry.sentry_metrics.use_case_id_registry import UseCaseID
  18. from sentry.snuba.metrics.naming_layer import SessionMRI, TransactionMRI
  19. from sentry.snuba.metrics_layer.query import run_query
  20. from sentry.testutils.cases import BaseMetricsTestCase, TestCase
  21. pytestmark = pytest.mark.sentry_metrics
  22. class SnQLTest(TestCase, BaseMetricsTestCase):
  23. def ts(self, dt: datetime) -> int:
  24. return int(dt.timestamp())
  25. def setUp(self) -> None:
  26. super().setUp()
  27. self.metrics: Mapping[str, Literal["counter", "set", "distribution"]] = {
  28. TransactionMRI.DURATION.value: "distribution",
  29. TransactionMRI.USER.value: "set",
  30. TransactionMRI.COUNT_PER_ROOT_PROJECT.value: "counter",
  31. }
  32. self.now = datetime.now(tz=timezone.utc).replace(microsecond=0)
  33. self.hour_ago = self.now - timedelta(hours=1)
  34. self.org_id = self.project.organization_id
  35. for mri, metric_type in self.metrics.items():
  36. assert metric_type in {"counter", "distribution", "set"}
  37. for i in range(360):
  38. self.store_metric(
  39. self.org_id,
  40. self.project.id,
  41. metric_type,
  42. mri,
  43. {
  44. "transaction": f"transaction_{i % 2}",
  45. "status_code": "500" if i % 10 == 0 else "200",
  46. "device": "BlackBerry" if i % 3 == 0 else "Nokia",
  47. },
  48. self.ts(self.hour_ago + timedelta(minutes=1 * i)),
  49. i,
  50. UseCaseID.TRANSACTIONS,
  51. )
  52. def test_basic(self) -> None:
  53. query = MetricsQuery(
  54. query=Timeseries(
  55. metric=Metric(
  56. "transaction.duration",
  57. TransactionMRI.DURATION.value,
  58. ),
  59. aggregate="max",
  60. ),
  61. start=self.hour_ago,
  62. end=self.now,
  63. rollup=Rollup(interval=60, granularity=60),
  64. scope=MetricsScope(
  65. org_ids=[self.org_id],
  66. project_ids=[self.project.id],
  67. use_case_id=UseCaseID.TRANSACTIONS.value,
  68. ),
  69. )
  70. request = Request(
  71. dataset="generic_metrics",
  72. app_id="tests",
  73. query=query,
  74. tenant_ids={"referrer": "metrics.testing.test", "organization_id": self.org_id},
  75. )
  76. result = run_query(request)
  77. assert len(result["data"]) == 61
  78. rows = result["data"]
  79. for i in range(61):
  80. assert rows[i]["aggregate_value"] == i
  81. assert (
  82. rows[i]["time"]
  83. == (
  84. self.hour_ago.replace(second=0, microsecond=0) + timedelta(minutes=1 * i)
  85. ).isoformat()
  86. )
  87. def test_groupby(self) -> None:
  88. query = MetricsQuery(
  89. query=Timeseries(
  90. metric=Metric(
  91. "transaction.duration",
  92. TransactionMRI.DURATION.value,
  93. ),
  94. aggregate="quantiles",
  95. aggregate_params=[0.5, 0.99],
  96. groupby=[Column("transaction")],
  97. ),
  98. start=self.hour_ago,
  99. end=self.now,
  100. rollup=Rollup(interval=60, granularity=60),
  101. scope=MetricsScope(
  102. org_ids=[self.org_id],
  103. project_ids=[self.project.id],
  104. use_case_id=UseCaseID.TRANSACTIONS.value,
  105. ),
  106. )
  107. request = Request(
  108. dataset="generic_metrics",
  109. app_id="tests",
  110. query=query,
  111. tenant_ids={"referrer": "metrics.testing.test", "organization_id": self.org_id},
  112. )
  113. result = run_query(request)
  114. assert len(result["data"]) == 61
  115. rows = result["data"]
  116. for i in range(61):
  117. assert rows[i]["aggregate_value"] == [i, i]
  118. assert rows[i]["transaction"] == f"transaction_{i % 2}"
  119. assert (
  120. rows[i]["time"]
  121. == (
  122. self.hour_ago.replace(second=0, microsecond=0) + timedelta(minutes=1 * i)
  123. ).isoformat()
  124. )
  125. def test_filters(self) -> None:
  126. query = MetricsQuery(
  127. query=Timeseries(
  128. metric=Metric(
  129. "transaction.duration",
  130. TransactionMRI.DURATION.value,
  131. ),
  132. aggregate="quantiles",
  133. aggregate_params=[0.5],
  134. filters=[Condition(Column("status_code"), Op.EQ, "500")],
  135. ),
  136. filters=[Condition(Column("device"), Op.EQ, "BlackBerry")],
  137. start=self.hour_ago,
  138. end=self.now,
  139. rollup=Rollup(interval=60, granularity=60),
  140. scope=MetricsScope(
  141. org_ids=[self.org_id],
  142. project_ids=[self.project.id],
  143. use_case_id=UseCaseID.TRANSACTIONS.value,
  144. ),
  145. )
  146. request = Request(
  147. dataset="generic_metrics",
  148. app_id="tests",
  149. query=query,
  150. tenant_ids={"referrer": "metrics.testing.test", "organization_id": self.org_id},
  151. )
  152. result = run_query(request)
  153. assert len(result["data"]) == 3
  154. rows = result["data"]
  155. for i in range(3): # 500 status codes on Blackberry are sparse
  156. assert rows[i]["aggregate_value"] == [i * 30]
  157. assert (
  158. rows[i]["time"]
  159. == (
  160. self.hour_ago.replace(second=0, microsecond=0) + timedelta(minutes=30 * i)
  161. ).isoformat()
  162. )
  163. def test_complex(self) -> None:
  164. query = MetricsQuery(
  165. query=Timeseries(
  166. metric=Metric(
  167. "transaction.duration",
  168. TransactionMRI.DURATION.value,
  169. ),
  170. aggregate="quantiles",
  171. aggregate_params=[0.5],
  172. filters=[Condition(Column("status_code"), Op.EQ, "500")],
  173. groupby=[Column("transaction")],
  174. ),
  175. filters=[Condition(Column("device"), Op.EQ, "BlackBerry")],
  176. start=self.hour_ago,
  177. end=self.now,
  178. rollup=Rollup(interval=60, granularity=60),
  179. scope=MetricsScope(
  180. org_ids=[self.org_id],
  181. project_ids=[self.project.id],
  182. use_case_id=UseCaseID.TRANSACTIONS.value,
  183. ),
  184. )
  185. request = Request(
  186. dataset="generic_metrics",
  187. app_id="tests",
  188. query=query,
  189. tenant_ids={"referrer": "metrics.testing.test", "organization_id": self.org_id},
  190. )
  191. result = run_query(request)
  192. assert len(result["data"]) == 3
  193. rows = result["data"]
  194. for i in range(3): # 500 status codes on BB are sparse
  195. assert rows[i]["aggregate_value"] == [i * 30]
  196. assert rows[i]["transaction"] == "transaction_0"
  197. assert (
  198. rows[i]["time"]
  199. == (
  200. self.hour_ago.replace(second=0, microsecond=0) + timedelta(minutes=30 * i)
  201. ).isoformat()
  202. )
  203. def test_totals(self) -> None:
  204. query = MetricsQuery(
  205. query=Timeseries(
  206. metric=Metric(
  207. "transaction.duration",
  208. TransactionMRI.DURATION.value,
  209. ),
  210. aggregate="max",
  211. filters=[Condition(Column("status_code"), Op.EQ, "200")],
  212. groupby=[Column("transaction")],
  213. ),
  214. start=self.hour_ago,
  215. end=self.now,
  216. rollup=Rollup(totals=True, granularity=60, orderby=Direction.ASC),
  217. scope=MetricsScope(
  218. org_ids=[self.org_id],
  219. project_ids=[self.project.id],
  220. use_case_id=UseCaseID.TRANSACTIONS.value,
  221. ),
  222. )
  223. request = Request(
  224. dataset="generic_metrics",
  225. app_id="tests",
  226. query=query,
  227. tenant_ids={"referrer": "metrics.testing.test", "organization_id": self.org_id},
  228. )
  229. result = run_query(request)
  230. assert len(result["data"]) == 2
  231. rows = result["data"]
  232. assert rows[0]["aggregate_value"] == 58
  233. assert rows[0]["transaction"] == "transaction_0"
  234. assert rows[1]["aggregate_value"] == 59
  235. assert rows[1]["transaction"] == "transaction_1"
  236. def test_meta_data_in_response(self) -> None:
  237. query = MetricsQuery(
  238. query=Timeseries(
  239. metric=Metric(
  240. "transaction.duration",
  241. TransactionMRI.DURATION.value,
  242. ),
  243. aggregate="max",
  244. filters=[Condition(Column("status_code"), Op.EQ, "200")],
  245. groupby=[Column("transaction")],
  246. ),
  247. start=self.hour_ago.replace(minute=16, second=59),
  248. end=self.now.replace(minute=16, second=59),
  249. rollup=Rollup(interval=60, granularity=60),
  250. scope=MetricsScope(
  251. org_ids=[self.org_id],
  252. project_ids=[self.project.id],
  253. use_case_id=UseCaseID.TRANSACTIONS.value,
  254. ),
  255. )
  256. request = Request(
  257. dataset="generic_metrics",
  258. app_id="tests",
  259. query=query,
  260. tenant_ids={"referrer": "metrics.testing.test", "organization_id": self.org_id},
  261. )
  262. result = run_query(request)
  263. assert result["modified_start"] == self.hour_ago.replace(minute=16, second=0)
  264. assert result["modified_end"] == self.now.replace(minute=17, second=0)
  265. assert result["indexer_mappings"] == {
  266. "d:transactions/duration@millisecond": 9223372036854775909,
  267. "status_code": 10000,
  268. "transaction": 9223372036854776020,
  269. }
  270. def test_bad_query(self) -> None:
  271. query = MetricsQuery(
  272. query=Timeseries(
  273. metric=Metric(
  274. "transaction.duration",
  275. "not a real MRI",
  276. ),
  277. aggregate="max",
  278. ),
  279. start=self.hour_ago.replace(minute=16, second=59),
  280. end=self.now.replace(minute=16, second=59),
  281. rollup=Rollup(interval=60, granularity=60),
  282. scope=MetricsScope(
  283. org_ids=[self.org_id],
  284. project_ids=[self.project.id],
  285. use_case_id=UseCaseID.TRANSACTIONS.value,
  286. ),
  287. )
  288. request = Request(
  289. dataset="generic_metrics",
  290. app_id="tests",
  291. query=query,
  292. tenant_ids={"referrer": "metrics.testing.test", "organization_id": self.org_id},
  293. )
  294. with pytest.raises(InvalidParams):
  295. run_query(request)
  296. def test_interval_with_totals(self) -> None:
  297. query = MetricsQuery(
  298. query=Timeseries(
  299. metric=Metric(
  300. "transaction.duration",
  301. TransactionMRI.DURATION.value,
  302. ),
  303. aggregate="max",
  304. filters=[Condition(Column("status_code"), Op.EQ, "200")],
  305. groupby=[Column("transaction")],
  306. ),
  307. start=self.hour_ago,
  308. end=self.now,
  309. rollup=Rollup(interval=60, totals=True, granularity=60),
  310. scope=MetricsScope(
  311. org_ids=[self.org_id],
  312. project_ids=[self.project.id],
  313. use_case_id=UseCaseID.TRANSACTIONS.value,
  314. ),
  315. )
  316. request = Request(
  317. dataset="generic_metrics",
  318. app_id="tests",
  319. query=query,
  320. tenant_ids={"referrer": "metrics.testing.test", "organization_id": self.org_id},
  321. )
  322. result = run_query(request)
  323. assert len(result["data"]) == 54
  324. assert result["totals"]["aggregate_value"] == 59
  325. def test_automatic_granularity(self) -> None:
  326. query = MetricsQuery(
  327. query=Timeseries(
  328. metric=Metric(
  329. "transaction.duration",
  330. TransactionMRI.DURATION.value,
  331. ),
  332. aggregate="max",
  333. ),
  334. start=self.hour_ago,
  335. end=self.now,
  336. rollup=Rollup(interval=120),
  337. scope=MetricsScope(
  338. org_ids=[self.org_id],
  339. project_ids=[self.project.id],
  340. ),
  341. )
  342. request = Request(
  343. dataset="generic_metrics",
  344. app_id="tests",
  345. query=query,
  346. tenant_ids={"referrer": "metrics.testing.test", "organization_id": self.org_id},
  347. )
  348. result = run_query(request)
  349. # 30 since it's every 2 minutes.
  350. # # TODO(evanh): There's a flaky off by one error that comes from the interval rounding I don't care to fix right now, hence the 31 option.
  351. assert len(result["data"]) in [30, 31]
  352. def test_automatic_dataset(self) -> None:
  353. query = MetricsQuery(
  354. query=Timeseries(
  355. metric=Metric(
  356. None,
  357. SessionMRI.RAW_DURATION.value,
  358. ),
  359. aggregate="max",
  360. ),
  361. start=self.hour_ago,
  362. end=self.now,
  363. rollup=Rollup(interval=60, granularity=60),
  364. scope=MetricsScope(
  365. org_ids=[self.org_id],
  366. project_ids=[self.project.id],
  367. use_case_id=UseCaseID.SESSIONS.value,
  368. ),
  369. )
  370. request = Request(
  371. dataset="generic_metrics",
  372. app_id="tests",
  373. query=query,
  374. tenant_ids={"referrer": "metrics.testing.test", "organization_id": self.org_id},
  375. )
  376. result = run_query(request)
  377. assert request.dataset == "metrics"
  378. assert len(result["data"]) == 0