test_organization_events_trace.py 55 KB

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