traceTree.spec.tsx 62 KB

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