test_organization_events_trace.py 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. from datetime import timedelta
  2. from unittest import mock
  3. from uuid import uuid4
  4. import pytest
  5. from django.urls import NoReverseMatch, reverse
  6. from sentry import options
  7. from sentry.issues.grouptype import NoiseConfig, PerformanceFileIOMainThreadGroupType
  8. from sentry.testutils import APITestCase, SnubaTestCase
  9. from sentry.testutils.helpers import override_options
  10. from sentry.testutils.helpers.datetime import before_now, iso_format
  11. from sentry.testutils.silo import region_silo_test
  12. from sentry.utils.samples import load_data
  13. class OrganizationEventsTraceEndpointBase(APITestCase, SnubaTestCase):
  14. FEATURES = [
  15. "organizations:performance-view",
  16. "organizations:performance-file-io-main-thread-detector",
  17. ]
  18. def get_start_end(self, duration):
  19. return self.day_ago, self.day_ago + timedelta(milliseconds=duration)
  20. def create_event(
  21. self,
  22. trace,
  23. transaction,
  24. spans,
  25. parent_span_id,
  26. project_id,
  27. tags=None,
  28. duration=4000,
  29. span_id=None,
  30. measurements=None,
  31. file_io_performance_issue=False,
  32. **kwargs,
  33. ):
  34. start, end = self.get_start_end(duration)
  35. data = load_data(
  36. "transaction",
  37. trace=trace,
  38. spans=spans,
  39. timestamp=end,
  40. start_timestamp=start,
  41. )
  42. data["transaction"] = transaction
  43. data["contexts"]["trace"]["parent_span_id"] = parent_span_id
  44. if span_id:
  45. data["contexts"]["trace"]["span_id"] = span_id
  46. if measurements:
  47. for key, value in measurements.items():
  48. data["measurements"][key]["value"] = value
  49. if tags is not None:
  50. data["tags"] = tags
  51. if file_io_performance_issue:
  52. span = data["spans"][0]
  53. if "data" not in span:
  54. span["data"] = {}
  55. span["op"] = "file.write"
  56. span["data"].update({"duration": 1, "blocked_main_thread": True})
  57. with self.feature(self.FEATURES):
  58. with mock.patch.object(
  59. PerformanceFileIOMainThreadGroupType,
  60. "noise_config",
  61. new=NoiseConfig(0, timedelta(minutes=1)),
  62. ), override_options(
  63. {
  64. "performance.issues.all.problem-detection": 1.0,
  65. "performance-file-io-main-thread-creation": 1.0,
  66. }
  67. ), self.feature(
  68. ["projects:performance-suspect-spans-ingestion"]
  69. ):
  70. return self.store_event(data, project_id=project_id, **kwargs)
  71. def setUp(self):
  72. """
  73. Span structure:
  74. root
  75. gen1-0
  76. gen2-0
  77. gen3-0
  78. gen1-1
  79. gen2-1
  80. gen1-2
  81. gen2-2
  82. """
  83. super().setUp()
  84. options.set("performance.issues.all.problem-detection", 1.0)
  85. options.set("performance.issues.file_io_main_thread.problem-creation", 1.0)
  86. self.login_as(user=self.user)
  87. self.day_ago = before_now(days=1).replace(hour=10, minute=0, second=0, microsecond=0)
  88. self.root_span_ids = [uuid4().hex[:16] for _ in range(3)]
  89. self.trace_id = uuid4().hex
  90. self.url = reverse(
  91. self.url_name,
  92. kwargs={"organization_slug": self.project.organization.slug, "trace_id": self.trace_id},
  93. )
  94. def load_trace(self):
  95. self.root_event = self.create_event(
  96. trace=self.trace_id,
  97. transaction="root",
  98. spans=[
  99. {
  100. "same_process_as_parent": True,
  101. "op": "http",
  102. "description": f"GET gen1-{i}",
  103. "span_id": root_span_id,
  104. "trace_id": self.trace_id,
  105. }
  106. for i, root_span_id in enumerate(self.root_span_ids)
  107. ],
  108. measurements={
  109. "lcp": 1000,
  110. "fcp": 750,
  111. },
  112. parent_span_id=None,
  113. file_io_performance_issue=True,
  114. project_id=self.project.id,
  115. duration=3000,
  116. )
  117. # First Generation
  118. self.gen1_span_ids = [uuid4().hex[:16] for _ in range(3)]
  119. self.gen1_project = self.create_project(organization=self.organization)
  120. self.gen1_events = [
  121. self.create_event(
  122. trace=self.trace_id,
  123. transaction=f"/transaction/gen1-{i}",
  124. spans=[
  125. {
  126. "same_process_as_parent": True,
  127. "op": "http",
  128. "description": f"GET gen2-{i}",
  129. "span_id": gen1_span_id,
  130. "trace_id": self.trace_id,
  131. }
  132. ],
  133. parent_span_id=root_span_id,
  134. project_id=self.gen1_project.id,
  135. duration=2000,
  136. )
  137. for i, (root_span_id, gen1_span_id) in enumerate(
  138. zip(self.root_span_ids, self.gen1_span_ids)
  139. )
  140. ]
  141. # Second Generation
  142. self.gen2_span_ids = [uuid4().hex[:16] for _ in range(3)]
  143. self.gen2_project = self.create_project(organization=self.organization)
  144. # Intentially pick a span id that starts with 0s
  145. self.gen2_span_id = "0011" * 4
  146. self.gen2_events = [
  147. self.create_event(
  148. trace=self.trace_id,
  149. transaction=f"/transaction/gen2-{i}",
  150. spans=[
  151. {
  152. "same_process_as_parent": True,
  153. "op": "http",
  154. "description": f"GET gen3-{i}" if i == 0 else f"SPAN gen3-{i}",
  155. "span_id": gen2_span_id,
  156. "trace_id": self.trace_id,
  157. }
  158. ],
  159. parent_span_id=gen1_span_id,
  160. span_id=self.gen2_span_id if i == 0 else None,
  161. project_id=self.gen2_project.id,
  162. duration=1000,
  163. )
  164. for i, (gen1_span_id, gen2_span_id) in enumerate(
  165. zip(self.gen1_span_ids, self.gen2_span_ids)
  166. )
  167. ]
  168. # Third generation
  169. self.gen3_project = self.create_project(organization=self.organization)
  170. self.gen3_event = self.create_event(
  171. trace=self.trace_id,
  172. transaction="/transaction/gen3-0",
  173. spans=[],
  174. project_id=self.gen3_project.id,
  175. parent_span_id=self.gen2_span_id,
  176. duration=500,
  177. )
  178. def load_errors(self):
  179. start, _ = self.get_start_end(1000)
  180. error_data = load_data(
  181. "javascript",
  182. timestamp=start,
  183. )
  184. error_data["contexts"]["trace"] = {
  185. "type": "trace",
  186. "trace_id": self.trace_id,
  187. "span_id": self.gen1_span_ids[0],
  188. }
  189. error_data["level"] = "fatal"
  190. error = self.store_event(error_data, project_id=self.gen1_project.id)
  191. error_data["level"] = "warning"
  192. error1 = self.store_event(error_data, project_id=self.gen1_project.id)
  193. return error, error1
  194. def load_default(self):
  195. start, _ = self.get_start_end(1000)
  196. return self.store_event(
  197. {
  198. "timestamp": iso_format(start),
  199. "contexts": {
  200. "trace": {
  201. "type": "trace",
  202. "trace_id": self.trace_id,
  203. "span_id": self.root_span_ids[0],
  204. },
  205. },
  206. "level": "debug",
  207. "message": "this is a log message",
  208. },
  209. project_id=self.gen1_project.id,
  210. )
  211. @region_silo_test
  212. class OrganizationEventsTraceLightEndpointTest(OrganizationEventsTraceEndpointBase):
  213. url_name = "sentry-api-0-organization-events-trace-light"
  214. def test_no_projects(self):
  215. user = self.create_user()
  216. org = self.create_organization(owner=user)
  217. self.login_as(user=user)
  218. url = reverse(
  219. self.url_name,
  220. kwargs={"organization_slug": org.slug, "trace_id": uuid4().hex},
  221. )
  222. with self.feature(self.FEATURES):
  223. response = self.client.get(
  224. url,
  225. format="json",
  226. )
  227. assert response.status_code == 404, response.content
  228. def test_bad_ids(self):
  229. # Fake event id
  230. with self.feature(self.FEATURES):
  231. response = self.client.get(
  232. self.url,
  233. data={"event_id": uuid4().hex},
  234. format="json",
  235. )
  236. assert response.status_code == 404, response.content
  237. # Invalid event id
  238. with self.feature(self.FEATURES):
  239. response = self.client.get(
  240. self.url,
  241. data={"event_id": "not-a-event"},
  242. format="json",
  243. )
  244. assert response.status_code == 400, response.content
  245. # Fake trace id
  246. self.url = reverse(
  247. "sentry-api-0-organization-events-trace-light",
  248. kwargs={"organization_slug": self.project.organization.slug, "trace_id": uuid4().hex},
  249. )
  250. with self.feature(self.FEATURES):
  251. response = self.client.get(
  252. self.url,
  253. data={"event_id": "a" * 32},
  254. format="json",
  255. )
  256. assert response.status_code == 404, response.content
  257. # Invalid trace id
  258. with pytest.raises(NoReverseMatch):
  259. self.url = reverse(
  260. "sentry-api-0-organization-events-trace-light",
  261. kwargs={
  262. "organization_slug": self.project.organization.slug,
  263. "trace_id": "not-a-trace",
  264. },
  265. )
  266. def test_no_roots(self):
  267. """Even when there's no root, we return the current event"""
  268. self.load_trace()
  269. no_root_trace = uuid4().hex
  270. parent_span_id = uuid4().hex[:16]
  271. no_root_event = self.create_event(
  272. trace=no_root_trace,
  273. transaction="/not_root/but_only_transaction",
  274. spans=[],
  275. parent_span_id=parent_span_id,
  276. project_id=self.project.id,
  277. )
  278. url = reverse(
  279. "sentry-api-0-organization-events-trace-light",
  280. kwargs={"organization_slug": self.project.organization.slug, "trace_id": no_root_trace},
  281. )
  282. with self.feature(self.FEATURES):
  283. response = self.client.get(
  284. url,
  285. data={"event_id": no_root_event.event_id},
  286. format="json",
  287. )
  288. assert response.status_code == 200, response.content
  289. assert len(response.data) == 1
  290. event = response.data[0]
  291. # Basically know nothing about this event
  292. assert event["generation"] is None
  293. assert event["parent_event_id"] is None
  294. assert event["parent_span_id"] == parent_span_id
  295. assert event["event_id"] == no_root_event.event_id
  296. def test_multiple_roots(self):
  297. self.load_trace()
  298. second_root = self.create_event(
  299. trace=self.trace_id,
  300. transaction="/second_root",
  301. spans=[],
  302. parent_span_id=None,
  303. project_id=self.project.id,
  304. )
  305. with self.feature(self.FEATURES):
  306. response = self.client.get(
  307. self.url,
  308. data={"event_id": second_root.event_id, "project": -1},
  309. format="json",
  310. )
  311. assert response.status_code == 200, response.content
  312. assert len(response.data) == 1
  313. event = response.data[0]
  314. assert event["generation"] == 0
  315. assert event["parent_event_id"] is None
  316. assert event["parent_span_id"] is None
  317. def test_root_event(self):
  318. self.load_trace()
  319. root_event_id = self.root_event.event_id
  320. with self.feature(self.FEATURES):
  321. response = self.client.get(
  322. self.url,
  323. data={"event_id": root_event_id, "project": -1},
  324. format="json",
  325. )
  326. assert response.status_code == 200, response.content
  327. assert len(response.data) == 4
  328. events = {item["event_id"]: item for item in response.data}
  329. assert root_event_id in events
  330. event = events[root_event_id]
  331. assert event["generation"] == 0
  332. assert event["parent_event_id"] is None
  333. assert event["parent_span_id"] is None
  334. for i, child_event in enumerate(self.gen1_events):
  335. child_event_id = child_event.event_id
  336. assert child_event_id in events
  337. event = events[child_event_id]
  338. assert event["generation"] == 1
  339. assert event["parent_event_id"] == root_event_id
  340. assert event["parent_span_id"] == self.root_span_ids[i]
  341. def test_root_with_multiple_roots(self):
  342. self.load_trace()
  343. root_event_id = self.root_event.event_id
  344. self.create_event(
  345. trace=self.trace_id,
  346. transaction="/second_root",
  347. spans=[],
  348. parent_span_id=None,
  349. project_id=self.project.id,
  350. )
  351. with self.feature(self.FEATURES):
  352. response = self.client.get(
  353. self.url,
  354. data={"event_id": self.root_event.event_id},
  355. format="json",
  356. )
  357. assert response.status_code == 200, response.content
  358. assert len(response.data) == 4
  359. events = {item["event_id"]: item for item in response.data}
  360. assert root_event_id in events
  361. event = events[root_event_id]
  362. assert event["generation"] == 0
  363. assert event["parent_event_id"] is None
  364. assert event["parent_span_id"] is None
  365. for i, child_event in enumerate(self.gen1_events):
  366. child_event_id = child_event.event_id
  367. assert child_event_id in events
  368. event = events[child_event_id]
  369. assert event["generation"] == 1
  370. assert event["parent_event_id"] == root_event_id
  371. assert event["parent_span_id"] == self.root_span_ids[i]
  372. def test_direct_parent_with_children(self):
  373. self.load_trace()
  374. root_event_id = self.root_event.event_id
  375. current_event = self.gen1_events[0].event_id
  376. child_event_id = self.gen2_events[0].event_id
  377. with self.feature(self.FEATURES):
  378. response = self.client.get(
  379. self.url,
  380. data={"event_id": current_event, "project": -1},
  381. format="json",
  382. )
  383. assert response.status_code == 200, response.content
  384. assert len(response.data) == 3
  385. events = {item["event_id"]: item for item in response.data}
  386. assert root_event_id in events
  387. event = events[root_event_id]
  388. assert event["generation"] == 0
  389. assert event["parent_event_id"] is None
  390. assert event["parent_span_id"] is None
  391. assert current_event in events
  392. event = events[current_event]
  393. assert event["generation"] == 1
  394. assert event["parent_event_id"] == root_event_id
  395. assert event["parent_span_id"] == self.root_span_ids[0]
  396. assert child_event_id in events
  397. event = events[child_event_id]
  398. assert event["generation"] == 2
  399. assert event["parent_event_id"] == current_event
  400. assert event["parent_span_id"] == self.gen1_span_ids[0]
  401. def test_direct_parent_with_children_and_multiple_root(self):
  402. self.load_trace()
  403. root_event_id = self.root_event.event_id
  404. current_event = self.gen1_events[0].event_id
  405. child_event_id = self.gen2_events[0].event_id
  406. self.create_event(
  407. trace=self.trace_id,
  408. transaction="/second_root",
  409. spans=[],
  410. parent_span_id=None,
  411. project_id=self.project.id,
  412. )
  413. with self.feature(self.FEATURES):
  414. response = self.client.get(
  415. self.url,
  416. data={"event_id": current_event, "project": -1},
  417. format="json",
  418. )
  419. assert response.status_code == 200, response.content
  420. assert len(response.data) == 3
  421. events = {item["event_id"]: item for item in response.data}
  422. assert root_event_id in events
  423. event = events[root_event_id]
  424. assert event["generation"] == 0
  425. assert event["parent_event_id"] is None
  426. assert event["parent_span_id"] is None
  427. assert current_event in events
  428. event = events[current_event]
  429. assert event["generation"] == 1
  430. assert event["parent_event_id"] == root_event_id
  431. assert event["parent_span_id"] == self.root_span_ids[0]
  432. assert child_event_id in events
  433. event = events[child_event_id]
  434. assert event["generation"] == 2
  435. assert event["parent_event_id"] == current_event
  436. assert event["parent_span_id"] == self.gen1_span_ids[0]
  437. def test_second_generation_with_children(self):
  438. self.load_trace()
  439. current_event = self.gen2_events[0].event_id
  440. child_event_id = self.gen3_event.event_id
  441. with self.feature(self.FEATURES):
  442. response = self.client.get(
  443. self.url,
  444. data={"event_id": current_event, "project": -1},
  445. format="json",
  446. )
  447. assert response.status_code == 200, response.content
  448. assert len(response.data) == 2
  449. events = {item["event_id"]: item for item in response.data}
  450. assert current_event in events
  451. event = events[current_event]
  452. # Parent/generation is unknown in this case
  453. assert event["generation"] is None
  454. assert event["parent_event_id"] is None
  455. # But we still know the parent_span
  456. assert event["parent_span_id"] == self.gen1_span_ids[0]
  457. assert child_event_id in events
  458. event = events[child_event_id]
  459. assert event["generation"] is None
  460. assert event["parent_event_id"] == current_event
  461. assert event["parent_span_id"] == self.gen2_span_id
  462. def test_third_generation_no_children(self):
  463. self.load_trace()
  464. current_event = self.gen3_event.event_id
  465. with self.feature(self.FEATURES):
  466. response = self.client.get(
  467. self.url,
  468. data={"event_id": current_event, "project": -1},
  469. format="json",
  470. )
  471. assert response.status_code == 200, response.content
  472. assert len(response.data) == 1
  473. event = response.data[0]
  474. assert event["generation"] is None
  475. # Parent is unknown in this case
  476. assert event["parent_event_id"] is None
  477. # But we still know the parent_span
  478. assert event["parent_span_id"] == self.gen2_span_id
  479. def test_sibling_transactions(self):
  480. """More than one transaction can share a parent_span_id"""
  481. self.load_trace()
  482. gen3_event_siblings = [
  483. self.create_event(
  484. trace=self.trace_id,
  485. transaction="/transaction/gen3-1",
  486. spans=[],
  487. project_id=self.create_project(organization=self.organization).id,
  488. parent_span_id=self.gen2_span_ids[1],
  489. duration=500,
  490. ).event_id,
  491. self.create_event(
  492. trace=self.trace_id,
  493. transaction="/transaction/gen3-2",
  494. spans=[],
  495. project_id=self.create_project(organization=self.organization).id,
  496. parent_span_id=self.gen2_span_ids[1],
  497. duration=525,
  498. ).event_id,
  499. ]
  500. current_event = self.gen2_events[1].event_id
  501. with self.feature(self.FEATURES):
  502. response = self.client.get(
  503. self.url,
  504. data={"event_id": current_event, "project": -1},
  505. format="json",
  506. )
  507. assert len(response.data) == 3
  508. events = {item["event_id"]: item for item in response.data}
  509. for child_event_id in gen3_event_siblings:
  510. assert child_event_id in events
  511. event = events[child_event_id]
  512. assert event["generation"] is None
  513. assert event["parent_event_id"] == current_event
  514. assert event["parent_span_id"] == self.gen2_span_ids[1]
  515. def test_with_error_event(self):
  516. self.load_trace()
  517. root_event_id = self.root_event.event_id
  518. current_transaction_event = self.gen1_events[0].event_id
  519. start, _ = self.get_start_end(1000)
  520. error_data = load_data(
  521. "javascript",
  522. timestamp=start,
  523. )
  524. error_data["contexts"]["trace"] = {
  525. "type": "trace",
  526. "trace_id": self.trace_id,
  527. "span_id": self.gen1_span_ids[0],
  528. }
  529. error_data["tags"] = [["transaction", "/transaction/gen1-0"]]
  530. error = self.store_event(error_data, project_id=self.gen1_project.id)
  531. def assertions(response):
  532. assert response.status_code == 200, response.content
  533. assert len(response.data) == 3
  534. events = {item["event_id"]: item for item in response.data}
  535. assert root_event_id in events
  536. event = events[root_event_id]
  537. assert event["generation"] == 0
  538. assert event["parent_event_id"] is None
  539. assert event["parent_span_id"] is None
  540. assert len(event["errors"]) == 0
  541. assert current_transaction_event in events
  542. event = events[current_transaction_event]
  543. assert event["generation"] == 1
  544. assert event["parent_event_id"] == root_event_id
  545. assert event["parent_span_id"] == self.root_span_ids[0]
  546. assert len(event["errors"]) == 1
  547. assert event["errors"][0]["event_id"] == error.event_id
  548. assert event["errors"][0]["issue_id"] == error.group_id
  549. with self.feature(self.FEATURES):
  550. response = self.client.get(
  551. self.url,
  552. data={"event_id": error.event_id, "project": -1},
  553. format="json",
  554. )
  555. assertions(response)
  556. with self.feature(self.FEATURES):
  557. response = self.client.get(
  558. self.url,
  559. data={"event_id": current_transaction_event, "project": -1},
  560. format="json",
  561. )
  562. assertions(response)
  563. @region_silo_test
  564. class OrganizationEventsTraceEndpointTest(OrganizationEventsTraceEndpointBase):
  565. url_name = "sentry-api-0-organization-events-trace"
  566. def assert_event(self, result, event_data, message):
  567. assert result["event_id"] == event_data.event_id, message
  568. assert result["timestamp"] == event_data.data["timestamp"], message
  569. assert result["start_timestamp"] == event_data.data["start_timestamp"], message
  570. def assert_trace_data(self, root, gen2_no_children=True):
  571. """see the setUp docstring for an idea of what the response structure looks like"""
  572. self.assert_event(root, self.root_event, "root")
  573. assert root["parent_event_id"] is None
  574. assert root["parent_span_id"] is None
  575. assert root["generation"] == 0
  576. assert root["transaction.duration"] == 3000
  577. assert len(root["children"]) == 3
  578. # TODO: Uncomment this once perf issues are fixed here
  579. # assert len(root["performance_issues"]) == 1
  580. # assert root["performance_issues"][0]["suspect_spans"][0] == self.root_span_ids[0]
  581. for i, gen1 in enumerate(root["children"]):
  582. self.assert_event(gen1, self.gen1_events[i], f"gen1_{i}")
  583. assert gen1["parent_event_id"] == self.root_event.event_id
  584. assert gen1["parent_span_id"] == self.root_span_ids[i]
  585. assert gen1["generation"] == 1
  586. assert gen1["transaction.duration"] == 2000
  587. assert len(gen1["children"]) == 1
  588. gen2 = gen1["children"][0]
  589. self.assert_event(gen2, self.gen2_events[i], f"gen2_{i}")
  590. assert gen2["parent_event_id"] == self.gen1_events[i].event_id
  591. assert gen2["parent_span_id"] == self.gen1_span_ids[i]
  592. assert gen2["generation"] == 2
  593. assert gen2["transaction.duration"] == 1000
  594. # Only the first gen2 descendent has a child
  595. if i == 0:
  596. assert len(gen2["children"]) == 1
  597. gen3 = gen2["children"][0]
  598. self.assert_event(gen3, self.gen3_event, f"gen3_{i}")
  599. assert gen3["parent_event_id"] == self.gen2_events[i].event_id
  600. assert gen3["parent_span_id"] == self.gen2_span_id
  601. assert gen3["generation"] == 3
  602. assert gen3["transaction.duration"] == 500
  603. assert len(gen3["children"]) == 0
  604. elif gen2_no_children:
  605. assert len(gen2["children"]) == 0
  606. def test_no_projects(self):
  607. user = self.create_user()
  608. org = self.create_organization(owner=user)
  609. self.login_as(user=user)
  610. url = reverse(
  611. self.url_name,
  612. kwargs={"organization_slug": org.slug, "trace_id": uuid4().hex},
  613. )
  614. with self.feature(self.FEATURES):
  615. response = self.client.get(
  616. url,
  617. format="json",
  618. )
  619. assert response.status_code == 404, response.content
  620. def test_simple(self):
  621. self.load_trace()
  622. with self.feature(self.FEATURES):
  623. response = self.client.get(
  624. self.url,
  625. data={"project": -1},
  626. format="json",
  627. )
  628. assert response.status_code == 200, response.content
  629. self.assert_trace_data(response.data[0])
  630. # We shouldn't have detailed fields here
  631. assert "transaction.status" not in response.data[0]
  632. assert "tags" not in response.data[0]
  633. assert "measurements" not in response.data[0]
  634. def test_detailed_trace(self):
  635. self.load_trace()
  636. with self.feature(self.FEATURES):
  637. response = self.client.get(
  638. self.url,
  639. data={"project": -1, "detailed": 1},
  640. format="json",
  641. )
  642. assert response.status_code == 200, response.content
  643. self.assert_trace_data(response.data[0])
  644. root = response.data[0]
  645. assert root["transaction.status"] == "ok"
  646. root_tags = {tag["key"]: tag["value"] for tag in root["tags"]}
  647. for [key, value] in self.root_event.tags:
  648. if not key.startswith("sentry:"):
  649. assert root_tags[key] == value, f"tags - {key}"
  650. else:
  651. assert root_tags[key[7:]] == value, f"tags - {key}"
  652. assert root["measurements"]["lcp"]["value"] == 1000
  653. assert root["measurements"]["fcp"]["value"] == 750
  654. # TODO: Uncomment this once perf issues are fixed here
  655. # assert "issue_short_id" in response.data[0]["performance_issues"][0]
  656. # assert response.data[0]["performance_issues"][0]["culprit"] == "/country_by_code/"
  657. # assert (
  658. # response.data[0]["performance_issues"][0]["type"]
  659. # == PerformanceFileIOMainThreadGroupType.type_id
  660. # )
  661. def test_detailed_trace_with_bad_tags(self):
  662. """Basically test that we're actually using the event serializer's method for tags"""
  663. trace = uuid4().hex
  664. self.create_event(
  665. trace=trace,
  666. transaction="bad-tags",
  667. parent_span_id=None,
  668. spans=[],
  669. project_id=self.project.id,
  670. tags=[["somethinglong" * 250, "somethinglong" * 250]],
  671. duration=3000,
  672. assert_no_errors=False,
  673. )
  674. url = reverse(
  675. self.url_name,
  676. kwargs={"organization_slug": self.project.organization.slug, "trace_id": trace},
  677. )
  678. with self.feature(self.FEATURES):
  679. response = self.client.get(
  680. url,
  681. data={"project": -1, "detailed": 1},
  682. format="json",
  683. )
  684. assert response.status_code == 200, response.content
  685. root = response.data[0]
  686. assert root["transaction.status"] == "ok"
  687. assert {"key": None, "value": None} in root["tags"]
  688. def test_bad_span_loop(self):
  689. """Maliciously create a loop in the span structure
  690. Structure then becomes something like this:
  691. root
  692. gen1-0...
  693. gen1-1
  694. gen2-1
  695. gen3-1
  696. gen_2-1
  697. gen3-1...
  698. """
  699. self.load_trace()
  700. gen3_loop_event = self.create_event(
  701. trace=self.trace_id,
  702. transaction="/transaction/gen3-1/loop",
  703. spans=[
  704. {
  705. "same_process_as_parent": True,
  706. "op": "http",
  707. "description": "GET gen2-1",
  708. "span_id": self.gen1_span_ids[1],
  709. "trace_id": self.trace_id,
  710. }
  711. ],
  712. parent_span_id=self.gen2_span_ids[1],
  713. project_id=self.project.id,
  714. )
  715. with self.feature(self.FEATURES):
  716. response = self.client.get(
  717. self.url,
  718. data={"project": -1},
  719. format="json",
  720. )
  721. assert response.status_code == 200, response.content
  722. # Should be the same as the simple testcase
  723. self.assert_trace_data(response.data[0], gen2_no_children=False)
  724. # The difference is that gen3-1 should exist with no children
  725. gen2_1 = response.data[0]["children"][1]["children"][0]
  726. assert len(gen2_1["children"]) == 1
  727. gen3_1 = gen2_1["children"][0]
  728. assert gen3_1["event_id"] == gen3_loop_event.event_id
  729. # We didn't even try to start the loop of spans
  730. assert len(gen3_1["children"]) == 0
  731. def test_bad_orphan_span_loop(self):
  732. """Maliciously create a loop in the span structure but for an orphan event"""
  733. root_span_id = uuid4().hex[:16]
  734. root_parent_span = uuid4().hex[:16]
  735. root_event = self.create_event(
  736. trace=self.trace_id,
  737. transaction="/orphan/root/",
  738. spans=[
  739. {
  740. "same_process_as_parent": True,
  741. "op": "http",
  742. "description": "GET orphan_child",
  743. "span_id": root_span_id,
  744. "trace_id": self.trace_id,
  745. }
  746. ],
  747. parent_span_id=root_parent_span,
  748. project_id=self.project.id,
  749. duration=3000,
  750. )
  751. orphan_child = self.create_event(
  752. trace=self.trace_id,
  753. transaction="/orphan/child/",
  754. spans=[
  755. {
  756. "same_process_as_parent": True,
  757. "op": "http",
  758. "description": "GET orphan_root",
  759. "span_id": root_parent_span,
  760. "trace_id": self.trace_id,
  761. }
  762. ],
  763. parent_span_id=root_span_id,
  764. project_id=self.project.id,
  765. duration=300,
  766. )
  767. with self.feature(self.FEATURES):
  768. response = self.client.get(
  769. self.url,
  770. data={"project": -1},
  771. format="json",
  772. )
  773. assert response.status_code == 200, response.content
  774. assert len(response.data) == 1
  775. # There really isn't a right answer to which orphan is the "root" since this loops, but the current
  776. # implementation will make the older event the root
  777. root = response.data[0]
  778. self.assert_event(root, root_event, "root")
  779. assert len(root["children"]) == 1
  780. child = root["children"][0]
  781. self.assert_event(child, orphan_child, "child")
  782. def test_multiple_roots(self):
  783. trace_id = uuid4().hex
  784. first_root = self.create_event(
  785. trace=trace_id,
  786. transaction="/first_root",
  787. spans=[],
  788. parent_span_id=None,
  789. project_id=self.project.id,
  790. duration=500,
  791. )
  792. second_root = self.create_event(
  793. trace=trace_id,
  794. transaction="/second_root",
  795. spans=[],
  796. parent_span_id=None,
  797. project_id=self.project.id,
  798. duration=1000,
  799. )
  800. self.url = reverse(
  801. self.url_name,
  802. kwargs={"organization_slug": self.project.organization.slug, "trace_id": trace_id},
  803. )
  804. with self.feature(self.FEATURES):
  805. response = self.client.get(
  806. self.url,
  807. data={"project": -1},
  808. format="json",
  809. )
  810. assert response.status_code == 200, response.content
  811. assert len(response.data) == 2
  812. self.assert_event(response.data[0], first_root, "first_root")
  813. self.assert_event(response.data[1], second_root, "second_root")
  814. def test_sibling_transactions(self):
  815. """More than one transaction can share a parent_span_id"""
  816. self.load_trace()
  817. gen3_event_siblings = [
  818. self.create_event(
  819. trace=self.trace_id,
  820. transaction="/transaction/gen3-1",
  821. spans=[],
  822. project_id=self.create_project(organization=self.organization).id,
  823. parent_span_id=self.gen2_span_ids[1],
  824. duration=500,
  825. ).event_id,
  826. self.create_event(
  827. trace=self.trace_id,
  828. transaction="/transaction/gen3-2",
  829. spans=[],
  830. project_id=self.create_project(organization=self.organization).id,
  831. parent_span_id=self.gen2_span_ids[1],
  832. duration=525,
  833. ).event_id,
  834. ]
  835. with self.feature(self.FEATURES):
  836. response = self.client.get(
  837. self.url,
  838. data={"project": -1},
  839. format="json",
  840. )
  841. assert response.status_code == 200, response.content
  842. # Should be the same as the simple testcase, but skip checking gen2 children
  843. self.assert_trace_data(response.data[0], gen2_no_children=False)
  844. gen2_parent = response.data[0]["children"][1]["children"][0]
  845. assert len(gen2_parent["children"]) == 2
  846. assert [child["event_id"] for child in gen2_parent["children"]] == gen3_event_siblings
  847. def test_with_orphan_siblings(self):
  848. self.load_trace()
  849. parent_span_id = uuid4().hex[:16]
  850. root_event = self.create_event(
  851. trace=self.trace_id,
  852. transaction="/orphan/root",
  853. spans=[],
  854. # Some random id so its separated from the rest of the trace
  855. parent_span_id=parent_span_id,
  856. project_id=self.project.id,
  857. # Shorter duration means that this event happened first, and should be ordered first
  858. duration=1000,
  859. )
  860. root_sibling_event = self.create_event(
  861. trace=self.trace_id,
  862. transaction="/orphan/root-sibling",
  863. spans=[],
  864. parent_span_id=parent_span_id,
  865. project_id=self.project.id,
  866. duration=1250,
  867. )
  868. with self.feature(self.FEATURES):
  869. response = self.client.get(
  870. self.url,
  871. data={"project": -1},
  872. format="json",
  873. )
  874. assert response.status_code == 200, response.content
  875. assert len(response.data) == 3
  876. # The first item of the response should be the main trace
  877. main, *orphans = response.data
  878. self.assert_trace_data(main)
  879. assert [root_event.event_id, root_sibling_event.event_id] == [
  880. orphan["event_id"] for orphan in orphans
  881. ]
  882. def test_with_orphan_trace(self):
  883. self.load_trace()
  884. orphan_span_ids = {
  885. key: uuid4().hex[:16]
  886. for key in ["root", "root_span", "child", "child_span", "grandchild", "grandchild_span"]
  887. }
  888. # Create the orphan transactions
  889. root_event = self.create_event(
  890. trace=self.trace_id,
  891. transaction="/orphan/root",
  892. spans=[
  893. {
  894. "same_process_as_parent": True,
  895. "op": "http",
  896. "description": "GET gen1 orphan",
  897. "span_id": orphan_span_ids["root_span"],
  898. "trace_id": self.trace_id,
  899. }
  900. ],
  901. # Some random id so its separated from the rest of the trace
  902. parent_span_id=uuid4().hex[:16],
  903. span_id=orphan_span_ids["root"],
  904. project_id=self.project.id,
  905. duration=1000,
  906. )
  907. child_event = self.create_event(
  908. trace=self.trace_id,
  909. transaction="/orphan/child1-0",
  910. spans=[
  911. {
  912. "same_process_as_parent": True,
  913. "op": "http",
  914. "description": "GET gen1 orphan",
  915. "span_id": orphan_span_ids["child_span"],
  916. "trace_id": self.trace_id,
  917. }
  918. ],
  919. parent_span_id=orphan_span_ids["root_span"],
  920. span_id=orphan_span_ids["child"],
  921. project_id=self.gen1_project.id,
  922. # Because the snuba query orders based is_root then timestamp, this causes grandchild1-0 to be added to
  923. # results first before child1-0
  924. duration=2500,
  925. )
  926. grandchild_event = self.create_event(
  927. trace=self.trace_id,
  928. transaction="/orphan/grandchild1-0",
  929. spans=[
  930. {
  931. "same_process_as_parent": True,
  932. "op": "http",
  933. "description": "GET gen1 orphan",
  934. "span_id": orphan_span_ids["grandchild_span"],
  935. "trace_id": self.trace_id,
  936. }
  937. ],
  938. parent_span_id=orphan_span_ids["child_span"],
  939. span_id=orphan_span_ids["grandchild"],
  940. project_id=self.gen1_project.id,
  941. duration=1500,
  942. )
  943. with self.feature(self.FEATURES):
  944. response = self.client.get(
  945. self.url,
  946. data={"project": -1},
  947. format="json",
  948. )
  949. assert response.status_code == 200, response.content
  950. assert len(response.data) == 2
  951. # The first item of the response should be the main trace
  952. main, orphans = response.data
  953. self.assert_trace_data(main)
  954. self.assert_event(orphans, root_event, "orphan-root")
  955. assert len(orphans["children"]) == 1
  956. assert orphans["generation"] == 0
  957. assert orphans["parent_event_id"] is None
  958. child = orphans["children"][0]
  959. self.assert_event(child, child_event, "orphan-child")
  960. assert len(child["children"]) == 1
  961. assert child["generation"] == 1
  962. assert child["parent_event_id"] == root_event.event_id
  963. grandchild = child["children"][0]
  964. self.assert_event(grandchild, grandchild_event, "orphan-grandchild")
  965. assert grandchild["generation"] == 2
  966. assert grandchild["parent_event_id"] == child_event.event_id
  967. def test_with_errors(self):
  968. self.load_trace()
  969. error, error1 = self.load_errors()
  970. with self.feature(self.FEATURES):
  971. response = self.client.get(
  972. self.url,
  973. data={"project": -1},
  974. format="json",
  975. )
  976. assert response.status_code == 200, response.content
  977. self.assert_trace_data(response.data[0])
  978. gen1_event = response.data[0]["children"][0]
  979. assert len(gen1_event["errors"]) == 2
  980. assert {
  981. "event_id": error.event_id,
  982. "issue_id": error.group_id,
  983. "span": self.gen1_span_ids[0],
  984. "project_id": self.gen1_project.id,
  985. "project_slug": self.gen1_project.slug,
  986. "level": "fatal",
  987. "title": error.title,
  988. } in gen1_event["errors"]
  989. assert {
  990. "event_id": error1.event_id,
  991. "issue_id": error1.group_id,
  992. "span": self.gen1_span_ids[0],
  993. "project_id": self.gen1_project.id,
  994. "project_slug": self.gen1_project.slug,
  995. "level": "warning",
  996. "title": error1.title,
  997. } in gen1_event["errors"]
  998. def test_with_default(self):
  999. self.load_trace()
  1000. start, _ = self.get_start_end(1000)
  1001. default_event = self.load_default()
  1002. with self.feature(self.FEATURES):
  1003. response = self.client.get(
  1004. self.url,
  1005. data={"project": -1},
  1006. format="json",
  1007. )
  1008. assert response.status_code == 200, response.content
  1009. self.assert_trace_data(response.data[0])
  1010. root_event = response.data[0]
  1011. assert len(root_event["errors"]) == 1
  1012. assert {
  1013. "event_id": default_event.event_id,
  1014. "issue_id": default_event.group_id,
  1015. "span": self.root_span_ids[0],
  1016. "project_id": self.gen1_project.id,
  1017. "project_slug": self.gen1_project.slug,
  1018. "level": "debug",
  1019. "title": "this is a log message",
  1020. } in root_event["errors"]
  1021. def test_pruning_root(self):
  1022. self.load_trace()
  1023. # Pruning shouldn't happen for the root event
  1024. with self.feature(self.FEATURES):
  1025. response = self.client.get(
  1026. self.url,
  1027. data={"project": -1, "event_id": self.root_event.event_id},
  1028. format="json",
  1029. )
  1030. assert response.status_code == 200, response.content
  1031. self.assert_trace_data(response.data[0])
  1032. def test_pruning_event(self):
  1033. self.load_trace()
  1034. with self.feature(self.FEATURES):
  1035. response = self.client.get(
  1036. self.url,
  1037. data={"project": -1, "event_id": self.gen2_events[0].event_id},
  1038. format="json",
  1039. )
  1040. assert response.status_code == 200, response.content
  1041. root = response.data[0]
  1042. self.assert_event(root, self.root_event, "root")
  1043. # Because of snuba query orders by timestamp we should still have all of the root's children
  1044. assert len(root["children"]) == 3
  1045. for i, gen1 in enumerate(root["children"]):
  1046. self.assert_event(gen1, self.gen1_events[i], f"gen1_{i}")
  1047. if i == 0:
  1048. assert len(gen1["children"]) == 1
  1049. gen2 = gen1["children"][0]
  1050. self.assert_event(gen2, self.gen2_events[0], "gen2_0")
  1051. assert len(gen2["children"]) == 1
  1052. gen3 = gen2["children"][0]
  1053. self.assert_event(gen3, self.gen3_event, "gen3_0")
  1054. else:
  1055. assert len(gen1["children"]) == 0
  1056. @region_silo_test
  1057. class OrganizationEventsTraceMetaEndpointTest(OrganizationEventsTraceEndpointBase):
  1058. url_name = "sentry-api-0-organization-events-trace-meta"
  1059. def test_no_projects(self):
  1060. user = self.create_user()
  1061. org = self.create_organization(owner=user)
  1062. self.login_as(user=user)
  1063. url = reverse(
  1064. self.url_name,
  1065. kwargs={"organization_slug": org.slug, "trace_id": uuid4().hex},
  1066. )
  1067. with self.feature(self.FEATURES):
  1068. response = self.client.get(
  1069. url,
  1070. format="json",
  1071. )
  1072. assert response.status_code == 404, response.content
  1073. def test_bad_ids(self):
  1074. # Fake trace id
  1075. self.url = reverse(
  1076. self.url_name,
  1077. kwargs={"organization_slug": self.project.organization.slug, "trace_id": uuid4().hex},
  1078. )
  1079. with self.feature(self.FEATURES):
  1080. response = self.client.get(
  1081. self.url,
  1082. format="json",
  1083. )
  1084. assert response.status_code == 200, response.content
  1085. data = response.data
  1086. assert data["projects"] == 0
  1087. assert data["transactions"] == 0
  1088. assert data["errors"] == 0
  1089. assert data["performance_issues"] == 0
  1090. # Invalid trace id
  1091. with pytest.raises(NoReverseMatch):
  1092. self.url = reverse(
  1093. self.url_name,
  1094. kwargs={
  1095. "organization_slug": self.project.organization.slug,
  1096. "trace_id": "not-a-trace",
  1097. },
  1098. )
  1099. def test_simple(self):
  1100. self.load_trace()
  1101. with self.feature(self.FEATURES):
  1102. response = self.client.get(
  1103. self.url,
  1104. data={"project": -1},
  1105. format="json",
  1106. )
  1107. assert response.status_code == 200, response.content
  1108. data = response.data
  1109. assert data["projects"] == 4
  1110. assert data["transactions"] == 8
  1111. assert data["errors"] == 0
  1112. assert data["performance_issues"] == 1
  1113. def test_with_errors(self):
  1114. self.load_trace()
  1115. self.load_errors()
  1116. with self.feature(self.FEATURES):
  1117. response = self.client.get(
  1118. self.url,
  1119. data={"project": -1},
  1120. format="json",
  1121. )
  1122. assert response.status_code == 200, response.content
  1123. data = response.data
  1124. assert data["projects"] == 4
  1125. assert data["transactions"] == 8
  1126. assert data["errors"] == 2
  1127. assert data["performance_issues"] == 1
  1128. def test_with_default(self):
  1129. self.load_trace()
  1130. self.load_default()
  1131. with self.feature(self.FEATURES):
  1132. response = self.client.get(
  1133. self.url,
  1134. data={"project": -1},
  1135. format="json",
  1136. )
  1137. assert response.status_code == 200, response.content
  1138. data = response.data
  1139. assert data["projects"] == 4
  1140. assert data["transactions"] == 8
  1141. assert data["errors"] == 1
  1142. assert data["performance_issues"] == 1