traceTree.spec.tsx 62 KB

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