test_organization_events_trace.py 63 KB

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