test_batch.py 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  1. import logging
  2. from collections.abc import MutableMapping
  3. from datetime import datetime, timezone
  4. from enum import Enum
  5. from typing import Any
  6. from unittest.mock import patch
  7. import pytest
  8. import sentry_kafka_schemas
  9. from arroyo.backends.kafka import KafkaPayload
  10. from arroyo.types import BrokerValue, Message, Partition, Topic, Value
  11. from sentry.sentry_metrics.consumers.indexer.batch import IndexerBatch, PartitionIdxOffset
  12. from sentry.sentry_metrics.indexer.base import FetchType, FetchTypeExt, Metadata
  13. from sentry.snuba.metrics.naming_layer.mri import SessionMRI, TransactionMRI
  14. from sentry.utils import json
  15. class MockUseCaseID(Enum):
  16. TRANSACTIONS = "transactions"
  17. SESSIONS = "sessions"
  18. USE_CASE_1 = "use_case_1"
  19. USE_CASE_2 = "use_case_2"
  20. pytestmark = pytest.mark.sentry_metrics
  21. BROKER_TIMESTAMP = datetime.now(tz=timezone.utc)
  22. ts = int(datetime.now(tz=timezone.utc).timestamp())
  23. counter_payload = {
  24. "name": SessionMRI.SESSION.value,
  25. "tags": {
  26. "environment": "production",
  27. "session.status": "init",
  28. },
  29. "timestamp": ts,
  30. "type": "c",
  31. "value": 1,
  32. "org_id": 1,
  33. "retention_days": 90,
  34. "project_id": 3,
  35. }
  36. distribution_payload = {
  37. "name": SessionMRI.RAW_DURATION.value,
  38. "tags": {
  39. "environment": "production",
  40. "session.status": "healthy",
  41. },
  42. "timestamp": ts,
  43. "type": "d",
  44. "value": [4, 5, 6],
  45. "org_id": 1,
  46. "retention_days": 90,
  47. "project_id": 3,
  48. }
  49. set_payload = {
  50. "name": SessionMRI.ERROR.value,
  51. "tags": {
  52. "environment": "production",
  53. "session.status": "errored",
  54. },
  55. "timestamp": ts,
  56. "type": "s",
  57. "value": [3],
  58. "org_id": 1,
  59. "retention_days": 90,
  60. "project_id": 3,
  61. }
  62. extracted_string_output = {
  63. MockUseCaseID.SESSIONS: {
  64. 1: {
  65. "c:sessions/session@none",
  66. "d:sessions/duration@second",
  67. "environment",
  68. "errored",
  69. "healthy",
  70. "init",
  71. "production",
  72. "s:sessions/error@none",
  73. "session.status",
  74. }
  75. }
  76. }
  77. _INGEST_CODEC: sentry_kafka_schemas.codecs.Codec[Any] = sentry_kafka_schemas.get_codec(
  78. "ingest-metrics"
  79. )
  80. def _construct_messages(payloads):
  81. message_batch = []
  82. for i, (payload, headers) in enumerate(payloads):
  83. message_batch.append(
  84. Message(
  85. BrokerValue(
  86. KafkaPayload(None, json.dumps(payload).encode("utf-8"), headers or []),
  87. Partition(Topic("topic"), 0),
  88. i,
  89. BROKER_TIMESTAMP,
  90. )
  91. )
  92. )
  93. return message_batch
  94. def _construct_outer_message(payloads):
  95. message_batch = _construct_messages(payloads)
  96. # the outer message uses the last message's partition, offset, and timestamp
  97. last = message_batch[-1]
  98. outer_message = Message(Value(message_batch, last.committable))
  99. return outer_message
  100. def _deconstruct_messages(snuba_messages, kafka_logical_topic="snuba-metrics"):
  101. """
  102. Convert a list of messages returned by `reconstruct_messages` into python
  103. primitives, to run assertions on:
  104. assert _deconstruct_messages(batch.reconstruct_messages(...)) == [ ... ]
  105. This is slightly nicer to work with than:
  106. assert batch.reconstruct_messages(...) == _construct_messages([ ... ])
  107. ...because pytest's assertion diffs work better with python primitives.
  108. """
  109. rv = []
  110. codec = sentry_kafka_schemas.get_codec(kafka_logical_topic)
  111. for msg in snuba_messages:
  112. decoded = codec.decode(msg.payload.value, validate=True)
  113. rv.append((decoded, msg.payload.headers))
  114. return rv
  115. def _deconstruct_routing_messages(snuba_messages):
  116. """
  117. Similar to `_deconstruct_messages`, but for routing messages.
  118. """
  119. all_messages = []
  120. for msg in snuba_messages:
  121. headers: MutableMapping[str, str] = {}
  122. for key, value in msg.payload.routing_header.items():
  123. headers.update({key: value})
  124. payload = json.loads(msg.payload.routing_message.value.decode("utf-8"))
  125. all_messages.append((headers, payload, msg.payload.routing_message.headers))
  126. return all_messages
  127. def _get_string_indexer_log_records(caplog):
  128. """
  129. Get all log records and relevant extra arguments for easy snapshotting.
  130. """
  131. return [
  132. (
  133. rec.message,
  134. {
  135. k: v
  136. for k, v in rec.__dict__.items()
  137. if k
  138. in (
  139. "string_type",
  140. "is_global_quota",
  141. "num_global_quotas",
  142. "num_global_quotas",
  143. "org_batch_size",
  144. )
  145. },
  146. )
  147. for rec in caplog.records
  148. ]
  149. @patch("sentry.sentry_metrics.consumers.indexer.batch.UseCaseID", MockUseCaseID)
  150. @pytest.mark.parametrize(
  151. "should_index_tag_values, expected",
  152. [
  153. pytest.param(
  154. True,
  155. {
  156. MockUseCaseID.SESSIONS: {
  157. 1: {
  158. "c:sessions/session@none",
  159. "d:sessions/duration@second",
  160. "environment",
  161. "errored",
  162. "healthy",
  163. "init",
  164. "production",
  165. "s:sessions/error@none",
  166. "session.status",
  167. },
  168. }
  169. },
  170. id="index tag values true",
  171. ),
  172. pytest.param(
  173. False,
  174. {
  175. MockUseCaseID.SESSIONS: {
  176. 1: {
  177. "c:sessions/session@none",
  178. "d:sessions/duration@second",
  179. "environment",
  180. "s:sessions/error@none",
  181. "session.status",
  182. },
  183. }
  184. },
  185. id="index tag values false",
  186. ),
  187. ],
  188. )
  189. def test_extract_strings_with_rollout(should_index_tag_values, expected):
  190. """
  191. Test that the indexer batch extracts the correct strings from the messages
  192. based on whether tag values should be indexed or not.
  193. """
  194. outer_message = _construct_outer_message(
  195. [
  196. (counter_payload, []),
  197. (distribution_payload, []),
  198. (set_payload, []),
  199. ]
  200. )
  201. batch = IndexerBatch(
  202. outer_message,
  203. should_index_tag_values,
  204. False,
  205. input_codec=_INGEST_CODEC,
  206. )
  207. assert batch.extract_strings() == expected
  208. @patch("sentry.sentry_metrics.consumers.indexer.batch.UseCaseID", MockUseCaseID)
  209. def test_extract_strings_with_multiple_use_case_ids():
  210. """
  211. Verify that the extract string method can handle payloads that has multiple
  212. (generic) uses cases
  213. """
  214. counter_payload = {
  215. "name": "c:use_case_1/session@none",
  216. "tags": {
  217. "environment": "production",
  218. "session.status": "init",
  219. },
  220. "timestamp": ts,
  221. "type": "c",
  222. "value": 1,
  223. "org_id": 1,
  224. "retention_days": 90,
  225. "project_id": 3,
  226. }
  227. distribution_payload = {
  228. "name": "d:use_case_2/duration@second",
  229. "tags": {
  230. "environment": "production",
  231. "session.status": "healthy",
  232. },
  233. "timestamp": ts,
  234. "type": "d",
  235. "value": [4, 5, 6],
  236. "org_id": 1,
  237. "retention_days": 90,
  238. "project_id": 3,
  239. }
  240. set_payload = {
  241. "name": "s:use_case_2/error@none",
  242. "tags": {
  243. "environment": "production",
  244. "session.status": "errored",
  245. },
  246. "timestamp": ts,
  247. "type": "s",
  248. "value": [3],
  249. "org_id": 1,
  250. "retention_days": 90,
  251. "project_id": 3,
  252. }
  253. outer_message = _construct_outer_message(
  254. [
  255. (counter_payload, []),
  256. (distribution_payload, []),
  257. (set_payload, []),
  258. ]
  259. )
  260. batch = IndexerBatch(
  261. outer_message,
  262. True,
  263. False,
  264. input_codec=_INGEST_CODEC,
  265. )
  266. assert batch.extract_strings() == {
  267. MockUseCaseID.USE_CASE_1: {
  268. 1: {
  269. "c:use_case_1/session@none",
  270. "environment",
  271. "production",
  272. "session.status",
  273. "init",
  274. }
  275. },
  276. MockUseCaseID.USE_CASE_2: {
  277. 1: {
  278. "d:use_case_2/duration@second",
  279. "environment",
  280. "production",
  281. "session.status",
  282. "healthy",
  283. "s:use_case_2/error@none",
  284. "environment",
  285. "production",
  286. "session.status",
  287. "errored",
  288. }
  289. },
  290. }
  291. @patch("sentry.sentry_metrics.consumers.indexer.batch.UseCaseID", MockUseCaseID)
  292. def test_extract_strings_with_invalid_mri():
  293. """
  294. Verify that extract strings will drop payload that has invalid MRI in name field but continue processing the rest
  295. """
  296. bad_counter_payload = {
  297. "name": "invalid_MRI",
  298. "tags": {
  299. "environment": "production",
  300. "session.status": "init",
  301. },
  302. "timestamp": ts,
  303. "type": "c",
  304. "value": 1,
  305. "org_id": 100,
  306. "retention_days": 90,
  307. "project_id": 3,
  308. }
  309. counter_payload = {
  310. "name": "c:use_case_1/session@none",
  311. "tags": {
  312. "environment": "production",
  313. "session.status": "init",
  314. },
  315. "timestamp": ts,
  316. "type": "c",
  317. "value": 1,
  318. "org_id": 1,
  319. "retention_days": 90,
  320. "project_id": 3,
  321. }
  322. distribution_payload = {
  323. "name": "d:use_case_2/duration@second",
  324. "tags": {
  325. "environment": "production",
  326. "session.status": "healthy",
  327. },
  328. "timestamp": ts,
  329. "type": "d",
  330. "value": [4, 5, 6],
  331. "org_id": 1,
  332. "retention_days": 90,
  333. "project_id": 3,
  334. }
  335. set_payload = {
  336. "name": "s:use_case_2/error@none",
  337. "tags": {
  338. "environment": "production",
  339. "session.status": "errored",
  340. },
  341. "timestamp": ts,
  342. "type": "s",
  343. "value": [3],
  344. "org_id": 1,
  345. "retention_days": 90,
  346. "project_id": 3,
  347. }
  348. outer_message = _construct_outer_message(
  349. [
  350. (bad_counter_payload, []),
  351. (counter_payload, []),
  352. (distribution_payload, []),
  353. (set_payload, []),
  354. ]
  355. )
  356. batch = IndexerBatch(
  357. outer_message,
  358. True,
  359. False,
  360. input_codec=_INGEST_CODEC,
  361. )
  362. assert batch.extract_strings() == {
  363. MockUseCaseID.USE_CASE_1: {
  364. 1: {
  365. "c:use_case_1/session@none",
  366. "environment",
  367. "production",
  368. "session.status",
  369. "init",
  370. }
  371. },
  372. MockUseCaseID.USE_CASE_2: {
  373. 1: {
  374. "d:use_case_2/duration@second",
  375. "environment",
  376. "production",
  377. "session.status",
  378. "healthy",
  379. "s:use_case_2/error@none",
  380. "environment",
  381. "production",
  382. "session.status",
  383. "errored",
  384. }
  385. },
  386. }
  387. @patch("sentry.sentry_metrics.consumers.indexer.batch.UseCaseID", MockUseCaseID)
  388. def test_extract_strings_with_multiple_use_case_ids_and_org_ids():
  389. """
  390. Verify that the extract string method can handle payloads that has multiple
  391. (generic) uses cases and from different orgs
  392. """
  393. custom_uc_counter_payload = {
  394. "name": "c:use_case_1/session@none",
  395. "tags": {
  396. "environment": "production",
  397. "session.status": "init",
  398. },
  399. "timestamp": ts,
  400. "type": "c",
  401. "value": 1,
  402. "org_id": 1,
  403. "retention_days": 90,
  404. "project_id": 3,
  405. }
  406. perf_distribution_payload = {
  407. "name": TransactionMRI.MEASUREMENTS_FCP.value,
  408. "tags": {
  409. "environment": "production",
  410. "session.status": "healthy",
  411. },
  412. "timestamp": ts,
  413. "type": "d",
  414. "value": [4, 5, 6],
  415. "org_id": 1,
  416. "retention_days": 90,
  417. "project_id": 3,
  418. }
  419. custom_uc_set_payload = {
  420. "name": "s:use_case_1/error@none",
  421. "tags": {
  422. "environment": "production",
  423. "session.status": "errored",
  424. },
  425. "timestamp": ts,
  426. "type": "s",
  427. "value": [3],
  428. "org_id": 2,
  429. "retention_days": 90,
  430. "project_id": 3,
  431. }
  432. outer_message = _construct_outer_message(
  433. [
  434. (custom_uc_counter_payload, []),
  435. (perf_distribution_payload, []),
  436. (custom_uc_set_payload, []),
  437. ]
  438. )
  439. batch = IndexerBatch(
  440. outer_message,
  441. True,
  442. False,
  443. input_codec=_INGEST_CODEC,
  444. )
  445. assert batch.extract_strings() == {
  446. MockUseCaseID.USE_CASE_1: {
  447. 1: {
  448. "c:use_case_1/session@none",
  449. "environment",
  450. "production",
  451. "session.status",
  452. "init",
  453. },
  454. 2: {
  455. "s:use_case_1/error@none",
  456. "environment",
  457. "production",
  458. "session.status",
  459. "errored",
  460. },
  461. },
  462. MockUseCaseID.TRANSACTIONS: {
  463. 1: {
  464. TransactionMRI.MEASUREMENTS_FCP.value,
  465. "environment",
  466. "production",
  467. "session.status",
  468. "healthy",
  469. }
  470. },
  471. }
  472. @patch("sentry.sentry_metrics.consumers.indexer.batch.UseCaseID", MockUseCaseID)
  473. def test_all_resolved(caplog, settings):
  474. settings.SENTRY_METRICS_INDEXER_DEBUG_LOG_SAMPLE_RATE = 1.0
  475. outer_message = _construct_outer_message(
  476. [
  477. (counter_payload, []),
  478. (distribution_payload, []),
  479. (set_payload, []),
  480. ]
  481. )
  482. batch = IndexerBatch(
  483. outer_message,
  484. True,
  485. False,
  486. input_codec=_INGEST_CODEC,
  487. )
  488. assert batch.extract_strings() == (
  489. {
  490. MockUseCaseID.SESSIONS: {
  491. 1: {
  492. "c:sessions/session@none",
  493. "d:sessions/duration@second",
  494. "environment",
  495. "errored",
  496. "healthy",
  497. "init",
  498. "production",
  499. "s:sessions/error@none",
  500. "session.status",
  501. }
  502. }
  503. }
  504. )
  505. caplog.set_level(logging.ERROR)
  506. snuba_payloads = batch.reconstruct_messages(
  507. {
  508. 1: {
  509. "c:sessions/session@none": 1,
  510. "d:sessions/duration@second": 2,
  511. "environment": 3,
  512. "errored": 4,
  513. "healthy": 5,
  514. "init": 6,
  515. "production": 7,
  516. "s:sessions/error@none": 8,
  517. "session.status": 9,
  518. }
  519. },
  520. {
  521. 1: {
  522. "c:sessions/session@none": Metadata(id=1, fetch_type=FetchType.CACHE_HIT),
  523. "d:sessions/duration@second": Metadata(id=2, fetch_type=FetchType.CACHE_HIT),
  524. "environment": Metadata(id=3, fetch_type=FetchType.CACHE_HIT),
  525. "errored": Metadata(id=4, fetch_type=FetchType.DB_READ),
  526. "healthy": Metadata(id=5, fetch_type=FetchType.HARDCODED),
  527. "init": Metadata(id=6, fetch_type=FetchType.HARDCODED),
  528. "production": Metadata(id=7, fetch_type=FetchType.CACHE_HIT),
  529. "s:sessions/error@none": Metadata(id=8, fetch_type=FetchType.CACHE_HIT),
  530. "session.status": Metadata(id=9, fetch_type=FetchType.CACHE_HIT),
  531. }
  532. },
  533. )
  534. assert _get_string_indexer_log_records(caplog) == []
  535. assert _deconstruct_messages(snuba_payloads) == [
  536. (
  537. {
  538. "mapping_meta": {
  539. "c": {
  540. "1": "c:sessions/session@none",
  541. "3": "environment",
  542. "7": "production",
  543. "9": "session.status",
  544. },
  545. "h": {"6": "init"},
  546. },
  547. "metric_id": 1,
  548. "org_id": 1,
  549. "project_id": 3,
  550. "retention_days": 90,
  551. "tags": {"3": 7, "9": 6},
  552. "timestamp": ts,
  553. "type": "c",
  554. "use_case_id": "sessions",
  555. "value": 1.0,
  556. "sentry_received_timestamp": BROKER_TIMESTAMP.timestamp(),
  557. },
  558. [("mapping_sources", b"ch"), ("metric_type", "c")],
  559. ),
  560. (
  561. {
  562. "mapping_meta": {
  563. "c": {
  564. "2": "d:sessions/duration@second",
  565. "3": "environment",
  566. "7": "production",
  567. "9": "session.status",
  568. },
  569. "h": {"5": "healthy"},
  570. },
  571. "metric_id": 2,
  572. "org_id": 1,
  573. "project_id": 3,
  574. "retention_days": 90,
  575. "tags": {"3": 7, "9": 5},
  576. "timestamp": ts,
  577. "type": "d",
  578. "use_case_id": "sessions",
  579. "value": [4, 5, 6],
  580. "sentry_received_timestamp": BROKER_TIMESTAMP.timestamp(),
  581. },
  582. [("mapping_sources", b"ch"), ("metric_type", "d")],
  583. ),
  584. (
  585. {
  586. "mapping_meta": {
  587. "c": {
  588. "3": "environment",
  589. "7": "production",
  590. "8": "s:sessions/error@none",
  591. "9": "session.status",
  592. },
  593. "d": {"4": "errored"},
  594. },
  595. "metric_id": 8,
  596. "org_id": 1,
  597. "project_id": 3,
  598. "retention_days": 90,
  599. "tags": {"3": 7, "9": 4},
  600. "timestamp": ts,
  601. "type": "s",
  602. "use_case_id": "sessions",
  603. "value": [3],
  604. "sentry_received_timestamp": BROKER_TIMESTAMP.timestamp(),
  605. },
  606. [("mapping_sources", b"cd"), ("metric_type", "s")],
  607. ),
  608. ]
  609. @patch("sentry.sentry_metrics.consumers.indexer.batch.UseCaseID", MockUseCaseID)
  610. def test_all_resolved_with_routing_information(caplog, settings):
  611. settings.SENTRY_METRICS_INDEXER_DEBUG_LOG_SAMPLE_RATE = 1.0
  612. outer_message = _construct_outer_message(
  613. [
  614. (counter_payload, []),
  615. (distribution_payload, []),
  616. (set_payload, []),
  617. ]
  618. )
  619. batch = IndexerBatch(
  620. outer_message,
  621. True,
  622. True,
  623. input_codec=_INGEST_CODEC,
  624. )
  625. assert batch.extract_strings() == (
  626. {
  627. MockUseCaseID.SESSIONS: {
  628. 1: {
  629. "c:sessions/session@none",
  630. "d:sessions/duration@second",
  631. "environment",
  632. "errored",
  633. "healthy",
  634. "init",
  635. "production",
  636. "s:sessions/error@none",
  637. "session.status",
  638. }
  639. }
  640. }
  641. )
  642. caplog.set_level(logging.ERROR)
  643. snuba_payloads = batch.reconstruct_messages(
  644. {
  645. 1: {
  646. "c:sessions/session@none": 1,
  647. "d:sessions/duration@second": 2,
  648. "environment": 3,
  649. "errored": 4,
  650. "healthy": 5,
  651. "init": 6,
  652. "production": 7,
  653. "s:sessions/error@none": 8,
  654. "session.status": 9,
  655. }
  656. },
  657. {
  658. 1: {
  659. "c:sessions/session@none": Metadata(id=1, fetch_type=FetchType.CACHE_HIT),
  660. "d:sessions/duration@second": Metadata(id=2, fetch_type=FetchType.CACHE_HIT),
  661. "environment": Metadata(id=3, fetch_type=FetchType.CACHE_HIT),
  662. "errored": Metadata(id=4, fetch_type=FetchType.DB_READ),
  663. "healthy": Metadata(id=5, fetch_type=FetchType.HARDCODED),
  664. "init": Metadata(id=6, fetch_type=FetchType.HARDCODED),
  665. "production": Metadata(id=7, fetch_type=FetchType.CACHE_HIT),
  666. "s:sessions/error@none": Metadata(id=8, fetch_type=FetchType.CACHE_HIT),
  667. "session.status": Metadata(id=9, fetch_type=FetchType.CACHE_HIT),
  668. }
  669. },
  670. )
  671. assert _get_string_indexer_log_records(caplog) == []
  672. assert _deconstruct_routing_messages(snuba_payloads) == [
  673. (
  674. {"org_id": 1},
  675. {
  676. "mapping_meta": {
  677. "c": {
  678. "1": "c:sessions/session@none",
  679. "3": "environment",
  680. "7": "production",
  681. "9": "session.status",
  682. },
  683. "h": {"6": "init"},
  684. },
  685. "metric_id": 1,
  686. "org_id": 1,
  687. "project_id": 3,
  688. "retention_days": 90,
  689. "tags": {"3": 7, "9": 6},
  690. "timestamp": ts,
  691. "type": "c",
  692. "use_case_id": "sessions",
  693. "value": 1.0,
  694. "sentry_received_timestamp": BROKER_TIMESTAMP.timestamp(),
  695. },
  696. [("mapping_sources", b"ch"), ("metric_type", "c")],
  697. ),
  698. (
  699. {"org_id": 1},
  700. {
  701. "mapping_meta": {
  702. "c": {
  703. "2": "d:sessions/duration@second",
  704. "3": "environment",
  705. "7": "production",
  706. "9": "session.status",
  707. },
  708. "h": {"5": "healthy"},
  709. },
  710. "metric_id": 2,
  711. "org_id": 1,
  712. "project_id": 3,
  713. "retention_days": 90,
  714. "tags": {"3": 7, "9": 5},
  715. "timestamp": ts,
  716. "type": "d",
  717. "use_case_id": "sessions",
  718. "value": [4, 5, 6],
  719. "sentry_received_timestamp": BROKER_TIMESTAMP.timestamp(),
  720. },
  721. [
  722. ("mapping_sources", b"ch"),
  723. ("metric_type", "d"),
  724. ],
  725. ),
  726. (
  727. {"org_id": 1},
  728. {
  729. "mapping_meta": {
  730. "c": {
  731. "3": "environment",
  732. "7": "production",
  733. "8": "s:sessions/error@none",
  734. "9": "session.status",
  735. },
  736. "d": {"4": "errored"},
  737. },
  738. "metric_id": 8,
  739. "org_id": 1,
  740. "project_id": 3,
  741. "retention_days": 90,
  742. "tags": {"3": 7, "9": 4},
  743. "timestamp": ts,
  744. "type": "s",
  745. "use_case_id": "sessions",
  746. "value": [3],
  747. "sentry_received_timestamp": BROKER_TIMESTAMP.timestamp(),
  748. },
  749. [("mapping_sources", b"cd"), ("metric_type", "s")],
  750. ),
  751. ]
  752. @patch("sentry.sentry_metrics.consumers.indexer.batch.UseCaseID", MockUseCaseID)
  753. def test_all_resolved_retention_days_honored(caplog, settings):
  754. """
  755. Tests that the indexer batch honors the incoming retention_days values
  756. from Relay or falls back to 90.
  757. """
  758. distribution_payload_modified = distribution_payload.copy()
  759. distribution_payload_modified["retention_days"] = 30
  760. settings.SENTRY_METRICS_INDEXER_DEBUG_LOG_SAMPLE_RATE = 1.0
  761. outer_message = _construct_outer_message(
  762. [
  763. (counter_payload, []),
  764. (distribution_payload_modified, []),
  765. (set_payload, []),
  766. ]
  767. )
  768. batch = IndexerBatch(
  769. outer_message,
  770. True,
  771. False,
  772. input_codec=_INGEST_CODEC,
  773. )
  774. assert batch.extract_strings() == (
  775. {
  776. MockUseCaseID.SESSIONS: {
  777. 1: {
  778. "c:sessions/session@none",
  779. "d:sessions/duration@second",
  780. "environment",
  781. "errored",
  782. "healthy",
  783. "init",
  784. "production",
  785. "s:sessions/error@none",
  786. "session.status",
  787. }
  788. }
  789. }
  790. )
  791. caplog.set_level(logging.ERROR)
  792. snuba_payloads = batch.reconstruct_messages(
  793. {
  794. 1: {
  795. "c:sessions/session@none": 1,
  796. "d:sessions/duration@second": 2,
  797. "environment": 3,
  798. "errored": 4,
  799. "healthy": 5,
  800. "init": 6,
  801. "production": 7,
  802. "s:sessions/error@none": 8,
  803. "session.status": 9,
  804. }
  805. },
  806. {
  807. 1: {
  808. "c:sessions/session@none": Metadata(id=1, fetch_type=FetchType.CACHE_HIT),
  809. "d:sessions/duration@second": Metadata(id=2, fetch_type=FetchType.CACHE_HIT),
  810. "environment": Metadata(id=3, fetch_type=FetchType.CACHE_HIT),
  811. "errored": Metadata(id=4, fetch_type=FetchType.DB_READ),
  812. "healthy": Metadata(id=5, fetch_type=FetchType.HARDCODED),
  813. "init": Metadata(id=6, fetch_type=FetchType.HARDCODED),
  814. "production": Metadata(id=7, fetch_type=FetchType.CACHE_HIT),
  815. "s:sessions/error@none": Metadata(id=8, fetch_type=FetchType.CACHE_HIT),
  816. "session.status": Metadata(id=9, fetch_type=FetchType.CACHE_HIT),
  817. }
  818. },
  819. )
  820. assert _get_string_indexer_log_records(caplog) == []
  821. assert _deconstruct_messages(snuba_payloads) == [
  822. (
  823. {
  824. "mapping_meta": {
  825. "c": {
  826. "1": "c:sessions/session@none",
  827. "3": "environment",
  828. "7": "production",
  829. "9": "session.status",
  830. },
  831. "h": {"6": "init"},
  832. },
  833. "metric_id": 1,
  834. "org_id": 1,
  835. "project_id": 3,
  836. "retention_days": 90,
  837. "tags": {"3": 7, "9": 6},
  838. "timestamp": ts,
  839. "type": "c",
  840. "use_case_id": "sessions",
  841. "value": 1.0,
  842. "sentry_received_timestamp": BROKER_TIMESTAMP.timestamp(),
  843. },
  844. [("mapping_sources", b"ch"), ("metric_type", "c")],
  845. ),
  846. (
  847. {
  848. "mapping_meta": {
  849. "c": {
  850. "2": "d:sessions/duration@second",
  851. "3": "environment",
  852. "7": "production",
  853. "9": "session.status",
  854. },
  855. "h": {"5": "healthy"},
  856. },
  857. "metric_id": 2,
  858. "org_id": 1,
  859. "project_id": 3,
  860. "retention_days": 30,
  861. "tags": {"3": 7, "9": 5},
  862. "timestamp": ts,
  863. "type": "d",
  864. "use_case_id": "sessions",
  865. "value": [4, 5, 6],
  866. "sentry_received_timestamp": BROKER_TIMESTAMP.timestamp(),
  867. },
  868. [("mapping_sources", b"ch"), ("metric_type", "d")],
  869. ),
  870. (
  871. {
  872. "mapping_meta": {
  873. "c": {
  874. "3": "environment",
  875. "7": "production",
  876. "8": "s:sessions/error@none",
  877. "9": "session.status",
  878. },
  879. "d": {"4": "errored"},
  880. },
  881. "metric_id": 8,
  882. "org_id": 1,
  883. "project_id": 3,
  884. "retention_days": 90,
  885. "tags": {"3": 7, "9": 4},
  886. "timestamp": ts,
  887. "type": "s",
  888. "use_case_id": "sessions",
  889. "value": [3],
  890. "sentry_received_timestamp": BROKER_TIMESTAMP.timestamp(),
  891. },
  892. [("mapping_sources", b"cd"), ("metric_type", "s")],
  893. ),
  894. ]
  895. @patch("sentry.sentry_metrics.consumers.indexer.batch.UseCaseID", MockUseCaseID)
  896. def test_batch_resolve_with_values_not_indexed(caplog, settings):
  897. """
  898. Tests that the indexer batch skips resolving tag values for indexing and
  899. sends the raw tag value to Snuba.
  900. The difference between this test and test_all_resolved is that the tag values are
  901. strings instead of integers. Because of that indexed tag keys are
  902. different and mapping_meta is smaller. The payload also contains the
  903. version field to specify that the tag values are not indexed.
  904. """
  905. settings.SENTRY_METRICS_INDEXER_DEBUG_LOG_SAMPLE_RATE = 1.0
  906. outer_message = _construct_outer_message(
  907. [
  908. (counter_payload, []),
  909. (distribution_payload, []),
  910. (set_payload, []),
  911. ]
  912. )
  913. batch = IndexerBatch(
  914. outer_message,
  915. False,
  916. False,
  917. input_codec=_INGEST_CODEC,
  918. )
  919. assert batch.extract_strings() == (
  920. {
  921. MockUseCaseID.SESSIONS: {
  922. 1: {
  923. "c:sessions/session@none",
  924. "d:sessions/duration@second",
  925. "environment",
  926. "s:sessions/error@none",
  927. "session.status",
  928. }
  929. }
  930. }
  931. )
  932. caplog.set_level(logging.ERROR)
  933. snuba_payloads = batch.reconstruct_messages(
  934. {
  935. 1: {
  936. "c:sessions/session@none": 1,
  937. "d:sessions/duration@second": 2,
  938. "environment": 3,
  939. "s:sessions/error@none": 4,
  940. "session.status": 5,
  941. }
  942. },
  943. {
  944. 1: {
  945. "c:sessions/session@none": Metadata(id=1, fetch_type=FetchType.CACHE_HIT),
  946. "d:sessions/duration@second": Metadata(id=2, fetch_type=FetchType.CACHE_HIT),
  947. "environment": Metadata(id=3, fetch_type=FetchType.CACHE_HIT),
  948. "s:sessions/error@none": Metadata(id=4, fetch_type=FetchType.CACHE_HIT),
  949. "session.status": Metadata(id=5, fetch_type=FetchType.CACHE_HIT),
  950. }
  951. },
  952. )
  953. assert _get_string_indexer_log_records(caplog) == []
  954. assert _deconstruct_messages(snuba_payloads, kafka_logical_topic="snuba-generic-metrics") == [
  955. (
  956. {
  957. "version": 2,
  958. "mapping_meta": {
  959. "c": {
  960. "1": "c:sessions/session@none",
  961. "3": "environment",
  962. "5": "session.status",
  963. },
  964. },
  965. "metric_id": 1,
  966. "org_id": 1,
  967. "project_id": 3,
  968. "retention_days": 90,
  969. "tags": {"3": "production", "5": "init"},
  970. "timestamp": ts,
  971. "type": "c",
  972. "use_case_id": "sessions",
  973. "value": 1.0,
  974. "sentry_received_timestamp": BROKER_TIMESTAMP.timestamp(),
  975. },
  976. [("mapping_sources", b"c"), ("metric_type", "c")],
  977. ),
  978. (
  979. {
  980. "version": 2,
  981. "mapping_meta": {
  982. "c": {
  983. "2": "d:sessions/duration@second",
  984. "3": "environment",
  985. "5": "session.status",
  986. },
  987. },
  988. "metric_id": 2,
  989. "org_id": 1,
  990. "project_id": 3,
  991. "retention_days": 90,
  992. "tags": {"3": "production", "5": "healthy"},
  993. "timestamp": ts,
  994. "type": "d",
  995. "use_case_id": "sessions",
  996. "value": [4, 5, 6],
  997. "sentry_received_timestamp": BROKER_TIMESTAMP.timestamp(),
  998. },
  999. [
  1000. ("mapping_sources", b"c"),
  1001. ("metric_type", "d"),
  1002. ],
  1003. ),
  1004. (
  1005. {
  1006. "version": 2,
  1007. "mapping_meta": {
  1008. "c": {
  1009. "3": "environment",
  1010. "4": "s:sessions/error@none",
  1011. "5": "session.status",
  1012. },
  1013. },
  1014. "metric_id": 4,
  1015. "org_id": 1,
  1016. "project_id": 3,
  1017. "retention_days": 90,
  1018. "tags": {"3": "production", "5": "errored"},
  1019. "timestamp": ts,
  1020. "type": "s",
  1021. "use_case_id": "sessions",
  1022. "value": [3],
  1023. "sentry_received_timestamp": BROKER_TIMESTAMP.timestamp(),
  1024. },
  1025. [
  1026. ("mapping_sources", b"c"),
  1027. ("metric_type", "s"),
  1028. ],
  1029. ),
  1030. ]
  1031. @patch("sentry.sentry_metrics.consumers.indexer.batch.UseCaseID", MockUseCaseID)
  1032. def test_metric_id_rate_limited(caplog, settings):
  1033. settings.SENTRY_METRICS_INDEXER_DEBUG_LOG_SAMPLE_RATE = 1.0
  1034. outer_message = _construct_outer_message(
  1035. [
  1036. (counter_payload, []),
  1037. (distribution_payload, []),
  1038. (set_payload, []),
  1039. ]
  1040. )
  1041. batch = IndexerBatch(outer_message, True, False, input_codec=_INGEST_CODEC)
  1042. assert batch.extract_strings() == (
  1043. {
  1044. MockUseCaseID.SESSIONS: {
  1045. 1: {
  1046. "c:sessions/session@none",
  1047. "d:sessions/duration@second",
  1048. "environment",
  1049. "errored",
  1050. "healthy",
  1051. "init",
  1052. "production",
  1053. "s:sessions/error@none",
  1054. "session.status",
  1055. }
  1056. }
  1057. }
  1058. )
  1059. caplog.set_level(logging.ERROR)
  1060. snuba_payloads = batch.reconstruct_messages(
  1061. {
  1062. 1: {
  1063. "c:sessions/session@none": None,
  1064. "d:sessions/duration@second": None,
  1065. "environment": 3,
  1066. "errored": 4,
  1067. "healthy": 5,
  1068. "init": 6,
  1069. "production": 7,
  1070. "s:sessions/error@none": 8,
  1071. "session.status": 9,
  1072. }
  1073. },
  1074. {
  1075. 1: {
  1076. "c:sessions/session@none": Metadata(
  1077. id=None,
  1078. fetch_type=FetchType.RATE_LIMITED,
  1079. fetch_type_ext=FetchTypeExt(is_global=False),
  1080. ),
  1081. "d:sessions/duration@second": Metadata(
  1082. id=None, fetch_type=FetchType.RATE_LIMITED, fetch_type_ext=None
  1083. ),
  1084. "environment": Metadata(id=3, fetch_type=FetchType.CACHE_HIT),
  1085. "errored": Metadata(id=4, fetch_type=FetchType.DB_READ),
  1086. "healthy": Metadata(id=5, fetch_type=FetchType.HARDCODED),
  1087. "init": Metadata(id=6, fetch_type=FetchType.HARDCODED),
  1088. "production": Metadata(id=7, fetch_type=FetchType.CACHE_HIT),
  1089. "s:sessions/error@none": Metadata(id=None, fetch_type=FetchType.DB_READ),
  1090. "session.status": Metadata(id=9, fetch_type=FetchType.CACHE_HIT),
  1091. }
  1092. },
  1093. )
  1094. assert _deconstruct_messages(snuba_payloads) == [
  1095. (
  1096. {
  1097. "mapping_meta": {
  1098. "c": {"3": "environment", "7": "production", "9": "session.status"},
  1099. "d": {"4": "errored", "None": "s:sessions/error@none"},
  1100. },
  1101. "metric_id": 8,
  1102. "org_id": 1,
  1103. "project_id": 3,
  1104. "retention_days": 90,
  1105. "tags": {"3": 7, "9": 4},
  1106. "timestamp": ts,
  1107. "type": "s",
  1108. "use_case_id": "sessions",
  1109. "value": [3],
  1110. "sentry_received_timestamp": BROKER_TIMESTAMP.timestamp(),
  1111. },
  1112. [
  1113. ("mapping_sources", b"cd"),
  1114. ("metric_type", "s"),
  1115. ],
  1116. ),
  1117. ]
  1118. assert _get_string_indexer_log_records(caplog) == [
  1119. (
  1120. "process_messages.dropped_message",
  1121. {"org_batch_size": 9, "is_global_quota": False, "string_type": "metric_id"},
  1122. ),
  1123. (
  1124. "process_messages.dropped_message",
  1125. {"org_batch_size": 9, "is_global_quota": False, "string_type": "metric_id"},
  1126. ),
  1127. ]
  1128. @patch("sentry.sentry_metrics.consumers.indexer.batch.UseCaseID", MockUseCaseID)
  1129. def test_tag_key_rate_limited(caplog, settings):
  1130. settings.SENTRY_METRICS_INDEXER_DEBUG_LOG_SAMPLE_RATE = 1.0
  1131. outer_message = _construct_outer_message(
  1132. [
  1133. (counter_payload, []),
  1134. (distribution_payload, []),
  1135. (set_payload, []),
  1136. ]
  1137. )
  1138. batch = IndexerBatch(outer_message, True, False, input_codec=_INGEST_CODEC)
  1139. assert batch.extract_strings() == (
  1140. {
  1141. MockUseCaseID.SESSIONS: {
  1142. 1: {
  1143. "c:sessions/session@none",
  1144. "d:sessions/duration@second",
  1145. "environment",
  1146. "errored",
  1147. "healthy",
  1148. "init",
  1149. "production",
  1150. "s:sessions/error@none",
  1151. "session.status",
  1152. }
  1153. }
  1154. }
  1155. )
  1156. caplog.set_level(logging.ERROR)
  1157. snuba_payloads = batch.reconstruct_messages(
  1158. {
  1159. 1: {
  1160. "c:sessions/session@none": 1,
  1161. "d:sessions/duration@second": 2,
  1162. "environment": None,
  1163. "errored": 4,
  1164. "healthy": 5,
  1165. "init": 6,
  1166. "production": 7,
  1167. "s:sessions/error@none": 8,
  1168. "session.status": 9,
  1169. }
  1170. },
  1171. {
  1172. 1: {
  1173. "c:sessions/session@none": Metadata(id=1, fetch_type=FetchType.CACHE_HIT),
  1174. "d:sessions/duration@second": Metadata(id=2, fetch_type=FetchType.CACHE_HIT),
  1175. "environment": Metadata(
  1176. id=None,
  1177. fetch_type=FetchType.RATE_LIMITED,
  1178. fetch_type_ext=FetchTypeExt(is_global=False),
  1179. ),
  1180. "errored": Metadata(id=4, fetch_type=FetchType.DB_READ),
  1181. "healthy": Metadata(id=5, fetch_type=FetchType.HARDCODED),
  1182. "init": Metadata(id=6, fetch_type=FetchType.HARDCODED),
  1183. "production": Metadata(id=7, fetch_type=FetchType.CACHE_HIT),
  1184. "s:sessions/error@none": Metadata(id=8, fetch_type=FetchType.CACHE_HIT),
  1185. "session.status": Metadata(id=9, fetch_type=FetchType.CACHE_HIT),
  1186. }
  1187. },
  1188. )
  1189. assert _get_string_indexer_log_records(caplog) == [
  1190. (
  1191. "process_messages.dropped_message",
  1192. {"num_global_quotas": 0, "org_batch_size": 9, "string_type": "tags"},
  1193. ),
  1194. (
  1195. "process_messages.dropped_message",
  1196. {"num_global_quotas": 0, "org_batch_size": 9, "string_type": "tags"},
  1197. ),
  1198. (
  1199. "process_messages.dropped_message",
  1200. {"num_global_quotas": 0, "org_batch_size": 9, "string_type": "tags"},
  1201. ),
  1202. ]
  1203. assert _deconstruct_messages(snuba_payloads) == []
  1204. @patch("sentry.sentry_metrics.consumers.indexer.batch.UseCaseID", MockUseCaseID)
  1205. def test_tag_value_rate_limited(caplog, settings):
  1206. settings.SENTRY_METRICS_INDEXER_DEBUG_LOG_SAMPLE_RATE = 1.0
  1207. outer_message = _construct_outer_message(
  1208. [
  1209. (counter_payload, []),
  1210. (distribution_payload, []),
  1211. (set_payload, []),
  1212. ]
  1213. )
  1214. batch = IndexerBatch(outer_message, True, False, input_codec=_INGEST_CODEC)
  1215. assert batch.extract_strings() == (
  1216. {
  1217. MockUseCaseID.SESSIONS: {
  1218. 1: {
  1219. "c:sessions/session@none",
  1220. "d:sessions/duration@second",
  1221. "environment",
  1222. "errored",
  1223. "healthy",
  1224. "init",
  1225. "production",
  1226. "s:sessions/error@none",
  1227. "session.status",
  1228. }
  1229. }
  1230. }
  1231. )
  1232. caplog.set_level(logging.ERROR)
  1233. snuba_payloads = batch.reconstruct_messages(
  1234. {
  1235. 1: {
  1236. "c:sessions/session@none": 1,
  1237. "d:sessions/duration@second": 2,
  1238. "environment": 3,
  1239. "errored": None,
  1240. "healthy": 5,
  1241. "init": 6,
  1242. "production": 7,
  1243. "s:sessions/error@none": 8,
  1244. "session.status": 9,
  1245. }
  1246. },
  1247. {
  1248. 1: {
  1249. "c:sessions/session@none": Metadata(id=1, fetch_type=FetchType.CACHE_HIT),
  1250. "d:sessions/duration@second": Metadata(id=2, fetch_type=FetchType.CACHE_HIT),
  1251. "environment": Metadata(id=3, fetch_type=FetchType.CACHE_HIT),
  1252. "errored": Metadata(
  1253. id=None,
  1254. fetch_type=FetchType.RATE_LIMITED,
  1255. fetch_type_ext=FetchTypeExt(is_global=False),
  1256. ),
  1257. "healthy": Metadata(id=5, fetch_type=FetchType.HARDCODED),
  1258. "init": Metadata(id=6, fetch_type=FetchType.HARDCODED),
  1259. "production": Metadata(id=7, fetch_type=FetchType.CACHE_HIT),
  1260. "s:sessions/error@none": Metadata(id=8, fetch_type=FetchType.CACHE_HIT),
  1261. "session.status": Metadata(id=9, fetch_type=FetchType.CACHE_HIT),
  1262. }
  1263. },
  1264. )
  1265. assert _get_string_indexer_log_records(caplog) == [
  1266. (
  1267. "process_messages.dropped_message",
  1268. {"num_global_quotas": 0, "org_batch_size": 9, "string_type": "tags"},
  1269. ),
  1270. ]
  1271. assert _deconstruct_messages(snuba_payloads) == [
  1272. (
  1273. {
  1274. "mapping_meta": {
  1275. "c": {
  1276. "1": "c:sessions/session@none",
  1277. "3": "environment",
  1278. "7": "production",
  1279. "9": "session.status",
  1280. },
  1281. "h": {"6": "init"},
  1282. },
  1283. "metric_id": 1,
  1284. "org_id": 1,
  1285. "project_id": 3,
  1286. "retention_days": 90,
  1287. "tags": {"3": 7, "9": 6},
  1288. "timestamp": ts,
  1289. "type": "c",
  1290. "use_case_id": "sessions",
  1291. "value": 1.0,
  1292. "sentry_received_timestamp": BROKER_TIMESTAMP.timestamp(),
  1293. },
  1294. [
  1295. ("mapping_sources", b"ch"),
  1296. ("metric_type", "c"),
  1297. ],
  1298. ),
  1299. (
  1300. {
  1301. "mapping_meta": {
  1302. "c": {
  1303. "2": "d:sessions/duration@second",
  1304. "3": "environment",
  1305. "7": "production",
  1306. "9": "session.status",
  1307. },
  1308. "h": {"5": "healthy"},
  1309. },
  1310. "metric_id": 2,
  1311. "org_id": 1,
  1312. "project_id": 3,
  1313. "retention_days": 90,
  1314. "tags": {"3": 7, "9": 5},
  1315. "timestamp": ts,
  1316. "type": "d",
  1317. "use_case_id": "sessions",
  1318. "value": [4, 5, 6],
  1319. "sentry_received_timestamp": BROKER_TIMESTAMP.timestamp(),
  1320. },
  1321. [
  1322. ("mapping_sources", b"ch"),
  1323. ("metric_type", "d"),
  1324. ],
  1325. ),
  1326. ]
  1327. @patch("sentry.sentry_metrics.consumers.indexer.batch.UseCaseID", MockUseCaseID)
  1328. def test_one_org_limited(caplog, settings):
  1329. settings.SENTRY_METRICS_INDEXER_DEBUG_LOG_SAMPLE_RATE = 1.0
  1330. outer_message = _construct_outer_message(
  1331. [
  1332. (counter_payload, []),
  1333. ({**distribution_payload, "org_id": 2}, []),
  1334. ]
  1335. )
  1336. batch = IndexerBatch(
  1337. outer_message,
  1338. True,
  1339. False,
  1340. input_codec=_INGEST_CODEC,
  1341. )
  1342. assert batch.extract_strings() == (
  1343. {
  1344. MockUseCaseID.SESSIONS: {
  1345. 1: {
  1346. "c:sessions/session@none",
  1347. "environment",
  1348. "init",
  1349. "production",
  1350. "session.status",
  1351. },
  1352. 2: {
  1353. "d:sessions/duration@second",
  1354. "environment",
  1355. "healthy",
  1356. "production",
  1357. "session.status",
  1358. },
  1359. }
  1360. }
  1361. )
  1362. caplog.set_level(logging.ERROR)
  1363. snuba_payloads = batch.reconstruct_messages(
  1364. {
  1365. 1: {
  1366. "c:sessions/session@none": 1,
  1367. "environment": None,
  1368. "init": 3,
  1369. "production": 4,
  1370. "session.status": 5,
  1371. },
  1372. 2: {
  1373. "d:sessions/duration@second": 1,
  1374. "environment": 2,
  1375. "healthy": 3,
  1376. "production": 4,
  1377. "session.status": 5,
  1378. },
  1379. },
  1380. {
  1381. 1: {
  1382. "c:sessions/session@none": Metadata(id=1, fetch_type=FetchType.CACHE_HIT),
  1383. "environment": Metadata(
  1384. id=None,
  1385. fetch_type=FetchType.RATE_LIMITED,
  1386. fetch_type_ext=FetchTypeExt(is_global=False),
  1387. ),
  1388. "init": Metadata(id=3, fetch_type=FetchType.HARDCODED),
  1389. "production": Metadata(id=4, fetch_type=FetchType.CACHE_HIT),
  1390. "session.status": Metadata(id=5, fetch_type=FetchType.CACHE_HIT),
  1391. },
  1392. 2: {
  1393. "d:sessions/duration@second": Metadata(id=1, fetch_type=FetchType.CACHE_HIT),
  1394. "environment": Metadata(id=2, fetch_type=FetchType.CACHE_HIT),
  1395. "healthy": Metadata(id=3, fetch_type=FetchType.HARDCODED),
  1396. "production": Metadata(id=4, fetch_type=FetchType.CACHE_HIT),
  1397. "session.status": Metadata(id=5, fetch_type=FetchType.CACHE_HIT),
  1398. },
  1399. },
  1400. )
  1401. assert _get_string_indexer_log_records(caplog) == [
  1402. (
  1403. "process_messages.dropped_message",
  1404. {"num_global_quotas": 0, "org_batch_size": 5, "string_type": "tags"},
  1405. ),
  1406. ]
  1407. assert _deconstruct_messages(snuba_payloads) == [
  1408. (
  1409. {
  1410. "mapping_meta": {
  1411. "c": {
  1412. "1": "d:sessions/duration@second",
  1413. "2": "environment",
  1414. "4": "production",
  1415. "5": "session.status",
  1416. },
  1417. "h": {"3": "healthy"},
  1418. },
  1419. "metric_id": 1,
  1420. "org_id": 2,
  1421. "project_id": 3,
  1422. "retention_days": 90,
  1423. "tags": {"2": 4, "5": 3},
  1424. "timestamp": ts,
  1425. "type": "d",
  1426. "use_case_id": "sessions",
  1427. "value": [4, 5, 6],
  1428. "sentry_received_timestamp": BROKER_TIMESTAMP.timestamp(),
  1429. },
  1430. [
  1431. ("mapping_sources", b"ch"),
  1432. ("metric_type", "d"),
  1433. ],
  1434. ),
  1435. ]
  1436. @patch("sentry.sentry_metrics.consumers.indexer.batch.UseCaseID", MockUseCaseID)
  1437. def test_cardinality_limiter(caplog, settings):
  1438. """
  1439. Test functionality of the indexer batch related to cardinality-limiting. More concretely, assert that `IndexerBatch.filter_messages`:
  1440. 1. removes the messages from the outgoing batch
  1441. 2. prevents strings from filtered messages from being extracted & indexed
  1442. 3. does not crash when strings from filtered messages are not passed into reconstruct_messages
  1443. 4. still extracts strings that exist both in filtered and unfiltered messages (eg "environment")
  1444. """
  1445. settings.SENTRY_METRICS_INDEXER_DEBUG_LOG_SAMPLE_RATE = 1.0
  1446. outer_message = _construct_outer_message(
  1447. [
  1448. (counter_payload, []),
  1449. (distribution_payload, []),
  1450. (set_payload, []),
  1451. ]
  1452. )
  1453. batch = IndexerBatch(
  1454. outer_message,
  1455. True,
  1456. False,
  1457. input_codec=_INGEST_CODEC,
  1458. )
  1459. keys_to_remove = list(batch.parsed_payloads_by_offset)[:2]
  1460. # the messages come in a certain order, and Python dictionaries preserve
  1461. # their insertion order. So we can hardcode offsets here.
  1462. assert keys_to_remove == [
  1463. PartitionIdxOffset(partition_idx=0, offset=0),
  1464. PartitionIdxOffset(partition_idx=0, offset=1),
  1465. ]
  1466. batch.filter_messages(keys_to_remove)
  1467. assert batch.extract_strings() == {
  1468. MockUseCaseID.SESSIONS: {
  1469. 1: {
  1470. "environment",
  1471. "errored",
  1472. "production",
  1473. # Note, we only extracted one MRI, of the one metric that we didn't
  1474. # drop
  1475. "s:sessions/error@none",
  1476. "session.status",
  1477. },
  1478. }
  1479. }
  1480. snuba_payloads = batch.reconstruct_messages(
  1481. {
  1482. 1: {
  1483. "environment": 1,
  1484. "errored": 2,
  1485. "production": 3,
  1486. "s:sessions/error@none": 4,
  1487. "session.status": 5,
  1488. },
  1489. },
  1490. {
  1491. 1: {
  1492. "environment": Metadata(id=1, fetch_type=FetchType.CACHE_HIT),
  1493. "errored": Metadata(id=2, fetch_type=FetchType.CACHE_HIT),
  1494. "production": Metadata(id=3, fetch_type=FetchType.CACHE_HIT),
  1495. "s:sessions/error@none": Metadata(id=4, fetch_type=FetchType.CACHE_HIT),
  1496. "session.status": Metadata(id=5, fetch_type=FetchType.CACHE_HIT),
  1497. }
  1498. },
  1499. )
  1500. assert _deconstruct_messages(snuba_payloads) == [
  1501. (
  1502. {
  1503. "mapping_meta": {
  1504. "c": {
  1505. "1": "environment",
  1506. "2": "errored",
  1507. "3": "production",
  1508. "4": "s:sessions/error@none",
  1509. "5": "session.status",
  1510. },
  1511. },
  1512. "metric_id": 4,
  1513. "org_id": 1,
  1514. "project_id": 3,
  1515. "retention_days": 90,
  1516. "tags": {"1": 3, "5": 2},
  1517. "timestamp": ts,
  1518. "type": "s",
  1519. "use_case_id": "sessions",
  1520. "value": [3],
  1521. "sentry_received_timestamp": BROKER_TIMESTAMP.timestamp(),
  1522. },
  1523. [
  1524. ("mapping_sources", b"c"),
  1525. ("metric_type", "s"),
  1526. ],
  1527. )
  1528. ]