traceTree.spec.tsx 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {waitFor} from 'sentry-test/reactTestingLibrary';
  3. import type {RawSpanType} from 'sentry/components/events/interfaces/spans/types';
  4. import {EntryType, type Event, type EventTransaction} from 'sentry/types';
  5. import type {
  6. TraceFullDetailed,
  7. TraceSplitResults,
  8. } from 'sentry/utils/performance/quickTrace/types';
  9. import {TraceType} from '../traceDetails/newTraceDetailsContent';
  10. import {
  11. isAutogroupedNode,
  12. isMissingInstrumentationNode,
  13. isSpanNode,
  14. isTransactionNode,
  15. } from './guards';
  16. import {
  17. ParentAutogroupNode,
  18. type SiblingAutogroupNode,
  19. TraceTree,
  20. TraceTreeNode,
  21. } from './traceTree';
  22. function makeTrace(
  23. overrides: Partial<TraceSplitResults<TraceFullDetailed>>
  24. ): TraceSplitResults<TraceFullDetailed> {
  25. return {
  26. transactions: [],
  27. orphan_errors: [],
  28. ...overrides,
  29. } as TraceSplitResults<TraceFullDetailed>;
  30. }
  31. function makeTransaction(overrides: Partial<TraceFullDetailed> = {}): TraceFullDetailed {
  32. return {
  33. children: [],
  34. start_timestamp: 0,
  35. timestamp: 1,
  36. transaction: 'transaction',
  37. 'transaction.op': '',
  38. 'transaction.status': '',
  39. performance_issues: [],
  40. errors: [],
  41. ...overrides,
  42. } as TraceFullDetailed;
  43. }
  44. function makeSpan(overrides: Partial<RawSpanType> = {}): TraceTree.Span {
  45. return {
  46. op: '',
  47. description: '',
  48. span_id: '',
  49. start_timestamp: 0,
  50. timestamp: 10,
  51. event: makeEvent(),
  52. relatedErrors: [],
  53. childTxn: undefined,
  54. ...overrides,
  55. } as TraceTree.Span;
  56. }
  57. function makeTraceError(
  58. overrides: Partial<TraceTree.TraceError> = {}
  59. ): TraceTree.TraceError {
  60. return {
  61. title: 'MaybeEncodingError: Error sending result',
  62. level: 'error',
  63. event_type: 'error',
  64. data: {},
  65. ...overrides,
  66. } as TraceTree.TraceError;
  67. }
  68. function makeEvent(overrides: Partial<Event> = {}, spans: RawSpanType[] = []): Event {
  69. return {
  70. entries: [{type: EntryType.SPANS, data: spans}],
  71. ...overrides,
  72. } as Event;
  73. }
  74. function assertSpanNode(
  75. node: TraceTreeNode<TraceTree.NodeValue>
  76. ): asserts node is TraceTreeNode<TraceTree.Span> {
  77. if (!isSpanNode(node)) {
  78. throw new Error('node is not a span');
  79. }
  80. }
  81. // function assertTraceNode(
  82. // node: TraceTreeNode<TraceTree.NodeValue>
  83. // ): asserts node is TraceTreeNode<TraceTree.Trace> {
  84. // if (!isTraceNode(node)) {
  85. // throw new Error('node is not a trace');
  86. // }
  87. // }
  88. function assertTransactionNode(
  89. node: TraceTreeNode<TraceTree.NodeValue> | null
  90. ): asserts node is TraceTreeNode<TraceTree.Transaction> {
  91. if (!node || !isTransactionNode(node)) {
  92. throw new Error('node is not a transaction');
  93. }
  94. }
  95. function assertMissingInstrumentationNode(
  96. node: TraceTreeNode<TraceTree.NodeValue>
  97. ): asserts node is TraceTreeNode<TraceTree.MissingInstrumentationSpan> {
  98. if (!isMissingInstrumentationNode(node)) {
  99. throw new Error('node is not a missing instrumentation node');
  100. }
  101. }
  102. function assertAutogroupedNode(
  103. node: TraceTreeNode<TraceTree.NodeValue>
  104. ): asserts node is ParentAutogroupNode | SiblingAutogroupNode {
  105. if (!isAutogroupedNode(node)) {
  106. throw new Error('node is not a autogrouped node');
  107. }
  108. }
  109. function assertParentAutogroupedNode(
  110. node: TraceTreeNode<TraceTree.NodeValue>
  111. ): asserts node is ParentAutogroupNode {
  112. if (!(node instanceof ParentAutogroupNode)) {
  113. throw new Error('node is not a parent autogrouped node');
  114. }
  115. }
  116. // function _assertSiblingAutogroupedNode(
  117. // node: TraceTreeNode<TraceTree.NodeValue>
  118. // ): asserts node is ParentAutogroupNode {
  119. // if (!(node instanceof SiblingAutogroupNode)) {
  120. // throw new Error('node is not a parent node');
  121. // }
  122. // }
  123. describe('TreeNode', () => {
  124. it('expands transaction nodes by default', () => {
  125. const node = new TraceTreeNode(null, makeTransaction(), {
  126. project_slug: '',
  127. event_id: '',
  128. });
  129. expect(node.expanded).toBe(true);
  130. });
  131. it('points parent to node', () => {
  132. const root = new TraceTreeNode(null, makeTransaction(), {
  133. project_slug: '',
  134. event_id: '',
  135. });
  136. const child = new TraceTreeNode(root, makeTransaction(), {
  137. project_slug: '',
  138. event_id: '',
  139. });
  140. expect(child.parent).toBe(root);
  141. });
  142. it('depth', () => {
  143. const root = new TraceTreeNode(null, makeTransaction(), {
  144. project_slug: '',
  145. event_id: '',
  146. });
  147. const child = new TraceTreeNode(root, makeTransaction(), {
  148. project_slug: '',
  149. event_id: '',
  150. });
  151. const grandChild = new TraceTreeNode(child, makeTransaction(), {
  152. project_slug: '',
  153. event_id: '',
  154. });
  155. expect(grandChild.depth).toBe(1);
  156. });
  157. it('getVisibleChildren', () => {
  158. const root = new TraceTreeNode(null, makeTransaction(), {
  159. project_slug: '',
  160. event_id: '',
  161. });
  162. const child = new TraceTreeNode(root, makeTransaction(), {
  163. project_slug: '',
  164. event_id: '',
  165. });
  166. root.children.push(child);
  167. expect(root.getVisibleChildren()).toHaveLength(1);
  168. expect(root.getVisibleChildren()[0]).toBe(child);
  169. root.expanded = false;
  170. expect(root.getVisibleChildren()).toHaveLength(0);
  171. });
  172. it('getVisibleChildrenCount', () => {
  173. const root = new TraceTreeNode(null, makeTransaction(), {
  174. project_slug: '',
  175. event_id: '',
  176. });
  177. const child = new TraceTreeNode(root, makeTransaction(), {
  178. project_slug: '',
  179. event_id: '',
  180. });
  181. root.children.push(child);
  182. expect(root.getVisibleChildrenCount()).toBe(1);
  183. root.expanded = false;
  184. expect(root.getVisibleChildrenCount()).toBe(0);
  185. });
  186. describe('indicators', () => {
  187. it('collects indicator', () => {
  188. const tree = TraceTree.FromTrace(
  189. makeTrace({
  190. transactions: [
  191. makeTransaction({
  192. start_timestamp: 0,
  193. timestamp: 1,
  194. }),
  195. ],
  196. }),
  197. {
  198. measurements: {ttfb: {value: 0, unit: 'millisecond'}},
  199. } as unknown as EventTransaction
  200. );
  201. expect(tree.indicators.length).toBe(1);
  202. expect(tree.indicators[0].start).toBe(0);
  203. });
  204. it('converts timestamp to milliseconds', () => {
  205. const tree = TraceTree.FromTrace(
  206. makeTrace({
  207. transactions: [
  208. makeTransaction({
  209. start_timestamp: 0,
  210. timestamp: 1,
  211. }),
  212. ],
  213. }),
  214. {
  215. measurements: {
  216. ttfb: {value: 500, unit: 'millisecond'},
  217. fcp: {value: 0.5, unit: 'second'},
  218. lcp: {value: 500_000_000, unit: 'nanosecond'},
  219. },
  220. } as unknown as EventTransaction
  221. );
  222. expect(tree.indicators[0].start).toBe(500);
  223. expect(tree.indicators[1].start).toBe(500);
  224. expect(tree.indicators[2].start).toBe(500);
  225. });
  226. it('extends end timestamp to include measurement', () => {
  227. const tree = TraceTree.FromTrace(
  228. makeTrace({
  229. transactions: [
  230. makeTransaction({
  231. start_timestamp: 0,
  232. timestamp: 1,
  233. }),
  234. ],
  235. }),
  236. {
  237. measurements: {
  238. ttfb: {value: 2, unit: 'second'},
  239. },
  240. } as unknown as EventTransaction
  241. );
  242. expect(tree.root.space).toEqual([0, 2000]);
  243. });
  244. it('adjusts end and converst timestamp to ms', () => {
  245. const tree = TraceTree.FromTrace(
  246. makeTrace({
  247. transactions: [
  248. makeTransaction({
  249. start_timestamp: 0,
  250. timestamp: 1,
  251. }),
  252. ],
  253. }),
  254. {
  255. measurements: {
  256. ttfb: {value: 2000, unit: 'millisecond'},
  257. },
  258. } as unknown as EventTransaction
  259. );
  260. expect(tree.root.space).toEqual([0, 2000]);
  261. expect(tree.indicators[0].start).toBe(2000);
  262. });
  263. it('sorts measurements by start', () => {
  264. const tree = TraceTree.FromTrace(
  265. makeTrace({
  266. transactions: [
  267. makeTransaction({
  268. start_timestamp: 0,
  269. timestamp: 1,
  270. }),
  271. ],
  272. }),
  273. {
  274. measurements: {
  275. ttfb: {value: 2000, unit: 'millisecond'},
  276. lcp: {value: 1000, unit: 'millisecond'},
  277. },
  278. } as unknown as EventTransaction
  279. );
  280. expect(tree.indicators[0].start).toBe(1000);
  281. expect(tree.indicators[1].start).toBe(2000);
  282. });
  283. });
  284. describe('path', () => {
  285. describe('nested transactions', () => {
  286. let child: any = null;
  287. for (let i = 0; i < 3; i++) {
  288. const node = new TraceTreeNode(
  289. child,
  290. makeTransaction({
  291. event_id: i === 0 ? 'parent' : i === 1 ? 'child' : 'grandchild',
  292. }),
  293. {
  294. project_slug: '',
  295. event_id: '',
  296. }
  297. );
  298. child = node;
  299. }
  300. it('first txn node', () => {
  301. expect(child.parent.parent.path).toEqual(['txn:parent']);
  302. });
  303. it('leafmost node', () => {
  304. expect(child.path).toEqual(['txn:grandchild', 'txn:child', 'txn:parent']);
  305. });
  306. });
  307. it('orphan errors', () => {
  308. const tree = TraceTree.FromTrace(
  309. makeTrace({
  310. transactions: [],
  311. orphan_errors: [makeTraceError({event_id: 'error_id'})],
  312. })
  313. );
  314. expect(tree.list[1].path).toEqual(['error:error_id']);
  315. });
  316. describe('spans', () => {
  317. const tree = TraceTree.FromTrace(
  318. makeTrace({
  319. transactions: [
  320. makeTransaction({
  321. transaction: '/',
  322. project_slug: 'project',
  323. event_id: 'event_id',
  324. }),
  325. ],
  326. })
  327. );
  328. MockApiClient.addMockResponse({
  329. url: '/organizations/org-slug/events/project:event_id/',
  330. method: 'GET',
  331. body: makeEvent({}, [
  332. makeSpan({
  333. description: 'span',
  334. op: 'db',
  335. span_id: 'span',
  336. start_timestamp: 0,
  337. timestamp: 1,
  338. }),
  339. makeSpan({
  340. description: 'span',
  341. op: 'db',
  342. span_id: 'span',
  343. start_timestamp: 1.5,
  344. timestamp: 2,
  345. }),
  346. ]),
  347. });
  348. tree.zoomIn(tree.list[1], true, {
  349. api: new MockApiClient(),
  350. organization: OrganizationFixture(),
  351. });
  352. it('when span is a child of a txn', async () => {
  353. await waitFor(() => {
  354. expect(tree.list.length).toBe(5);
  355. });
  356. expect(tree.list[tree.list.length - 1].path).toEqual([
  357. 'span:span',
  358. 'txn:event_id',
  359. ]);
  360. });
  361. it('missing instrumentation', () => {
  362. expect(tree.list[3].path).toEqual(['ms:span', 'txn:event_id']);
  363. });
  364. });
  365. describe('autogrouped children', () => {
  366. const tree = TraceTree.FromTrace(
  367. makeTrace({
  368. transactions: [
  369. makeTransaction({
  370. transaction: '/',
  371. project_slug: 'project',
  372. event_id: 'event_id',
  373. }),
  374. ],
  375. })
  376. );
  377. MockApiClient.addMockResponse({
  378. url: '/organizations/org-slug/events/project:event_id/',
  379. method: 'GET',
  380. body: makeEvent({}, [
  381. makeSpan({description: 'span', op: 'db', span_id: '2'}),
  382. makeSpan({description: 'span', op: 'db', span_id: '3', parent_span_id: '2'}),
  383. makeSpan({description: 'span', op: 'db', span_id: '4', parent_span_id: '3'}),
  384. makeSpan({description: 'span', op: 'db', span_id: '5', parent_span_id: '4'}),
  385. ]),
  386. });
  387. tree.zoomIn(tree.list[1], true, {
  388. api: new MockApiClient(),
  389. organization: OrganizationFixture(),
  390. });
  391. it('autogrouped node', async () => {
  392. await waitFor(() => {
  393. expect(tree.list.length).toBe(3);
  394. });
  395. tree.expand(tree.list[2], true);
  396. assertAutogroupedNode(tree.list[2]);
  397. expect(tree.list[2].path).toEqual(['ag:2', 'txn:event_id']);
  398. });
  399. it('child is part of autogrouping', () => {
  400. expect(tree.list[tree.list.length - 1].path).toEqual([
  401. 'span:5',
  402. 'ag:2',
  403. 'txn:event_id',
  404. ]);
  405. });
  406. });
  407. describe('non expanded direct children autogrouped path', () => {
  408. const tree = TraceTree.FromTrace(
  409. makeTrace({
  410. transactions: [
  411. makeTransaction({
  412. transaction: '/',
  413. project_slug: 'project',
  414. event_id: 'event_id',
  415. }),
  416. ],
  417. })
  418. );
  419. MockApiClient.addMockResponse({
  420. url: '/organizations/org-slug/events/project:event_id/',
  421. method: 'GET',
  422. body: makeEvent({}, [
  423. makeSpan({description: 'span', op: 'db', span_id: '2'}),
  424. makeSpan({description: 'span', op: 'db', span_id: '3', parent_span_id: '2'}),
  425. makeSpan({description: 'span', op: 'db', span_id: '4', parent_span_id: '3'}),
  426. makeSpan({description: 'span', op: 'db', span_id: '5', parent_span_id: '4'}),
  427. makeSpan({description: 'span', op: '6', span_id: '6', parent_span_id: '5'}),
  428. ]),
  429. });
  430. tree.zoomIn(tree.list[1], true, {
  431. api: new MockApiClient(),
  432. organization: OrganizationFixture(),
  433. });
  434. it('autogrouped node', async () => {
  435. await waitFor(() => {
  436. expect(tree.list.length).toBe(4);
  437. });
  438. assertAutogroupedNode(tree.list[2]);
  439. expect(tree.list[2].path).toEqual(['ag:2', 'txn:event_id']);
  440. });
  441. it('span node skips autogrouped node because it is not expanded', async () => {
  442. await waitFor(() => {
  443. expect(tree.list.length).toBe(4);
  444. });
  445. expect(tree.list[tree.list.length - 1].path).toEqual(['span:6', 'txn:event_id']);
  446. });
  447. });
  448. it.todo('sibling autogrouped node paths');
  449. it.todo('nested transactions autogrouped node paths');
  450. });
  451. });
  452. describe('TraceTree', () => {
  453. beforeEach(() => {
  454. MockApiClient.clearMockResponses();
  455. });
  456. it('builds from transactions', () => {
  457. const tree = TraceTree.FromTrace(
  458. makeTrace({
  459. transactions: [
  460. makeTransaction({
  461. children: [],
  462. }),
  463. makeTransaction({
  464. children: [],
  465. }),
  466. ],
  467. })
  468. );
  469. expect(tree.list).toHaveLength(3);
  470. });
  471. it('builds orphan errors as well', () => {
  472. const tree = TraceTree.FromTrace(
  473. makeTrace({
  474. transactions: [
  475. makeTransaction({
  476. children: [],
  477. }),
  478. makeTransaction({
  479. children: [],
  480. }),
  481. ],
  482. orphan_errors: [makeTraceError()],
  483. })
  484. );
  485. expect(tree.list).toHaveLength(4);
  486. });
  487. it('adjusts trace bounds by orphan error timestamp as well', () => {
  488. const tree = TraceTree.FromTrace(
  489. makeTrace({
  490. transactions: [
  491. makeTransaction({
  492. start_timestamp: 0.1,
  493. timestamp: 0.15,
  494. children: [],
  495. }),
  496. makeTransaction({
  497. start_timestamp: 0.2,
  498. timestamp: 0.25,
  499. children: [],
  500. }),
  501. ],
  502. orphan_errors: [
  503. makeTraceError({timestamp: 0.05}),
  504. makeTraceError({timestamp: 0.3}),
  505. ],
  506. })
  507. );
  508. expect(tree.list).toHaveLength(5);
  509. expect(tree.root.space).toStrictEqual([
  510. 0.05 * tree.root.multiplier,
  511. (0.3 - 0.05) * tree.root.multiplier,
  512. ]);
  513. });
  514. it('calculates correct trace type', () => {
  515. let tree = TraceTree.FromTrace(
  516. makeTrace({
  517. transactions: [],
  518. orphan_errors: [],
  519. })
  520. );
  521. expect(TraceTree.GetTraceType(tree.root)).toBe(TraceType.EMPTY_TRACE);
  522. tree = TraceTree.FromTrace(
  523. makeTrace({
  524. transactions: [
  525. makeTransaction({
  526. children: [],
  527. }),
  528. makeTransaction({
  529. children: [],
  530. }),
  531. ],
  532. orphan_errors: [],
  533. })
  534. );
  535. expect(TraceTree.GetTraceType(tree.root)).toBe(TraceType.NO_ROOT);
  536. tree = TraceTree.FromTrace(
  537. makeTrace({
  538. transactions: [
  539. makeTransaction({
  540. parent_span_id: null,
  541. children: [],
  542. }),
  543. ],
  544. orphan_errors: [],
  545. })
  546. );
  547. expect(TraceTree.GetTraceType(tree.root)).toBe(TraceType.ONE_ROOT);
  548. tree = TraceTree.FromTrace(
  549. makeTrace({
  550. transactions: [
  551. makeTransaction({
  552. parent_span_id: null,
  553. children: [],
  554. }),
  555. makeTransaction({
  556. children: [],
  557. }),
  558. ],
  559. orphan_errors: [],
  560. })
  561. );
  562. expect(TraceTree.GetTraceType(tree.root)).toBe(TraceType.BROKEN_SUBTRACES);
  563. tree = TraceTree.FromTrace(
  564. makeTrace({
  565. transactions: [
  566. makeTransaction({
  567. parent_span_id: null,
  568. children: [],
  569. }),
  570. makeTransaction({
  571. parent_span_id: null,
  572. children: [],
  573. }),
  574. ],
  575. orphan_errors: [],
  576. })
  577. );
  578. expect(TraceTree.GetTraceType(tree.root)).toBe(TraceType.MULTIPLE_ROOTS);
  579. tree = TraceTree.FromTrace(
  580. makeTrace({
  581. transactions: [],
  582. orphan_errors: [makeTraceError()],
  583. })
  584. );
  585. expect(TraceTree.GetTraceType(tree.root)).toBe(TraceType.ONLY_ERRORS);
  586. });
  587. it('builds from spans when root is a transaction node', () => {
  588. const root = new TraceTreeNode(
  589. null,
  590. makeTransaction({
  591. children: [],
  592. }),
  593. {project_slug: '', event_id: ''}
  594. );
  595. const node = TraceTree.FromSpans(
  596. root,
  597. makeEvent(),
  598. [
  599. makeSpan({start_timestamp: 0, op: '1', span_id: '1'}),
  600. makeSpan({start_timestamp: 1, op: '2', span_id: '2', parent_span_id: '1'}),
  601. makeSpan({start_timestamp: 2, op: '3', span_id: '3', parent_span_id: '2'}),
  602. makeSpan({start_timestamp: 3, op: '4', span_id: '4', parent_span_id: '1'}),
  603. ],
  604. {sdk: undefined}
  605. );
  606. if (!isSpanNode(node.children[0])) {
  607. throw new Error('Child needs to be a span');
  608. }
  609. expect(node.children[0].value.span_id).toBe('1');
  610. expect(node.children[0].value.start_timestamp).toBe(0);
  611. expect(node.children.length).toBe(1);
  612. assertSpanNode(node.children[0].children[0]);
  613. assertSpanNode(node.children[0].children[0].children[0]);
  614. assertSpanNode(node.children[0].children[1]);
  615. expect(node.children[0].children[0].value.start_timestamp).toBe(1);
  616. expect(node.children[0].children[0].children[0].value.start_timestamp).toBe(2);
  617. expect(node.children[0].children[1].value.start_timestamp).toBe(3);
  618. });
  619. it('builds from spans and copies txn nodes', () => {
  620. // transaction transaction
  621. // - child transaction -> - span
  622. // - span
  623. // - child-transaction
  624. // - span
  625. const root = new TraceTreeNode(
  626. null,
  627. makeTransaction({
  628. children: [],
  629. }),
  630. {project_slug: '', event_id: ''}
  631. );
  632. root.children.push(
  633. new TraceTreeNode(
  634. root,
  635. makeTransaction({
  636. parent_span_id: 'child-transaction',
  637. }),
  638. {project_slug: '', event_id: ''}
  639. )
  640. );
  641. const node = TraceTree.FromSpans(
  642. root,
  643. makeEvent(),
  644. [
  645. makeSpan({start_timestamp: 0, timestamp: 0.1, op: 'span', span_id: 'none'}),
  646. makeSpan({
  647. start_timestamp: 0.1,
  648. timestamp: 0.2,
  649. op: 'child-transaction',
  650. span_id: 'child-transaction',
  651. }),
  652. makeSpan({start_timestamp: 0.2, timestamp: 0.25, op: 'span', span_id: 'none'}),
  653. ],
  654. {sdk: undefined}
  655. );
  656. assertSpanNode(node.children[1]);
  657. assertTransactionNode(node.children[1].children[0]);
  658. });
  659. it('builds from spans and copies txn nodes to nested children', () => {
  660. // parent transaction parent transaction
  661. // - child transaction -> - span
  662. // - grandchild transaction -> - child-transaction
  663. // - grandchild-transaction
  664. //
  665. const root = new TraceTreeNode(
  666. null,
  667. makeTransaction({
  668. span_id: 'parent-transaction',
  669. children: [],
  670. }),
  671. {project_slug: '', event_id: ''}
  672. );
  673. let start: TraceTreeNode<TraceTree.NodeValue> = root;
  674. for (let i = 0; i < 2; i++) {
  675. const node = new TraceTreeNode(
  676. start,
  677. makeTransaction({
  678. transaction: `${i === 0 ? 'child' : 'grandchild'}-transaction`,
  679. parent_span_id: `${i === 0 ? 'child' : 'grandchild'}-transaction`,
  680. }),
  681. {project_slug: '', event_id: ''}
  682. );
  683. start.children.push(node);
  684. start = node;
  685. }
  686. const node = TraceTree.FromSpans(
  687. root,
  688. makeEvent(),
  689. [
  690. makeSpan({start_timestamp: 0, timestamp: 0.1, op: 'span', span_id: 'none'}),
  691. makeSpan({
  692. start_timestamp: 0.1,
  693. timestamp: 0.2,
  694. op: 'child-transaction',
  695. span_id: 'child-transaction',
  696. }),
  697. ],
  698. {sdk: undefined}
  699. );
  700. assertSpanNode(node.children[1]);
  701. assertTransactionNode(node.children[1].children[0]);
  702. assertTransactionNode(node.children[1].children[0].children[0]);
  703. });
  704. it('injects missing spans', () => {
  705. const root = new TraceTreeNode(
  706. null,
  707. makeTransaction({
  708. children: [],
  709. }),
  710. {project_slug: '', event_id: ''}
  711. );
  712. const date = new Date().getTime();
  713. const node = TraceTree.FromSpans(
  714. root,
  715. makeEvent(),
  716. [
  717. makeSpan({
  718. start_timestamp: date,
  719. timestamp: date + 1,
  720. span_id: '1',
  721. op: 'span 1',
  722. }),
  723. makeSpan({
  724. start_timestamp: date + 2,
  725. timestamp: date + 4,
  726. op: 'span 2',
  727. span_id: '2',
  728. }),
  729. ],
  730. {sdk: undefined}
  731. );
  732. assertSpanNode(node.children[0]);
  733. assertMissingInstrumentationNode(node.children[1]);
  734. assertSpanNode(node.children[2]);
  735. expect(node.children.length).toBe(3);
  736. expect(node.children[0].value.op).toBe('span 1');
  737. expect(node.children[1].value.type).toBe('missing_instrumentation');
  738. expect(node.children[2].value.op).toBe('span 2');
  739. });
  740. it('does not inject missing spans for javascript platform', () => {
  741. const root = new TraceTreeNode(
  742. null,
  743. makeTransaction({
  744. children: [],
  745. }),
  746. {project_slug: '', event_id: ''}
  747. );
  748. const date = new Date().getTime();
  749. const node = TraceTree.FromSpans(
  750. root,
  751. makeEvent(),
  752. [
  753. makeSpan({
  754. start_timestamp: date,
  755. timestamp: date + 1,
  756. span_id: '1',
  757. op: 'span 1',
  758. }),
  759. makeSpan({
  760. start_timestamp: date + 2,
  761. timestamp: date + 4,
  762. op: 'span 2',
  763. span_id: '2',
  764. }),
  765. ],
  766. {sdk: 'sentry.javascript.browser'}
  767. );
  768. assertSpanNode(node.children[0]);
  769. assertSpanNode(node.children[1]);
  770. expect(node.children.length).toBe(2);
  771. expect(node.children[0].value.op).toBe('span 1');
  772. expect(node.children[1].value.op).toBe('span 2');
  773. });
  774. it('builds and preserves list order', async () => {
  775. const organization = OrganizationFixture();
  776. const api = new MockApiClient();
  777. const tree = TraceTree.FromTrace(
  778. makeTrace({
  779. transactions: [
  780. makeTransaction({
  781. transaction: 'txn 1',
  782. start_timestamp: 0,
  783. children: [makeTransaction({start_timestamp: 1, transaction: 'txn 2'})],
  784. }),
  785. ],
  786. })
  787. );
  788. tree.expand(tree.list[0], true);
  789. const node = tree.list[1];
  790. const request = MockApiClient.addMockResponse({
  791. url: '/organizations/org-slug/events/undefined:undefined/',
  792. method: 'GET',
  793. body: makeEvent({startTimestamp: 0}, [
  794. makeSpan({start_timestamp: 1, op: 'span 1', span_id: '1'}),
  795. makeSpan({
  796. start_timestamp: 2,
  797. op: 'span 2',
  798. span_id: '2',
  799. parent_span_id: '1',
  800. }),
  801. makeSpan({start_timestamp: 3, op: 'span 3', parent_span_id: '2'}),
  802. makeSpan({start_timestamp: 4, op: 'span 4', parent_span_id: '1'}),
  803. ]),
  804. });
  805. // 0
  806. // 1
  807. // 2
  808. // 3
  809. // 4
  810. tree.zoomIn(node, true, {api, organization});
  811. await waitFor(() => {
  812. expect(node.zoomedIn).toBe(true);
  813. });
  814. expect(request).toHaveBeenCalled();
  815. expect(tree.list.length).toBe(6);
  816. assertTransactionNode(tree.list[1]);
  817. assertSpanNode(tree.list[2]);
  818. assertSpanNode(tree.list[3]);
  819. expect(tree.list[1].value.start_timestamp).toBe(0);
  820. expect(tree.list[2].value.start_timestamp).toBe(1);
  821. expect(tree.list[3].value.start_timestamp).toBe(2);
  822. });
  823. it('preserves input order', () => {
  824. const firstChild = makeTransaction({
  825. start_timestamp: 0,
  826. timestamp: 1,
  827. children: [],
  828. });
  829. const secondChild = makeTransaction({
  830. start_timestamp: 1,
  831. timestamp: 2,
  832. children: [],
  833. });
  834. const tree = TraceTree.FromTrace(
  835. makeTrace({
  836. transactions: [
  837. makeTransaction({
  838. start_timestamp: 0,
  839. timestamp: 2,
  840. children: [firstChild, secondChild],
  841. }),
  842. makeTransaction({
  843. start_timestamp: 2,
  844. timestamp: 4,
  845. }),
  846. ],
  847. })
  848. );
  849. expect(tree.list).toHaveLength(5);
  850. expect(tree.expand(tree.list[1], false)).toBe(true);
  851. expect(tree.list).toHaveLength(3);
  852. expect(tree.expand(tree.list[1], true)).toBe(true);
  853. expect(tree.list).toHaveLength(5);
  854. expect(tree.list[2].value).toBe(firstChild);
  855. expect(tree.list[3].value).toBe(secondChild);
  856. });
  857. it('creates children -> parent references', () => {
  858. const tree = TraceTree.FromTrace(
  859. makeTrace({
  860. transactions: [
  861. makeTransaction({
  862. start_timestamp: 0,
  863. timestamp: 2,
  864. children: [makeTransaction({start_timestamp: 1, timestamp: 2})],
  865. }),
  866. makeTransaction({
  867. start_timestamp: 2,
  868. timestamp: 4,
  869. }),
  870. ],
  871. })
  872. );
  873. expect(tree.list).toHaveLength(4);
  874. expect(tree.list[2].parent?.value).toBe(tree.list[1].value);
  875. });
  876. it('establishes parent-child relationships', () => {
  877. const tree = TraceTree.FromTrace(
  878. makeTrace({
  879. transactions: [
  880. makeTransaction({
  881. children: [makeTransaction()],
  882. }),
  883. ],
  884. })
  885. );
  886. expect(tree.root.children).toHaveLength(1);
  887. expect(tree.root.children[0].children).toHaveLength(1);
  888. });
  889. it('isLastChild', () => {
  890. const tree = TraceTree.FromTrace(
  891. makeTrace({
  892. transactions: [
  893. makeTransaction({
  894. children: [makeTransaction(), makeTransaction()],
  895. }),
  896. makeTransaction(),
  897. ],
  898. orphan_errors: [],
  899. })
  900. );
  901. tree.expand(tree.list[1], true);
  902. expect(tree.list[0].isLastChild).toBe(true);
  903. expect(tree.list[1].isLastChild).toBe(false);
  904. expect(tree.list[2].isLastChild).toBe(false);
  905. expect(tree.list[3].isLastChild).toBe(true);
  906. expect(tree.list[4].isLastChild).toBe(true);
  907. });
  908. describe('connectors', () => {
  909. it('computes transaction connectors', () => {
  910. const tree = TraceTree.FromTrace(
  911. makeTrace({
  912. transactions: [
  913. makeTransaction({
  914. transaction: 'sibling',
  915. children: [
  916. makeTransaction({transaction: 'child'}),
  917. makeTransaction({transaction: 'child'}),
  918. ],
  919. }),
  920. makeTransaction({transaction: 'sibling'}),
  921. ],
  922. })
  923. );
  924. // -1 root
  925. // ------ list begins here
  926. // 0 transaction
  927. // 0 |- sibling
  928. // -1, 2| | - child
  929. // -1| | - child
  930. // 0 |- sibling
  931. tree.expand(tree.list[1], true);
  932. expect(tree.list.length).toBe(5);
  933. expect(tree.list[0].connectors.length).toBe(0);
  934. expect(tree.list[1].connectors.length).toBe(1);
  935. expect(tree.list[1].connectors[0]).toBe(-1);
  936. expect(tree.list[2].connectors[0]).toBe(-1);
  937. expect(tree.list[2].connectors[1]).toBe(2);
  938. expect(tree.list[2].connectors.length).toBe(2);
  939. expect(tree.list[3].connectors[0]).toBe(-1);
  940. expect(tree.list[3].connectors.length).toBe(1);
  941. expect(tree.list[4].connectors.length).toBe(0);
  942. });
  943. it('computes span connectors', async () => {
  944. const tree = TraceTree.FromTrace(
  945. makeTrace({
  946. transactions: [
  947. makeTransaction({
  948. project_slug: 'project',
  949. event_id: 'event_id',
  950. transaction: 'transaction',
  951. children: [],
  952. }),
  953. ],
  954. })
  955. );
  956. // root
  957. // |- node1 []
  958. // |- node2 []
  959. MockApiClient.addMockResponse({
  960. url: '/organizations/org-slug/events/project:event_id/',
  961. method: 'GET',
  962. body: makeEvent({}, [makeSpan({start_timestamp: 0, op: 'span', span_id: '1'})]),
  963. });
  964. expect(tree.list.length).toBe(2);
  965. tree.zoomIn(tree.list[1], true, {
  966. api: new MockApiClient(),
  967. organization: OrganizationFixture(),
  968. });
  969. await waitFor(() => {
  970. expect(tree.list.length).toBe(3);
  971. });
  972. // root
  973. // |- node1 []
  974. // |- node2 []
  975. // |- span1 []
  976. const span = tree.list[tree.list.length - 1];
  977. expect(span.connectors.length).toBe(0);
  978. });
  979. });
  980. describe('expanding', () => {
  981. it('expands a node and updates the list', () => {
  982. const tree = TraceTree.FromTrace(
  983. makeTrace({transactions: [makeTransaction({children: [makeTransaction()]})]})
  984. );
  985. const node = tree.list[1];
  986. expect(tree.expand(node, false)).toBe(true);
  987. expect(tree.list.length).toBe(2);
  988. expect(node.expanded).toBe(false);
  989. expect(tree.expand(node, true)).toBe(true);
  990. expect(node.expanded).toBe(true);
  991. // Assert that the list has been updated
  992. expect(tree.list).toHaveLength(3);
  993. expect(tree.list[2]).toBe(node.children[0]);
  994. });
  995. it('collapses a node and updates the list', () => {
  996. const tree = TraceTree.FromTrace(
  997. makeTrace({transactions: [makeTransaction({children: [makeTransaction()]})]})
  998. );
  999. const node = tree.list[1];
  1000. tree.expand(node, true);
  1001. expect(tree.list.length).toBe(3);
  1002. expect(tree.expand(node, false)).toBe(true);
  1003. expect(node.expanded).toBe(false);
  1004. // Assert that the list has been updated
  1005. expect(tree.list).toHaveLength(2);
  1006. expect(tree.list[1]).toBe(node);
  1007. });
  1008. it('preserves children expanded state', () => {
  1009. const tree = TraceTree.FromTrace(
  1010. makeTrace({
  1011. transactions: [
  1012. makeTransaction({
  1013. children: [
  1014. makeTransaction({children: [makeTransaction({start_timestamp: 1000})]}),
  1015. makeTransaction({start_timestamp: 5}),
  1016. ],
  1017. }),
  1018. ],
  1019. })
  1020. );
  1021. expect(tree.expand(tree.list[2], false)).toBe(true);
  1022. // Assert that the list has been updated
  1023. expect(tree.list).toHaveLength(4);
  1024. expect(tree.expand(tree.list[2], true)).toBe(true);
  1025. expect(tree.list.length).toBe(5);
  1026. expect(tree.list[tree.list.length - 1].value).toEqual(
  1027. makeTransaction({start_timestamp: 5})
  1028. );
  1029. });
  1030. it('expanding or collapsing a zoomed in node doesnt do anything', async () => {
  1031. const organization = OrganizationFixture();
  1032. const api = new MockApiClient();
  1033. const tree = TraceTree.FromTrace(
  1034. makeTrace({transactions: [makeTransaction({children: [makeTransaction()]})]})
  1035. );
  1036. const node = tree.list[0];
  1037. const request = MockApiClient.addMockResponse({
  1038. url: '/organizations/org-slug/events/undefined:undefined/',
  1039. method: 'GET',
  1040. body: makeEvent(),
  1041. });
  1042. tree.zoomIn(node, true, {api, organization});
  1043. await waitFor(() => {
  1044. expect(node.zoomedIn).toBe(true);
  1045. });
  1046. expect(request).toHaveBeenCalled();
  1047. expect(tree.expand(node, true)).toBe(false);
  1048. });
  1049. });
  1050. describe('zooming', () => {
  1051. it('marks node as zoomed in', async () => {
  1052. const organization = OrganizationFixture();
  1053. const api = new MockApiClient();
  1054. const tree = TraceTree.FromTrace(
  1055. makeTrace({
  1056. transactions: [
  1057. makeTransaction({project_slug: 'project', event_id: 'event_id'}),
  1058. ],
  1059. })
  1060. );
  1061. const request = MockApiClient.addMockResponse({
  1062. url: '/organizations/org-slug/events/project:event_id/',
  1063. method: 'GET',
  1064. body: makeEvent(),
  1065. });
  1066. const node = tree.list[1];
  1067. expect(node.zoomedIn).toBe(false);
  1068. tree.zoomIn(node, true, {api, organization});
  1069. await waitFor(() => {
  1070. expect(node.zoomedIn).toBe(true);
  1071. });
  1072. expect(request).toHaveBeenCalled();
  1073. });
  1074. it('fetches spans for node when zooming in', async () => {
  1075. const tree = TraceTree.FromTrace(
  1076. makeTrace({
  1077. transactions: [
  1078. makeTransaction({
  1079. transaction: 'txn',
  1080. project_slug: 'project',
  1081. event_id: 'event_id',
  1082. }),
  1083. ],
  1084. })
  1085. );
  1086. const request = MockApiClient.addMockResponse({
  1087. url: '/organizations/org-slug/events/project:event_id/',
  1088. method: 'GET',
  1089. body: makeEvent({}, [makeSpan()]),
  1090. });
  1091. const node = tree.list[1];
  1092. expect(node.children).toHaveLength(0);
  1093. tree.zoomIn(node, true, {
  1094. api: new MockApiClient(),
  1095. organization: OrganizationFixture(),
  1096. });
  1097. expect(request).toHaveBeenCalled();
  1098. await waitFor(() => {
  1099. expect(node.children).toHaveLength(1);
  1100. });
  1101. // Assert that the children have been updated
  1102. assertTransactionNode(node.children[0].parent);
  1103. expect(node.children[0].parent.value.transaction).toBe('txn');
  1104. expect(node.children[0].depth).toBe(node.depth + 1);
  1105. });
  1106. it('handles bottom up zooming', async () => {
  1107. const tree = TraceTree.FromTrace(
  1108. makeTrace({
  1109. transactions: [
  1110. makeTransaction({
  1111. transaction: 'transaction',
  1112. project_slug: 'project',
  1113. event_id: 'event_id',
  1114. children: [
  1115. makeTransaction({
  1116. parent_span_id: 'span',
  1117. transaction: 'child transaction',
  1118. project_slug: 'child_project',
  1119. event_id: 'child_event_id',
  1120. }),
  1121. ],
  1122. }),
  1123. ],
  1124. })
  1125. );
  1126. const first_request = MockApiClient.addMockResponse({
  1127. url: '/organizations/org-slug/events/project:event_id/',
  1128. method: 'GET',
  1129. body: makeEvent({}, [makeSpan({op: 'db', span_id: 'span'})]),
  1130. });
  1131. const second_request = MockApiClient.addMockResponse({
  1132. url: '/organizations/org-slug/events/child_project:child_event_id/',
  1133. method: 'GET',
  1134. body: makeEvent({}, [
  1135. makeSpan({op: 'db', span_id: 'span'}),
  1136. makeSpan({op: 'db', span_id: 'span 1', parent_span_id: 'span'}),
  1137. makeSpan({op: 'db', span_id: 'span 2', parent_span_id: 'span 1'}),
  1138. makeSpan({op: 'db', span_id: 'span 3', parent_span_id: 'span 2'}),
  1139. makeSpan({op: 'db', span_id: 'span 4', parent_span_id: 'span 3'}),
  1140. makeSpan({op: 'db', span_id: 'span 5', parent_span_id: 'span 4'}),
  1141. ]),
  1142. });
  1143. tree.zoomIn(tree.list[2], true, {
  1144. api: new MockApiClient(),
  1145. organization: OrganizationFixture(),
  1146. });
  1147. await waitFor(() => {
  1148. expect(second_request).toHaveBeenCalled();
  1149. });
  1150. assertParentAutogroupedNode(tree.list[tree.list.length - 1]);
  1151. tree.zoomIn(tree.list[1], true, {
  1152. api: new MockApiClient(),
  1153. organization: OrganizationFixture(),
  1154. });
  1155. await waitFor(() => {
  1156. expect(first_request).toHaveBeenCalled();
  1157. });
  1158. assertParentAutogroupedNode(tree.list[tree.list.length - 1]);
  1159. });
  1160. it('zooms out', async () => {
  1161. const tree = TraceTree.FromTrace(
  1162. makeTrace({
  1163. transactions: [
  1164. makeTransaction({project_slug: 'project', event_id: 'event_id'}),
  1165. ],
  1166. })
  1167. );
  1168. MockApiClient.addMockResponse({
  1169. url: '/organizations/org-slug/events/project:event_id/',
  1170. method: 'GET',
  1171. body: makeEvent({}, [makeSpan({span_id: 'span1', description: 'span1'})]),
  1172. });
  1173. tree.zoomIn(tree.list[1], true, {
  1174. api: new MockApiClient(),
  1175. organization: OrganizationFixture(),
  1176. });
  1177. await waitFor(() => {
  1178. assertSpanNode(tree.list[1].children[0]);
  1179. expect(tree.list[1].children[0].value.description).toBe('span1');
  1180. });
  1181. tree.zoomIn(tree.list[1], false, {
  1182. api: new MockApiClient(),
  1183. organization: OrganizationFixture(),
  1184. });
  1185. await waitFor(() => {
  1186. // Assert child no longer points to children
  1187. expect(tree.list[1].zoomedIn).toBe(false);
  1188. expect(tree.list[1].children[0]).toBe(undefined);
  1189. expect(tree.list[2]).toBe(undefined);
  1190. });
  1191. });
  1192. it('zooms in and out', async () => {
  1193. const tree = TraceTree.FromTrace(
  1194. makeTrace({
  1195. transactions: [
  1196. makeTransaction({project_slug: 'project', event_id: 'event_id'}),
  1197. ],
  1198. })
  1199. );
  1200. MockApiClient.addMockResponse({
  1201. url: '/organizations/org-slug/events/project:event_id/',
  1202. method: 'GET',
  1203. body: makeEvent({}, [makeSpan({span_id: 'span 1', description: 'span1'})]),
  1204. });
  1205. // Zoom in
  1206. tree.zoomIn(tree.list[1], true, {
  1207. api: new MockApiClient(),
  1208. organization: OrganizationFixture(),
  1209. });
  1210. await waitFor(() => {
  1211. expect(tree.list[1].zoomedIn).toBe(true);
  1212. assertSpanNode(tree.list[1].children[0]);
  1213. expect(tree.list[1].children[0].value.description).toBe('span1');
  1214. });
  1215. // Zoom out
  1216. tree.zoomIn(tree.list[1], false, {
  1217. api: new MockApiClient(),
  1218. organization: OrganizationFixture(),
  1219. });
  1220. await waitFor(() => {
  1221. expect(tree.list[2]).toBe(undefined);
  1222. });
  1223. // Zoom in
  1224. tree.zoomIn(tree.list[1], true, {
  1225. api: new MockApiClient(),
  1226. organization: OrganizationFixture(),
  1227. });
  1228. await waitFor(() => {
  1229. assertSpanNode(tree.list[1].children[0]);
  1230. expect(tree.list[1].children[0].value?.description).toBe('span1');
  1231. });
  1232. });
  1233. it('zooms in and out preserving siblings', async () => {
  1234. const tree = TraceTree.FromTrace(
  1235. makeTrace({
  1236. transactions: [
  1237. makeTransaction({
  1238. project_slug: 'project',
  1239. event_id: 'event_id',
  1240. start_timestamp: 0,
  1241. children: [
  1242. makeTransaction({
  1243. start_timestamp: 1,
  1244. timestamp: 2,
  1245. project_slug: 'other_project',
  1246. event_id: 'event_id',
  1247. }),
  1248. makeTransaction({start_timestamp: 2, timestamp: 3}),
  1249. ],
  1250. }),
  1251. ],
  1252. })
  1253. );
  1254. const request = MockApiClient.addMockResponse({
  1255. url: '/organizations/org-slug/events/other_project:event_id/',
  1256. method: 'GET',
  1257. body: makeEvent({}, [makeSpan({description: 'span1'})]),
  1258. });
  1259. tree.expand(tree.list[1], true);
  1260. tree.zoomIn(tree.list[2], true, {
  1261. api: new MockApiClient(),
  1262. organization: OrganizationFixture(),
  1263. });
  1264. expect(request).toHaveBeenCalled();
  1265. // Zoom in
  1266. await waitFor(() => {
  1267. expect(tree.list.length).toBe(5);
  1268. });
  1269. // Zoom out
  1270. tree.zoomIn(tree.list[2], false, {
  1271. api: new MockApiClient(),
  1272. organization: OrganizationFixture(),
  1273. });
  1274. await waitFor(() => {
  1275. expect(tree.list.length).toBe(4);
  1276. });
  1277. });
  1278. it('preserves expanded state when zooming in and out', async () => {
  1279. const tree = TraceTree.FromTrace(
  1280. makeTrace({
  1281. transactions: [
  1282. makeTransaction({
  1283. project_slug: 'project',
  1284. event_id: 'event_id',
  1285. children: [
  1286. makeTransaction({project_slug: 'other_project', event_id: 'event_id'}),
  1287. ],
  1288. }),
  1289. ],
  1290. })
  1291. );
  1292. MockApiClient.addMockResponse({
  1293. url: '/organizations/org-slug/events/project:event_id/',
  1294. method: 'GET',
  1295. body: makeEvent({}, [
  1296. makeSpan({description: 'span1'}),
  1297. makeSpan({description: 'span2'}),
  1298. ]),
  1299. });
  1300. tree.expand(tree.list[1], true);
  1301. expect(tree.list.length).toBe(3);
  1302. tree.zoomIn(tree.list[1], true, {
  1303. api: new MockApiClient(),
  1304. organization: OrganizationFixture(),
  1305. });
  1306. await waitFor(() => {
  1307. expect(tree.list.length).toBe(4);
  1308. });
  1309. tree.zoomIn(tree.list[1], false, {
  1310. api: new MockApiClient(),
  1311. organization: OrganizationFixture(),
  1312. });
  1313. await waitFor(() => {
  1314. expect(tree.list.length).toBe(3);
  1315. });
  1316. expect(tree.list[1].expanded).toBe(true);
  1317. });
  1318. });
  1319. describe('autogrouping', () => {
  1320. it('auto groups sibling spans and preserves tail spans', () => {
  1321. const root = new TraceTreeNode(null, makeSpan({description: 'span1'}), {
  1322. project_slug: '',
  1323. event_id: '',
  1324. });
  1325. for (let i = 0; i < 5; i++) {
  1326. root.children.push(
  1327. new TraceTreeNode(root, makeSpan({description: 'span', op: 'db'}), {
  1328. project_slug: '',
  1329. event_id: '',
  1330. })
  1331. );
  1332. }
  1333. root.children.push(
  1334. new TraceTreeNode(root, makeSpan({description: 'span', op: 'http'}), {
  1335. project_slug: '',
  1336. event_id: '',
  1337. })
  1338. );
  1339. expect(root.children.length).toBe(6);
  1340. TraceTree.AutogroupSiblingSpanNodes(root);
  1341. expect(root.children.length).toBe(2);
  1342. });
  1343. it('autogroups when number of children is exactly 5', () => {
  1344. const root = new TraceTreeNode(null, makeSpan({description: 'span1'}), {
  1345. project_slug: '',
  1346. event_id: '',
  1347. });
  1348. for (let i = 0; i < 5; i++) {
  1349. root.children.push(
  1350. new TraceTreeNode(root, makeSpan({description: 'span', op: 'db'}), {
  1351. project_slug: '',
  1352. event_id: '',
  1353. })
  1354. );
  1355. }
  1356. expect(root.children.length).toBe(5);
  1357. TraceTree.AutogroupSiblingSpanNodes(root);
  1358. expect(root.children.length).toBe(1);
  1359. });
  1360. it('adds autogrouped siblings as children under autogrouped node', () => {
  1361. const root = new TraceTreeNode(null, makeSpan({description: 'span1'}), {
  1362. project_slug: '',
  1363. event_id: '',
  1364. });
  1365. for (let i = 0; i < 5; i++) {
  1366. root.children.push(
  1367. new TraceTreeNode(root, makeSpan({description: 'span', op: 'db'}), {
  1368. project_slug: '',
  1369. event_id: '',
  1370. })
  1371. );
  1372. }
  1373. expect(root.children.length).toBe(5);
  1374. TraceTree.AutogroupSiblingSpanNodes(root);
  1375. expect(root.children.length).toBe(1);
  1376. const autoGroupedNode = root.children[0];
  1377. assertAutogroupedNode(autoGroupedNode);
  1378. expect(autoGroupedNode.groupCount).toBe(5);
  1379. expect(autoGroupedNode.children.length).toBe(5);
  1380. });
  1381. it('autogroups when number of children is > 5', () => {
  1382. const root = new TraceTreeNode(null, makeSpan({description: 'span1'}), {
  1383. project_slug: '',
  1384. event_id: '',
  1385. });
  1386. for (let i = 0; i < 7; i++) {
  1387. root.children.push(
  1388. new TraceTreeNode(root, makeSpan({description: 'span', op: 'db'}), {
  1389. project_slug: '',
  1390. event_id: '',
  1391. })
  1392. );
  1393. }
  1394. expect(root.children.length).toBe(7);
  1395. TraceTree.AutogroupSiblingSpanNodes(root);
  1396. expect(root.children.length).toBe(1);
  1397. });
  1398. it('autogroups direct children case', () => {
  1399. // db db db
  1400. // http -> parent autogroup (3) -> parent autogroup (3)
  1401. // http http
  1402. // http http
  1403. // http
  1404. const root: TraceTreeNode<TraceTree.Span> = new TraceTreeNode(
  1405. null,
  1406. makeSpan({
  1407. description: `span1`,
  1408. span_id: `1`,
  1409. op: 'db',
  1410. }),
  1411. {project_slug: '', event_id: ''}
  1412. );
  1413. let last: TraceTreeNode<any> = root;
  1414. for (let i = 0; i < 3; i++) {
  1415. const node = new TraceTreeNode(
  1416. last,
  1417. makeSpan({
  1418. description: `span${i}`,
  1419. span_id: `${i}`,
  1420. op: 'http',
  1421. }),
  1422. {
  1423. project_slug: '',
  1424. event_id: '',
  1425. }
  1426. );
  1427. last.children.push(node);
  1428. last = node;
  1429. }
  1430. if (!root) {
  1431. throw new Error('root is null');
  1432. }
  1433. expect(root.children.length).toBe(1);
  1434. expect(root.children[0].children.length).toBe(1);
  1435. TraceTree.AutogroupDirectChildrenSpanNodes(root);
  1436. expect(root.children.length).toBe(1);
  1437. assertAutogroupedNode(root.children[0]);
  1438. expect(root.children[0].children.length).toBe(0);
  1439. root.children[0].expanded = true;
  1440. expect((root.children[0].children[0].value as RawSpanType).description).toBe(
  1441. 'span0'
  1442. );
  1443. });
  1444. it('autogrouping direct children skips rendering intermediary nodes', () => {
  1445. // db db db
  1446. // http autogrouped (3) autogrouped (3)
  1447. // http -> db -> http
  1448. // http http
  1449. // db http
  1450. // db
  1451. const root = new TraceTreeNode(
  1452. null,
  1453. makeSpan({span_id: 'span1', description: 'span1', op: 'db'}),
  1454. {
  1455. project_slug: '',
  1456. event_id: '',
  1457. }
  1458. );
  1459. let last = root;
  1460. for (let i = 0; i < 4; i++) {
  1461. const node = new TraceTreeNode(
  1462. last,
  1463. makeSpan({
  1464. span_id: `span`,
  1465. description: `span`,
  1466. op: i === 3 ? 'db' : 'http',
  1467. }),
  1468. {
  1469. project_slug: '',
  1470. event_id: '',
  1471. }
  1472. );
  1473. last.children.push(node);
  1474. last = node;
  1475. }
  1476. TraceTree.AutogroupDirectChildrenSpanNodes(root);
  1477. const autoGroupedNode = root.children[0];
  1478. assertAutogroupedNode(autoGroupedNode);
  1479. expect(autoGroupedNode.children.length).toBe(1);
  1480. expect((autoGroupedNode.children[0].value as RawSpanType).op).toBe('db');
  1481. autoGroupedNode.expanded = true;
  1482. expect(autoGroupedNode.children.length).toBe(1);
  1483. expect((autoGroupedNode.children[0].value as RawSpanType).op).toBe('http');
  1484. });
  1485. it('nested direct autogrouping', () => {
  1486. // db db db
  1487. // http -> parent autogroup (3) -> parent autogroup (3)
  1488. // http db http
  1489. // http parent autogroup (3) http
  1490. // db http
  1491. // http db
  1492. // http parent autogrouped (3)
  1493. // http http
  1494. // http
  1495. // http
  1496. const root = new TraceTreeNode(
  1497. null,
  1498. makeSpan({span_id: 'span', description: 'span', op: 'db'}),
  1499. {
  1500. project_slug: '',
  1501. event_id: '',
  1502. }
  1503. );
  1504. let last = root;
  1505. for (let i = 0; i < 3; i++) {
  1506. if (i === 1) {
  1507. const autogroupBreakingSpan = new TraceTreeNode(
  1508. last,
  1509. makeSpan({span_id: 'span', description: 'span', op: 'db'}),
  1510. {
  1511. project_slug: '',
  1512. event_id: '',
  1513. }
  1514. );
  1515. last.children.push(autogroupBreakingSpan);
  1516. last = autogroupBreakingSpan;
  1517. } else {
  1518. for (let j = 0; j < 3; j++) {
  1519. const node = new TraceTreeNode(
  1520. last,
  1521. makeSpan({span_id: `span${j}`, description: `span${j}`, op: 'http'}),
  1522. {
  1523. project_slug: '',
  1524. event_id: '',
  1525. }
  1526. );
  1527. last.children.push(node);
  1528. last = node;
  1529. }
  1530. }
  1531. }
  1532. TraceTree.AutogroupDirectChildrenSpanNodes(root);
  1533. assertAutogroupedNode(root.children[0]);
  1534. assertAutogroupedNode(root.children[0].children[0].children[0]);
  1535. });
  1536. it('sibling autogrouping', () => {
  1537. // db db
  1538. // http sibling autogrouped (5)
  1539. // http
  1540. // http ->
  1541. // http
  1542. // http
  1543. const root = new TraceTreeNode(
  1544. null,
  1545. makeTransaction({start_timestamp: 0, timestamp: 10}),
  1546. {
  1547. project_slug: '',
  1548. event_id: '',
  1549. }
  1550. );
  1551. for (let i = 0; i < 5; i++) {
  1552. root.children.push(
  1553. new TraceTreeNode(root, makeSpan({start_timestamp: i, timestamp: i + 1}), {
  1554. project_slug: '',
  1555. event_id: '',
  1556. })
  1557. );
  1558. }
  1559. TraceTree.AutogroupSiblingSpanNodes(root);
  1560. expect(root.children.length).toBe(1);
  1561. assertAutogroupedNode(root.children[0]);
  1562. });
  1563. it('multiple sibling autogrouping', () => {
  1564. // db db
  1565. // http sibling autogrouped (5)
  1566. // http db
  1567. // http -> sibling autogrouped (5)
  1568. // http
  1569. // http
  1570. // db
  1571. // http
  1572. // http
  1573. // http
  1574. // http
  1575. // http
  1576. const root = new TraceTreeNode(
  1577. null,
  1578. makeTransaction({start_timestamp: 0, timestamp: 10}),
  1579. {
  1580. project_slug: '',
  1581. event_id: '',
  1582. }
  1583. );
  1584. for (let i = 0; i < 10; i++) {
  1585. if (i === 5) {
  1586. root.children.push(
  1587. new TraceTreeNode(
  1588. root,
  1589. makeSpan({start_timestamp: i, timestamp: i + 1, op: 'db'}),
  1590. {
  1591. project_slug: '',
  1592. event_id: '',
  1593. }
  1594. )
  1595. );
  1596. }
  1597. root.children.push(
  1598. new TraceTreeNode(
  1599. root,
  1600. makeSpan({start_timestamp: i, timestamp: i + 1, op: 'http'}),
  1601. {
  1602. project_slug: '',
  1603. event_id: '',
  1604. }
  1605. )
  1606. );
  1607. }
  1608. TraceTree.AutogroupSiblingSpanNodes(root);
  1609. assertAutogroupedNode(root.children[0]);
  1610. expect(root.children).toHaveLength(3);
  1611. assertAutogroupedNode(root.children[2]);
  1612. });
  1613. it('renders children of autogrouped direct children nodes', async () => {
  1614. const tree = TraceTree.FromTrace(
  1615. makeTrace({
  1616. transactions: [
  1617. makeTransaction({
  1618. transaction: '/',
  1619. project_slug: 'project',
  1620. event_id: 'event_id',
  1621. }),
  1622. ],
  1623. })
  1624. );
  1625. MockApiClient.addMockResponse({
  1626. url: '/organizations/org-slug/events/project:event_id/',
  1627. method: 'GET',
  1628. body: makeEvent({}, [
  1629. makeSpan({description: 'parent span', op: 'http', span_id: '1'}),
  1630. makeSpan({description: 'span', op: 'db', span_id: '2', parent_span_id: '1'}),
  1631. makeSpan({description: 'span', op: 'db', span_id: '3', parent_span_id: '2'}),
  1632. makeSpan({description: 'span', op: 'db', span_id: '4', parent_span_id: '3'}),
  1633. makeSpan({description: 'span', op: 'db', span_id: '5', parent_span_id: '4'}),
  1634. makeSpan({
  1635. description: 'span',
  1636. op: 'redis',
  1637. span_id: '6',
  1638. parent_span_id: '5',
  1639. }),
  1640. makeSpan({description: 'span', op: 'https', parent_span_id: '1'}),
  1641. ]),
  1642. });
  1643. expect(tree.list.length).toBe(2);
  1644. tree.zoomIn(tree.list[1], true, {
  1645. api: new MockApiClient(),
  1646. organization: OrganizationFixture(),
  1647. });
  1648. await waitFor(() => {
  1649. expect(tree.list.length).toBe(6);
  1650. });
  1651. const autogroupedNode = tree.list[tree.list.length - 3];
  1652. assertParentAutogroupedNode(autogroupedNode);
  1653. expect('autogrouped_by' in autogroupedNode?.value).toBeTruthy();
  1654. expect(autogroupedNode.groupCount).toBe(4);
  1655. expect(autogroupedNode.head.value.span_id).toBe('2');
  1656. expect(autogroupedNode.tail.value.span_id).toBe('5');
  1657. // Expand autogrouped node
  1658. expect(tree.expand(autogroupedNode, true)).toBe(true);
  1659. expect(tree.list.length).toBe(10);
  1660. // Collapse autogrouped node
  1661. expect(tree.expand(autogroupedNode, false)).toBe(true);
  1662. expect(tree.list.length).toBe(6);
  1663. expect(autogroupedNode.children[0].depth).toBe(4);
  1664. });
  1665. });
  1666. });