test_organization_events_trace.py 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  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. "organizations:trace-view-load-more",
  20. ]
  21. def get_start_end(self, duration):
  22. return self.day_ago, self.day_ago + timedelta(milliseconds=duration)
  23. def create_event(
  24. self,
  25. trace,
  26. transaction,
  27. spans,
  28. parent_span_id,
  29. project_id,
  30. tags=None,
  31. duration=4000,
  32. span_id=None,
  33. measurements=None,
  34. file_io_performance_issue=False,
  35. **kwargs,
  36. ):
  37. start, end = self.get_start_end(duration)
  38. data = load_data(
  39. "transaction",
  40. trace=trace,
  41. spans=spans,
  42. timestamp=end,
  43. start_timestamp=start,
  44. )
  45. data["transaction"] = transaction
  46. data["contexts"]["trace"]["parent_span_id"] = parent_span_id
  47. if span_id:
  48. data["contexts"]["trace"]["span_id"] = span_id
  49. if measurements:
  50. for key, value in measurements.items():
  51. data["measurements"][key]["value"] = value
  52. if tags is not None:
  53. data["tags"] = tags
  54. if file_io_performance_issue:
  55. span = data["spans"][0]
  56. if "data" not in span:
  57. span["data"] = {}
  58. span["op"] = "file.write"
  59. span["data"].update({"duration": 1, "blocked_main_thread": True})
  60. with self.feature(self.FEATURES):
  61. with mock.patch.object(
  62. PerformanceFileIOMainThreadGroupType,
  63. "noise_config",
  64. new=NoiseConfig(0, timedelta(minutes=1)),
  65. ), override_options(
  66. {
  67. "performance.issues.all.problem-detection": 1.0,
  68. "performance-file-io-main-thread-creation": 1.0,
  69. }
  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. def assert_orphan_error_response(self, response, error, span_id):
  565. assert response.status_code == 200, response.content
  566. assert response.data["transactions"] == []
  567. assert len(response.data["orphan_errors"]) == 1
  568. assert {
  569. "event_id": error.event_id,
  570. "issue_id": error.group_id,
  571. "span": span_id,
  572. "project_id": self.project.id,
  573. "project_slug": self.project.slug,
  574. "level": "fatal",
  575. "title": error.title,
  576. "timestamp": to_timestamp_from_iso_format(error.timestamp),
  577. "generation": 0,
  578. "event_type": "error",
  579. } == response.data["orphan_errors"][0]
  580. def test_with_one_orphan_error(self):
  581. self.load_trace()
  582. span_id = uuid4().hex[:16]
  583. start, _ = self.get_start_end(1000)
  584. error_data = load_data(
  585. "javascript",
  586. timestamp=start,
  587. )
  588. error_data["contexts"]["trace"] = {
  589. "type": "trace",
  590. "trace_id": self.trace_id,
  591. "span_id": span_id,
  592. }
  593. error_data["level"] = "fatal"
  594. error = self.store_event(error_data, project_id=self.project.id)
  595. with self.feature(
  596. [*self.FEATURES, "organizations:performance-tracing-without-performance"]
  597. ):
  598. response = self.client.get(
  599. self.url,
  600. data={"event_id": error.event_id, "project": -1},
  601. format="json",
  602. )
  603. self.assert_orphan_error_response(response, error, span_id)
  604. def test_with_multiple_orphan_errors(self):
  605. self.load_trace()
  606. span_id = uuid4().hex[:16]
  607. start, end = self.get_start_end(1000)
  608. error_data = load_data(
  609. "javascript",
  610. timestamp=start,
  611. )
  612. error_data["contexts"]["trace"] = {
  613. "type": "trace",
  614. "trace_id": self.trace_id,
  615. "span_id": span_id,
  616. }
  617. error_data["level"] = "fatal"
  618. error = self.store_event(error_data, project_id=self.project.id)
  619. error_data1 = load_data(
  620. "javascript",
  621. timestamp=end,
  622. )
  623. error_data1["contexts"]["trace"] = {
  624. "type": "trace",
  625. "trace_id": self.trace_id,
  626. "span_id": span_id,
  627. }
  628. error_data1["level"] = "warning"
  629. self.store_event(error_data1, project_id=self.project.id)
  630. with self.feature(
  631. [*self.FEATURES, "organizations:performance-tracing-without-performance"]
  632. ):
  633. response = self.client.get(
  634. self.url,
  635. data={"event_id": error.event_id, "project": -1},
  636. format="json",
  637. )
  638. self.assert_orphan_error_response(response, error, span_id)
  639. def test_with_unknown_event(self):
  640. with self.feature(
  641. [*self.FEATURES, "organizations:performance-tracing-without-performance"]
  642. ):
  643. response = self.client.get(
  644. self.url,
  645. data={"event_id": "766758c00ff54d8ab865369ecab53ae6", "project": "-1"},
  646. format="json",
  647. )
  648. assert response.status_code == 404
  649. @region_silo_test
  650. class OrganizationEventsTraceEndpointTest(OrganizationEventsTraceEndpointBase):
  651. url_name = "sentry-api-0-organization-events-trace"
  652. def assert_event(self, result, event_data, message):
  653. assert result["event_id"] == event_data.event_id, message
  654. assert result["timestamp"] == event_data.data["timestamp"], message
  655. assert result["start_timestamp"] == event_data.data["start_timestamp"], message
  656. def assert_trace_data(self, root, gen2_no_children=True):
  657. """see the setUp docstring for an idea of what the response structure looks like"""
  658. self.assert_event(root, self.root_event, "root")
  659. assert root["parent_event_id"] is None
  660. assert root["parent_span_id"] is None
  661. assert root["generation"] == 0
  662. assert root["transaction.duration"] == 3000
  663. assert len(root["children"]) == 3
  664. assert len(root["performance_issues"]) == 1
  665. assert root["performance_issues"][0]["suspect_spans"][0] == self.root_span_ids[0]
  666. for i, gen1 in enumerate(root["children"]):
  667. self.assert_event(gen1, self.gen1_events[i], f"gen1_{i}")
  668. assert gen1["parent_event_id"] == self.root_event.event_id
  669. assert gen1["parent_span_id"] == self.root_span_ids[i]
  670. assert gen1["generation"] == 1
  671. assert gen1["transaction.duration"] == 2000
  672. assert len(gen1["children"]) == 1
  673. gen2 = gen1["children"][0]
  674. self.assert_event(gen2, self.gen2_events[i], f"gen2_{i}")
  675. assert gen2["parent_event_id"] == self.gen1_events[i].event_id
  676. assert gen2["parent_span_id"] == self.gen1_span_ids[i]
  677. assert gen2["generation"] == 2
  678. assert gen2["transaction.duration"] == 1000
  679. # Only the first gen2 descendent has a child
  680. if i == 0:
  681. assert len(gen2["children"]) == 1
  682. gen3 = gen2["children"][0]
  683. self.assert_event(gen3, self.gen3_event, f"gen3_{i}")
  684. assert gen3["parent_event_id"] == self.gen2_events[i].event_id
  685. assert gen3["parent_span_id"] == self.gen2_span_id
  686. assert gen3["generation"] == 3
  687. assert gen3["transaction.duration"] == 500
  688. assert len(gen3["children"]) == 0
  689. elif gen2_no_children:
  690. assert len(gen2["children"]) == 0
  691. def test_no_projects(self):
  692. user = self.create_user()
  693. org = self.create_organization(owner=user)
  694. self.login_as(user=user)
  695. url = reverse(
  696. self.url_name,
  697. kwargs={"organization_slug": org.slug, "trace_id": uuid4().hex},
  698. )
  699. with self.feature(self.FEATURES):
  700. response = self.client.get(
  701. url,
  702. format="json",
  703. )
  704. assert response.status_code == 404, response.content
  705. def test_simple(self):
  706. self.load_trace()
  707. with self.feature(self.FEATURES):
  708. response = self.client.get(
  709. self.url,
  710. data={"project": -1},
  711. format="json",
  712. )
  713. assert response.status_code == 200, response.content
  714. self.assert_trace_data(response.data[0])
  715. # We shouldn't have detailed fields here
  716. assert "transaction.status" not in response.data[0]
  717. assert "tags" not in response.data[0]
  718. assert "measurements" not in response.data[0]
  719. def test_simple_with_limit(self):
  720. self.load_trace()
  721. with self.feature(self.FEATURES):
  722. response = self.client.get(
  723. self.url,
  724. data={"project": -1, "limit": 200},
  725. format="json",
  726. )
  727. assert response.status_code == 200, response.content
  728. self.assert_trace_data(response.data[0])
  729. # We shouldn't have detailed fields here
  730. assert "transaction.status" not in response.data[0]
  731. assert "tags" not in response.data[0]
  732. assert "measurements" not in response.data[0]
  733. def test_detailed_trace(self):
  734. self.load_trace()
  735. with self.feature(self.FEATURES):
  736. response = self.client.get(
  737. self.url,
  738. data={"project": -1, "detailed": 1},
  739. format="json",
  740. )
  741. assert response.status_code == 200, response.content
  742. self.assert_trace_data(response.data[0])
  743. root = response.data[0]
  744. assert root["transaction.status"] == "ok"
  745. root_tags = {tag["key"]: tag["value"] for tag in root["tags"]}
  746. for [key, value] in self.root_event.tags:
  747. if not key.startswith("sentry:"):
  748. assert root_tags[key] == value, f"tags - {key}"
  749. else:
  750. assert root_tags[key[7:]] == value, f"tags - {key}"
  751. assert root["measurements"]["lcp"]["value"] == 1000
  752. assert root["measurements"]["fcp"]["value"] == 750
  753. assert "issue_short_id" in response.data[0]["performance_issues"][0]
  754. assert response.data[0]["performance_issues"][0]["culprit"] == "root"
  755. assert (
  756. response.data[0]["performance_issues"][0]["type"]
  757. == PerformanceFileIOMainThreadGroupType.type_id
  758. )
  759. def test_detailed_trace_with_bad_tags(self):
  760. """Basically test that we're actually using the event serializer's method for tags"""
  761. trace = uuid4().hex
  762. self.create_event(
  763. trace=trace,
  764. transaction="bad-tags",
  765. parent_span_id=None,
  766. spans=[],
  767. project_id=self.project.id,
  768. tags=[["somethinglong" * 250, "somethinglong" * 250]],
  769. duration=3000,
  770. assert_no_errors=False,
  771. )
  772. url = reverse(
  773. self.url_name,
  774. kwargs={"organization_slug": self.project.organization.slug, "trace_id": trace},
  775. )
  776. with self.feature(self.FEATURES):
  777. response = self.client.get(
  778. url,
  779. data={"project": -1, "detailed": 1},
  780. format="json",
  781. )
  782. assert response.status_code == 200, response.content
  783. root = response.data[0]
  784. assert root["transaction.status"] == "ok"
  785. assert {"key": None, "value": None} in root["tags"]
  786. def test_bad_span_loop(self):
  787. """Maliciously create a loop in the span structure
  788. Structure then becomes something like this:
  789. root
  790. gen1-0...
  791. gen1-1
  792. gen2-1
  793. gen3-1
  794. gen_2-1
  795. gen3-1...
  796. """
  797. self.load_trace()
  798. gen3_loop_event = self.create_event(
  799. trace=self.trace_id,
  800. transaction="/transaction/gen3-1/loop",
  801. spans=[
  802. {
  803. "same_process_as_parent": True,
  804. "op": "http",
  805. "description": "GET gen2-1",
  806. "span_id": self.gen1_span_ids[1],
  807. "trace_id": self.trace_id,
  808. }
  809. ],
  810. parent_span_id=self.gen2_span_ids[1],
  811. project_id=self.project.id,
  812. )
  813. with self.feature(self.FEATURES):
  814. response = self.client.get(
  815. self.url,
  816. data={"project": -1},
  817. format="json",
  818. )
  819. assert response.status_code == 200, response.content
  820. # Should be the same as the simple testcase
  821. self.assert_trace_data(response.data[0], gen2_no_children=False)
  822. # The difference is that gen3-1 should exist with no children
  823. gen2_1 = response.data[0]["children"][1]["children"][0]
  824. assert len(gen2_1["children"]) == 1
  825. gen3_1 = gen2_1["children"][0]
  826. assert gen3_1["event_id"] == gen3_loop_event.event_id
  827. # We didn't even try to start the loop of spans
  828. assert len(gen3_1["children"]) == 0
  829. def test_bad_orphan_span_loop(self):
  830. """Maliciously create a loop in the span structure but for an orphan event"""
  831. root_span_id = uuid4().hex[:16]
  832. root_parent_span = uuid4().hex[:16]
  833. root_event = self.create_event(
  834. trace=self.trace_id,
  835. transaction="/orphan/root/",
  836. spans=[
  837. {
  838. "same_process_as_parent": True,
  839. "op": "http",
  840. "description": "GET orphan_child",
  841. "span_id": root_span_id,
  842. "trace_id": self.trace_id,
  843. }
  844. ],
  845. parent_span_id=root_parent_span,
  846. project_id=self.project.id,
  847. duration=3000,
  848. )
  849. orphan_child = self.create_event(
  850. trace=self.trace_id,
  851. transaction="/orphan/child/",
  852. spans=[
  853. {
  854. "same_process_as_parent": True,
  855. "op": "http",
  856. "description": "GET orphan_root",
  857. "span_id": root_parent_span,
  858. "trace_id": self.trace_id,
  859. }
  860. ],
  861. parent_span_id=root_span_id,
  862. project_id=self.project.id,
  863. duration=300,
  864. )
  865. with self.feature(self.FEATURES):
  866. response = self.client.get(
  867. self.url,
  868. data={"project": -1},
  869. format="json",
  870. )
  871. assert response.status_code == 200, response.content
  872. assert len(response.data) == 1
  873. # There really isn't a right answer to which orphan is the "root" since this loops, but the current
  874. # implementation will make the older event the root
  875. root = response.data[0]
  876. self.assert_event(root, root_event, "root")
  877. assert len(root["children"]) == 1
  878. child = root["children"][0]
  879. self.assert_event(child, orphan_child, "child")
  880. def test_multiple_roots(self):
  881. trace_id = uuid4().hex
  882. first_root = self.create_event(
  883. trace=trace_id,
  884. transaction="/first_root",
  885. spans=[],
  886. parent_span_id=None,
  887. project_id=self.project.id,
  888. duration=500,
  889. )
  890. second_root = self.create_event(
  891. trace=trace_id,
  892. transaction="/second_root",
  893. spans=[],
  894. parent_span_id=None,
  895. project_id=self.project.id,
  896. duration=1000,
  897. )
  898. self.url = reverse(
  899. self.url_name,
  900. kwargs={"organization_slug": self.project.organization.slug, "trace_id": trace_id},
  901. )
  902. with self.feature(self.FEATURES):
  903. response = self.client.get(
  904. self.url,
  905. data={"project": -1},
  906. format="json",
  907. )
  908. assert response.status_code == 200, response.content
  909. assert len(response.data) == 2
  910. self.assert_event(response.data[0], first_root, "first_root")
  911. self.assert_event(response.data[1], second_root, "second_root")
  912. def test_sibling_transactions(self):
  913. """More than one transaction can share a parent_span_id"""
  914. self.load_trace()
  915. gen3_event_siblings = [
  916. self.create_event(
  917. trace=self.trace_id,
  918. transaction="/transaction/gen3-1",
  919. spans=[],
  920. project_id=self.create_project(organization=self.organization).id,
  921. parent_span_id=self.gen2_span_ids[1],
  922. duration=500,
  923. ).event_id,
  924. self.create_event(
  925. trace=self.trace_id,
  926. transaction="/transaction/gen3-2",
  927. spans=[],
  928. project_id=self.create_project(organization=self.organization).id,
  929. parent_span_id=self.gen2_span_ids[1],
  930. duration=525,
  931. ).event_id,
  932. ]
  933. with self.feature(self.FEATURES):
  934. response = self.client.get(
  935. self.url,
  936. data={"project": -1},
  937. format="json",
  938. )
  939. assert response.status_code == 200, response.content
  940. # Should be the same as the simple testcase, but skip checking gen2 children
  941. self.assert_trace_data(response.data[0], gen2_no_children=False)
  942. gen2_parent = response.data[0]["children"][1]["children"][0]
  943. assert len(gen2_parent["children"]) == 2
  944. assert [child["event_id"] for child in gen2_parent["children"]] == gen3_event_siblings
  945. def test_with_orphan_siblings(self):
  946. self.load_trace()
  947. parent_span_id = uuid4().hex[:16]
  948. root_event = self.create_event(
  949. trace=self.trace_id,
  950. transaction="/orphan/root",
  951. spans=[],
  952. # Some random id so its separated from the rest of the trace
  953. parent_span_id=parent_span_id,
  954. project_id=self.project.id,
  955. # Shorter duration means that this event happened first, and should be ordered first
  956. duration=1000,
  957. )
  958. root_sibling_event = self.create_event(
  959. trace=self.trace_id,
  960. transaction="/orphan/root-sibling",
  961. spans=[],
  962. parent_span_id=parent_span_id,
  963. project_id=self.project.id,
  964. duration=1250,
  965. )
  966. with self.feature(self.FEATURES):
  967. response = self.client.get(
  968. self.url,
  969. data={"project": -1},
  970. format="json",
  971. )
  972. assert response.status_code == 200, response.content
  973. assert len(response.data) == 3
  974. # The first item of the response should be the main trace
  975. main, *orphans = response.data
  976. self.assert_trace_data(main)
  977. assert [root_event.event_id, root_sibling_event.event_id] == [
  978. orphan["event_id"] for orphan in orphans
  979. ]
  980. def test_with_orphan_trace(self):
  981. self.load_trace()
  982. orphan_span_ids = {
  983. key: uuid4().hex[:16]
  984. for key in ["root", "root_span", "child", "child_span", "grandchild", "grandchild_span"]
  985. }
  986. # Create the orphan transactions
  987. root_event = self.create_event(
  988. trace=self.trace_id,
  989. transaction="/orphan/root",
  990. spans=[
  991. {
  992. "same_process_as_parent": True,
  993. "op": "http",
  994. "description": "GET gen1 orphan",
  995. "span_id": orphan_span_ids["root_span"],
  996. "trace_id": self.trace_id,
  997. }
  998. ],
  999. # Some random id so its separated from the rest of the trace
  1000. parent_span_id=uuid4().hex[:16],
  1001. span_id=orphan_span_ids["root"],
  1002. project_id=self.project.id,
  1003. duration=1000,
  1004. )
  1005. child_event = self.create_event(
  1006. trace=self.trace_id,
  1007. transaction="/orphan/child1-0",
  1008. spans=[
  1009. {
  1010. "same_process_as_parent": True,
  1011. "op": "http",
  1012. "description": "GET gen1 orphan",
  1013. "span_id": orphan_span_ids["child_span"],
  1014. "trace_id": self.trace_id,
  1015. }
  1016. ],
  1017. parent_span_id=orphan_span_ids["root_span"],
  1018. span_id=orphan_span_ids["child"],
  1019. project_id=self.gen1_project.id,
  1020. # Because the snuba query orders based is_root then timestamp, this causes grandchild1-0 to be added to
  1021. # results first before child1-0
  1022. duration=2500,
  1023. )
  1024. grandchild_event = self.create_event(
  1025. trace=self.trace_id,
  1026. transaction="/orphan/grandchild1-0",
  1027. spans=[
  1028. {
  1029. "same_process_as_parent": True,
  1030. "op": "http",
  1031. "description": "GET gen1 orphan",
  1032. "span_id": orphan_span_ids["grandchild_span"],
  1033. "trace_id": self.trace_id,
  1034. }
  1035. ],
  1036. parent_span_id=orphan_span_ids["child_span"],
  1037. span_id=orphan_span_ids["grandchild"],
  1038. project_id=self.gen1_project.id,
  1039. duration=1500,
  1040. )
  1041. with self.feature(self.FEATURES):
  1042. response = self.client.get(
  1043. self.url,
  1044. data={"project": -1},
  1045. format="json",
  1046. )
  1047. assert response.status_code == 200, response.content
  1048. assert len(response.data) == 2
  1049. # The first item of the response should be the main trace
  1050. main, orphans = response.data
  1051. self.assert_trace_data(main)
  1052. self.assert_event(orphans, root_event, "orphan-root")
  1053. assert len(orphans["children"]) == 1
  1054. assert orphans["generation"] == 0
  1055. assert orphans["parent_event_id"] is None
  1056. child = orphans["children"][0]
  1057. self.assert_event(child, child_event, "orphan-child")
  1058. assert len(child["children"]) == 1
  1059. assert child["generation"] == 1
  1060. assert child["parent_event_id"] == root_event.event_id
  1061. grandchild = child["children"][0]
  1062. self.assert_event(grandchild, grandchild_event, "orphan-grandchild")
  1063. assert grandchild["generation"] == 2
  1064. assert grandchild["parent_event_id"] == child_event.event_id
  1065. def test_with_errors(self):
  1066. self.load_trace()
  1067. error, error1 = self.load_errors()
  1068. with self.feature(self.FEATURES):
  1069. response = self.client.get(
  1070. self.url,
  1071. data={"project": -1},
  1072. format="json",
  1073. )
  1074. assert response.status_code == 200, response.content
  1075. self.assert_trace_data(response.data[0])
  1076. gen1_event = response.data[0]["children"][0]
  1077. assert len(gen1_event["errors"]) == 2
  1078. assert {
  1079. "event_id": error.event_id,
  1080. "issue_id": error.group_id,
  1081. "span": self.gen1_span_ids[0],
  1082. "project_id": self.gen1_project.id,
  1083. "project_slug": self.gen1_project.slug,
  1084. "level": "fatal",
  1085. "title": error.title,
  1086. "timestamp": to_timestamp_from_iso_format(error.timestamp),
  1087. "generation": 0,
  1088. "event_type": "error",
  1089. } in gen1_event["errors"]
  1090. assert {
  1091. "event_id": error1.event_id,
  1092. "issue_id": error1.group_id,
  1093. "span": self.gen1_span_ids[0],
  1094. "project_id": self.gen1_project.id,
  1095. "project_slug": self.gen1_project.slug,
  1096. "level": "warning",
  1097. "title": error1.title,
  1098. "timestamp": to_timestamp_from_iso_format(error1.timestamp),
  1099. "generation": 0,
  1100. "event_type": "error",
  1101. } in gen1_event["errors"]
  1102. def test_with_only_orphan_errors_with_same_span_ids(self):
  1103. span_id = uuid4().hex[:16]
  1104. start, end = self.get_start_end(10000)
  1105. # Error 1
  1106. error_data = load_data(
  1107. "javascript",
  1108. timestamp=end,
  1109. )
  1110. error_data["contexts"]["trace"] = {
  1111. "type": "trace",
  1112. "trace_id": self.trace_id,
  1113. "span_id": span_id,
  1114. }
  1115. error_data["level"] = "fatal"
  1116. error = self.store_event(error_data, project_id=self.project.id)
  1117. # Error 2 before after Error 1
  1118. error_data1 = load_data(
  1119. "javascript",
  1120. timestamp=start,
  1121. )
  1122. error_data1["level"] = "warning"
  1123. error_data1["contexts"]["trace"] = {
  1124. "type": "trace",
  1125. "trace_id": self.trace_id,
  1126. "span_id": span_id,
  1127. }
  1128. error1 = self.store_event(error_data1, project_id=self.project.id)
  1129. with self.feature(
  1130. [*self.FEATURES, "organizations:performance-tracing-without-performance"]
  1131. ):
  1132. response = self.client.get(
  1133. self.url,
  1134. data={"project": -1},
  1135. format="json",
  1136. )
  1137. assert response.status_code == 200, response.content
  1138. assert len(response.data) == 2
  1139. # Sorting by timestamp puts Error1 after Error2 in the response
  1140. assert {
  1141. "event_id": error.event_id,
  1142. "issue_id": error.group_id,
  1143. "span": span_id,
  1144. "project_id": self.project.id,
  1145. "project_slug": self.project.slug,
  1146. "level": "fatal",
  1147. "title": error.title,
  1148. "timestamp": to_timestamp_from_iso_format(error.timestamp),
  1149. "generation": 0,
  1150. "event_type": "error",
  1151. } == response.data["orphan_errors"][1]
  1152. assert {
  1153. "event_id": error1.event_id,
  1154. "issue_id": error1.group_id,
  1155. "span": span_id,
  1156. "project_id": self.project.id,
  1157. "project_slug": self.project.slug,
  1158. "level": "warning",
  1159. "title": error1.title,
  1160. "timestamp": to_timestamp_from_iso_format(error1.timestamp),
  1161. "generation": 0,
  1162. "event_type": "error",
  1163. } == response.data["orphan_errors"][0]
  1164. def test_with_only_orphan_errors_with_different_span_ids(self):
  1165. start, _ = self.get_start_end(1000)
  1166. span_id = uuid4().hex[:16]
  1167. error_data = load_data(
  1168. "javascript",
  1169. timestamp=start,
  1170. )
  1171. error_data["contexts"]["trace"] = {
  1172. "type": "trace",
  1173. "trace_id": self.trace_id,
  1174. "span_id": span_id,
  1175. }
  1176. error_data["level"] = "fatal"
  1177. error = self.store_event(error_data, project_id=self.project.id)
  1178. error_data["level"] = "warning"
  1179. span_id1 = uuid4().hex[:16]
  1180. error_data["contexts"]["trace"] = {
  1181. "type": "trace",
  1182. "trace_id": self.trace_id,
  1183. "span_id": span_id1,
  1184. }
  1185. error1 = self.store_event(error_data, project_id=self.project.id)
  1186. with self.feature(
  1187. [*self.FEATURES, "organizations:performance-tracing-without-performance"]
  1188. ):
  1189. response = self.client.get(
  1190. self.url,
  1191. data={"project": -1},
  1192. format="json",
  1193. )
  1194. assert response.status_code == 200, response.content
  1195. assert len(response.data["orphan_errors"]) == 2
  1196. assert {
  1197. "event_id": error.event_id,
  1198. "issue_id": error.group_id,
  1199. "span": span_id,
  1200. "project_id": self.project.id,
  1201. "project_slug": self.project.slug,
  1202. "level": "fatal",
  1203. "title": error.title,
  1204. "timestamp": to_timestamp_from_iso_format(error.timestamp),
  1205. "generation": 0,
  1206. "event_type": "error",
  1207. } in response.data["orphan_errors"]
  1208. assert {
  1209. "event_id": error1.event_id,
  1210. "issue_id": error1.group_id,
  1211. "span": span_id1,
  1212. "project_id": self.project.id,
  1213. "project_slug": self.project.slug,
  1214. "level": "warning",
  1215. "title": error1.title,
  1216. "timestamp": to_timestamp_from_iso_format(error1.timestamp),
  1217. "generation": 0,
  1218. "event_type": "error",
  1219. } in response.data["orphan_errors"]
  1220. def test_with_mixup_of_orphan_errors_with_simple_trace_data(self):
  1221. self.load_trace()
  1222. start, _ = self.get_start_end(1000)
  1223. span_id = uuid4().hex[:16]
  1224. error_data = load_data(
  1225. "javascript",
  1226. timestamp=start,
  1227. )
  1228. error_data["contexts"]["trace"] = {
  1229. "type": "trace",
  1230. "trace_id": self.trace_id,
  1231. "span_id": span_id,
  1232. }
  1233. error_data["level"] = "fatal"
  1234. error = self.store_event(error_data, project_id=self.project.id)
  1235. error_data["level"] = "warning"
  1236. span_id1 = uuid4().hex[:16]
  1237. error_data["contexts"]["trace"] = {
  1238. "type": "trace",
  1239. "trace_id": self.trace_id,
  1240. "span_id": span_id1,
  1241. }
  1242. with self.feature(
  1243. [*self.FEATURES, "organizations:performance-tracing-without-performance"]
  1244. ):
  1245. response = self.client.get(
  1246. self.url,
  1247. data={"project": -1},
  1248. format="json",
  1249. )
  1250. assert response.status_code == 200, response.content
  1251. assert len(response.data["transactions"]) == 1
  1252. assert len(response.data["orphan_errors"]) == 1
  1253. self.assert_trace_data(response.data["transactions"][0])
  1254. assert {
  1255. "event_id": error.event_id,
  1256. "issue_id": error.group_id,
  1257. "span": span_id,
  1258. "project_id": self.project.id,
  1259. "project_slug": self.project.slug,
  1260. "level": "fatal",
  1261. "title": error.title,
  1262. "timestamp": to_timestamp_from_iso_format(error.timestamp),
  1263. "generation": 0,
  1264. "event_type": "error",
  1265. } in response.data["orphan_errors"]
  1266. def test_with_default(self):
  1267. self.load_trace()
  1268. start, _ = self.get_start_end(1000)
  1269. default_event = self.load_default()
  1270. with self.feature(self.FEATURES):
  1271. response = self.client.get(
  1272. self.url,
  1273. data={"project": -1},
  1274. format="json",
  1275. )
  1276. assert response.status_code == 200, response.content
  1277. self.assert_trace_data(response.data[0])
  1278. root_event = response.data[0]
  1279. assert len(root_event["errors"]) == 1
  1280. assert {
  1281. "event_id": default_event.event_id,
  1282. "issue_id": default_event.group_id,
  1283. "span": self.root_span_ids[0],
  1284. "project_id": self.gen1_project.id,
  1285. "project_slug": self.gen1_project.slug,
  1286. "level": "debug",
  1287. "title": "this is a log message",
  1288. "timestamp": to_timestamp_from_iso_format(default_event.timestamp),
  1289. "generation": 0,
  1290. "event_type": "error",
  1291. } in root_event["errors"]
  1292. def test_pruning_root(self):
  1293. self.load_trace()
  1294. # Pruning shouldn't happen for the root event
  1295. with self.feature(self.FEATURES):
  1296. response = self.client.get(
  1297. self.url,
  1298. data={"project": -1, "event_id": self.root_event.event_id},
  1299. format="json",
  1300. )
  1301. assert response.status_code == 200, response.content
  1302. self.assert_trace_data(response.data[0])
  1303. def test_pruning_event(self):
  1304. self.load_trace()
  1305. with self.feature(self.FEATURES):
  1306. response = self.client.get(
  1307. self.url,
  1308. data={"project": -1, "event_id": self.gen2_events[0].event_id},
  1309. format="json",
  1310. )
  1311. assert response.status_code == 200, response.content
  1312. root = response.data[0]
  1313. self.assert_event(root, self.root_event, "root")
  1314. # Because of snuba query orders by timestamp we should still have all of the root's children
  1315. assert len(root["children"]) == 3
  1316. for i, gen1 in enumerate(root["children"]):
  1317. self.assert_event(gen1, self.gen1_events[i], f"gen1_{i}")
  1318. if i == 0:
  1319. assert len(gen1["children"]) == 1
  1320. gen2 = gen1["children"][0]
  1321. self.assert_event(gen2, self.gen2_events[0], "gen2_0")
  1322. assert len(gen2["children"]) == 1
  1323. gen3 = gen2["children"][0]
  1324. self.assert_event(gen3, self.gen3_event, "gen3_0")
  1325. else:
  1326. assert len(gen1["children"]) == 0
  1327. @region_silo_test
  1328. class OrganizationEventsTraceMetaEndpointTest(OrganizationEventsTraceEndpointBase):
  1329. url_name = "sentry-api-0-organization-events-trace-meta"
  1330. def test_no_projects(self):
  1331. user = self.create_user()
  1332. org = self.create_organization(owner=user)
  1333. self.login_as(user=user)
  1334. url = reverse(
  1335. self.url_name,
  1336. kwargs={"organization_slug": org.slug, "trace_id": uuid4().hex},
  1337. )
  1338. with self.feature(self.FEATURES):
  1339. response = self.client.get(
  1340. url,
  1341. format="json",
  1342. )
  1343. assert response.status_code == 404, response.content
  1344. def test_bad_ids(self):
  1345. # Fake trace id
  1346. self.url = reverse(
  1347. self.url_name,
  1348. kwargs={"organization_slug": self.project.organization.slug, "trace_id": uuid4().hex},
  1349. )
  1350. with self.feature(self.FEATURES):
  1351. response = self.client.get(
  1352. self.url,
  1353. format="json",
  1354. )
  1355. assert response.status_code == 200, response.content
  1356. data = response.data
  1357. assert data["projects"] == 0
  1358. assert data["transactions"] == 0
  1359. assert data["errors"] == 0
  1360. assert data["performance_issues"] == 0
  1361. # Invalid trace id
  1362. with pytest.raises(NoReverseMatch):
  1363. self.url = reverse(
  1364. self.url_name,
  1365. kwargs={
  1366. "organization_slug": self.project.organization.slug,
  1367. "trace_id": "not-a-trace",
  1368. },
  1369. )
  1370. def test_simple(self):
  1371. self.load_trace()
  1372. with self.feature(self.FEATURES):
  1373. response = self.client.get(
  1374. self.url,
  1375. data={"project": -1},
  1376. format="json",
  1377. )
  1378. assert response.status_code == 200, response.content
  1379. data = response.data
  1380. assert data["projects"] == 4
  1381. assert data["transactions"] == 8
  1382. assert data["errors"] == 0
  1383. assert data["performance_issues"] == 1
  1384. def test_with_errors(self):
  1385. self.load_trace()
  1386. self.load_errors()
  1387. with self.feature(self.FEATURES):
  1388. response = self.client.get(
  1389. self.url,
  1390. data={"project": -1},
  1391. format="json",
  1392. )
  1393. assert response.status_code == 200, response.content
  1394. data = response.data
  1395. assert data["projects"] == 4
  1396. assert data["transactions"] == 8
  1397. assert data["errors"] == 2
  1398. assert data["performance_issues"] == 1
  1399. def test_with_default(self):
  1400. self.load_trace()
  1401. self.load_default()
  1402. with self.feature(self.FEATURES):
  1403. response = self.client.get(
  1404. self.url,
  1405. data={"project": -1},
  1406. format="json",
  1407. )
  1408. assert response.status_code == 200, response.content
  1409. data = response.data
  1410. assert data["projects"] == 4
  1411. assert data["transactions"] == 8
  1412. assert data["errors"] == 1
  1413. assert data["performance_issues"] == 1