test_organization_events_trace.py 45 KB

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